diff --git a/src/env.c b/src/env.c index 5e8e6b3..e1ce7d2 100644 --- a/src/env.c +++ b/src/env.c @@ -70,6 +70,7 @@ struct Environment envForLambda(const Object* params, const Object* arg_forms, i if (paramCount == 0) { if (outer) { + outer->refs += 1; return *outer; } env.strings = NULL; diff --git a/src/examples/web/webby.pl b/src/examples/web/webby.pl index 793fe9c..a69f9b9 100755 --- a/src/examples/web/webby.pl +++ b/src/examples/web/webby.pl @@ -18,7 +18,7 @@ (def div (element "div")) (def article (element "article")) -(def singleton (fn (type) (fn (text) (cat "<" type " " (reduce (text "") cat) ">")))) +(def singleton (fn (type) (fn (text) (cat "<" type " " (reduce text cat "") ">")))) (def link (singleton "link")) (def attribute (fn (type) (fn (value) (cat type "='" value "'")))) diff --git a/src/object.c b/src/object.c index ee5e3b9..8ed9a67 100644 --- a/src/object.c +++ b/src/object.c @@ -674,12 +674,17 @@ inline Object boolObject(int b) return o; } -// Skips first and last chars! Assumed to be '"' +/// Skips first and last chars! Assumed to be '"' inline Object objFromSlice(const char* string, int len) { return stringFromSlice(&string[1], len - 1); } +inline Object nullTerminated(const char* string) +{ + return stringFromSlice(string, strlen(string)); +} + inline Object stringFromSlice(const char* string, int len) { Object o = symFromSlice(string, len); diff --git a/src/object.h b/src/object.h index 47bbebe..7ed44f9 100644 --- a/src/object.h +++ b/src/object.h @@ -200,6 +200,8 @@ Object startList(Object start); Object objFromSlice(const char* string, int len); +Object nullTerminated(const char* string); + Object stringFromSlice(const char* string, int len); Object symFromSlice(const char* string, int len); diff --git a/src/web.c b/src/web.c index 919822f..76b542f 100644 --- a/src/web.c +++ b/src/web.c @@ -74,16 +74,25 @@ answer_to_connection(void* cls, struct MHD_Connection* connection, size_t* upload_data_size, void** con_cls) { char* page = NULL; - printf("%s URL: '%s' :: ", method, url); - printf("version: %s\n", version); - printf("upload_data: %s\n", upload_data); + printf("%s :: %s URL: '%s'\n", method, version, url); + if (upload_data) { + printf("upload_data: %s\n", upload_data); + } for (int i = 0; i < routeCount; i++) { if (methodMatches(method, &routes[i]) && strcmp(url, routes[i].path) == 0) { Object queryParams = listObject(); MHD_get_connection_values(connection, MHD_GET_ARGUMENT_KIND, add_query_param, &queryParams); + char* password = NULL; + char* username = MHD_basic_auth_get_username_password(connection, &password); + Object usernameO = username ? nullTerminated(username) : stringFromSlice("", 0); + Object passwordO = password ? nullTerminated(password) : stringFromSlice("", 0); + MHD_free(username); + MHD_free(password); Object res = structObject(requestDefinition); res.structObject->fields[0] = queryParams; + res.structObject->fields[1] = usernameO; + res.structObject->fields[2] = passwordO; Object route = cloneObject(routes[i].routeAction); Object result = listEvalLambda(&route, &res, 2, routes[i].env); @@ -99,10 +108,6 @@ answer_to_connection(void* cls, struct MHD_Connection* connection, } (void) cls; /* Unused. Silent compiler warning. */ - (void) url; /* Unused. Silent compiler warning. */ - (void) method; /* Unused. Silent compiler warning. */ - (void) version; /* Unused. Silent compiler warning. */ - (void) upload_data; /* Unused. Silent compiler warning. */ (void) upload_data_size; /* Unused. Silent compiler warning. */ (void) con_cls; /* Unused. Silent compiler warning. */ @@ -117,7 +122,7 @@ answer_to_connection(void* cls, struct MHD_Connection* connection, void initialize() { printf("Initializing...\n"); - Object o = parseEval("(struct Request (queryParams))", global()); + Object o = parseEval("(struct Request (queryParams username password))", global()); cleanObject(&o); requestDefinition = getStructIndex("Request"); }