Complete parameter parsing

This commit is contained in:
Jordan Bancino 2022-10-15 19:17:49 -04:00
parent 52abd18324
commit 5ca5ec7dd4
2 changed files with 119 additions and 129 deletions

View file

@ -374,126 +374,116 @@ HttpUrlDecode(char *str)
} }
HashMap * HashMap *
HttpParamDecode(char * in) HttpParamDecode(char *in)
{ {
HashMap *params; HashMap *params;
if (!in) if (!in)
{ {
return NULL; return NULL;
} }
printf("HttpParamDecode(%s)\n", in); params = HashMapCreate();
fflush(stdout); if (!params)
{
return NULL;
}
params = HashMapCreate(); while (*in)
if (!params) {
{ char *buf;
return NULL; size_t allocated;
} size_t len;
while (*in) char *decKey;
{ char *decVal;
char *buf;
size_t allocated;
size_t len;
char *decKey; /* Read in key */
char *decVal;
/* Read in key */ allocated = TELODENDRIA_STRING_CHUNK;
buf = Malloc(allocated);
len = 0;
allocated = TELODENDRIA_STRING_CHUNK; while (*in && *in != '=')
buf = Malloc(allocated); {
len = 0; if (len >= allocated - 1)
{
allocated += TELODENDRIA_STRING_CHUNK;
buf = Realloc(buf, allocated);
}
while (*in && *in != '=') buf[len] = *in;
{ len++;
if (len >= allocated) in++;
{ }
allocated += TELODENDRIA_STRING_CHUNK;
buf = Realloc(buf, allocated);
}
buf[len] = *in; buf[len] = '\0';
len++;
in++;
}
/* Sanity check */ /* Sanity check */
if (*in != '=') if (*in != '=')
{ {
/* Malformed param */ /* Malformed param */
Free(buf); Free(buf);
Free(params); Free(params);
return NULL; return NULL;
} }
in++; in++;
/* Decode key */ /* Decode key */
decKey = HttpUrlDecode(buf); decKey = HttpUrlDecode(buf);
Free(buf); Free(buf);
if (!decKey) if (!decKey)
{ {
/* Decoding error */ /* Decoding error */
Free(params); Free(params);
return NULL; return NULL;
} }
/* Read in value */ /* Read in value */
allocated = TELODENDRIA_STRING_CHUNK; allocated = TELODENDRIA_STRING_CHUNK;
buf = Malloc(allocated); buf = Malloc(allocated);
len = 0; len = 0;
while (*in && *in != '&') while (*in && *in != '&')
{ {
if (len >= allocated) if (len >= allocated - 1)
{ {
allocated += TELODENDRIA_STRING_CHUNK; allocated += TELODENDRIA_STRING_CHUNK;
buf = Realloc(buf, allocated); buf = Realloc(buf, allocated);
} }
buf[len] = *in; buf[len] = *in;
len++; len++;
in++; in++;
} }
/* Decode value */ buf[len] = '\0';
decVal = HttpUrlDecode(buf);
Free(buf);
if (!decVal) /* Decode value */
{ decVal = HttpUrlDecode(buf);
/* Decoding error */ Free(buf);
Free(params);
return NULL;
}
HashMapSet(params, decKey, decVal); if (!decVal)
{
/* Decoding error */
Free(params);
return NULL;
}
if (*in == '&') HashMapSet(params, decKey, decVal);
{
in++;
continue;
}
else
{
break;
}
}
printf("Done with decoding, here's what we got:\n"); if (*in == '&')
{ {
char *key; in++;
char *val; continue;
}
while (HashMapIterate(params, &key, (void **) &val)) else
{ {
printf(" [%s]: [%s]\n", key, val); break;
} }
} }
return params; return params;
} }
@ -503,7 +493,7 @@ HttpParamEncode(HashMap * params)
{ {
char *key; char *key;
char *val; char *val;
char *out = NULL; char *out = NULL;
if (!params || !out) if (!params || !out)
{ {
@ -521,16 +511,16 @@ HttpParamEncode(HashMap * params)
if (!encKey || !encVal) if (!encKey || !encVal)
{ {
/* Memory error */ /* Memory error */
Free(encKey); Free(encKey);
Free(encVal); Free(encVal);
return NULL; return NULL;
} }
/* TODO */ /* TODO */
Free(encKey); Free(encKey);
Free(encVal); Free(encVal);
} }
return out; return out;
} }

View file

@ -65,7 +65,7 @@ struct HttpServerContext
HashMap *requestHeaders; HashMap *requestHeaders;
HttpRequestMethod requestMethod; HttpRequestMethod requestMethod;
char *requestPath; char *requestPath;
HashMap *requestParams; HashMap *requestParams;
HashMap *responseHeaders; HashMap *responseHeaders;
HttpStatus responseStatus; HttpStatus responseStatus;
@ -75,7 +75,7 @@ struct HttpServerContext
static HttpServerContext * static HttpServerContext *
HttpServerContextCreate(HttpRequestMethod requestMethod, HttpServerContextCreate(HttpRequestMethod requestMethod,
char *requestPath, HashMap *requestParams, FILE * stream) char *requestPath, HashMap * requestParams, FILE * stream)
{ {
HttpServerContext *c; HttpServerContext *c;
@ -102,7 +102,7 @@ HttpServerContextCreate(HttpRequestMethod requestMethod,
c->requestMethod = requestMethod; c->requestMethod = requestMethod;
c->requestPath = requestPath; c->requestPath = requestPath;
c->requestParams = requestParams; c->requestParams = requestParams;
c->stream = stream; c->stream = stream;
c->responseStatus = HTTP_OK; c->responseStatus = HTTP_OK;
@ -135,13 +135,13 @@ HttpServerContextFree(HttpServerContext * c)
HashMapFree(c->responseHeaders); HashMapFree(c->responseHeaders);
while (HashMapIterate(c->requestParams, &key, &val)) while (HashMapIterate(c->requestParams, &key, &val))
{ {
Free(key); Free(key);
Free(val); Free(val);
} }
HashMapFree(c->requestParams); HashMapFree(c->requestParams);
Free(c->requestPath); Free(c->requestPath);
fclose(c->stream); fclose(c->stream);
@ -185,10 +185,10 @@ HttpRequestPath(HttpServerContext * c)
HashMap * HashMap *
HttpRequestParams(HttpServerContext * c) HttpRequestParams(HttpServerContext * c)
{ {
if (!c) if (!c)
{ {
return NULL; return NULL;
} }
return c->requestParams; return c->requestParams;
} }
@ -416,8 +416,8 @@ HttpServerWorkerThread(void *args)
char *requestPath; char *requestPath;
char *requestProtocol; char *requestProtocol;
HashMap *requestParams; HashMap *requestParams;
ssize_t requestPathLen; ssize_t requestPathLen;
ssize_t i = 0; ssize_t i = 0;
HttpRequestMethod requestMethod; HttpRequestMethod requestMethod;
@ -470,7 +470,7 @@ HttpServerWorkerThread(void *args)
} }
} }
requestPathLen = i; requestPathLen = i;
requestPath = Malloc(((requestPathLen + 1) * sizeof(char))); requestPath = Malloc(((requestPathLen + 1) * sizeof(char)));
strcpy(requestPath, pathPtr); strcpy(requestPath, pathPtr);
@ -482,17 +482,17 @@ HttpServerWorkerThread(void *args)
goto bad_request; goto bad_request;
} }
/* Find request params */ /* Find request params */
for (i = 0; i < requestPathLen; i++) for (i = 0; i < requestPathLen; i++)
{ {
if (requestPath[i] == '?') if (requestPath[i] == '?')
{ {
break; break;
} }
} }
requestPath[i] = '\0'; requestPath[i] = '\0';
requestParams = HttpParamDecode(requestPath + i + 1); requestParams = HttpParamDecode(requestPath + i + 1);
context = HttpServerContextCreate(requestMethod, requestPath, requestParams, fp); context = HttpServerContextCreate(requestMethod, requestPath, requestParams, fp);
if (!context) if (!context)