Start work on param parsing.

This commit is contained in:
Jordan Bancino 2022-09-08 21:09:47 -04:00
parent 485941e8a8
commit 685b7c016d
2 changed files with 97 additions and 2 deletions

View file

@ -27,6 +27,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <Constants.h>
#include <HashMap.h>
const char * const char *
HttpRequestMethodToString(const HttpRequestMethod method) HttpRequestMethodToString(const HttpRequestMethod method)
{ {
@ -226,7 +229,7 @@ HttpUrlEncode(char *str)
return NULL; return NULL;
} }
size = 16; size = TELODENDRIA_STRING_CHUNK;
len = 0; len = 0;
encoded = malloc(size); encoded = malloc(size);
if (!encoded) if (!encoded)
@ -242,7 +245,7 @@ HttpUrlEncode(char *str)
{ {
char *tmp; char *tmp;
size *= 2; size += TELODENDRIA_STRING_CHUNK;
tmp = realloc(encoded, size); tmp = realloc(encoded, size);
if (!tmp) if (!tmp)
{ {
@ -360,3 +363,87 @@ HttpUrlDecode(char *str)
return decoded; return decoded;
} }
HashMap *
HttpParamDecode(char *str)
{
HashMap *decoded;
if (!str)
{
return NULL;
}
decoded = HashMapCreate();
if (!decoded)
{
return NULL;
}
while (*str)
{
char *key;
char *val;
char *decVal;
decVal = HttpParamDecode(val);
if (!decVal)
{
/* Memory error */
}
free(val);
str++;
}
return decoded;
}
char *
HttpParamEncode(HashMap *params)
{
char *key;
char *val;
size_t len;
size_t size = TELODENDRIA_STRING_CHUNK;
char *encoded;
if (!params)
{
return NULL;
}
len = 0;
encoded = malloc(size);
if (!encoded)
{
return NULL;
}
while (HashMapIterate(params, &key, (void *) &val))
{
/* Print key */
/* Print = */
/* Print encoded value */
char *encVal = HttpParamEncode(val);
if (!encVal)
{
/* Memory error */
free(encoded);
return NULL;
}
free(encVal);
/* Print & */
}
/* Overwrite last & */
return encoded;
}

View file

@ -24,6 +24,8 @@
#ifndef TELODENDRIA_HTTP_H #ifndef TELODENDRIA_HTTP_H
#define TELODENDRIA_HTTP_H #define TELODENDRIA_HTTP_H
#include <HashMap.h>
typedef enum HttpRequestMethod typedef enum HttpRequestMethod
{ {
HTTP_METHOD_UNKNOWN, HTTP_METHOD_UNKNOWN,
@ -115,4 +117,10 @@ extern char *
extern char * extern char *
HttpUrlDecode(char *); HttpUrlDecode(char *);
extern HashMap *
HttpParamDecode(char *);
extern char *
HttpParamEncode(HashMap *);
#endif #endif