forked from lda/telodendria
Hook param parsing into the request logic.
This commit is contained in:
parent
14c79a901a
commit
b645a0b2c1
3 changed files with 67 additions and 20 deletions
35
src/Http.c
35
src/Http.c
|
@ -374,22 +374,37 @@ HttpUrlDecode(char *str)
|
||||||
}
|
}
|
||||||
|
|
||||||
HashMap *
|
HashMap *
|
||||||
HttpParamDecode(FILE * in)
|
HttpParamDecode(char * in)
|
||||||
|
{
|
||||||
|
HashMap *params;
|
||||||
|
|
||||||
|
if (!in)
|
||||||
{
|
{
|
||||||
/* TODO */
|
|
||||||
(void) in;
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
printf("HttpParamDecode(%s)\n", in);
|
||||||
HttpParamEncode(HashMap * params, FILE * out)
|
fflush(stdout);
|
||||||
|
|
||||||
|
params = HashMapCreate();
|
||||||
|
if (!params)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *
|
||||||
|
HttpParamEncode(HashMap * params)
|
||||||
{
|
{
|
||||||
char *key;
|
char *key;
|
||||||
char *val;
|
char *val;
|
||||||
|
char *out = NULL;
|
||||||
|
|
||||||
if (!params || !out)
|
if (!params || !out)
|
||||||
{
|
{
|
||||||
return;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (HashMapIterate(params, &key, (void *) &val))
|
while (HashMapIterate(params, &key, (void *) &val))
|
||||||
|
@ -403,12 +418,16 @@ HttpParamEncode(HashMap * params, FILE * out)
|
||||||
if (!encKey || !encVal)
|
if (!encKey || !encVal)
|
||||||
{
|
{
|
||||||
/* Memory error */
|
/* Memory error */
|
||||||
return;
|
Free(encKey);
|
||||||
|
Free(encVal);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(out, "%s=%s&", encKey, encVal);
|
/* TODO */
|
||||||
|
|
||||||
Free(encKey);
|
Free(encKey);
|
||||||
Free(encVal);
|
Free(encVal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return out;
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,6 +65,7 @@ struct HttpServerContext
|
||||||
HashMap *requestHeaders;
|
HashMap *requestHeaders;
|
||||||
HttpRequestMethod requestMethod;
|
HttpRequestMethod requestMethod;
|
||||||
char *requestPath;
|
char *requestPath;
|
||||||
|
HashMap *requestParams;
|
||||||
|
|
||||||
HashMap *responseHeaders;
|
HashMap *responseHeaders;
|
||||||
HttpStatus responseStatus;
|
HttpStatus responseStatus;
|
||||||
|
@ -74,7 +75,7 @@ struct HttpServerContext
|
||||||
|
|
||||||
static HttpServerContext *
|
static HttpServerContext *
|
||||||
HttpServerContextCreate(HttpRequestMethod requestMethod,
|
HttpServerContextCreate(HttpRequestMethod requestMethod,
|
||||||
char *requestPath, FILE * stream)
|
char *requestPath, HashMap *requestParams, FILE * stream)
|
||||||
{
|
{
|
||||||
HttpServerContext *c;
|
HttpServerContext *c;
|
||||||
|
|
||||||
|
@ -101,6 +102,7 @@ HttpServerContextCreate(HttpRequestMethod requestMethod,
|
||||||
|
|
||||||
c->requestMethod = requestMethod;
|
c->requestMethod = requestMethod;
|
||||||
c->requestPath = requestPath;
|
c->requestPath = requestPath;
|
||||||
|
c->requestParams = requestParams;
|
||||||
c->stream = stream;
|
c->stream = stream;
|
||||||
c->responseStatus = HTTP_OK;
|
c->responseStatus = HTTP_OK;
|
||||||
|
|
||||||
|
@ -133,6 +135,14 @@ HttpServerContextFree(HttpServerContext * c)
|
||||||
|
|
||||||
HashMapFree(c->responseHeaders);
|
HashMapFree(c->responseHeaders);
|
||||||
|
|
||||||
|
while (HashMapIterate(c->requestParams, &key, &val))
|
||||||
|
{
|
||||||
|
Free(key);
|
||||||
|
Free(val);
|
||||||
|
}
|
||||||
|
|
||||||
|
HashMapFree(c->requestParams);
|
||||||
|
|
||||||
Free(c->requestPath);
|
Free(c->requestPath);
|
||||||
fclose(c->stream);
|
fclose(c->stream);
|
||||||
|
|
||||||
|
@ -175,11 +185,14 @@ HttpRequestPath(HttpServerContext * c)
|
||||||
HashMap *
|
HashMap *
|
||||||
HttpRequestParams(HttpServerContext * c)
|
HttpRequestParams(HttpServerContext * c)
|
||||||
{
|
{
|
||||||
/* TODO: Implement param parsing */
|
if (!c)
|
||||||
(void) c;
|
{
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return c->requestParams;
|
||||||
|
}
|
||||||
|
|
||||||
char *
|
char *
|
||||||
HttpResponseHeader(HttpServerContext * c, char *key, char *val)
|
HttpResponseHeader(HttpServerContext * c, char *key, char *val)
|
||||||
{
|
{
|
||||||
|
@ -403,12 +416,15 @@ HttpServerWorkerThread(void *args)
|
||||||
char *requestPath;
|
char *requestPath;
|
||||||
char *requestProtocol;
|
char *requestProtocol;
|
||||||
|
|
||||||
|
HashMap *requestParams;
|
||||||
|
ssize_t requestPathLen;
|
||||||
|
|
||||||
ssize_t i = 0;
|
ssize_t i = 0;
|
||||||
HttpRequestMethod requestMethod;
|
HttpRequestMethod requestMethod;
|
||||||
|
|
||||||
if (!fp)
|
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 */
|
* murder the CPU */
|
||||||
UtilSleepMillis(1);
|
UtilSleepMillis(1);
|
||||||
continue;
|
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);
|
strcpy(requestPath, pathPtr);
|
||||||
|
|
||||||
requestProtocol = &pathPtr[i + 1];
|
requestProtocol = &pathPtr[i + 1];
|
||||||
|
@ -462,11 +479,22 @@ HttpServerWorkerThread(void *args)
|
||||||
|
|
||||||
if (strcmp(requestProtocol, "HTTP/1.1") != 0 && strcmp(requestProtocol, "HTTP/1.0") != 0)
|
if (strcmp(requestProtocol, "HTTP/1.1") != 0 && strcmp(requestProtocol, "HTTP/1.0") != 0)
|
||||||
{
|
{
|
||||||
printf("Bad protocol: [%s]\n", requestProtocol);
|
|
||||||
goto bad_request;
|
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)
|
if (!context)
|
||||||
{
|
{
|
||||||
goto internal_error;
|
goto internal_error;
|
||||||
|
|
|
@ -120,9 +120,9 @@ extern char *
|
||||||
HttpUrlDecode(char *);
|
HttpUrlDecode(char *);
|
||||||
|
|
||||||
extern HashMap *
|
extern HashMap *
|
||||||
HttpParamDecode(FILE *);
|
HttpParamDecode(char *);
|
||||||
|
|
||||||
extern void
|
extern char *
|
||||||
HttpParamEncode(HashMap *, FILE *);
|
HttpParamEncode(HashMap *);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue