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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 610 - (hide annotations)
Fri Sep 24 20:35:36 2010 UTC (8 years, 11 months ago) by siliconforks
File MIME type: text/plain
File size: 6149 byte(s)
Remove hard-coded port numbers.

1 siliconforks 114 /*
2     http-server-chunked.c - HTTP server that outputs chunked response
3 siliconforks 505 Copyright (C) 2008, 2009, 2010 siliconforks.com
4 siliconforks 114
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 siliconforks 126 #include <config.h>
21    
22 siliconforks 114 #include <assert.h>
23     #include <string.h>
24    
25 siliconforks 126 #include "http-server.h"
26 siliconforks 114 #include "stream.h"
27 siliconforks 120 #include "util.h"
28 siliconforks 114
29 siliconforks 610 int main(int argc, const char ** argv) {
30 siliconforks 126 #ifdef __MINGW32__
31     WSADATA data;
32     if (WSAStartup(MAKEWORD(1, 1), &data) != 0) {
33     return 1;
34     }
35     #endif
36 siliconforks 114
37 siliconforks 610 assert(argc == 2);
38     uint16_t port = (uint16_t) atoi(argv[1]);
39    
40 siliconforks 126 SOCKET s = socket(PF_INET, SOCK_STREAM, 0);
41     assert(s != INVALID_SOCKET);
42    
43 siliconforks 114 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 siliconforks 610 a.sin_port = htons(port);
49 siliconforks 114 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 siliconforks 135 break;
92 siliconforks 114 }
93     }
94     }
95    
96     char * method;
97     char * url;
98 siliconforks 135 char * request_line = (char *) stream->data;
99 siliconforks 120 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 siliconforks 135 method = xstrndup(request_line, first_space - request_line);
104 siliconforks 120 url = xstrndup(first_space + 1, second_space - (first_space + 1));
105 siliconforks 114
106     /* send response */
107     char * message;
108 siliconforks 610 if (strcmp(url, "/lower") == 0) {
109 siliconforks 114 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 siliconforks 484 "a\r\n"
115     "abcdefghij\r\n"
116     "10\r\n"
117     "klmnopqrstuvwxyz\r\n"
118 siliconforks 114 "1\r\n"
119     "\n\r\n"
120     "0\r\n"
121     "\r\n";
122     }
123 siliconforks 610 else if (strcmp(url, "/upper") == 0) {
124 siliconforks 114 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 siliconforks 484 "10\r\n"
130     "ABCDEFGHIJKLMNOP\r\n"
131     "A\r\n"
132     "QRSTUVWXYZ\r\n"
133 siliconforks 114 "1\r\n"
134     "\n\r\n"
135     "0\r\n"
136     "\r\n";
137     }
138 siliconforks 610 else if (strcmp(url, "/javascript") == 0) {
139 siliconforks 114 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 siliconforks 484 "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 siliconforks 114 "1\r\n"
151     "\n\r\n"
152     "0\r\n"
153     "\r\n";
154     }
155 siliconforks 610 else if (strcmp(url, "/trailer") == 0) {
156 siliconforks 114 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 siliconforks 610 else if (strcmp(url, "/overflow") == 0) {
171 siliconforks 114 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 siliconforks 610 else if (strcmp(url, "/multiple") == 0) {
184 siliconforks 114 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 siliconforks 120 assert(bytes_sent == (ssize_t) message_length);
203 siliconforks 114
204 siliconforks 126 closesocket(client_socket);
205 siliconforks 114 }
206     return 0;
207     }

  ViewVC Help
Powered by ViewVC 1.1.24