Changes to update Tomato RAF.
[tomato.git] / release / src / router / httpd / blackhole.c
blobab7df7053d46f6c09f043d6d8e79e2ff0cd1dea6
1 /*
3 Tomato Firmware
4 Copyright (C) 2006-2009 Jonathan Zarate
6 */
8 #include "tomato.h"
10 #include <time.h>
13 #ifdef BLACKHOLE
14 void wi_blackhole(char *url, int len, char *boundary)
16 char buf[2048];
17 int size;
18 int n;
19 time_t tick;
20 int foo;
21 const char *error;
22 int blen;
23 FILE *f;
25 if (!post) {
26 send_header(200, NULL, mime_html, 0);
27 web_printf(
28 "<h1>Upload Test</h1>"
29 "<form method='post' action='blackhole.cgi?_http_id=%s' encType='multipart/form-data'>"
30 "<input type='file' name='file'><input type='submit'>"
31 "</form>",
32 nvram_safe_get("http_id"));
33 return;
36 check_id();
38 cprintf("\nblackhole\n");
39 cprintf("%s<\n", boundary);
41 if ((blen = strlen(boundary)) == 0) {
42 error = "no boundary";
43 ERROR:
44 cprintf("ERROR: %s\n", error);
46 web_eat(len);
48 send_header(200, NULL, mime_html, 0);
49 web_printf("ERROR: %s", error);
50 return;
53 if (blen > (sizeof(buf) - 32)) {
54 error = "boundary is too big";
55 goto ERROR;
58 // --b\r\n
59 // <data>\r\n
60 // --b--\r\n
61 if (len < ((blen * 2) + 12)) {
62 error = "not enough data";
63 goto ERROR;
66 foo = 1;
67 while (len > 0) {
68 if (!web_getline(buf, MIN(len, sizeof(buf)))) {
69 break;
71 n = strlen(buf);
72 len -= n;
74 if (n < 2) {
75 error = "n < 2";
76 goto ERROR;
78 if (buf[--n] != '\n') {
79 error = "\\n not found";
80 goto ERROR;
82 if (buf[--n] != '\r') {
83 error = "\\n without \\r";
84 goto ERROR;
86 if (n == 0) break;
87 buf[n] = 0;
89 cprintf("%s<\n", buf);
91 if (foo) {
92 if ((buf[0] != '-') || (buf[1] != '-') || (strcmp(buf + 2, boundary) != 0)) {
93 error = "boundary not found on first line";
94 goto ERROR;
96 foo = 0;
100 if (foo != 0) {
101 error = "boundary not found before reaching end of header";
102 goto ERROR;
105 blen += 6;
106 len -= (blen + 2);
107 if (len < 0) {
108 error = "not enough data for end boundary";
109 goto ERROR;
112 unlink("/tmp/blackhole");
113 /* if (len < (1*1024*1024)) f = fopen("/tmp/blackhole", "w");
114 else*/ f = NULL;
116 #if 0
117 // read half, trigger tcp reset
118 len >>= 1;
119 #endif
121 size = len;
122 tick = time(0);
123 while (len > 0) {
124 if ((n = web_read(buf, MIN(len, sizeof(buf)))) <= 0) {
125 break;
127 if (f) fwrite(buf, n, 1, f);
128 len -= n;
130 tick = time(0) - tick;
131 if (f) fclose(f);
133 if (len > 0) {
134 error = "not all data was read";
135 goto ERROR;
138 if (web_read(buf, blen) != blen) {
139 error = "error while reading end boundary";
140 goto ERROR;
142 buf[blen] = 0;
143 cprintf(">>%s<<\n", buf);
144 if ((strncmp(buf, "\r\n--", 4) != 0) ||
145 (buf[blen - 1] != '-') || (buf[blen - 2] != '-')
146 || (strncmp(buf + 4, boundary, blen - 6) != 0)) {
147 error = "end boundary not found";
148 goto ERROR;
151 len += 2;
152 if (len > 0) {
153 if ((n = web_read(buf, MIN(len, sizeof(buf) - 1))) > 0) {
154 len -= n;
155 buf[n] = 0;
156 cprintf("last >>%s<<\n", buf);
160 web_eat(len);
161 cprintf("len=%d\n", len);
163 if (tick > 0) n = size / tick;
164 else n = 0;
166 send_header(200, NULL, mime_html, 0);
167 web_printf(
168 "<pre>"
169 "Size .......: %d bytes\n"
170 "Time .......: %d seconds\n"
171 "Speed ......: %.2f kb/s | %.2f mb/s | %.2f KB/s | %.2f MB/s\n",
172 size,
173 tick,
174 n / 128.0, n / 131072.0,
175 n / 1024.0, n / 1048576.0);
176 if (f) {
177 web_pipecmd("md5sum /tmp/blackhole", WOF_NONE);
179 web_puts("</pre>");
181 cprintf("done...\n");
183 #endif