File: cgi.c

package info (click to toggle)
gatling 0.13-6
  • links: PTS
  • area: main
  • in suites: sid, stretch
  • size: 1,196 kB
  • ctags: 1,115
  • sloc: ansic: 23,805; makefile: 143; sh: 71; perl: 30
file content (72 lines) | stat: -rw-r--r-- 1,355 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fmt.h>
#include <scan.h>
#include <socket.h>
#include <getopt.h>
#include <ip6.h>
#include <buffer.h>

#define MAXARGLEN (64*1024)

enum {
  CGI, SCGI, FASTCGI
} cgimode;

/* return content length */
static long do_cgi(char** res) {
  long l=-1;
  *res=0;
  char* method=getenv("REQUEST_METHOD");
  if (method) {
    if (!strcmp(method,"GET")) {
      *res=getenv("QUERY_STRING");
      if (*res)
	l=strlen(*res);
      else
	l=0;
    } else if (!strcmp(method,"POST")) {
      char* x;
      if ((x=getenv("CONTENT_LENGTH"))) {
	l=atol(x);
	if ((l>0) && (l<MAXARGLEN) && (x=malloc(l+1))) {
	  long rest=l;
	  *res=x;
	  while (rest) {
	    long r=read(0,x,rest);
	    if (r<=0) {
	      *res=0;
	      return -1;
	    }
	    rest-=r;
	    x+=r;
	  }
	  *x=0;
	} else
	  l=0;
      }
    }
  }
  return l;
}

int main(int argc,char* argv[],char* envp[]) {
  int i;
  char* c;
  long l;
  unsigned long port;
  (void)argc;
  (void)argv;
  buffer_puts(buffer_1,"Content-Type: text/plain\r\n\r\n");
  l=do_cgi(&c);
  if (l>0) {
    buffer_puts(buffer_1,"CGI arguments:\n\n  --==[snip]==--\n");
    buffer_put(buffer_1,c,l);
    buffer_puts(buffer_1,"\n\n  --==[snip]==--\n\n");
  }
  for (i=0; envp[i]; ++i)
    buffer_putm(buffer_1,envp[i],"\n");
  buffer_flush(buffer_1);
  return 0;
}