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 *
HttpParamDecode(char *str)
HttpParamDecode(FILE *in)
{
HashMap *decoded;
if (!str)
{
/* TODO */
(void) in;
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)
void
HttpParamEncode(HashMap * params, FILE * out)
{
char *key;
char *val;
size_t len;
size_t size = TELODENDRIA_STRING_CHUNK;
char *encoded;
if (!params)
if (!params || ! out)
{
return NULL;
}
len = 0;
encoded = malloc(size);
if (!encoded)
{
return NULL;
return;
}
while (HashMapIterate(params, &key, (void *) &val))
{
/* Print key */
char *encKey;
char *encVal;
/* Print = */
encKey = HttpUrlEncode(key);
encVal = HttpUrlEncode(val);
/* Print encoded value */
char *encVal = HttpParamEncode(val);
if (!encVal)
if (!encKey || !encVal)
{
/* Memory error */
free(encoded);
return NULL;
return;
}
fprintf(out, "%s=%s&", encKey, encVal);
free(encKey);
free(encVal);
/* Print & */
}
/* Overwrite last & */
return encoded;
}

View file

@ -24,6 +24,8 @@
#ifndef TELODENDRIA_HTTP_H
#define TELODENDRIA_HTTP_H
#include <stdio.h>
#include <HashMap.h>
typedef enum HttpRequestMethod
@ -118,9 +120,9 @@ extern char *
HttpUrlDecode(char *);
extern HashMap *
HttpParamDecode(char *);
HttpParamDecode(FILE *);
extern char *
HttpParamEncode(HashMap *);
extern void
HttpParamEncode(HashMap *, FILE *);
#endif