Implement param encoding, clean up warnings in decoding function.

This commit is contained in:
Jordan Bancino 2022-10-03 13:40:29 -04:00
parent fdac1361c1
commit 6b57fc45a2
2 changed files with 22 additions and 65 deletions

View file

@ -373,86 +373,41 @@ HttpUrlDecode(char *str)
} }
HashMap * HashMap *
HttpParamDecode(char *str) HttpParamDecode(FILE *in)
{ {
HashMap *decoded; /* TODO */
(void) in;
if (!str)
{
return NULL; 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 * void
HttpParamEncode(HashMap * params) HttpParamEncode(HashMap * params, FILE * out)
{ {
char *key; char *key;
char *val; char *val;
size_t len; if (!params || ! out)
size_t size = TELODENDRIA_STRING_CHUNK;
char *encoded;
if (!params)
{ {
return NULL; return;
}
len = 0;
encoded = malloc(size);
if (!encoded)
{
return NULL;
} }
while (HashMapIterate(params, &key, (void *) &val)) while (HashMapIterate(params, &key, (void *) &val))
{ {
/* Print key */ char *encKey;
char *encVal;
/* Print = */ encKey = HttpUrlEncode(key);
encVal = HttpUrlEncode(val);
/* Print encoded value */ if (!encKey || !encVal)
char *encVal = HttpParamEncode(val);
if (!encVal)
{ {
/* Memory error */ /* Memory error */
free(encoded); return;
return NULL;
} }
fprintf(out, "%s=%s&", encKey, encVal);
free(encKey);
free(encVal); 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 <stdio.h>
#include <HashMap.h> #include <HashMap.h>
typedef enum HttpRequestMethod typedef enum HttpRequestMethod
@ -118,9 +120,9 @@ extern char *
HttpUrlDecode(char *); HttpUrlDecode(char *);
extern HashMap * extern HashMap *
HttpParamDecode(char *); HttpParamDecode(FILE *);
extern char * extern void
HttpParamEncode(HashMap *); HttpParamEncode(HashMap *, FILE *);
#endif #endif