/[jscoverage]/trunk/tests/http-server-empty-header-value.c
ViewVC logotype

Annotation of /trunk/tests/http-server-empty-header-value.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 570 - (hide annotations)
Mon May 24 03:13:28 2010 UTC (10 years, 5 months ago) by siliconforks
File MIME type: text/plain
File size: 3157 byte(s)
Remove all timing dependencies.

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

  ViewVC Help
Powered by ViewVC 1.1.24