Merge branch 'master' of github:szmoore/MCTX3420
[matches/MCTX3420.git] / testing / fastcgi-approach / post / post.c
1 #include <fcgi_stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <ctype.h>
5 char *FCGI_URLDecode(char *buf);
6
7 int main() {
8         while (FCGI_Accept() >= 0) {
9                 char buf[BUFSIZ];
10                 printf("Content-type: text/plain\r\n\r\n");
11
12
13                 while(fgets(buf, BUFSIZ, stdin)) {
14                         printf("POST (raw):\r\n");
15                         printf("%s", buf);
16                         printf("\r\nPOST (decoded):\r\n");
17                         printf("%s", FCGI_URLDecode(buf));
18                 }
19
20                 snprintf(buf, BUFSIZ, "%s", getenv("QUERY_STRING"));
21                 printf("\r\nGET (raw):\r\n");
22                 printf("%s", getenv("QUERY_STRING"));
23
24                 printf("\r\nGET (decoded):\r\n");
25                 printf("%s", FCGI_URLDecode(buf));
26         }
27         return 0;
28
29 }
30
31 char *FCGI_URLDecode(char *buf) {
32         char *head = buf, *tail = buf;
33         char hex[3] = {0};
34         while (*tail) {
35                 if (*tail == '%') {
36                         tail++;
37                         if (isxdigit(*tail) && isxdigit(*(tail+1))) {
38                                 hex[0] = *tail++;
39                                 hex[1] = *tail++;
40                                 *head++ = (char)strtol(hex, NULL, 16);
41                         } else {
42                                 head++;
43                         }
44                 } else if (*tail == '+') {
45                         tail++;
46                         *head++ = ' ';
47                 } else {
48                         *head++ = *tail++;
49                 }
50         }
51         *head = 0;
52         return buf;
53 }

UCC git Repository :: git.ucc.asn.au