/[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 427 - (hide annotations)
Wed Feb 18 16:08:33 2009 UTC (10 years, 7 months ago) by siliconforks
File MIME type: text/plain
File size: 6071 byte(s)
Update Copyright year.
1 siliconforks 114 /*
2     http-server-chunked.c - HTTP server that outputs chunked response
3 siliconforks 427 Copyright (C) 2008, 2009 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     int main(void) {
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 126 SOCKET s = socket(PF_INET, SOCK_STREAM, 0);
38     assert(s != INVALID_SOCKET);
39    
40 siliconforks 114 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 siliconforks 135 break;
89 siliconforks 114 }
90     }
91     }
92    
93     char * method;
94     char * url;
95 siliconforks 135 char * request_line = (char *) stream->data;
96 siliconforks 120 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 siliconforks 135 method = xstrndup(request_line, first_space - request_line);
101 siliconforks 120 url = xstrndup(first_space + 1, second_space - (first_space + 1));
102 siliconforks 114
103     /* send response */
104     char * message;
105 siliconforks 120 if (strcmp(url, "http://127.0.0.1:8000/lower") == 0 || strcmp(url, "/lower") == 0) {
106 siliconforks 114 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     "b\r\n"
112     "hello world\r\n"
113     "1\r\n"
114     "\n\r\n"
115     "0\r\n"
116     "\r\n";
117     }
118     else if (strcmp(url, "http://127.0.0.1:8000/upper") == 0 || strcmp(url, "/upper") == 0) {
119     message = "HTTP/1.1 200 OK\r\n"
120     "Connection: close\r\n"
121     "Content-type: text/html\r\n"
122     "Transfer-Encoding: chunked\r\n"
123     "\r\n"
124     "B\r\n"
125     "HELLO WORLD\r\n"
126     "1\r\n"
127     "\n\r\n"
128     "0\r\n"
129     "\r\n";
130     }
131     else if (strcmp(url, "http://127.0.0.1:8000/javascript") == 0 || strcmp(url, "/javascript") == 0) {
132     message = "HTTP/1.1 200 OK\r\n"
133     "Connection: close\r\n"
134     "Content-Type: text/javascript\r\n"
135     "Transfer-Encoding: chunked\r\n"
136     "\r\n"
137     "B\r\n"
138     "hello = 10;\r\n"
139     "1\r\n"
140     "\n\r\n"
141     "0\r\n"
142     "\r\n";
143     }
144     else if (strcmp(url, "http://127.0.0.1:8000/trailer") == 0 || strcmp(url, "/trailer") == 0) {
145     message = "HTTP/1.1 200 OK\r\n"
146     "Connection: close\r\n"
147     "Content-type: text/html\r\n"
148     "Transfer-Encoding: chunked\r\n"
149     "\r\n"
150     "b\r\n"
151     "hello world\r\n"
152     "1\r\n"
153     "\n\r\n"
154     "0\r\n"
155     "X-Foo: bar\r\n"
156     "X-Bar: foo\r\n"
157     "\r\n";
158     }
159     else if (strcmp(url, "http://127.0.0.1:8000/overflow") == 0) {
160     message = "HTTP/1.1 200 OK\r\n"
161     "Connection: close\r\n"
162     "Content-type: text/html\r\n"
163     "Transfer-Encoding: chunked\r\n"
164     "\r\n"
165     "100000000\r\n"
166     "hello world\r\n"
167     "1\r\n"
168     "\n\r\n"
169     "0\r\n"
170     "\r\n";
171     }
172     else if (strcmp(url, "http://127.0.0.1:8000/multiple") == 0) {
173     message = "HTTP/1.1 200 OK\r\n"
174     "Connection: close\r\n"
175     "Content-type: text/html\r\n"
176     "Transfer-Encoding: foo; foo = bar, bar; foo = \"bar\"\r\n"
177     "Transfer-Encoding: foobar; foo = \"\\\"bar\\\"\", chunked\r\n"
178     "\r\n"
179     "b\r\n"
180     "hello world\r\n"
181     "1\r\n"
182     "\n\r\n"
183     "0\r\n"
184     "\r\n";
185     }
186     else {
187     abort();
188     }
189     size_t message_length = strlen(message);
190     ssize_t bytes_sent = send(client_socket, message, message_length, 0);
191 siliconforks 120 assert(bytes_sent == (ssize_t) message_length);
192 siliconforks 114
193 siliconforks 126 closesocket(client_socket);
194 siliconforks 114 }
195     return 0;
196     }

  ViewVC Help
Powered by ViewVC 1.1.24