/[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 120 - (hide annotations)
Sun Jun 1 14:06:35 2008 UTC (11 years, 6 months ago) by siliconforks
File MIME type: text/plain
File size: 5930 byte(s)
Do not use `%as'.

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

  ViewVC Help
Powered by ViewVC 1.1.24