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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

1 /*
2 http-server-charset.c - HTTP server that outputs different charset values
3 Copyright (C) 2008, 2009, 2010 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(int argc, const char ** argv) {
30 #ifdef __MINGW32__
31 WSADATA data;
32 if (WSAStartup(MAKEWORD(1, 1), &data) != 0) {
33 return 1;
34 }
35 #endif
36
37 assert(argc == 2);
38 uint16_t port = (uint16_t) atoi(argv[1]);
39
40 SOCKET s = socket(PF_INET, SOCK_STREAM, 0);
41 assert(s != INVALID_SOCKET);
42
43 int optval = 1;
44 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *) &optval, sizeof(optval));
45
46 struct sockaddr_in a;
47 a.sin_family = AF_INET;
48 a.sin_port = htons(port);
49 a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
50 int result = bind(s, (struct sockaddr *) &a, sizeof(a));
51 assert(result == 0);
52
53 result = listen(s, 5);
54 assert(result == 0);
55
56 for (;;) {
57 struct sockaddr_in client_address;
58 size_t size = sizeof(client_address);
59 int client_socket = accept(s, (struct sockaddr *) &client_address, &size);
60 assert(client_socket > 0);
61
62 /* read request */
63 Stream * stream = Stream_new(0);
64 int state = 0;
65 while (state != 2) {
66 uint8_t buffer[8192];
67 ssize_t bytes_read = recv(client_socket, buffer, 8192, 0);
68 assert(bytes_read > 0);
69 Stream_write(stream, buffer, bytes_read);
70 for (int i = 0; i < bytes_read && state != 2; i++) {
71 uint8_t byte = buffer[i];
72 switch (state) {
73 case 0:
74 if (byte == '\n') {
75 state = 1;
76 }
77 break;
78 case 1:
79 if (byte == '\n') {
80 state = 2;
81 }
82 else if (byte == '\r') {
83 state = 1;
84 }
85 else {
86 state = 0;
87 }
88 break;
89 }
90 }
91 }
92
93 char * method;
94 char * url;
95 char * request_line = (char *) stream->data;
96 char * first_space = strchr(request_line, ' ');
97 assert(first_space != NULL);
98 char * second_space = strchr(first_space + 1, ' ');
99 assert(second_space != NULL);
100 method = xstrndup(request_line, first_space - request_line);
101 url = xstrndup(first_space + 1, second_space - (first_space + 1));
102
103 /* send response */
104 char * message;
105 if (strcmp(url, "/utf-8.js") == 0) {
106 message = "HTTP/1.1 200 OK\r\n"
107 "Connection: close\r\n"
108 "Content-type: text/javascript; charset=UTF-8\r\n"
109 "\r\n"
110 "var s = 'eèéê';\n"
111 "var r = /eèéê/;\n";
112 }
113 else if (strcmp(url, "/iso-8859-1.js") == 0) {
114 message = "HTTP/1.1 200 OK\r\n"
115 "Connection: close\r\n"
116 "Content-type: text/javascript; charset=ISO-8859-1\r\n"
117 "\r\n"
118 "var s = 'eèéê';\n"
119 "var r = /eèéê/;\n";
120 }
121 else if (strcmp(url, "/bogus.js") == 0) {
122 message = "HTTP/1.1 200 OK\r\n"
123 "Connection: close\r\n"
124 "Content-type: text/javascript; charset=BOGUS\r\n"
125 "\r\n"
126 "var s = 'eèéê';\n"
127 "var r = /eèéê/;\n";
128 }
129 else if (strcmp(url, "/malformed.js") == 0) {
130 message = "HTTP/1.1 200 OK\r\n"
131 "Connection: close\r\n"
132 "Content-type: text/javascript; charset=UTF-8\r\n"
133 "\r\n"
134 "var s = 'eèéê';\n"
135 "var r = /eèéê/;\n";
136 }
137 else {
138 abort();
139 }
140 size_t message_length = strlen(message);
141 ssize_t bytes_sent = send(client_socket, message, message_length, 0);
142 assert(bytes_sent == (ssize_t) message_length);
143
144 closesocket(client_socket);
145 }
146 return 0;
147 }

  ViewVC Help
Powered by ViewVC 1.1.24