/[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 126 - (show annotations)
Mon Jun 2 17:53:43 2008 UTC (11 years, 2 months ago) by siliconforks
File MIME type: text/plain
File size: 6039 byte(s)
Fixes for MinGW.

1 /*
2 http-server-chunked.c - HTTP server that outputs chunked response
3 Copyright (C) 2008 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 }
89 }
90 }
91
92 char * method;
93 char * url;
94 char * request_line = stream->data;
95 char * first_space = strchr(request_line, ' ');
96 assert(first_space != NULL);
97 char * second_space = strchr(first_space + 1, ' ');
98 assert(second_space != NULL);
99 method = xstrndup(stream->data, first_space - request_line);
100 url = xstrndup(first_space + 1, second_space - (first_space + 1));
101
102 /* send response */
103 char * message;
104 if (strcmp(url, "http://127.0.0.1:8000/lower") == 0 || strcmp(url, "/lower") == 0) {
105 message = "HTTP/1.1 200 OK\r\n"
106 "Connection: close\r\n"
107 "Content-type: text/html\r\n"
108 "Transfer-Encoding: chunked\r\n"
109 "\r\n"
110 "b\r\n"
111 "hello world\r\n"
112 "1\r\n"
113 "\n\r\n"
114 "0\r\n"
115 "\r\n";
116 }
117 else if (strcmp(url, "http://127.0.0.1:8000/upper") == 0 || strcmp(url, "/upper") == 0) {
118 message = "HTTP/1.1 200 OK\r\n"
119 "Connection: close\r\n"
120 "Content-type: text/html\r\n"
121 "Transfer-Encoding: chunked\r\n"
122 "\r\n"
123 "B\r\n"
124 "HELLO WORLD\r\n"
125 "1\r\n"
126 "\n\r\n"
127 "0\r\n"
128 "\r\n";
129 }
130 else if (strcmp(url, "http://127.0.0.1:8000/javascript") == 0 || strcmp(url, "/javascript") == 0) {
131 message = "HTTP/1.1 200 OK\r\n"
132 "Connection: close\r\n"
133 "Content-Type: text/javascript\r\n"
134 "Transfer-Encoding: chunked\r\n"
135 "\r\n"
136 "B\r\n"
137 "hello = 10;\r\n"
138 "1\r\n"
139 "\n\r\n"
140 "0\r\n"
141 "\r\n";
142 }
143 else if (strcmp(url, "http://127.0.0.1:8000/trailer") == 0 || strcmp(url, "/trailer") == 0) {
144 message = "HTTP/1.1 200 OK\r\n"
145 "Connection: close\r\n"
146 "Content-type: text/html\r\n"
147 "Transfer-Encoding: chunked\r\n"
148 "\r\n"
149 "b\r\n"
150 "hello world\r\n"
151 "1\r\n"
152 "\n\r\n"
153 "0\r\n"
154 "X-Foo: bar\r\n"
155 "X-Bar: foo\r\n"
156 "\r\n";
157 }
158 else if (strcmp(url, "http://127.0.0.1:8000/overflow") == 0) {
159 message = "HTTP/1.1 200 OK\r\n"
160 "Connection: close\r\n"
161 "Content-type: text/html\r\n"
162 "Transfer-Encoding: chunked\r\n"
163 "\r\n"
164 "100000000\r\n"
165 "hello world\r\n"
166 "1\r\n"
167 "\n\r\n"
168 "0\r\n"
169 "\r\n";
170 }
171 else if (strcmp(url, "http://127.0.0.1:8000/multiple") == 0) {
172 message = "HTTP/1.1 200 OK\r\n"
173 "Connection: close\r\n"
174 "Content-type: text/html\r\n"
175 "Transfer-Encoding: foo; foo = bar, bar; foo = \"bar\"\r\n"
176 "Transfer-Encoding: foobar; foo = \"\\\"bar\\\"\", chunked\r\n"
177 "\r\n"
178 "b\r\n"
179 "hello world\r\n"
180 "1\r\n"
181 "\n\r\n"
182 "0\r\n"
183 "\r\n";
184 }
185 else {
186 abort();
187 }
188 size_t message_length = strlen(message);
189 ssize_t bytes_sent = send(client_socket, message, message_length, 0);
190 assert(bytes_sent == (ssize_t) message_length);
191
192 closesocket(client_socket);
193 }
194 return 0;
195 }

  ViewVC Help
Powered by ViewVC 1.1.24