aboutsummaryrefslogtreecommitdiffstats
path: root/tools/node_modules/expresso/deps/jscoverage/http-server.c
blob: 853d28535664939b1d97e2f32ff6c2637ed6f4bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
    http-server.c - generic HTTP server
    Copyright (C) 2008 siliconforks.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include <config.h>

#include "http-server.h"

#include <stdarg.h>
#include <string.h>

#ifdef HAVE_PTHREAD_H
#include <pthread.h>
#endif

#ifdef __MINGW32__
#include <process.h>
#endif

#include "util.h"

#ifdef __MINGW32__
typedef void ThreadRoutineReturnType;
#define THREAD_ROUTINE_RETURN return
#else
typedef void * ThreadRoutineReturnType;
#define THREAD_ROUTINE_RETURN return NULL
#endif

struct HTTPServer {
  char * ip_address;
  uint16_t port;
  HTTPServerHandler handler;
  SOCKET s;
};

struct HTTPServerConnection {
  HTTPConnection * connection;
  struct HTTPServer * server;
};

static bool is_shutdown = false;
#ifdef __MINGW32__
CRITICAL_SECTION shutdown_mutex;
#define LOCK EnterCriticalSection
#define UNLOCK LeaveCriticalSection
#else
pthread_mutex_t shutdown_mutex = PTHREAD_MUTEX_INITIALIZER;
#define LOCK pthread_mutex_lock
#define UNLOCK pthread_mutex_unlock
#endif

static ThreadRoutineReturnType handle_connection(void * p) {
  struct HTTPServerConnection * connection = p;
  uint16_t port = connection->server->port;

  HTTPExchange * exchange = HTTPExchange_new(connection->connection);
  if (HTTPExchange_read_request_headers(exchange) == 0) {
    connection->server->handler(exchange);
  }
  else {
    HTTPExchange_set_status_code(exchange, 400);
    const char * message = "Could not parse request headers\n";
    if (HTTPExchange_write_response(exchange, message, strlen(message)) != 0) {
      HTTPServer_log_err("Warning: error writing to client\n");
    }
  }
  if (HTTPExchange_flush_response(exchange) != 0) {
    HTTPServer_log_err("Warning: error writing to client\n");
  }
  HTTPExchange_delete(exchange);
  if (HTTPConnection_delete(connection->connection) != 0) {
    HTTPServer_log_err("Warning: error closing connection to client\n");
  }
  free(connection);

  /* HACK: make connection to server to force accept() to return */
  LOCK(&shutdown_mutex);
  if (is_shutdown) {
    SOCKET s = socket(PF_INET, SOCK_STREAM, 0);
    if (s == INVALID_SOCKET) {
      HTTPServer_log_err("Warning: error creating socket\n");
    }
    else {
      struct sockaddr_in a;
      a.sin_family = AF_INET;
      a.sin_port = htons(port);
      a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
      if (connect(s, (struct sockaddr *) &a, sizeof(a)) == -1) {
        HTTPServer_log_err("Warning: error connecting to server\n");
      }
      closesocket(s);
    }
  }
  UNLOCK(&shutdown_mutex);

  THREAD_ROUTINE_RETURN;
}

static struct HTTPServer * HTTPServer_new(const char * ip_address, uint16_t port, HTTPServerHandler handler) {
  struct HTTPServer * result = xmalloc(sizeof(struct HTTPServer));
  if (ip_address == NULL) {
    result->ip_address = NULL;
  }
  else {
    result->ip_address = xstrdup(ip_address);
  }
  result->port = port;
  result->handler = handler;
  result->s = -1;
  return result;
}

static void HTTPServer_delete(struct HTTPServer * server) {
  free(server->ip_address);
  closesocket(server->s);
  free(server);
}

void HTTPServer_run(const char * ip_address, uint16_t port, HTTPServerHandler handler) {
  struct HTTPServer * server = HTTPServer_new(ip_address, port, handler);

#ifdef __MINGW32__
  WSADATA data;
  if (WSAStartup(MAKEWORD(1, 1), &data) != 0) {
    fatal("could not start Winsock");
  }
  InitializeCriticalSection(&shutdown_mutex);
#endif

  server->s = socket(PF_INET, SOCK_STREAM, 0);
  if (server->s == INVALID_SOCKET) {
    fatal("could not create socket");
  }

  /* http://hea-www.harvard.edu/~fine/Tech/addrinuse.html */
  int optval = 1;
  setsockopt(server->s, SOL_SOCKET, SO_REUSEADDR, (const char *) &optval, sizeof(optval));

  struct sockaddr_in a;
  a.sin_family = AF_INET;
  a.sin_port = htons(server->port);
  if (server->ip_address == NULL) {
    /*
    a.sin_addr.s_addr = htonl(INADDR_ANY);
    */
    a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  }
  else {
    if (inet_aton(server->ip_address, &(a.sin_addr)) == 0) {
      closesocket(server->s);
      fatal("invalid address: %s", server->ip_address);
    }
  }

  if (bind(server->s, (struct sockaddr *) &a, sizeof(a)) == -1) {
    closesocket(server->s);
    fatal("could not bind to address");
  }

  if (listen(server->s, 5) == -1) {
    closesocket(server->s);
    fatal("could not listen for connections");
  }

  for (;;) {
    struct sockaddr_in client_address;
    size_t client_address_size = sizeof(client_address);
    SOCKET s = accept(server->s, (struct sockaddr *) &client_address, &client_address_size);
    if (s == INVALID_SOCKET) {
      HTTPServer_log_err("Warning: could not accept client connection\n");
      continue;
    }

    LOCK(&shutdown_mutex);
    if (is_shutdown) {
      closesocket(s);
      break;
    }
    UNLOCK(&shutdown_mutex);

    struct HTTPServerConnection * connection = xmalloc(sizeof(struct HTTPServerConnection));
    connection->server = server;
    connection->connection = HTTPConnection_new_server(s);

#ifdef __MINGW32__
    unsigned long thread = _beginthread(handle_connection, 0, connection);
#else
    pthread_t thread;
    pthread_attr_t a;
    pthread_attr_init(&a);
    pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED);
    pthread_create(&thread, &a, handle_connection, connection);
    pthread_attr_destroy(&a);
#endif
  }

  HTTPServer_delete(server);
}

void HTTPServer_shutdown(void) {
  LOCK(&shutdown_mutex);
  is_shutdown = true;
  UNLOCK(&shutdown_mutex);
}

void HTTPServer_log_out(const char * format, ...) {
  va_list a;
  va_start(a, format);
  vfprintf(stdout, format, a);
  va_end(a);
  fflush(stdout);
}

void HTTPServer_log_err(const char * format, ...) {
  va_list a;
  va_start(a, format);
  vfprintf(stderr, format, a);
  va_end(a);
  fflush(stderr);
}