diff --git a/src/Http.c b/src/Http.c index ef5df20..c1b02cc 100644 --- a/src/Http.c +++ b/src/Http.c @@ -374,126 +374,116 @@ HttpUrlDecode(char *str) } HashMap * -HttpParamDecode(char * in) +HttpParamDecode(char *in) { - HashMap *params; + HashMap *params; - if (!in) - { - return NULL; - } + if (!in) + { + return NULL; + } - printf("HttpParamDecode(%s)\n", in); - fflush(stdout); + params = HashMapCreate(); + if (!params) + { + return NULL; + } - params = HashMapCreate(); - if (!params) - { - return NULL; - } + while (*in) + { + char *buf; + size_t allocated; + size_t len; - while (*in) - { - char *buf; - size_t allocated; - size_t len; + char *decKey; + char *decVal; - char *decKey; - char *decVal; + /* Read in key */ - /* Read in key */ + allocated = TELODENDRIA_STRING_CHUNK; + buf = Malloc(allocated); + len = 0; - allocated = TELODENDRIA_STRING_CHUNK; - buf = Malloc(allocated); - len = 0; + while (*in && *in != '=') + { + if (len >= allocated - 1) + { + allocated += TELODENDRIA_STRING_CHUNK; + buf = Realloc(buf, allocated); + } - while (*in && *in != '=') - { - if (len >= allocated) - { - allocated += TELODENDRIA_STRING_CHUNK; - buf = Realloc(buf, allocated); - } + buf[len] = *in; + len++; + in++; + } - buf[len] = *in; - len++; - in++; - } + buf[len] = '\0'; - /* Sanity check */ - if (*in != '=') - { - /* Malformed param */ - Free(buf); - Free(params); - return NULL; - } + /* Sanity check */ + if (*in != '=') + { + /* Malformed param */ + Free(buf); + Free(params); + return NULL; + } - in++; + in++; - /* Decode key */ - decKey = HttpUrlDecode(buf); - Free(buf); + /* Decode key */ + decKey = HttpUrlDecode(buf); + Free(buf); - if (!decKey) - { - /* Decoding error */ - Free(params); - return NULL; - } + if (!decKey) + { + /* Decoding error */ + Free(params); + return NULL; + } - /* Read in value */ - allocated = TELODENDRIA_STRING_CHUNK; - buf = Malloc(allocated); - len = 0; + /* Read in value */ + allocated = TELODENDRIA_STRING_CHUNK; + buf = Malloc(allocated); + len = 0; - while (*in && *in != '&') - { - if (len >= allocated) - { - allocated += TELODENDRIA_STRING_CHUNK; - buf = Realloc(buf, allocated); - } + while (*in && *in != '&') + { + if (len >= allocated - 1) + { + allocated += TELODENDRIA_STRING_CHUNK; + buf = Realloc(buf, allocated); + } - buf[len] = *in; - len++; - in++; - } + buf[len] = *in; + len++; + in++; + } - /* Decode value */ - decVal = HttpUrlDecode(buf); - Free(buf); + buf[len] = '\0'; - if (!decVal) - { - /* Decoding error */ - Free(params); - return NULL; - } + /* Decode value */ + decVal = HttpUrlDecode(buf); + Free(buf); - HashMapSet(params, decKey, decVal); + if (!decVal) + { + /* Decoding error */ + Free(params); + return NULL; + } - if (*in == '&') - { - in++; - continue; - } - else - { - break; - } - } + HashMapSet(params, decKey, decVal); - printf("Done with decoding, here's what we got:\n"); - { - char *key; - char *val; - - while (HashMapIterate(params, &key, (void **) &val)) - { - printf(" [%s]: [%s]\n", key, val); - } - } + if (*in == '&') + { + in++; + continue; + } + else + { + break; + } + } return params; } @@ -503,7 +493,7 @@ HttpParamEncode(HashMap * params) { char *key; char *val; - char *out = NULL; + char *out = NULL; if (!params || !out) { @@ -521,16 +511,16 @@ HttpParamEncode(HashMap * params) if (!encKey || !encVal) { /* Memory error */ - Free(encKey); - Free(encVal); + Free(encKey); + Free(encVal); return NULL; } - /* TODO */ + /* TODO */ Free(encKey); Free(encVal); } - return out; + return out; } diff --git a/src/HttpServer.c b/src/HttpServer.c index 9c18a23..ce52ca9 100644 --- a/src/HttpServer.c +++ b/src/HttpServer.c @@ -65,7 +65,7 @@ struct HttpServerContext HashMap *requestHeaders; HttpRequestMethod requestMethod; char *requestPath; - HashMap *requestParams; + HashMap *requestParams; HashMap *responseHeaders; HttpStatus responseStatus; @@ -75,7 +75,7 @@ struct HttpServerContext static HttpServerContext * HttpServerContextCreate(HttpRequestMethod requestMethod, - char *requestPath, HashMap *requestParams, FILE * stream) + char *requestPath, HashMap * requestParams, FILE * stream) { HttpServerContext *c; @@ -102,7 +102,7 @@ HttpServerContextCreate(HttpRequestMethod requestMethod, c->requestMethod = requestMethod; c->requestPath = requestPath; - c->requestParams = requestParams; + c->requestParams = requestParams; c->stream = stream; c->responseStatus = HTTP_OK; @@ -135,13 +135,13 @@ HttpServerContextFree(HttpServerContext * c) HashMapFree(c->responseHeaders); - while (HashMapIterate(c->requestParams, &key, &val)) - { - Free(key); - Free(val); - } + while (HashMapIterate(c->requestParams, &key, &val)) + { + Free(key); + Free(val); + } - HashMapFree(c->requestParams); + HashMapFree(c->requestParams); Free(c->requestPath); fclose(c->stream); @@ -185,10 +185,10 @@ HttpRequestPath(HttpServerContext * c) HashMap * HttpRequestParams(HttpServerContext * c) { - if (!c) - { - return NULL; - } + if (!c) + { + return NULL; + } return c->requestParams; } @@ -416,8 +416,8 @@ HttpServerWorkerThread(void *args) char *requestPath; char *requestProtocol; - HashMap *requestParams; - ssize_t requestPathLen; + HashMap *requestParams; + ssize_t requestPathLen; ssize_t i = 0; HttpRequestMethod requestMethod; @@ -470,7 +470,7 @@ HttpServerWorkerThread(void *args) } } - requestPathLen = i; + requestPathLen = i; requestPath = Malloc(((requestPathLen + 1) * sizeof(char))); strcpy(requestPath, pathPtr); @@ -482,17 +482,17 @@ HttpServerWorkerThread(void *args) goto bad_request; } - /* Find request params */ - for (i = 0; i < requestPathLen; i++) - { - if (requestPath[i] == '?') - { - break; - } - } + /* Find request params */ + for (i = 0; i < requestPathLen; i++) + { + if (requestPath[i] == '?') + { + break; + } + } - requestPath[i] = '\0'; - requestParams = HttpParamDecode(requestPath + i + 1); + requestPath[i] = '\0'; + requestParams = HttpParamDecode(requestPath + i + 1); context = HttpServerContextCreate(requestMethod, requestPath, requestParams, fp); if (!context)