8 char *file
= "/tmp/log";
13 unsigned char *string
;
17 } tab
[100000]; /* should be enough */
19 unsigned char header
[] =
20 "#!/usr/bin/env python\n"
21 "import BaseHTTPServer\n\n";
23 unsigned char footer
[] =
24 "class Serwer(BaseHTTPServer.BaseHTTPRequestHandler):\n"
25 "\tdef do_GET(self):\n"
26 "\t\tglobal slownik\n"
27 "\t\tof = open(slownik[self.path])\n"
28 "\t\tprint (self.path)\n"
29 "\t\tself.wfile.write(of.read())\n"
31 "\tdef do_POST(self):\n"
32 "\t\tself.do_GET()\n\n"
33 "def run(server_class = BaseHTTPServer.HTTPServer, handler_class = Serwer):\n"
34 "\tserver_address = ('', 8000)\n"
35 "\thttpd = server_class(server_address, handler_class)\n"
36 "\thttpd.serve_forever()\n\n"
39 static unsigned char *
40 find(unsigned char *from
, unsigned char *key
)
42 unsigned char *end
= data
+ st
.st_size
;
46 for (beg
= from
;; beg
++) {
47 beg
= memchr(beg
, key
[0], end
- beg
);
50 if (!strncmp(beg
, key
, l
))
59 unsigned char *current
= data
;
62 unsigned char *conn
= find(current
, "CONNECTION:");
63 unsigned char *get
= find(current
, "GET /");
64 unsigned char *post
= find(current
, "POST /");
65 unsigned char *host
, *space
, *http
;
69 unsigned char *min
= data
+ st
.st_size
;
75 if (post
&& post
< min
)
77 tab
[counter
- 1].length
= min
- http
;
95 space
= strchr(current
, ' ');
99 host
= find(space
+ 1, "Host: ");
104 tab
[counter
].string
= current
;
106 enter
= strcspn(host
, "\r\n\0");
108 tab
[counter
].host
= host
;
110 http
= find(host
+ enter
+ 1, "HTTP/");
113 tab
[counter
++].data
= http
;
122 fprintf(f
, "slownik = {\n");
123 for (i
= 0; i
< counter
- 1; i
++) {
124 fprintf(f
, "\t'http://%s%s' : '%d.http',\n", tab
[i
].host
, tab
[i
].string
, i
);
126 for (; i
< counter
; i
++) {
127 fprintf(f
, "\t'http://%s%s' : '%d.http'\n", tab
[i
].host
, tab
[i
].string
, i
);
138 for (i
= 0; i
< counter
; i
++) {
141 snprintf(buf
, 12, "%d.http", i
);
145 fwrite(tab
[i
].data
, 1, tab
[i
].length
, f
);
148 f
= fopen("proxy.py", "w");
151 fprintf(f
, "%s", header
);
153 fprintf(f
, "%s", footer
);
158 main(int argc
, char **argv
)
164 f
= fopen(file
, "r");
168 data
= calloc(1, st
.st_size
+ 1);
171 fread(data
, 1, st
.st_size
, f
);