1 |
/* |
2 |
http-server.c - generic HTTP server |
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 "http-server.h" |
23 |
|
24 |
#include <stdarg.h> |
25 |
#include <string.h> |
26 |
|
27 |
#ifdef HAVE_PTHREAD_H |
28 |
#include <pthread.h> |
29 |
#endif |
30 |
|
31 |
#ifdef __MINGW32__ |
32 |
#include <process.h> |
33 |
#endif |
34 |
|
35 |
#include "util.h" |
36 |
|
37 |
#ifdef __MINGW32__ |
38 |
typedef void ThreadRoutineReturnType; |
39 |
#define THREAD_ROUTINE_RETURN return |
40 |
#else |
41 |
typedef void * ThreadRoutineReturnType; |
42 |
#define THREAD_ROUTINE_RETURN return NULL |
43 |
#endif |
44 |
|
45 |
struct HTTPServer { |
46 |
char * ip_address; |
47 |
uint16_t port; |
48 |
HTTPServerHandler handler; |
49 |
SOCKET s; |
50 |
}; |
51 |
|
52 |
struct HTTPServerConnection { |
53 |
HTTPConnection * connection; |
54 |
struct HTTPServer * server; |
55 |
}; |
56 |
|
57 |
static bool is_shutdown = false; |
58 |
#ifdef __MINGW32__ |
59 |
CRITICAL_SECTION shutdown_mutex; |
60 |
#define LOCK EnterCriticalSection |
61 |
#define UNLOCK LeaveCriticalSection |
62 |
#else |
63 |
pthread_mutex_t shutdown_mutex = PTHREAD_MUTEX_INITIALIZER; |
64 |
#define LOCK pthread_mutex_lock |
65 |
#define UNLOCK pthread_mutex_unlock |
66 |
#endif |
67 |
|
68 |
static ThreadRoutineReturnType handle_connection(void * p) { |
69 |
struct HTTPServerConnection * connection = p; |
70 |
uint16_t port = connection->server->port; |
71 |
|
72 |
HTTPExchange * exchange = HTTPExchange_new(connection->connection); |
73 |
if (HTTPExchange_read_request_headers(exchange) == 0) { |
74 |
connection->server->handler(exchange); |
75 |
} |
76 |
else { |
77 |
HTTPExchange_set_status_code(exchange, 400); |
78 |
const char * message = "Could not parse request headers\n"; |
79 |
if (HTTPExchange_write_response(exchange, message, strlen(message)) != 0) { |
80 |
HTTPServer_log_err("Warning: error writing to client\n"); |
81 |
} |
82 |
} |
83 |
if (HTTPExchange_flush_response(exchange) != 0) { |
84 |
HTTPServer_log_err("Warning: error writing to client\n"); |
85 |
} |
86 |
HTTPExchange_delete(exchange); |
87 |
if (HTTPConnection_delete(connection->connection) != 0) { |
88 |
HTTPServer_log_err("Warning: error closing connection to client\n"); |
89 |
} |
90 |
free(connection); |
91 |
|
92 |
/* HACK: make connection to server to force accept() to return */ |
93 |
LOCK(&shutdown_mutex); |
94 |
if (is_shutdown) { |
95 |
SOCKET s = socket(PF_INET, SOCK_STREAM, 0); |
96 |
if (s == INVALID_SOCKET) { |
97 |
HTTPServer_log_err("Warning: error creating socket\n"); |
98 |
} |
99 |
else { |
100 |
struct sockaddr_in a; |
101 |
a.sin_family = AF_INET; |
102 |
a.sin_port = htons(port); |
103 |
a.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
104 |
if (connect(s, (struct sockaddr *) &a, sizeof(a)) == -1) { |
105 |
HTTPServer_log_err("Warning: error connecting to server\n"); |
106 |
} |
107 |
closesocket(s); |
108 |
} |
109 |
} |
110 |
UNLOCK(&shutdown_mutex); |
111 |
|
112 |
THREAD_ROUTINE_RETURN; |
113 |
} |
114 |
|
115 |
static struct HTTPServer * HTTPServer_new(const char * ip_address, uint16_t port, HTTPServerHandler handler) { |
116 |
struct HTTPServer * result = xmalloc(sizeof(struct HTTPServer)); |
117 |
if (ip_address == NULL) { |
118 |
result->ip_address = NULL; |
119 |
} |
120 |
else { |
121 |
result->ip_address = xstrdup(ip_address); |
122 |
} |
123 |
result->port = port; |
124 |
result->handler = handler; |
125 |
result->s = -1; |
126 |
return result; |
127 |
} |
128 |
|
129 |
static void HTTPServer_delete(struct HTTPServer * server) { |
130 |
free(server->ip_address); |
131 |
closesocket(server->s); |
132 |
free(server); |
133 |
} |
134 |
|
135 |
void HTTPServer_run(const char * ip_address, uint16_t port, HTTPServerHandler handler) { |
136 |
struct HTTPServer * server = HTTPServer_new(ip_address, port, handler); |
137 |
|
138 |
#ifdef __MINGW32__ |
139 |
WSADATA data; |
140 |
if (WSAStartup(MAKEWORD(1, 1), &data) != 0) { |
141 |
fatal("could not start Winsock"); |
142 |
} |
143 |
InitializeCriticalSection(&shutdown_mutex); |
144 |
#endif |
145 |
|
146 |
server->s = socket(PF_INET, SOCK_STREAM, 0); |
147 |
if (server->s == INVALID_SOCKET) { |
148 |
fatal("could not create socket"); |
149 |
} |
150 |
|
151 |
/* http://hea-www.harvard.edu/~fine/Tech/addrinuse.html */ |
152 |
/* note that SO_REUSEADDR has different semantics on Windows */ |
153 |
#ifndef __MINGW32__ |
154 |
int optval = 1; |
155 |
setsockopt(server->s, SOL_SOCKET, SO_REUSEADDR, (const char *) &optval, sizeof(optval)); |
156 |
#endif |
157 |
|
158 |
struct sockaddr_in a; |
159 |
a.sin_family = AF_INET; |
160 |
a.sin_port = htons(server->port); |
161 |
if (server->ip_address == NULL) { |
162 |
/* |
163 |
a.sin_addr.s_addr = htonl(INADDR_ANY); |
164 |
*/ |
165 |
a.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
166 |
} |
167 |
else { |
168 |
if (inet_aton(server->ip_address, &(a.sin_addr)) == 0) { |
169 |
closesocket(server->s); |
170 |
fatal("invalid address: %s", server->ip_address); |
171 |
} |
172 |
} |
173 |
|
174 |
if (bind(server->s, (struct sockaddr *) &a, sizeof(a)) == -1) { |
175 |
closesocket(server->s); |
176 |
fatal("could not bind to address"); |
177 |
} |
178 |
|
179 |
if (listen(server->s, 5) == -1) { |
180 |
closesocket(server->s); |
181 |
fatal("could not listen for connections"); |
182 |
} |
183 |
|
184 |
for (;;) { |
185 |
struct sockaddr_in client_address; |
186 |
size_t client_address_size = sizeof(client_address); |
187 |
SOCKET s = accept(server->s, (struct sockaddr *) &client_address, &client_address_size); |
188 |
if (s == INVALID_SOCKET) { |
189 |
HTTPServer_log_err("Warning: could not accept client connection\n"); |
190 |
continue; |
191 |
} |
192 |
|
193 |
LOCK(&shutdown_mutex); |
194 |
if (is_shutdown) { |
195 |
closesocket(s); |
196 |
break; |
197 |
} |
198 |
UNLOCK(&shutdown_mutex); |
199 |
|
200 |
struct HTTPServerConnection * connection = xmalloc(sizeof(struct HTTPServerConnection)); |
201 |
connection->server = server; |
202 |
connection->connection = HTTPConnection_new_server(s); |
203 |
|
204 |
#ifdef __MINGW32__ |
205 |
unsigned long thread = _beginthread(handle_connection, 0, connection); |
206 |
#else |
207 |
pthread_t thread; |
208 |
pthread_attr_t a; |
209 |
pthread_attr_init(&a); |
210 |
pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED); |
211 |
pthread_create(&thread, &a, handle_connection, connection); |
212 |
pthread_attr_destroy(&a); |
213 |
#endif |
214 |
} |
215 |
|
216 |
HTTPServer_delete(server); |
217 |
} |
218 |
|
219 |
void HTTPServer_shutdown(void) { |
220 |
LOCK(&shutdown_mutex); |
221 |
is_shutdown = true; |
222 |
UNLOCK(&shutdown_mutex); |
223 |
} |
224 |
|
225 |
void HTTPServer_log_out(const char * format, ...) { |
226 |
va_list a; |
227 |
va_start(a, format); |
228 |
vfprintf(stdout, format, a); |
229 |
va_end(a); |
230 |
fflush(stdout); |
231 |
} |
232 |
|
233 |
void HTTPServer_log_err(const char * format, ...) { |
234 |
va_list a; |
235 |
va_start(a, format); |
236 |
vfprintf(stderr, format, a); |
237 |
va_end(a); |
238 |
fflush(stderr); |
239 |
} |