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

Annotation of /trunk/tests/http-server-charset.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 256 - (hide annotations)
Sun Oct 5 18:10:38 2008 UTC (11 years, 1 month ago) by siliconforks
File MIME type: text/plain
File size: 4585 byte(s)
Add more tests.
1 siliconforks 251 /*
2     http-server-charset.c - HTTP server that outputs different charset values
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 <config.h>
21    
22     #include <assert.h>
23     #include <string.h>
24    
25     #include "http-server.h"
26     #include "stream.h"
27     #include "util.h"
28    
29     int main(void) {
30     #ifdef __MINGW32__
31     WSADATA data;
32     if (WSAStartup(MAKEWORD(1, 1), &data) != 0) {
33     return 1;
34     }
35     #endif
36    
37     SOCKET s = socket(PF_INET, SOCK_STREAM, 0);
38     assert(s != INVALID_SOCKET);
39    
40     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     break;
75     case 1:
76     if (byte == '\n') {
77     state = 2;
78     }
79     else if (byte == '\r') {
80     state = 1;
81     }
82     else {
83     state = 0;
84     }
85     break;
86     }
87     }
88     }
89    
90     char * method;
91     char * url;
92     char * request_line = (char *) stream->data;
93     char * first_space = strchr(request_line, ' ');
94     assert(first_space != NULL);
95     char * second_space = strchr(first_space + 1, ' ');
96     assert(second_space != NULL);
97     method = xstrndup(request_line, first_space - request_line);
98     url = xstrndup(first_space + 1, second_space - (first_space + 1));
99    
100     /* send response */
101     char * message;
102     if (strcmp(url, "http://127.0.0.1:8000/utf-8.js") == 0 || strcmp(url, "/utf-8.js") == 0) {
103     message = "HTTP/1.1 200 OK\r\n"
104     "Connection: close\r\n"
105     "Content-type: text/javascript; charset=UTF-8\r\n"
106     "\r\n"
107     "var s = 'eèéê';\n"
108     "var r = /eèéê/;\n";
109     }
110     else if (strcmp(url, "http://127.0.0.1:8000/iso-8859-1.js") == 0 || strcmp(url, "/iso-8859-1.js") == 0) {
111     message = "HTTP/1.1 200 OK\r\n"
112     "Connection: close\r\n"
113     "Content-type: text/javascript; charset=ISO-8859-1\r\n"
114     "\r\n"
115     "var s = 'eèéê';\n"
116     "var r = /eèéê/;\n";
117     }
118 siliconforks 256 else if (strcmp(url, "http://127.0.0.1:8000/bogus.js") == 0 || strcmp(url, "/bogus.js") == 0) {
119     message = "HTTP/1.1 200 OK\r\n"
120     "Connection: close\r\n"
121     "Content-type: text/javascript; charset=BOGUS\r\n"
122     "\r\n"
123     "var s = 'eèéê';\n"
124     "var r = /eèéê/;\n";
125     }
126     else if (strcmp(url, "http://127.0.0.1:8000/malformed.js") == 0 || strcmp(url, "/malformed.js") == 0) {
127     message = "HTTP/1.1 200 OK\r\n"
128     "Connection: close\r\n"
129     "Content-type: text/javascript; charset=UTF-8\r\n"
130     "\r\n"
131     "var s = 'eèéê';\n"
132     "var r = /eèéê/;\n";
133     }
134 siliconforks 251 else {
135     abort();
136     }
137     size_t message_length = strlen(message);
138     ssize_t bytes_sent = send(client_socket, message, message_length, 0);
139     assert(bytes_sent == (ssize_t) message_length);
140    
141     closesocket(client_socket);
142     }
143     return 0;
144     }

  ViewVC Help
Powered by ViewVC 1.1.24