/[jscoverage]/trunk/tests/http-server-chunked.c
ViewVC logotype

Contents of /trunk/tests/http-server-chunked.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 505 - (show annotations)
Sat Jan 2 04:55:54 2010 UTC (9 years, 8 months ago) by siliconforks
File MIME type: text/plain
File size: 6378 byte(s)
Update copyright year.

1 /*
2 http-server-chunked.c - HTTP server that outputs chunked response
3 Copyright (C) 2008, 2009, 2010 siliconforks.com
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include <config.h>
21
22 #include <assert.h>
23 #include <string.h>
24
25 #include "http-server.h"
26 #include "stream.h"
27 #include "util.h"
28
29 int main(void) {
30 #ifdef __MINGW32__
31 WSADATA data;
32 if (WSAStartup(MAKEWORD(1, 1), &data) != 0) {
33 return 1;
34 }
35 #endif
36
37 SOCKET s = socket(PF_INET, SOCK_STREAM, 0);
38 assert(s != INVALID_SOCKET);
39
40 int optval = 1;
41 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *) &optval, sizeof(optval));
42
43 struct sockaddr_in a;
44 a.sin_family = AF_INET;
45 a.sin_port = htons(8000);
46 a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
47 int result = bind(s, (struct sockaddr *) &a, sizeof(a));
48 assert(result == 0);
49
50 result = listen(s, 5);
51 assert(result == 0);
52
53 for (;;) {
54 struct sockaddr_in client_address;
55 size_t size = sizeof(client_address);
56 int client_socket = accept(s, (struct sockaddr *) &client_address, &size);
57 assert(client_socket > 0);
58
59 /* read request */
60 Stream * stream = Stream_new(0);
61 int state = 0;
62 while (state != 2) {
63 uint8_t buffer[8192];
64 ssize_t bytes_read = recv(client_socket, buffer, 8192, 0);
65 assert(bytes_read > 0);
66 Stream_write(stream, buffer, bytes_read);
67 for (int i = 0; i < bytes_read && state != 2; i++) {
68 uint8_t byte = buffer[i];
69 switch (state) {
70 case 0:
71 if (byte == '\n') {
72 state = 1;
73 }
74 else {
75 state = 0;
76 }
77 break;
78 case 1:
79 if (byte == '\n') {
80 state = 2;
81 }
82 else if (byte == '\r') {
83 state = 1;
84 }
85 else {
86 state = 0;
87 }
88 break;
89 }
90 }
91 }
92
93 char * method;
94 char * url;
95 char * request_line = (char *) stream->data;
96 char * first_space = strchr(request_line, ' ');
97 assert(first_space != NULL);
98 char * second_space = strchr(first_space + 1, ' ');
99 assert(second_space != NULL);
100 method = xstrndup(request_line, first_space - request_line);
101 url = xstrndup(first_space + 1, second_space - (first_space + 1));
102
103 /* send response */
104 char * message;
105 if (strcmp(url, "http://127.0.0.1:8000/lower") == 0 || strcmp(url, "/lower") == 0) {
106 message = "HTTP/1.1 200 OK\r\n"
107 "Connection: close\r\n"
108 "Content-type: text/html\r\n"
109 "Transfer-Encoding: chunked\r\n"
110 "\r\n"
111 "a\r\n"
112 "abcdefghij\r\n"
113 "10\r\n"
114 "klmnopqrstuvwxyz\r\n"
115 "1\r\n"
116 "\n\r\n"
117 "0\r\n"
118 "\r\n";
119 }
120 else if (strcmp(url, "http://127.0.0.1:8000/upper") == 0 || strcmp(url, "/upper") == 0) {
121 message = "HTTP/1.1 200 OK\r\n"
122 "Connection: close\r\n"
123 "Content-type: text/html\r\n"
124 "Transfer-Encoding: chunked\r\n"
125 "\r\n"
126 "10\r\n"
127 "ABCDEFGHIJKLMNOP\r\n"
128 "A\r\n"
129 "QRSTUVWXYZ\r\n"
130 "1\r\n"
131 "\n\r\n"
132 "0\r\n"
133 "\r\n";
134 }
135 else if (strcmp(url, "http://127.0.0.1:8000/javascript") == 0 || strcmp(url, "/javascript") == 0) {
136 message = "HTTP/1.1 200 OK\r\n"
137 "Connection: close\r\n"
138 "Content-Type: text/javascript\r\n"
139 "Transfer-Encoding: chunked\r\n"
140 "\r\n"
141 "9\r\n"
142 "if (1) {\n\r\n"
143 "C\r\n"
144 "hello = 10;\n\r\n"
145 "2\r\n"
146 "}\n\r\n"
147 "1\r\n"
148 "\n\r\n"
149 "0\r\n"
150 "\r\n";
151 }
152 else if (strcmp(url, "http://127.0.0.1:8000/trailer") == 0 || strcmp(url, "/trailer") == 0) {
153 message = "HTTP/1.1 200 OK\r\n"
154 "Connection: close\r\n"
155 "Content-type: text/html\r\n"
156 "Transfer-Encoding: chunked\r\n"
157 "\r\n"
158 "b\r\n"
159 "hello world\r\n"
160 "1\r\n"
161 "\n\r\n"
162 "0\r\n"
163 "X-Foo: bar\r\n"
164 "X-Bar: foo\r\n"
165 "\r\n";
166 }
167 else if (strcmp(url, "http://127.0.0.1:8000/overflow") == 0 || strcmp(url, "/overflow") == 0) {
168 message = "HTTP/1.1 200 OK\r\n"
169 "Connection: close\r\n"
170 "Content-type: text/html\r\n"
171 "Transfer-Encoding: chunked\r\n"
172 "\r\n"
173 "100000000\r\n"
174 "hello world\r\n"
175 "1\r\n"
176 "\n\r\n"
177 "0\r\n"
178 "\r\n";
179 }
180 else if (strcmp(url, "http://127.0.0.1:8000/multiple") == 0 || strcmp(url, "/multiple") == 0) {
181 message = "HTTP/1.1 200 OK\r\n"
182 "Connection: close\r\n"
183 "Content-type: text/html\r\n"
184 "Transfer-Encoding: foo; foo = bar, bar; foo = \"bar\"\r\n"
185 "Transfer-Encoding: foobar; foo = \"\\\"bar\\\"\", chunked\r\n"
186 "\r\n"
187 "b\r\n"
188 "hello world\r\n"
189 "1\r\n"
190 "\n\r\n"
191 "0\r\n"
192 "\r\n";
193 }
194 else {
195 abort();
196 }
197 size_t message_length = strlen(message);
198 ssize_t bytes_sent = send(client_socket, message, message_length, 0);
199 assert(bytes_sent == (ssize_t) message_length);
200
201 closesocket(client_socket);
202 }
203 return 0;
204 }

  ViewVC Help
Powered by ViewVC 1.1.24