Commit 43072d0a566bb7acc995f194d5424af239a732fc

Authored by Georg Hopp
1 parent 365fdd74

urldecode post values

... ... @@ -30,6 +30,7 @@
30 30
31 31 size_t rfc1123Gmt(char *, size_t, const time_t *);
32 32 size_t rfc1123GmtNow(char *, size_t);
  33 +size_t urldecode(char *, size_t);
33 34 char isHttpVersion(const char *, size_t);
34 35 HttpMessage httpGetMessage(
35 36 const char *, size_t,
... ...
... ... @@ -28,6 +28,8 @@
28 28 #include "hash.h"
29 29 #include "class.h"
30 30
  31 +#include "utils/http.h"
  32 +
31 33 /**
32 34 * \todo this is very similar to other pair parsing
33 35 * things... key1=val1<delim>key2=val2<delim>...keyn=valn
... ... @@ -40,6 +42,8 @@ httpParserPostVars(HttpParser this)
40 42 char * pair = this->current->body;
41 43 ssize_t togo = this->current->nbody;
42 44
  45 + togo = urldecode(pair, togo);
  46 +
43 47 while(NULL != pair && 0 < togo) {
44 48 char * key = pair;
45 49 char * eqsign = memchr(key, '=', togo);
... ...
... ... @@ -22,6 +22,7 @@
22 22
23 23 #include <time.h>
24 24 #include <stdlib.h>
  25 +#include <ctype.h>
25 26 #include <sys/types.h>
26 27 #include <string.h>
27 28
... ... @@ -33,6 +34,10 @@
33 34
34 35 #include "commons.h"
35 36
  37 +#define ALPHAVAL(x) (tolower((x)) - 'a' + 0xa)
  38 +#define DIGITVAL(x) ((x) - '0')
  39 +#define ALNUMVAL(x) (isdigit((x))?DIGITVAL((x)):ALPHAVAL((x)))
  40 +
36 41
37 42 static const char *DAY_NAMES[] = {
38 43 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
... ... @@ -68,6 +73,41 @@ rfc1123GmtNow(char * buffer, size_t _nbuf)
68 73 return rfc1123Gmt(buffer, _nbuf, &t);
69 74 }
70 75
  76 +/**
  77 + * Decode an url encoded string. This expects a valid url
  78 + * encoded string and it size as arguments, else the behaviour
  79 + * of this function is undefined.
  80 + * This function modifies the data in buffer. No copy is made.
  81 + * The reason for this is only performance.
  82 + */
  83 +size_t
  84 +urldecode(char * buffer, size_t nbuffer)
  85 +{
  86 + char * buf_ptr = buffer;
  87 + char * res_ptr = buffer;
  88 +
  89 + for(; 0 < nbuffer; nbuffer--, buf_ptr++, res_ptr++) {
  90 + switch(*buf_ptr) {
  91 + case '%':
  92 + *res_ptr = (ALNUMVAL(buf_ptr[1]) << 4) | ALNUMVAL(buf_ptr[2]);
  93 + buf_ptr += 2;
  94 + nbuffer -= 2;
  95 + break;
  96 +
  97 + case '+':
  98 + *buf_ptr = ' ';
  99 + /* intended drop through */
  100 +
  101 + default:
  102 + *res_ptr = *buf_ptr;
  103 + break;
  104 + }
  105 + }
  106 + *res_ptr = 0;
  107 +
  108 + return res_ptr - buffer;
  109 +}
  110 +
71 111 char
72 112 isHttpVersion(const char * str, size_t len)
73 113 {
... ...
Please register or login to post a comment