From b645a0b2c13f47d29f252ac0df99d77af59d1aec Mon Sep 17 00:00:00 2001 From: Jordan Bancino Date: Sat, 15 Oct 2022 16:28:32 -0400 Subject: [PATCH] Hook param parsing into the request logic. --- src/Http.c | 37 ++++++++++++++++++++++++++++--------- src/HttpServer.c | 44 ++++++++++++++++++++++++++++++++++++-------- src/include/Http.h | 6 +++--- 3 files changed, 67 insertions(+), 20 deletions(-) diff --git a/src/Http.c b/src/Http.c index d898aa2..a32727d 100644 --- a/src/Http.c +++ b/src/Http.c @@ -374,22 +374,37 @@ HttpUrlDecode(char *str) } HashMap * -HttpParamDecode(FILE * in) +HttpParamDecode(char * in) { - /* TODO */ - (void) in; - return NULL; + HashMap *params; + + if (!in) + { + return NULL; + } + + printf("HttpParamDecode(%s)\n", in); + fflush(stdout); + + params = HashMapCreate(); + if (!params) + { + return NULL; + } + + return params; } -void -HttpParamEncode(HashMap * params, FILE * out) +char * +HttpParamEncode(HashMap * params) { char *key; char *val; + char *out = NULL; if (!params || !out) { - return; + return NULL; } while (HashMapIterate(params, &key, (void *) &val)) @@ -403,12 +418,16 @@ HttpParamEncode(HashMap * params, FILE * out) if (!encKey || !encVal) { /* Memory error */ - return; + Free(encKey); + Free(encVal); + return NULL; } - fprintf(out, "%s=%s&", encKey, encVal); + /* TODO */ Free(encKey); Free(encVal); } + + return out; } diff --git a/src/HttpServer.c b/src/HttpServer.c index f05f5af..9c18a23 100644 --- a/src/HttpServer.c +++ b/src/HttpServer.c @@ -65,6 +65,7 @@ struct HttpServerContext HashMap *requestHeaders; HttpRequestMethod requestMethod; char *requestPath; + HashMap *requestParams; HashMap *responseHeaders; HttpStatus responseStatus; @@ -74,7 +75,7 @@ struct HttpServerContext static HttpServerContext * HttpServerContextCreate(HttpRequestMethod requestMethod, - char *requestPath, FILE * stream) + char *requestPath, HashMap *requestParams, FILE * stream) { HttpServerContext *c; @@ -101,6 +102,7 @@ HttpServerContextCreate(HttpRequestMethod requestMethod, c->requestMethod = requestMethod; c->requestPath = requestPath; + c->requestParams = requestParams; c->stream = stream; c->responseStatus = HTTP_OK; @@ -133,6 +135,14 @@ HttpServerContextFree(HttpServerContext * c) HashMapFree(c->responseHeaders); + while (HashMapIterate(c->requestParams, &key, &val)) + { + Free(key); + Free(val); + } + + HashMapFree(c->requestParams); + Free(c->requestPath); fclose(c->stream); @@ -175,9 +185,12 @@ HttpRequestPath(HttpServerContext * c) HashMap * HttpRequestParams(HttpServerContext * c) { - /* TODO: Implement param parsing */ - (void) c; - return NULL; + if (!c) + { + return NULL; + } + + return c->requestParams; } char * @@ -403,12 +416,15 @@ HttpServerWorkerThread(void *args) char *requestPath; char *requestProtocol; + HashMap *requestParams; + ssize_t requestPathLen; + ssize_t i = 0; HttpRequestMethod requestMethod; if (!fp) { - /* Block for 1 millisecond before continuting so we don't + /* Block for 1 millisecond before continuing so we don't * murder the CPU */ UtilSleepMillis(1); continue; @@ -454,7 +470,8 @@ HttpServerWorkerThread(void *args) } } - requestPath = Malloc((i * sizeof(char)) + 1); + requestPathLen = i; + requestPath = Malloc(((requestPathLen + 1) * sizeof(char))); strcpy(requestPath, pathPtr); requestProtocol = &pathPtr[i + 1]; @@ -462,11 +479,22 @@ HttpServerWorkerThread(void *args) if (strcmp(requestProtocol, "HTTP/1.1") != 0 && strcmp(requestProtocol, "HTTP/1.0") != 0) { - printf("Bad protocol: [%s]\n", requestProtocol); goto bad_request; } - context = HttpServerContextCreate(requestMethod, requestPath, fp); + /* Find request params */ + for (i = 0; i < requestPathLen; i++) + { + if (requestPath[i] == '?') + { + break; + } + } + + requestPath[i] = '\0'; + requestParams = HttpParamDecode(requestPath + i + 1); + + context = HttpServerContextCreate(requestMethod, requestPath, requestParams, fp); if (!context) { goto internal_error; diff --git a/src/include/Http.h b/src/include/Http.h index 21f4114..18f8cce 100644 --- a/src/include/Http.h +++ b/src/include/Http.h @@ -120,9 +120,9 @@ extern char * HttpUrlDecode(char *); extern HashMap * - HttpParamDecode(FILE *); + HttpParamDecode(char *); -extern void - HttpParamEncode(HashMap *, FILE *); +extern char * + HttpParamEncode(HashMap *); #endif