1
1
Fork 0
mirror of https://github.com/QB64-Phoenix-Edition/QB64pe.git synced 2024-07-06 05:50:22 +00:00

Add _StatusCode command for HTTP handles

The _StatusCode command returns the status code on the HTTP response
when given a HTTP handle from _OpenClient().
This commit is contained in:
Matthew Kilgore 2022-11-20 04:04:02 -05:00
parent 610c914d36
commit 12c9c35db5
18 changed files with 199 additions and 101 deletions

View file

@ -21699,6 +21699,24 @@ int32 func_eof(int32 i) {
return 0;
}
int32 func__statusCode(int32 handle) {
if (handle >= 0) {
error(52);
return -1;
}
int real_handle = -(handle + 1);
int code = libqb_http_get_status_code(real_handle);
if (code == -1) {
// Connection isn't valid, it has no status code.
error(9);
return -1;
}
return code;
}
void sub_seek(int32 i, int64 pos) {
if (new_error)
return;

View file

@ -23,6 +23,9 @@ int libqb_http_get_length(int handle, size_t *length);
// Gets the value from the Content-Length HTTP header. If none was provided, returns an error
int libqb_http_get_content_length(int handle, uint64_t *length);
// Returns positive status code. -1 indicates there was none (Ex. Connection was unsuccessful)
int libqb_http_get_status_code(int handle);
// Returns the "effective url" as reported by curl, it indicates the location
// actually connected to after redirects and such.
//

View file

@ -41,6 +41,10 @@ int libqb_http_get_content_length(int id, uint64_t *ptr) {
return -1;
}
int libqb_http_get_status_code(int id) {
return -1;
}
const char *libqb_http_get_url(int handle) {
return NULL;
}

View file

@ -37,6 +37,8 @@ struct handle {
uint64_t content_length = 0;
char *url = NULL;
int status_code = -1;
handle() {
io_lock = libqb_mutex_new();
libqb_buffer_init(&out);
@ -109,6 +111,8 @@ static void __fillout_curl_info(struct handle *handle) {
if (urlp)
handle->url = strdup(urlp);
res = curl_easy_getinfo(handle->con, CURLINFO_RESPONSE_CODE, &handle->status_code);
// If someone is waiting for the info then we let them know
if (handle->response_started) {
completion_finish(handle->response_started);
@ -326,6 +330,21 @@ int libqb_http_get_content_length(int id, uint64_t *length) {
}
}
int libqb_http_get_status_code(int id) {
if (!is_valid_http_id(id))
return -1;
struct handle *handle = curl_state.handle_table[id];
wait_for_info(handle);
{
libqb_mutex_guard guard(handle->io_lock);
return handle->status_code;
}
}
const char *libqb_http_get_url(int id) {
if (!is_valid_http_id(id))
return NULL;

View file

@ -574,6 +574,7 @@ extern void sub_seek(int32 i, int64 pos);
extern int64 func_seek(int32 i);
extern int64 func_loc(int32 i);
extern qbs *func_input(int32 n, int32 i, int32 passed);
extern int32 func__statusCode(int32 handle);
extern double func_sqr(double value);
extern void sub_beep();

View file

@ -1933,6 +1933,7 @@ DO
CASE "HTTP"
unstableFlags(UNSTABLE_HTTP) = -1
regUnstableHttp
CASE ELSE
a$ = "Unrecognized unstable flag " + AddQuotes$(token$)
@ -21272,6 +21273,29 @@ SUB reginternal
reginternalsubfunc = 0
END SUB
SUB regUnstableHttp
reginternalsubfunc = 1
clearid
id.n = qb64prefix$ + "StatusCode" ' Name in CaMeL case
id.subfunc = 1 ' 1 = function, 2 = sub
id.callname = "func__statusCode" ' C/C++ function name
id.args = 1
id.arg = MKL$(LONGTYPE - ISPOINTER)
id.ret = LONGTYPE - ISPOINTER
id.hr_syntax = "_STATUSCODE(httpHandle&)" ' syntax help
regid
' If we're doing $NOPREFIX then we register it again with the underscore
IF qb64prefix_set THEN
id.n = "_StatusCode"
regid
END IF
reginternalsubfunc = 0
END SUB
'this sub is faulty atm!
'sub replacelement (a$, i, newe$)
''note: performs no action for out of range values of i

View file

@ -6,4 +6,5 @@ listOfKeywords$ = listOfKeywords$ + "_GLPOPATTRIB@_GLPOPCLIENTATTRIB@_GLPOPMATRI
listOfKeywords$ = listOfKeywords$ + "_SOFTWARE@_SQUAREPIXELS@_STRETCH@_ALLOWFULLSCREEN@_ALL@_ECHO@_INSTRREV@_TRIM$@_ACCEPTFILEDROP@_FINISHDROP@_TOTALDROPPEDFILES@_DROPPEDFILE@_DROPPEDFILE$@_SHR@_SHL@_ROR@_ROL@" ' a740g: added ROR & ROL
listOfKeywords$ = listOfKeywords$ + "_DEFLATE$@_INFLATE$@_READBIT@_RESETBIT@_SETBIT@_TOGGLEBIT@$ASSERTS@_ASSERT@_CAPSLOCK@_NUMLOCK@_SCROLLLOCK@_TOGGLE@_CONSOLEFONT@_CONSOLECURSOR@_CONSOLEINPUT@_CINP@$NOPREFIX@$COLOR@$DEBUG@_ENVIRONCOUNT@$UNSTABLE@$MIDISOUNDFONT@"
listOfKeywords$ = listOfKeywords$ + "_NOTIFYPOPUP@_MESSAGEBOX@_INPUTBOX$@_SELECTFOLDERDIALOG$@_COLORCHOOSERDIALOG@_OPENFILEDIALOG$@_SAVEFILEDIALOG$@" ' a740g: added common dialog keywords
listOfKeywords$ = listOfKeywords$ + "_STATUSCODE@" ' a740g: added common dialog keywords

View file

@ -10,6 +10,7 @@ Print h&
' situation.
h& = _OpenClient("https://www.example.com/unknownUrl")
Print h&
Print _StatusCode(h&)
System

View file

@ -1,3 +1,4 @@
5 0
0
-2
404

View file

@ -22,6 +22,7 @@ End If
Print "Content-Length: "; length~&
Print "Url: "; url$
Print "Status Code: "; _StatusCode(h&)
Print
Print result$

View file

@ -1,6 +1,7 @@
-2
Content-Length: 1256
Url: HTTP:https://www.example.com
Status Code: 200
<!doctype html>
<html>

View file

@ -27,7 +27,7 @@ While Not Done&
Wend
For x = LBound(handles&) TO UBound(handles&)
Print "Handle:"; handles&(x); ", LOF:"; Lof(handles&(x)); ", Content length:"; Len(content$(x))
Print "Handle:"; handles&(x); ", LOF:"; Lof(handles&(x)); ", Content length:"; Len(content$(x)); ", Status Code: "; _StatusCode(handles&(x))
Close #handles&(x)
Next

View file

@ -1,100 +1,100 @@
Handle:-2 , LOF: 1256 , Content length: 1256
Handle:-69 , LOF: 1256 , Content length: 1256
Handle:-36 , LOF: 1256 , Content length: 1256
Handle:-3 , LOF: 1256 , Content length: 1256
Handle:-70 , LOF: 1256 , Content length: 1256
Handle:-37 , LOF: 1256 , Content length: 1256
Handle:-4 , LOF: 1256 , Content length: 1256
Handle:-71 , LOF: 1256 , Content length: 1256
Handle:-38 , LOF: 1256 , Content length: 1256
Handle:-5 , LOF: 1256 , Content length: 1256
Handle:-72 , LOF: 1256 , Content length: 1256
Handle:-39 , LOF: 1256 , Content length: 1256
Handle:-6 , LOF: 1256 , Content length: 1256
Handle:-73 , LOF: 1256 , Content length: 1256
Handle:-40 , LOF: 1256 , Content length: 1256
Handle:-7 , LOF: 1256 , Content length: 1256
Handle:-74 , LOF: 1256 , Content length: 1256
Handle:-41 , LOF: 1256 , Content length: 1256
Handle:-8 , LOF: 1256 , Content length: 1256
Handle:-75 , LOF: 1256 , Content length: 1256
Handle:-42 , LOF: 1256 , Content length: 1256
Handle:-9 , LOF: 1256 , Content length: 1256
Handle:-76 , LOF: 1256 , Content length: 1256
Handle:-43 , LOF: 1256 , Content length: 1256
Handle:-10 , LOF: 1256 , Content length: 1256
Handle:-77 , LOF: 1256 , Content length: 1256
Handle:-44 , LOF: 1256 , Content length: 1256
Handle:-11 , LOF: 1256 , Content length: 1256
Handle:-78 , LOF: 1256 , Content length: 1256
Handle:-45 , LOF: 1256 , Content length: 1256
Handle:-12 , LOF: 1256 , Content length: 1256
Handle:-79 , LOF: 1256 , Content length: 1256
Handle:-46 , LOF: 1256 , Content length: 1256
Handle:-13 , LOF: 1256 , Content length: 1256
Handle:-80 , LOF: 1256 , Content length: 1256
Handle:-47 , LOF: 1256 , Content length: 1256
Handle:-14 , LOF: 1256 , Content length: 1256
Handle:-81 , LOF: 1256 , Content length: 1256
Handle:-48 , LOF: 1256 , Content length: 1256
Handle:-15 , LOF: 1256 , Content length: 1256
Handle:-82 , LOF: 1256 , Content length: 1256
Handle:-49 , LOF: 1256 , Content length: 1256
Handle:-16 , LOF: 1256 , Content length: 1256
Handle:-83 , LOF: 1256 , Content length: 1256
Handle:-50 , LOF: 1256 , Content length: 1256
Handle:-17 , LOF: 1256 , Content length: 1256
Handle:-84 , LOF: 1256 , Content length: 1256
Handle:-51 , LOF: 1256 , Content length: 1256
Handle:-18 , LOF: 1256 , Content length: 1256
Handle:-85 , LOF: 1256 , Content length: 1256
Handle:-52 , LOF: 1256 , Content length: 1256
Handle:-19 , LOF: 1256 , Content length: 1256
Handle:-86 , LOF: 1256 , Content length: 1256
Handle:-53 , LOF: 1256 , Content length: 1256
Handle:-20 , LOF: 1256 , Content length: 1256
Handle:-87 , LOF: 1256 , Content length: 1256
Handle:-54 , LOF: 1256 , Content length: 1256
Handle:-21 , LOF: 1256 , Content length: 1256
Handle:-88 , LOF: 1256 , Content length: 1256
Handle:-55 , LOF: 1256 , Content length: 1256
Handle:-22 , LOF: 1256 , Content length: 1256
Handle:-89 , LOF: 1256 , Content length: 1256
Handle:-56 , LOF: 1256 , Content length: 1256
Handle:-23 , LOF: 1256 , Content length: 1256
Handle:-90 , LOF: 1256 , Content length: 1256
Handle:-57 , LOF: 1256 , Content length: 1256
Handle:-24 , LOF: 1256 , Content length: 1256
Handle:-91 , LOF: 1256 , Content length: 1256
Handle:-58 , LOF: 1256 , Content length: 1256
Handle:-25 , LOF: 1256 , Content length: 1256
Handle:-92 , LOF: 1256 , Content length: 1256
Handle:-59 , LOF: 1256 , Content length: 1256
Handle:-26 , LOF: 1256 , Content length: 1256
Handle:-93 , LOF: 1256 , Content length: 1256
Handle:-60 , LOF: 1256 , Content length: 1256
Handle:-27 , LOF: 1256 , Content length: 1256
Handle:-94 , LOF: 1256 , Content length: 1256
Handle:-61 , LOF: 1256 , Content length: 1256
Handle:-28 , LOF: 1256 , Content length: 1256
Handle:-95 , LOF: 1256 , Content length: 1256
Handle:-62 , LOF: 1256 , Content length: 1256
Handle:-29 , LOF: 1256 , Content length: 1256
Handle:-96 , LOF: 1256 , Content length: 1256
Handle:-63 , LOF: 1256 , Content length: 1256
Handle:-30 , LOF: 1256 , Content length: 1256
Handle:-97 , LOF: 1256 , Content length: 1256
Handle:-64 , LOF: 1256 , Content length: 1256
Handle:-31 , LOF: 1256 , Content length: 1256
Handle:-98 , LOF: 1256 , Content length: 1256
Handle:-65 , LOF: 1256 , Content length: 1256
Handle:-32 , LOF: 1256 , Content length: 1256
Handle:-99 , LOF: 1256 , Content length: 1256
Handle:-66 , LOF: 1256 , Content length: 1256
Handle:-33 , LOF: 1256 , Content length: 1256
Handle:-100 , LOF: 1256 , Content length: 1256
Handle:-67 , LOF: 1256 , Content length: 1256
Handle:-34 , LOF: 1256 , Content length: 1256
Handle:-101 , LOF: 1256 , Content length: 1256
Handle:-68 , LOF: 1256 , Content length: 1256
Handle:-35 , LOF: 1256 , Content length: 1256
Handle:-2 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-69 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-36 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-3 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-70 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-37 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-4 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-71 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-38 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-5 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-72 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-39 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-6 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-73 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-40 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-7 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-74 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-41 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-8 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-75 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-42 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-9 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-76 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-43 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-10 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-77 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-44 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-11 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-78 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-45 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-12 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-79 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-46 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-13 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-80 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-47 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-14 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-81 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-48 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-15 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-82 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-49 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-16 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-83 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-50 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-17 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-84 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-51 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-18 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-85 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-52 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-19 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-86 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-53 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-20 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-87 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-54 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-21 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-88 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-55 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-22 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-89 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-56 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-23 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-90 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-57 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-24 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-91 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-58 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-25 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-92 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-59 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-26 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-93 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-60 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-27 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-94 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-61 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-28 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-95 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-62 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-29 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-96 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-63 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-30 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-97 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-64 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-31 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-98 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-65 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-32 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-99 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-66 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-33 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-100 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-67 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-34 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-101 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-68 , LOF: 1256 , Content length: 1256 , Status Code: 200
Handle:-35 , LOF: 1256 , Content length: 1256 , Status Code: 200

View file

@ -0,0 +1,3 @@
' _StatusCode cannot be used without $Unstable:Http
Print _StatusCode(2)

View file

@ -0,0 +1,4 @@
Invalid name
Caused by (or after):PRINT _STATUSCODE ( 2 )
LINE 3:Print _StatusCode(2)

View file

@ -0,0 +1,5 @@
$Unstable:Http
Option _ExplicitArray
' Error, StatusCode() is missing prefix
Print StatusCode(2)

View file

@ -0,0 +1,4 @@
Array 'StatusCode' (SINGLE) not defined
Caused by (or after):PRINT STATUSCODE ( 2 )
LINE 5:Print StatusCode(2)

View file

@ -0,0 +1,8 @@
$NOPREFIX
$Unstable:Http
' Important so that `StatusCode()` cannot be an array, ensures that this test
' fails if `StatusCode()` cannot be called
Option ExplicitArray
Print StatusCode(2)