mirror of
https://github.com/Ylianst/MeshAgent
synced 2025-12-10 05:13:38 +00:00
Added username/password parsing ability to http.parseUri()
This commit is contained in:
@@ -219,6 +219,21 @@ void *Duktape_GetPointerProperty(duk_context *ctx, duk_idx_t i, char* propertyNa
|
||||
return retVal;
|
||||
}
|
||||
|
||||
char* Duktape_GetStringPropertyIndexValueEx(duk_context *ctx, duk_idx_t i, duk_uarridx_t x, char* defaultValue, duk_size_t *len)
|
||||
{
|
||||
char *retVal = defaultValue;
|
||||
if (ctx != NULL && duk_has_prop_index(ctx, i, x))
|
||||
{
|
||||
duk_get_prop_index(ctx, i, x);
|
||||
retVal = (char*)duk_get_lstring(ctx, -1, len);
|
||||
duk_pop(ctx);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (len != NULL) { *len = (defaultValue == NULL) ? 0 : strnlen_s(defaultValue, sizeof(ILibScratchPad)); }
|
||||
}
|
||||
return retVal;
|
||||
}
|
||||
char* Duktape_GetStringPropertyValueEx(duk_context *ctx, duk_idx_t i, char* propertyName, char* defaultValue, duk_size_t *len)
|
||||
{
|
||||
char *retVal = defaultValue;
|
||||
|
||||
@@ -79,6 +79,8 @@ char *Duktape_GetStashKey(void* value);
|
||||
char* Duktape_GetBuffer(duk_context *ctx, duk_idx_t i, duk_size_t *bufLen);
|
||||
void Duktape_CreateEnumEx(duk_context *ctx, char** fieldNames, int * fieldValues, int numFields);
|
||||
void Duktape_CreateEnum(duk_context *ctx, char* enumName, char **fieldNames, int *fieldValues, int numFields);
|
||||
char* Duktape_GetStringPropertyIndexValueEx(duk_context *ctx, duk_idx_t i, duk_uarridx_t x, char* defaultValue, duk_size_t *len);
|
||||
#define Duktape_GetStringPropertyIndexValue(ctx, i, ax, defaultValue) Duktape_GetStringPropertyIndexValueEx(ctx, i, ax, defaultValue, NULL)
|
||||
char* Duktape_GetStringPropertyValueEx(duk_context *ctx, duk_idx_t i, char* propertyName, char* defaultValue, duk_size_t *len);
|
||||
#define Duktape_GetStringPropertyValue(ctx, i, propertyName, defaultValue) Duktape_GetStringPropertyValueEx(ctx, i, propertyName, defaultValue, NULL)
|
||||
int Duktape_GetIntPropertyValue(duk_context *ctx, duk_idx_t i, char* propertyName, int defaultValue);
|
||||
@@ -115,6 +117,7 @@ char *Duktape_Duplicate_GetStringEx(duk_context *ctx, duk_idx_t i, duk_size_t *l
|
||||
|
||||
#define duk_buffer_slice(ctx, i, start, len) duk_dup(ctx, i);duk_get_prop_string(ctx, -1, "slice");duk_swap_top(ctx, -2);duk_push_int(ctx, start);duk_push_int(ctx, len);duk_pcall_method(ctx, 2);duk_remove(ctx, -2);
|
||||
#define duk_string_concat(ctx, i) duk_dup(ctx, i);duk_get_prop_string(ctx, -1, "concat");duk_swap_top(ctx, -2);duk_dup(ctx, -3);duk_pcall_method(ctx, 1);duk_remove(ctx, -2);
|
||||
#define duk_string_split(ctx, i, delim) duk_dup(ctx, i);duk_get_prop_string(ctx, -1, "split");duk_swap_top(ctx, -2);duk_push_string(ctx, delim);duk_call_method(ctx, 1);
|
||||
|
||||
int Duktape_GetBooleanProperty(duk_context *ctx, duk_idx_t i, char *propertyName, int defaultValue);
|
||||
struct sockaddr_in6* Duktape_IPAddress4_FromString(char* address, unsigned short port);
|
||||
|
||||
@@ -3605,7 +3605,32 @@ duk_ret_t ILibDuktape_httpStream_parseUri(duk_context *ctx)
|
||||
unsigned short port;
|
||||
int protocolIndex;
|
||||
|
||||
char *username = NULL;
|
||||
char *password = NULL;
|
||||
|
||||
duk_string_split(ctx, 0, "@"); // [array]
|
||||
if (duk_get_length(ctx, -1) == 2)
|
||||
{
|
||||
duk_array_shift(ctx, -1); // [array][http://user:pwd]
|
||||
duk_string_split(ctx, -1, "://"); // [array][http://user:pwd][array]
|
||||
duk_array_shift(ctx, -1); // [array][http://user:pwd][array][http]
|
||||
duk_array_pop(ctx, -2); // [array][http://user:pwd][array][http][user:pwd]
|
||||
duk_string_split(ctx, -1, ":"); // [array][http://user:pwd][array][http][user:pwd][array]
|
||||
username = (char*)Duktape_GetStringPropertyIndexValue(ctx, -1, 0, NULL);
|
||||
password = (char*)Duktape_GetStringPropertyIndexValue(ctx, -1, 1, NULL);
|
||||
duk_string_split(ctx, 0, "@"); // [array][http://user:pwd][array][http][user:pwd][array][array]
|
||||
duk_array_shift(ctx, -1); duk_pop(ctx); // [array][http://user:pwd][array][http][user:pwd][array][mydomain.com:xx/xx]
|
||||
duk_push_string(ctx, "://"); // [array][http://user:pwd][array][http][user:pwd][array][mydomain.com:xx/xx][://]
|
||||
duk_string_concat(ctx, -5); // [array][http://user:pwd][array][http][user:pwd][array][mydomain.com:xx/xx][http://]
|
||||
duk_array_unshift(ctx, -2); // [array][http://user:pwd][array][http][user:pwd][array][http://mydomain.com:xx/xx]
|
||||
duk_array_join(ctx, -1, "");
|
||||
uri = (char*)duk_get_lstring(ctx, -1, &uriLen);
|
||||
}
|
||||
else
|
||||
{
|
||||
uri = (char*)duk_get_lstring(ctx, 0, &uriLen);
|
||||
}
|
||||
|
||||
protocolIndex = 1 + ILibString_IndexOf(uri, (int)uriLen, "://", 3);
|
||||
if (protocolIndex > 0)
|
||||
{
|
||||
@@ -3622,6 +3647,8 @@ duk_ret_t ILibDuktape_httpStream_parseUri(duk_context *ctx)
|
||||
duk_put_prop_string(ctx, -2, "path"); // [options]
|
||||
duk_push_string(ctx, "GET"); // [options][method]
|
||||
duk_put_prop_string(ctx, -2, "method"); // [options]
|
||||
if (username != NULL) { duk_push_string(ctx, username); duk_put_prop_string(ctx, -2, "username"); }
|
||||
if (password != NULL) { duk_push_string(ctx, password); duk_put_prop_string(ctx, -2, "password"); }
|
||||
|
||||
free(path);
|
||||
free(addr);
|
||||
|
||||
Reference in New Issue
Block a user