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

Annotation of /trunk/tests/http-server-bad-body.c

Parent Directory Parent Directory | Revision Log Revision Log


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

1 siliconforks 114 /*
2     http-server-bad-body.c - HTTP server that outputs a bad body
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
27 siliconforks 610 int main(int argc, const char ** argv) {
28 siliconforks 126 #ifdef __MINGW32__
29     WSADATA data;
30     if (WSAStartup(MAKEWORD(1, 1), &data) != 0) {
31     return 1;
32     }
33     #endif
34 siliconforks 114
35 siliconforks 610 assert(argc == 2);
36     uint16_t port = (uint16_t) atoi(argv[1]);
37    
38 siliconforks 126 SOCKET s = socket(PF_INET, SOCK_STREAM, 0);
39     assert(s != INVALID_SOCKET);
40    
41 siliconforks 114 int optval = 1;
42     setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *) &optval, sizeof(optval));
43    
44     struct sockaddr_in a;
45     a.sin_family = AF_INET;
46 siliconforks 610 a.sin_port = htons(port);
47 siliconforks 114 a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
48     int result = bind(s, (struct sockaddr *) &a, sizeof(a));
49     assert(result == 0);
50    
51     result = listen(s, 5);
52     assert(result == 0);
53    
54     for (;;) {
55     struct sockaddr_in client_address;
56     size_t size = sizeof(client_address);
57     int client_socket = accept(s, (struct sockaddr *) &client_address, &size);
58     assert(client_socket > 0);
59    
60     /* read request */
61 siliconforks 570 bool ping = false;
62     bool first = true;
63 siliconforks 114 int state = 0;
64     while (state != 2) {
65     uint8_t buffer[8192];
66     ssize_t bytes_read = recv(client_socket, buffer, 8192, 0);
67     assert(bytes_read > 0);
68 siliconforks 570 if (first) {
69     if (strncmp("GET /ping", buffer, 9) == 0) {
70     ping = true;
71     }
72     first = false;
73     }
74 siliconforks 114 for (int i = 0; i < bytes_read && state != 2; i++) {
75     uint8_t byte = buffer[i];
76     switch (state) {
77     case 0:
78     if (byte == '\n') {
79     state = 1;
80     }
81     else {
82     state = 0;
83     }
84     break;
85     case 1:
86     if (byte == '\n') {
87     state = 2;
88     }
89     else if (byte == '\r') {
90     state = 1;
91     }
92     else {
93     state = 0;
94     }
95     }
96     }
97     }
98    
99 siliconforks 570 char * message;
100     if (ping) {
101     /* send normal response */
102     message = "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-type: text/html\r\n\r\nHello\n";
103     }
104     else {
105     /* send bogus response */
106     message = "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-type: text/html\r\nTransfer-Encoding: chunked\r\n\r\nHello\n";
107     }
108 siliconforks 114 size_t message_length = strlen(message);
109     ssize_t bytes_sent = send(client_socket, message, message_length, 0);
110 siliconforks 126 assert(bytes_sent == (ssize_t) message_length);
111 siliconforks 114
112 siliconforks 126 closesocket(client_socket);
113 siliconforks 114 }
114     return 0;
115     }

  ViewVC Help
Powered by ViewVC 1.1.24