/[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 114 - (hide annotations)
Sat May 31 21:39:39 2008 UTC (13 years, 11 months ago) by siliconforks
File MIME type: text/plain
File size: 5573 byte(s)
Add tests for jscoverage-server.

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    
29     int main(void) {
30     int s = socket(PF_INET, SOCK_STREAM, 0);
31     assert(s > 0);
32    
33     int optval = 1;
34     setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *) &optval, sizeof(optval));
35    
36     struct sockaddr_in a;
37     a.sin_family = AF_INET;
38     a.sin_port = htons(8000);
39     a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
40     int result = bind(s, (struct sockaddr *) &a, sizeof(a));
41     assert(result == 0);
42    
43     result = listen(s, 5);
44     assert(result == 0);
45    
46     for (;;) {
47     struct sockaddr_in client_address;
48     size_t size = sizeof(client_address);
49     int client_socket = accept(s, (struct sockaddr *) &client_address, &size);
50     assert(client_socket > 0);
51    
52     /* read request */
53     Stream * stream = Stream_new(0);
54     int state = 0;
55     while (state != 2) {
56     uint8_t buffer[8192];
57     ssize_t bytes_read = recv(client_socket, buffer, 8192, 0);
58     assert(bytes_read > 0);
59     Stream_write(stream, buffer, bytes_read);
60     for (int i = 0; i < bytes_read && state != 2; i++) {
61     uint8_t byte = buffer[i];
62     switch (state) {
63     case 0:
64     if (byte == '\n') {
65     state = 1;
66     }
67     else {
68     state = 0;
69     }
70     break;
71     case 1:
72     if (byte == '\n') {
73     state = 2;
74     }
75     else if (byte == '\r') {
76     state = 1;
77     }
78     else {
79     state = 0;
80     }
81     }
82     }
83     }
84    
85     char * method;
86     char * url;
87     sscanf(stream->data, "%as %as", &method, &url);
88    
89     /* send response */
90     char * message;
91     if (strcmp(url, "http://127.0.0.1:8000/lower") == 0) {
92     message = "HTTP/1.1 200 OK\r\n"
93     "Connection: close\r\n"
94     "Content-type: text/html\r\n"
95     "Transfer-Encoding: chunked\r\n"
96     "\r\n"
97     "b\r\n"
98     "hello world\r\n"
99     "1\r\n"
100     "\n\r\n"
101     "0\r\n"
102     "\r\n";
103     }
104     else if (strcmp(url, "http://127.0.0.1:8000/upper") == 0 || strcmp(url, "/upper") == 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/javascript") == 0 || strcmp(url, "/javascript") == 0) {
118     message = "HTTP/1.1 200 OK\r\n"
119     "Connection: close\r\n"
120     "Content-Type: text/javascript\r\n"
121     "Transfer-Encoding: chunked\r\n"
122     "\r\n"
123     "B\r\n"
124     "hello = 10;\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/trailer") == 0 || strcmp(url, "/trailer") == 0) {
131     message = "HTTP/1.1 200 OK\r\n"
132     "Connection: close\r\n"
133     "Content-type: text/html\r\n"
134     "Transfer-Encoding: chunked\r\n"
135     "\r\n"
136     "b\r\n"
137     "hello world\r\n"
138     "1\r\n"
139     "\n\r\n"
140     "0\r\n"
141     "X-Foo: bar\r\n"
142     "X-Bar: foo\r\n"
143     "\r\n";
144     }
145     else if (strcmp(url, "http://127.0.0.1:8000/overflow") == 0) {
146     message = "HTTP/1.1 200 OK\r\n"
147     "Connection: close\r\n"
148     "Content-type: text/html\r\n"
149     "Transfer-Encoding: chunked\r\n"
150     "\r\n"
151     "100000000\r\n"
152     "hello world\r\n"
153     "1\r\n"
154     "\n\r\n"
155     "0\r\n"
156     "\r\n";
157     }
158     else if (strcmp(url, "http://127.0.0.1:8000/multiple") == 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: foo; foo = bar, bar; foo = \"bar\"\r\n"
163     "Transfer-Encoding: foobar; foo = \"\\\"bar\\\"\", chunked\r\n"
164     "\r\n"
165     "b\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 {
173     abort();
174     }
175     size_t message_length = strlen(message);
176     ssize_t bytes_sent = send(client_socket, message, message_length, 0);
177     assert(bytes_sent == message_length);
178    
179     close(client_socket);
180     }
181     return 0;
182     }

  ViewVC Help
Powered by ViewVC 1.1.24