/[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 610 - (show annotations)
Fri Sep 24 20:35:36 2010 UTC (9 years, 2 months ago) by siliconforks
File MIME type: text/plain
File size: 6149 byte(s)
Remove hard-coded port numbers.

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(int argc, const char ** argv) {
30 #ifdef __MINGW32__
31 WSADATA data;
32 if (WSAStartup(MAKEWORD(1, 1), &data) != 0) {
33 return 1;
34 }
35 #endif
36
37 assert(argc == 2);
38 uint16_t port = (uint16_t) atoi(argv[1]);
39
40 SOCKET s = socket(PF_INET, SOCK_STREAM, 0);
41 assert(s != INVALID_SOCKET);
42
43 int optval = 1;
44 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *) &optval, sizeof(optval));
45
46 struct sockaddr_in a;
47 a.sin_family = AF_INET;
48 a.sin_port = htons(port);
49 a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
50 int result = bind(s, (struct sockaddr *) &a, sizeof(a));
51 assert(result == 0);
52
53 result = listen(s, 5);
54 assert(result == 0);
55
56 for (;;) {
57 struct sockaddr_in client_address;
58 size_t size = sizeof(client_address);
59 int client_socket = accept(s, (struct sockaddr *) &client_address, &size);
60 assert(client_socket > 0);
61
62 /* read request */
63 Stream * stream = Stream_new(0);
64 int state = 0;
65 while (state != 2) {
66 uint8_t buffer[8192];
67 ssize_t bytes_read = recv(client_socket, buffer, 8192, 0);
68 assert(bytes_read > 0);
69 Stream_write(stream, buffer, bytes_read);
70 for (int i = 0; i < bytes_read && state != 2; i++) {
71 uint8_t byte = buffer[i];
72 switch (state) {
73 case 0:
74 if (byte == '\n') {
75 state = 1;
76 }
77 else {
78 state = 0;
79 }
80 break;
81 case 1:
82 if (byte == '\n') {
83 state = 2;
84 }
85 else if (byte == '\r') {
86 state = 1;
87 }
88 else {
89 state = 0;
90 }
91 break;
92 }
93 }
94 }
95
96 char * method;
97 char * url;
98 char * request_line = (char *) stream->data;
99 char * first_space = strchr(request_line, ' ');
100 assert(first_space != NULL);
101 char * second_space = strchr(first_space + 1, ' ');
102 assert(second_space != NULL);
103 method = xstrndup(request_line, first_space - request_line);
104 url = xstrndup(first_space + 1, second_space - (first_space + 1));
105
106 /* send response */
107 char * message;
108 if (strcmp(url, "/lower") == 0) {
109 message = "HTTP/1.1 200 OK\r\n"
110 "Connection: close\r\n"
111 "Content-type: text/html\r\n"
112 "Transfer-Encoding: chunked\r\n"
113 "\r\n"
114 "a\r\n"
115 "abcdefghij\r\n"
116 "10\r\n"
117 "klmnopqrstuvwxyz\r\n"
118 "1\r\n"
119 "\n\r\n"
120 "0\r\n"
121 "\r\n";
122 }
123 else if (strcmp(url, "/upper") == 0) {
124 message = "HTTP/1.1 200 OK\r\n"
125 "Connection: close\r\n"
126 "Content-type: text/html\r\n"
127 "Transfer-Encoding: chunked\r\n"
128 "\r\n"
129 "10\r\n"
130 "ABCDEFGHIJKLMNOP\r\n"
131 "A\r\n"
132 "QRSTUVWXYZ\r\n"
133 "1\r\n"
134 "\n\r\n"
135 "0\r\n"
136 "\r\n";
137 }
138 else if (strcmp(url, "/javascript") == 0) {
139 message = "HTTP/1.1 200 OK\r\n"
140 "Connection: close\r\n"
141 "Content-Type: text/javascript\r\n"
142 "Transfer-Encoding: chunked\r\n"
143 "\r\n"
144 "9\r\n"
145 "if (1) {\n\r\n"
146 "C\r\n"
147 "hello = 10;\n\r\n"
148 "2\r\n"
149 "}\n\r\n"
150 "1\r\n"
151 "\n\r\n"
152 "0\r\n"
153 "\r\n";
154 }
155 else if (strcmp(url, "/trailer") == 0) {
156 message = "HTTP/1.1 200 OK\r\n"
157 "Connection: close\r\n"
158 "Content-type: text/html\r\n"
159 "Transfer-Encoding: chunked\r\n"
160 "\r\n"
161 "b\r\n"
162 "hello world\r\n"
163 "1\r\n"
164 "\n\r\n"
165 "0\r\n"
166 "X-Foo: bar\r\n"
167 "X-Bar: foo\r\n"
168 "\r\n";
169 }
170 else if (strcmp(url, "/overflow") == 0) {
171 message = "HTTP/1.1 200 OK\r\n"
172 "Connection: close\r\n"
173 "Content-type: text/html\r\n"
174 "Transfer-Encoding: chunked\r\n"
175 "\r\n"
176 "100000000\r\n"
177 "hello world\r\n"
178 "1\r\n"
179 "\n\r\n"
180 "0\r\n"
181 "\r\n";
182 }
183 else if (strcmp(url, "/multiple") == 0) {
184 message = "HTTP/1.1 200 OK\r\n"
185 "Connection: close\r\n"
186 "Content-type: text/html\r\n"
187 "Transfer-Encoding: foo; foo = bar, bar; foo = \"bar\"\r\n"
188 "Transfer-Encoding: foobar; foo = \"\\\"bar\\\"\", chunked\r\n"
189 "\r\n"
190 "b\r\n"
191 "hello world\r\n"
192 "1\r\n"
193 "\n\r\n"
194 "0\r\n"
195 "\r\n";
196 }
197 else {
198 abort();
199 }
200 size_t message_length = strlen(message);
201 ssize_t bytes_sent = send(client_socket, message, message_length, 0);
202 assert(bytes_sent == (ssize_t) message_length);
203
204 closesocket(client_socket);
205 }
206 return 0;
207 }

  ViewVC Help
Powered by ViewVC 1.1.24