1 |
/* |
2 |
jscoverage-server.c - JSCoverage server main routine |
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 <ctype.h> |
24 |
#include <signal.h> |
25 |
#include <stdint.h> |
26 |
#include <string.h> |
27 |
|
28 |
#include <dirent.h> |
29 |
#ifdef HAVE_PTHREAD_H |
30 |
#include <pthread.h> |
31 |
#endif |
32 |
|
33 |
#include "encoding.h" |
34 |
#include "global.h" |
35 |
#include "http-server.h" |
36 |
#include "instrument-js.h" |
37 |
#include "resource-manager.h" |
38 |
#include "stream.h" |
39 |
#include "util.h" |
40 |
|
41 |
static const char * specified_encoding = NULL; |
42 |
const char * jscoverage_encoding = "ISO-8859-1"; |
43 |
bool jscoverage_highlight = true; |
44 |
|
45 |
typedef struct SourceCache { |
46 |
char * url; |
47 |
uint16_t * characters; |
48 |
size_t num_characters; |
49 |
struct SourceCache * next; |
50 |
} SourceCache; |
51 |
|
52 |
static SourceCache * source_cache = NULL; |
53 |
|
54 |
static const struct { |
55 |
const char * extension; |
56 |
const char * mime_type; |
57 |
} mime_types[] = { |
58 |
{".gif", "image/gif"}, |
59 |
{".jpg", "image/jpeg"}, |
60 |
{".jpeg", "image/jpeg"}, |
61 |
{".png", "image/png"}, |
62 |
{".css", "text/css"}, |
63 |
{".html", "text/html"}, |
64 |
{".htm", "text/html"}, |
65 |
{".js", "text/javascript"}, |
66 |
{".txt", "text/plain"}, |
67 |
{".xml", "application/xml"}, |
68 |
}; |
69 |
|
70 |
static bool verbose = false; |
71 |
static const char * report_directory = "jscoverage-report"; |
72 |
static const char * document_root = "."; |
73 |
static bool proxy = false; |
74 |
static const char ** no_instrument; |
75 |
static size_t num_no_instrument = 0; |
76 |
|
77 |
#ifdef __MINGW32__ |
78 |
CRITICAL_SECTION javascript_mutex; |
79 |
CRITICAL_SECTION source_cache_mutex; |
80 |
#define LOCK EnterCriticalSection |
81 |
#define UNLOCK LeaveCriticalSection |
82 |
#else |
83 |
pthread_mutex_t javascript_mutex = PTHREAD_MUTEX_INITIALIZER; |
84 |
pthread_mutex_t source_cache_mutex = PTHREAD_MUTEX_INITIALIZER; |
85 |
#define LOCK pthread_mutex_lock |
86 |
#define UNLOCK pthread_mutex_unlock |
87 |
#endif |
88 |
|
89 |
static const SourceCache * find_cached_source(const char * url) { |
90 |
SourceCache * result = NULL; |
91 |
LOCK(&source_cache_mutex); |
92 |
for (SourceCache * p = source_cache; p != NULL; p = p->next) { |
93 |
if (strcmp(url, p->url) == 0) { |
94 |
result = p; |
95 |
break; |
96 |
} |
97 |
} |
98 |
UNLOCK(&source_cache_mutex); |
99 |
return result; |
100 |
} |
101 |
|
102 |
static void add_cached_source(const char * url, uint16_t * characters, size_t num_characters) { |
103 |
SourceCache * new_source_cache = xmalloc(sizeof(SourceCache)); |
104 |
new_source_cache->url = xstrdup(url); |
105 |
new_source_cache->characters = characters; |
106 |
new_source_cache->num_characters = num_characters; |
107 |
LOCK(&source_cache_mutex); |
108 |
new_source_cache->next = source_cache; |
109 |
source_cache = new_source_cache; |
110 |
UNLOCK(&source_cache_mutex); |
111 |
} |
112 |
|
113 |
static int get(const char * url, uint16_t ** characters, size_t * num_characters) __attribute__((warn_unused_result)); |
114 |
|
115 |
static int get(const char * url, uint16_t ** characters, size_t * num_characters) { |
116 |
char * host = NULL; |
117 |
uint16_t port; |
118 |
char * abs_path = NULL; |
119 |
char * query = NULL; |
120 |
HTTPConnection * connection = NULL; |
121 |
HTTPExchange * exchange = NULL; |
122 |
Stream * stream = NULL; |
123 |
|
124 |
int result = URL_parse(url, &host, &port, &abs_path, &query); |
125 |
if (result != 0) { |
126 |
goto done; |
127 |
} |
128 |
|
129 |
connection = HTTPConnection_new_client(host, port); |
130 |
if (connection == NULL) { |
131 |
result = -1; |
132 |
goto done; |
133 |
} |
134 |
|
135 |
exchange = HTTPExchange_new(connection); |
136 |
HTTPExchange_set_request_uri(exchange, url); |
137 |
result = HTTPExchange_write_request_headers(exchange); |
138 |
if (result != 0) { |
139 |
goto done; |
140 |
} |
141 |
|
142 |
result = HTTPExchange_read_response_headers(exchange); |
143 |
if (result != 0) { |
144 |
goto done; |
145 |
} |
146 |
|
147 |
stream = Stream_new(0); |
148 |
result = HTTPExchange_read_entire_response_entity_body(exchange, stream); |
149 |
if (result != 0) { |
150 |
goto done; |
151 |
} |
152 |
char * encoding = HTTPMessage_get_charset(HTTPExchange_get_response_message(exchange)); |
153 |
if (encoding == NULL) { |
154 |
encoding = xstrdup(jscoverage_encoding); |
155 |
} |
156 |
result = jscoverage_bytes_to_characters(encoding, stream->data, stream->length, characters, num_characters); |
157 |
free(encoding); |
158 |
if (result != 0) { |
159 |
goto done; |
160 |
} |
161 |
|
162 |
result = 0; |
163 |
|
164 |
done: |
165 |
if (stream != NULL) { |
166 |
Stream_delete(stream); |
167 |
} |
168 |
if (exchange != NULL) { |
169 |
HTTPExchange_delete(exchange); |
170 |
} |
171 |
if (connection != NULL) { |
172 |
if (HTTPConnection_delete(connection) != 0) { |
173 |
HTTPServer_log_err("Warning: error closing connection after retrieving URL: %s\n", url); |
174 |
} |
175 |
} |
176 |
free(host); |
177 |
free(abs_path); |
178 |
free(query); |
179 |
return result; |
180 |
} |
181 |
|
182 |
static void send_response(HTTPExchange * exchange, uint16_t status_code, const char * html) { |
183 |
HTTPExchange_set_status_code(exchange, status_code); |
184 |
if (HTTPExchange_write_response(exchange, html, strlen(html)) != 0) { |
185 |
HTTPServer_log_err("Warning: error writing to client\n"); |
186 |
} |
187 |
} |
188 |
|
189 |
/* |
190 |
RFC 2396, Appendix A: we are checking for `pchar' |
191 |
*/ |
192 |
static bool is_escaped(char c) { |
193 |
/* `pchar' */ |
194 |
if (strchr(":@&=+$,", c) != NULL) { |
195 |
return false; |
196 |
} |
197 |
|
198 |
if (isalnum((unsigned char) c)) { |
199 |
return false; |
200 |
} |
201 |
|
202 |
/* `mark' */ |
203 |
if (strchr("-_.!~*'()", c) != NULL) { |
204 |
return false; |
205 |
} |
206 |
|
207 |
return true; |
208 |
} |
209 |
|
210 |
static char * encode_uri_component(const char * s) { |
211 |
size_t length = 0; |
212 |
for (const char * p = s; *p != '\0'; p++) { |
213 |
if (is_escaped(*p)) { |
214 |
length = addst(length, 3); |
215 |
} |
216 |
else { |
217 |
length = addst(length, 1); |
218 |
} |
219 |
} |
220 |
|
221 |
length = addst(length, 1); |
222 |
char * result = xmalloc(length); |
223 |
size_t i = 0; |
224 |
for (const char * p = s; *p != '\0'; p++) { |
225 |
if (is_escaped(*p)) { |
226 |
result[i] = '%'; |
227 |
i++; |
228 |
snprintf(result + i, 3, "%02X", *p); |
229 |
i += 2; |
230 |
} |
231 |
else { |
232 |
result[i] = *p; |
233 |
i++; |
234 |
} |
235 |
} |
236 |
result[i] = '\0'; |
237 |
|
238 |
return result; |
239 |
} |
240 |
|
241 |
static unsigned int hex_value(char c) { |
242 |
if ('0' <= c && c <= '9') { |
243 |
return c - '0'; |
244 |
} |
245 |
else if ('A' <= c && c <= 'F') { |
246 |
return c - 'A' + 10; |
247 |
} |
248 |
else if ('a' <= c && c <= 'f') { |
249 |
return c - 'a' + 10; |
250 |
} |
251 |
else { |
252 |
return 0; |
253 |
} |
254 |
} |
255 |
|
256 |
static char * decode_uri_component(const char * s) { |
257 |
size_t length = strlen(s); |
258 |
char * result = xmalloc(length + 1); |
259 |
char * p = result; |
260 |
while (*s != '\0') { |
261 |
if (*s == '%') { |
262 |
if (s[1] == '\0' || s[2] == '\0') { |
263 |
*p = '\0'; |
264 |
return result; |
265 |
} |
266 |
*p = hex_value(s[1]) * 16 + hex_value(s[2]); |
267 |
s += 2; |
268 |
} |
269 |
else { |
270 |
*p = *s; |
271 |
} |
272 |
p++; |
273 |
s++; |
274 |
} |
275 |
*p = '\0'; |
276 |
return result; |
277 |
} |
278 |
|
279 |
static const char * get_entity(char c) { |
280 |
switch(c) { |
281 |
case '<': |
282 |
return "<"; |
283 |
case '>': |
284 |
return ">"; |
285 |
case '&': |
286 |
return "&"; |
287 |
case '\'': |
288 |
return "'"; |
289 |
case '"': |
290 |
return """; |
291 |
default: |
292 |
return NULL; |
293 |
} |
294 |
} |
295 |
|
296 |
static char * encode_html(const char * s) { |
297 |
size_t length = 0; |
298 |
for (const char * p = s; *p != '\0'; p++) { |
299 |
const char * entity = get_entity(*p); |
300 |
if (entity == NULL) { |
301 |
length = addst(length, 1); |
302 |
} |
303 |
else { |
304 |
length = addst(length, strlen(entity)); |
305 |
} |
306 |
} |
307 |
|
308 |
length = addst(length, 1); |
309 |
char * result = xmalloc(length); |
310 |
size_t i = 0; |
311 |
for (const char * p = s; *p != '\0'; p++) { |
312 |
const char * entity = get_entity(*p); |
313 |
if (entity == NULL) { |
314 |
result[i] = *p; |
315 |
i++; |
316 |
} |
317 |
else { |
318 |
strcpy(result + i, entity); |
319 |
i += strlen(entity); |
320 |
} |
321 |
} |
322 |
result[i] = '\0'; |
323 |
|
324 |
return result; |
325 |
} |
326 |
|
327 |
static const char * get_content_type(const char * path) { |
328 |
char * last_dot = strrchr(path, '.'); |
329 |
if (last_dot == NULL) { |
330 |
return "application/octet-stream"; |
331 |
} |
332 |
for (size_t i = 0; i < sizeof(mime_types) / sizeof(mime_types[0]); i++) { |
333 |
if (strcmp(last_dot, mime_types[i].extension) == 0) { |
334 |
return mime_types[i].mime_type; |
335 |
} |
336 |
} |
337 |
return "application/octet-stream"; |
338 |
} |
339 |
|
340 |
/** |
341 |
Checks whether a URI is on the no-instrument list. |
342 |
@param uri the HTTP "Request-URI"; must not be NULL, and must not be a zero-length string |
343 |
@return true if the URI is on the no-instrument list, false otherwise |
344 |
*/ |
345 |
static bool is_no_instrument(const char * uri) { |
346 |
assert(*uri != '\0'); |
347 |
|
348 |
for (size_t i = 0; i < num_no_instrument; i++) { |
349 |
if (str_starts_with(uri, no_instrument[i])) { |
350 |
return true; |
351 |
} |
352 |
|
353 |
/* |
354 |
For a local URL, accept "/foo/bar" and "foo/bar" on the no-instrument list. |
355 |
*/ |
356 |
if (! proxy && str_starts_with(uri + 1, no_instrument[i])) { |
357 |
return true; |
358 |
} |
359 |
} |
360 |
|
361 |
return false; |
362 |
} |
363 |
|
364 |
static bool is_javascript(HTTPExchange * exchange) { |
365 |
const char * header = HTTPExchange_find_response_header(exchange, HTTP_CONTENT_TYPE); |
366 |
if (header == NULL) { |
367 |
/* guess based on extension */ |
368 |
return str_ends_with(HTTPExchange_get_request_uri(exchange), ".js"); |
369 |
} |
370 |
else { |
371 |
char * semicolon = strchr(header, ';'); |
372 |
char * content_type; |
373 |
if (semicolon == NULL) { |
374 |
content_type = xstrdup(header); |
375 |
} |
376 |
else { |
377 |
content_type = xstrndup(header, semicolon - header); |
378 |
} |
379 |
/* RFC 4329 */ |
380 |
bool result = strcmp(content_type, "text/javascript") == 0 || |
381 |
strcmp(content_type, "text/ecmascript") == 0 || |
382 |
strcmp(content_type, "text/javascript1.0") == 0 || |
383 |
strcmp(content_type, "text/javascript1.1") == 0 || |
384 |
strcmp(content_type, "text/javascript1.2") == 0 || |
385 |
strcmp(content_type, "text/javascript1.3") == 0 || |
386 |
strcmp(content_type, "text/javascript1.4") == 0 || |
387 |
strcmp(content_type, "text/javascript1.5") == 0 || |
388 |
strcmp(content_type, "text/jscript") == 0 || |
389 |
strcmp(content_type, "text/livescript") == 0 || |
390 |
strcmp(content_type, "text/x-javascript") == 0 || |
391 |
strcmp(content_type, "text/x-ecmascript") == 0 || |
392 |
strcmp(content_type, "application/x-javascript") == 0 || |
393 |
strcmp(content_type, "application/x-ecmascript") == 0 || |
394 |
strcmp(content_type, "application/javascript") == 0 || |
395 |
strcmp(content_type, "application/ecmascript") == 0; |
396 |
free(content_type); |
397 |
return result; |
398 |
} |
399 |
} |
400 |
|
401 |
static bool should_instrument_request(HTTPExchange * exchange) { |
402 |
if (! is_javascript(exchange)) { |
403 |
return false; |
404 |
} |
405 |
|
406 |
if (is_no_instrument(HTTPExchange_get_request_uri(exchange))) { |
407 |
return false; |
408 |
} |
409 |
|
410 |
return true; |
411 |
} |
412 |
|
413 |
static int merge(Coverage * coverage, FILE * f) __attribute__((warn_unused_result)); |
414 |
|
415 |
static int merge(Coverage * coverage, FILE * f) { |
416 |
Stream * stream = Stream_new(0); |
417 |
Stream_write_file_contents(stream, f); |
418 |
|
419 |
LOCK(&javascript_mutex); |
420 |
int result = jscoverage_parse_json(coverage, stream->data, stream->length); |
421 |
UNLOCK(&javascript_mutex); |
422 |
|
423 |
Stream_delete(stream); |
424 |
return result; |
425 |
} |
426 |
|
427 |
static void write_js_quoted_string(FILE * f, char * data, size_t length) { |
428 |
putc('"', f); |
429 |
for (size_t i = 0; i < length; i++) { |
430 |
char c = data[i]; |
431 |
switch (c) { |
432 |
case '\b': |
433 |
fputs("\\b", f); |
434 |
break; |
435 |
case '\f': |
436 |
fputs("\\f", f); |
437 |
break; |
438 |
case '\n': |
439 |
fputs("\\n", f); |
440 |
break; |
441 |
case '\r': |
442 |
fputs("\\r", f); |
443 |
break; |
444 |
case '\t': |
445 |
fputs("\\t", f); |
446 |
break; |
447 |
case '\v': |
448 |
fputs("\\v", f); |
449 |
break; |
450 |
case '"': |
451 |
fputs("\\\"", f); |
452 |
break; |
453 |
case '\\': |
454 |
fputs("\\\\", f); |
455 |
break; |
456 |
default: |
457 |
putc(c, f); |
458 |
break; |
459 |
} |
460 |
} |
461 |
putc('"', f); |
462 |
} |
463 |
|
464 |
static void write_source(const char * id, const uint16_t * characters, size_t num_characters, FILE * f) { |
465 |
Stream * output = Stream_new(num_characters); |
466 |
jscoverage_write_source(id, characters, num_characters, output); |
467 |
fwrite(output->data, 1, output->length, f); |
468 |
Stream_delete(output); |
469 |
} |
470 |
|
471 |
static void write_json_for_file(const FileCoverage * file_coverage, int i, void * p) { |
472 |
FILE * f = p; |
473 |
|
474 |
if (i > 0) { |
475 |
putc(',', f); |
476 |
} |
477 |
|
478 |
write_js_quoted_string(f, file_coverage->id, strlen(file_coverage->id)); |
479 |
|
480 |
fputs(":{\"coverage\":[", f); |
481 |
for (uint32_t i = 0; i < file_coverage->num_coverage_lines; i++) { |
482 |
if (i > 0) { |
483 |
putc(',', f); |
484 |
} |
485 |
int timesExecuted = file_coverage->coverage_lines[i]; |
486 |
if (timesExecuted < 0) { |
487 |
fputs("null", f); |
488 |
} |
489 |
else { |
490 |
fprintf(f, "%d", timesExecuted); |
491 |
} |
492 |
} |
493 |
fputs("],\"source\":", f); |
494 |
if (file_coverage->source_lines == NULL) { |
495 |
if (proxy) { |
496 |
const SourceCache * cached = find_cached_source(file_coverage->id); |
497 |
if (cached == NULL) { |
498 |
uint16_t * characters; |
499 |
size_t num_characters; |
500 |
if (get(file_coverage->id, &characters, &num_characters) == 0) { |
501 |
write_source(file_coverage->id, characters, num_characters, f); |
502 |
add_cached_source(file_coverage->id, characters, num_characters); |
503 |
} |
504 |
else { |
505 |
fputs("[]", f); |
506 |
HTTPServer_log_err("Warning: cannot retrieve URL: %s\n", file_coverage->id); |
507 |
} |
508 |
} |
509 |
else { |
510 |
write_source(file_coverage->id, cached->characters, cached->num_characters, f); |
511 |
} |
512 |
} |
513 |
else { |
514 |
/* check that the path begins with / */ |
515 |
if (file_coverage->id[0] == '/') { |
516 |
char * source_path = make_path(document_root, file_coverage->id + 1); |
517 |
FILE * source_file = fopen(source_path, "rb"); |
518 |
free(source_path); |
519 |
if (source_file == NULL) { |
520 |
fputs("[]", f); |
521 |
HTTPServer_log_err("Warning: cannot open file: %s\n", file_coverage->id); |
522 |
} |
523 |
else { |
524 |
Stream * stream = Stream_new(0); |
525 |
Stream_write_file_contents(stream, source_file); |
526 |
fclose(source_file); |
527 |
uint16_t * characters; |
528 |
size_t num_characters; |
529 |
int result = jscoverage_bytes_to_characters(jscoverage_encoding, stream->data, stream->length, &characters, &num_characters); |
530 |
Stream_delete(stream); |
531 |
if (result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED) { |
532 |
fputs("[]", f); |
533 |
HTTPServer_log_err("Warning: encoding %s not supported\n", jscoverage_encoding); |
534 |
} |
535 |
else if (result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE) { |
536 |
fputs("[]", f); |
537 |
HTTPServer_log_err("Warning: error decoding %s in file %s\n", jscoverage_encoding, file_coverage->id); |
538 |
} |
539 |
else { |
540 |
write_source(file_coverage->id, characters, num_characters, f); |
541 |
free(characters); |
542 |
} |
543 |
} |
544 |
} |
545 |
else { |
546 |
/* path does not begin with / */ |
547 |
fputs("[]", f); |
548 |
HTTPServer_log_err("Warning: invalid source path: %s\n", file_coverage->id); |
549 |
} |
550 |
} |
551 |
} |
552 |
else { |
553 |
fputc('[', f); |
554 |
for (uint32_t i = 0; i < file_coverage->num_source_lines; i++) { |
555 |
if (i > 0) { |
556 |
fputc(',', f); |
557 |
} |
558 |
char * source_line = file_coverage->source_lines[i]; |
559 |
write_js_quoted_string(f, source_line, strlen(source_line)); |
560 |
} |
561 |
fputc(']', f); |
562 |
} |
563 |
fputc('}', f); |
564 |
} |
565 |
|
566 |
static int write_json(Coverage * coverage, const char * path) __attribute__((warn_unused_result)); |
567 |
|
568 |
static int write_json(Coverage * coverage, const char * path) { |
569 |
/* write the JSON */ |
570 |
FILE * f = fopen(path, "wb"); |
571 |
if (f == NULL) { |
572 |
return -1; |
573 |
} |
574 |
putc('{', f); |
575 |
Coverage_foreach_file(coverage, write_json_for_file, f); |
576 |
putc('}', f); |
577 |
if (fclose(f) == EOF) { |
578 |
return -1; |
579 |
} |
580 |
return 0; |
581 |
} |
582 |
|
583 |
static void handle_jscoverage_request(HTTPExchange * exchange) { |
584 |
/* set the `Server' response-header (RFC 2616 14.38, 3.8) */ |
585 |
HTTPExchange_set_response_header(exchange, HTTP_SERVER, "jscoverage-server/" VERSION); |
586 |
|
587 |
const char * abs_path = HTTPExchange_get_abs_path(exchange); |
588 |
assert(*abs_path != '\0'); |
589 |
if (str_starts_with(abs_path, "/jscoverage-store")) { |
590 |
if (strcmp(HTTPExchange_get_method(exchange), "POST") != 0) { |
591 |
HTTPExchange_set_response_header(exchange, HTTP_ALLOW, "POST"); |
592 |
send_response(exchange, 405, "Method not allowed\n"); |
593 |
return; |
594 |
} |
595 |
|
596 |
Stream * json = Stream_new(0); |
597 |
|
598 |
/* read the POST body */ |
599 |
if (HTTPExchange_read_entire_request_entity_body(exchange, json) != 0) { |
600 |
Stream_delete(json); |
601 |
send_response(exchange, 400, "Could not read request body\n"); |
602 |
return; |
603 |
} |
604 |
|
605 |
Coverage * coverage = Coverage_new(); |
606 |
LOCK(&javascript_mutex); |
607 |
int result = jscoverage_parse_json(coverage, json->data, json->length); |
608 |
UNLOCK(&javascript_mutex); |
609 |
Stream_delete(json); |
610 |
|
611 |
if (result != 0) { |
612 |
Coverage_delete(coverage); |
613 |
send_response(exchange, 400, "Could not parse coverage data\n"); |
614 |
return; |
615 |
} |
616 |
|
617 |
mkdir_if_necessary(report_directory); |
618 |
char * current_report_directory; |
619 |
if (str_starts_with(abs_path, "/jscoverage-store/") && abs_path[18] != '\0') { |
620 |
char * dir = decode_uri_component(abs_path + 18); |
621 |
current_report_directory = make_path(report_directory, dir); |
622 |
free(dir); |
623 |
} |
624 |
else { |
625 |
current_report_directory = xstrdup(report_directory); |
626 |
} |
627 |
mkdir_if_necessary(current_report_directory); |
628 |
char * path = make_path(current_report_directory, "jscoverage.json"); |
629 |
|
630 |
/* check if the JSON file exists */ |
631 |
struct stat buf; |
632 |
if (stat(path, &buf) == 0) { |
633 |
/* it exists: merge */ |
634 |
FILE * f = fopen(path, "rb"); |
635 |
if (f == NULL) { |
636 |
result = 1; |
637 |
} |
638 |
else { |
639 |
result = merge(coverage, f); |
640 |
if (fclose(f) == EOF) { |
641 |
result = 1; |
642 |
} |
643 |
} |
644 |
if (result != 0) { |
645 |
free(current_report_directory); |
646 |
free(path); |
647 |
Coverage_delete(coverage); |
648 |
send_response(exchange, 500, "Could not merge with existing coverage data\n"); |
649 |
return; |
650 |
} |
651 |
} |
652 |
|
653 |
result = write_json(coverage, path); |
654 |
free(path); |
655 |
Coverage_delete(coverage); |
656 |
if (result != 0) { |
657 |
free(current_report_directory); |
658 |
send_response(exchange, 500, "Could not write coverage data\n"); |
659 |
return; |
660 |
} |
661 |
|
662 |
/* copy other files */ |
663 |
jscoverage_copy_resources(current_report_directory); |
664 |
path = make_path(current_report_directory, "jscoverage.js"); |
665 |
free(current_report_directory); |
666 |
FILE * f = fopen(path, "ab"); |
667 |
free(path); |
668 |
if (f == NULL) { |
669 |
send_response(exchange, 500, "Could not write to file: jscoverage.js\n"); |
670 |
return; |
671 |
} |
672 |
fputs("jscoverage_isReport = true;\r\n", f); |
673 |
if (fclose(f) == EOF) { |
674 |
send_response(exchange, 500, "Could not write to file: jscoverage.js\n"); |
675 |
return; |
676 |
} |
677 |
|
678 |
send_response(exchange, 200, "Coverage data stored\n"); |
679 |
} |
680 |
else if (str_starts_with(abs_path, "/jscoverage-shutdown")) { |
681 |
if (strcmp(HTTPExchange_get_method(exchange), "POST") != 0) { |
682 |
HTTPExchange_set_response_header(exchange, HTTP_ALLOW, "POST"); |
683 |
send_response(exchange, 405, "Method not allowed\n"); |
684 |
return; |
685 |
} |
686 |
|
687 |
/* allow only from localhost */ |
688 |
struct sockaddr_in client; |
689 |
if (HTTPExchange_get_peer(exchange, &client) != 0) { |
690 |
send_response(exchange, 500, "Cannot get client address\n"); |
691 |
return; |
692 |
} |
693 |
if (client.sin_addr.s_addr != htonl(INADDR_LOOPBACK)) { |
694 |
send_response(exchange, 403, "This operation can be performed only by localhost\n"); |
695 |
return; |
696 |
} |
697 |
|
698 |
send_response(exchange, 200, "The server will now shut down\n"); |
699 |
HTTPServer_shutdown(); |
700 |
} |
701 |
else { |
702 |
const char * path = abs_path + 1; |
703 |
const struct Resource * resource = get_resource(path); |
704 |
if (resource == NULL) { |
705 |
send_response(exchange, 404, "Not found\n"); |
706 |
return; |
707 |
} |
708 |
HTTPExchange_set_response_header(exchange, HTTP_CONTENT_TYPE, get_content_type(path)); |
709 |
if (HTTPExchange_write_response(exchange, resource->data, resource->length) != 0) { |
710 |
HTTPServer_log_err("Warning: error writing to client\n"); |
711 |
return; |
712 |
} |
713 |
if (strcmp(abs_path, "/jscoverage.js") == 0) { |
714 |
const char * s = "jscoverage_isServer = true;\r\n"; |
715 |
if (HTTPExchange_write_response(exchange, s, strlen(s)) != 0) { |
716 |
HTTPServer_log_err("Warning: error writing to client\n"); |
717 |
} |
718 |
} |
719 |
} |
720 |
} |
721 |
|
722 |
static void instrument_js(const char * id, const uint16_t * characters, size_t num_characters, Stream * output_stream) { |
723 |
const struct Resource * resource = get_resource("report.js"); |
724 |
Stream_write(output_stream, resource->data, resource->length); |
725 |
|
726 |
LOCK(&javascript_mutex); |
727 |
jscoverage_instrument_js(id, characters, num_characters, output_stream); |
728 |
UNLOCK(&javascript_mutex); |
729 |
} |
730 |
|
731 |
static bool is_hop_by_hop_header(const char * h) { |
732 |
/* hop-by-hop headers (RFC 2616 13.5.1) */ |
733 |
return strcasecmp(h, HTTP_CONNECTION) == 0 || |
734 |
strcasecmp(h, "Keep-Alive") == 0 || |
735 |
strcasecmp(h, HTTP_PROXY_AUTHENTICATE) == 0 || |
736 |
strcasecmp(h, HTTP_PROXY_AUTHORIZATION) == 0 || |
737 |
strcasecmp(h, HTTP_TE) == 0 || |
738 |
strcasecmp(h, HTTP_TRAILER) == 0 || |
739 |
strcasecmp(h, HTTP_TRANSFER_ENCODING) == 0 || |
740 |
strcasecmp(h, HTTP_UPGRADE) == 0; |
741 |
} |
742 |
|
743 |
static void add_via_header(HTTPMessage * message, const char * version) { |
744 |
char * value; |
745 |
xasprintf(&value, "%s jscoverage-server", version); |
746 |
HTTPMessage_add_header(message, HTTP_VIA, value); |
747 |
free(value); |
748 |
} |
749 |
|
750 |
static int copy_http_message_body(HTTPMessage * from, HTTPMessage * to) __attribute__((warn_unused_result)); |
751 |
|
752 |
static int copy_http_message_body(HTTPMessage * from, HTTPMessage * to) { |
753 |
uint8_t * buffer[8192]; |
754 |
for (;;) { |
755 |
size_t bytes_read; |
756 |
int result = HTTPMessage_read_message_body(from, buffer, 8192, &bytes_read); |
757 |
if (result != 0) { |
758 |
return result; |
759 |
} |
760 |
if (bytes_read == 0) { |
761 |
return 0; |
762 |
} |
763 |
result = HTTPMessage_write(to, buffer, bytes_read); |
764 |
if (result != 0) { |
765 |
return result; |
766 |
} |
767 |
} |
768 |
} |
769 |
|
770 |
static void handle_proxy_request(HTTPExchange * client_exchange) { |
771 |
HTTPConnection * server_connection = NULL; |
772 |
HTTPExchange * server_exchange = NULL; |
773 |
|
774 |
const char * abs_path = HTTPExchange_get_abs_path(client_exchange); |
775 |
if (str_starts_with(abs_path, "/jscoverage")) { |
776 |
handle_jscoverage_request(client_exchange); |
777 |
return; |
778 |
} |
779 |
|
780 |
const char * host = HTTPExchange_get_host(client_exchange); |
781 |
uint16_t port = HTTPExchange_get_port(client_exchange); |
782 |
|
783 |
/* create a new connection */ |
784 |
server_connection = HTTPConnection_new_client(host, port); |
785 |
if (server_connection == NULL) { |
786 |
send_response(client_exchange, 504, "Could not connect to server\n"); |
787 |
goto done; |
788 |
} |
789 |
|
790 |
/* create a new exchange */ |
791 |
server_exchange = HTTPExchange_new(server_connection); |
792 |
HTTPExchange_set_method(server_exchange, HTTPExchange_get_method(client_exchange)); |
793 |
HTTPExchange_set_request_uri(server_exchange, HTTPExchange_get_request_uri(client_exchange)); |
794 |
for (const HTTPHeader * h = HTTPExchange_get_request_headers(client_exchange); h != NULL; h = h->next) { |
795 |
if (strcasecmp(h->name, HTTP_TRAILER) == 0 || strcasecmp(h->name, HTTP_TRANSFER_ENCODING) == 0) { |
796 |
/* do nothing: we want to keep this header */ |
797 |
} |
798 |
else if (is_hop_by_hop_header(h->name) || |
799 |
strcasecmp(h->name, HTTP_ACCEPT_ENCODING) == 0 || |
800 |
strcasecmp(h->name, HTTP_RANGE) == 0) { |
801 |
continue; |
802 |
} |
803 |
HTTPExchange_add_request_header(server_exchange, h->name, h->value); |
804 |
} |
805 |
add_via_header(HTTPExchange_get_request_message(server_exchange), HTTPExchange_get_request_http_version(client_exchange)); |
806 |
|
807 |
/* send the request */ |
808 |
if (HTTPExchange_write_request_headers(server_exchange) != 0) { |
809 |
send_response(client_exchange, 502, "Could not write to server\n"); |
810 |
goto done; |
811 |
} |
812 |
|
813 |
/* handle POST or PUT */ |
814 |
if (HTTPExchange_request_has_body(client_exchange)) { |
815 |
HTTPMessage * client_request = HTTPExchange_get_request_message(client_exchange); |
816 |
HTTPMessage * server_request = HTTPExchange_get_request_message(server_exchange); |
817 |
if (copy_http_message_body(client_request, server_request) != 0) { |
818 |
send_response(client_exchange, 400, "Error copying request body from client to server\n"); |
819 |
goto done; |
820 |
} |
821 |
} |
822 |
|
823 |
if (HTTPExchange_flush_request(server_exchange) != 0) { |
824 |
send_response(client_exchange, 502, "Could not write to server\n"); |
825 |
goto done; |
826 |
} |
827 |
|
828 |
/* receive the response */ |
829 |
if (HTTPExchange_read_response_headers(server_exchange) != 0) { |
830 |
send_response(client_exchange, 502, "Could not read headers from server\n"); |
831 |
goto done; |
832 |
} |
833 |
|
834 |
HTTPExchange_set_status_code(client_exchange, HTTPExchange_get_status_code(server_exchange)); |
835 |
|
836 |
if (HTTPExchange_response_has_body(server_exchange) && should_instrument_request(server_exchange)) { |
837 |
/* needs instrumentation */ |
838 |
Stream * input_stream = Stream_new(0); |
839 |
if (HTTPExchange_read_entire_response_entity_body(server_exchange, input_stream) != 0) { |
840 |
Stream_delete(input_stream); |
841 |
send_response(client_exchange, 502, "Could not read body from server\n"); |
842 |
goto done; |
843 |
} |
844 |
|
845 |
const char * request_uri = HTTPExchange_get_request_uri(client_exchange); |
846 |
char * encoding = HTTPMessage_get_charset(HTTPExchange_get_response_message(server_exchange)); |
847 |
if (encoding == NULL) { |
848 |
encoding = xstrdup(jscoverage_encoding); |
849 |
} |
850 |
uint16_t * characters; |
851 |
size_t num_characters; |
852 |
int result = jscoverage_bytes_to_characters(encoding, input_stream->data, input_stream->length, &characters, &num_characters); |
853 |
free(encoding); |
854 |
Stream_delete(input_stream); |
855 |
if (result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED) { |
856 |
send_response(client_exchange, 500, "Encoding not supported\n"); |
857 |
goto done; |
858 |
} |
859 |
else if (result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE) { |
860 |
send_response(client_exchange, 502, "Error decoding response\n"); |
861 |
goto done; |
862 |
} |
863 |
|
864 |
Stream * output_stream = Stream_new(0); |
865 |
instrument_js(request_uri, characters, num_characters, output_stream); |
866 |
|
867 |
/* send the headers to the client */ |
868 |
for (const HTTPHeader * h = HTTPExchange_get_response_headers(server_exchange); h != NULL; h = h->next) { |
869 |
if (is_hop_by_hop_header(h->name) || strcasecmp(h->name, HTTP_CONTENT_LENGTH) == 0) { |
870 |
continue; |
871 |
} |
872 |
else if (strcasecmp(h->name, HTTP_CONTENT_TYPE) == 0) { |
873 |
HTTPExchange_add_response_header(client_exchange, HTTP_CONTENT_TYPE, "text/javascript; charset=ISO-8859-1"); |
874 |
continue; |
875 |
} |
876 |
HTTPExchange_add_response_header(client_exchange, h->name, h->value); |
877 |
} |
878 |
add_via_header(HTTPExchange_get_response_message(client_exchange), HTTPExchange_get_response_http_version(server_exchange)); |
879 |
HTTPExchange_set_response_content_length(client_exchange, output_stream->length); |
880 |
|
881 |
/* send the instrumented code to the client */ |
882 |
if (HTTPExchange_write_response(client_exchange, output_stream->data, output_stream->length) != 0) { |
883 |
HTTPServer_log_err("Warning: error writing to client\n"); |
884 |
} |
885 |
|
886 |
/* characters go on the cache */ |
887 |
/* |
888 |
free(characters); |
889 |
*/ |
890 |
Stream_delete(output_stream); |
891 |
add_cached_source(request_uri, characters, num_characters); |
892 |
} |
893 |
else { |
894 |
/* does not need instrumentation */ |
895 |
|
896 |
/* send the headers to the client */ |
897 |
for (const HTTPHeader * h = HTTPExchange_get_response_headers(server_exchange); h != NULL; h = h->next) { |
898 |
if (strcasecmp(h->name, HTTP_TRAILER) == 0 || strcasecmp(h->name, HTTP_TRANSFER_ENCODING) == 0) { |
899 |
/* do nothing: we want to keep this header */ |
900 |
} |
901 |
else if (is_hop_by_hop_header(h->name)) { |
902 |
continue; |
903 |
} |
904 |
HTTPExchange_add_response_header(client_exchange, h->name, h->value); |
905 |
} |
906 |
add_via_header(HTTPExchange_get_response_message(client_exchange), HTTPExchange_get_response_http_version(server_exchange)); |
907 |
|
908 |
if (HTTPExchange_write_response_headers(client_exchange) != 0) { |
909 |
HTTPServer_log_err("Warning: error writing to client\n"); |
910 |
goto done; |
911 |
} |
912 |
|
913 |
if (HTTPExchange_response_has_body(server_exchange)) { |
914 |
/* read the body from the server and send it to the client */ |
915 |
HTTPMessage * client_response = HTTPExchange_get_response_message(client_exchange); |
916 |
HTTPMessage * server_response = HTTPExchange_get_response_message(server_exchange); |
917 |
if (copy_http_message_body(server_response, client_response) != 0) { |
918 |
HTTPServer_log_err("Warning: error copying response body from server to client\n"); |
919 |
goto done; |
920 |
} |
921 |
} |
922 |
} |
923 |
|
924 |
done: |
925 |
if (server_exchange != NULL) { |
926 |
HTTPExchange_delete(server_exchange); |
927 |
} |
928 |
if (server_connection != NULL) { |
929 |
if (HTTPConnection_delete(server_connection) != 0) { |
930 |
HTTPServer_log_err("Warning: error closing connection to server\n"); |
931 |
} |
932 |
} |
933 |
} |
934 |
|
935 |
static void handle_local_request(HTTPExchange * exchange) { |
936 |
/* add the `Server' response-header (RFC 2616 14.38, 3.8) */ |
937 |
HTTPExchange_add_response_header(exchange, HTTP_SERVER, "jscoverage-server/" VERSION); |
938 |
|
939 |
char * decoded_path = NULL; |
940 |
char * filesystem_path = NULL; |
941 |
|
942 |
const char * abs_path = HTTPExchange_get_abs_path(exchange); |
943 |
assert(*abs_path != '\0'); |
944 |
|
945 |
decoded_path = decode_uri_component(abs_path); |
946 |
|
947 |
if (str_starts_with(decoded_path, "/jscoverage")) { |
948 |
handle_jscoverage_request(exchange); |
949 |
goto done; |
950 |
} |
951 |
|
952 |
if (strstr(decoded_path, "..") != NULL) { |
953 |
send_response(exchange, 403, "Forbidden\n"); |
954 |
goto done; |
955 |
} |
956 |
|
957 |
filesystem_path = make_path(document_root, decoded_path + 1); |
958 |
size_t filesystem_path_length = strlen(filesystem_path); |
959 |
if (filesystem_path_length > 0 && filesystem_path[filesystem_path_length - 1] == '/') { |
960 |
/* stat on Windows doesn't work with trailing slash */ |
961 |
filesystem_path[filesystem_path_length - 1] = '\0'; |
962 |
} |
963 |
|
964 |
struct stat buf; |
965 |
if (stat(filesystem_path, &buf) == -1) { |
966 |
send_response(exchange, 404, "Not found\n"); |
967 |
goto done; |
968 |
} |
969 |
|
970 |
if (S_ISDIR(buf.st_mode)) { |
971 |
if (abs_path[strlen(abs_path) - 1] != '/') { |
972 |
const char * request_uri = HTTPExchange_get_request_uri(exchange); |
973 |
char * uri = xmalloc(strlen(request_uri) + 2); |
974 |
strcpy(uri, request_uri); |
975 |
strcat(uri, "/"); |
976 |
HTTPExchange_add_response_header(exchange, "Location", uri); |
977 |
free(uri); |
978 |
send_response(exchange, 301, "Moved permanently\n"); |
979 |
goto done; |
980 |
} |
981 |
|
982 |
DIR * d = opendir(filesystem_path); |
983 |
if (d == NULL) { |
984 |
send_response(exchange, 404, "Not found\n"); |
985 |
goto done; |
986 |
} |
987 |
|
988 |
struct dirent * entry; |
989 |
while ((entry = readdir(d)) != NULL) { |
990 |
char * href = encode_uri_component(entry->d_name); |
991 |
char * html_href = encode_html(href); |
992 |
char * link = encode_html(entry->d_name); |
993 |
char * directory_entry; |
994 |
xasprintf(&directory_entry, "<a href=\"%s\">%s</a><br>\n", html_href, link); |
995 |
if (HTTPExchange_write_response(exchange, directory_entry, strlen(directory_entry)) != 0) { |
996 |
HTTPServer_log_err("Warning: error writing to client\n"); |
997 |
} |
998 |
free(directory_entry); |
999 |
free(href); |
1000 |
free(html_href); |
1001 |
free(link); |
1002 |
} |
1003 |
closedir(d); |
1004 |
} |
1005 |
else if (S_ISREG(buf.st_mode)) { |
1006 |
FILE * f = fopen(filesystem_path, "rb"); |
1007 |
if (f == NULL) { |
1008 |
send_response(exchange, 404, "Not found\n"); |
1009 |
goto done; |
1010 |
} |
1011 |
|
1012 |
/* |
1013 |
When do we send a charset with Content-Type? |
1014 |
if Content-Type is "text" or "application" |
1015 |
if instrumented JavaScript |
1016 |
use Content-Type: application/javascript; charset=ISO-8859-1 |
1017 |
else if --encoding is given |
1018 |
use that encoding |
1019 |
else |
1020 |
send no charset |
1021 |
else |
1022 |
send no charset |
1023 |
*/ |
1024 |
const char * content_type = get_content_type(filesystem_path); |
1025 |
if (strcmp(content_type, "text/javascript") == 0 && ! is_no_instrument(abs_path)) { |
1026 |
HTTPExchange_set_response_header(exchange, HTTP_CONTENT_TYPE, "text/javascript; charset=ISO-8859-1"); |
1027 |
|
1028 |
Stream * input_stream = Stream_new(0); |
1029 |
Stream_write_file_contents(input_stream, f); |
1030 |
|
1031 |
uint16_t * characters; |
1032 |
size_t num_characters; |
1033 |
int result = jscoverage_bytes_to_characters(jscoverage_encoding, input_stream->data, input_stream->length, &characters, &num_characters); |
1034 |
Stream_delete(input_stream); |
1035 |
|
1036 |
if (result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED) { |
1037 |
send_response(exchange, 500, "Encoding not supported\n"); |
1038 |
goto done; |
1039 |
} |
1040 |
else if (result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE) { |
1041 |
send_response(exchange, 500, "Error decoding JavaScript file\n"); |
1042 |
goto done; |
1043 |
} |
1044 |
|
1045 |
Stream * output_stream = Stream_new(0); |
1046 |
instrument_js(abs_path, characters, num_characters, output_stream); |
1047 |
free(characters); |
1048 |
|
1049 |
if (HTTPExchange_write_response(exchange, output_stream->data, output_stream->length) != 0) { |
1050 |
HTTPServer_log_err("Warning: error writing to client\n"); |
1051 |
} |
1052 |
Stream_delete(output_stream); |
1053 |
} |
1054 |
else { |
1055 |
/* send the Content-Type with charset if necessary */ |
1056 |
if (specified_encoding != NULL && (str_starts_with(content_type, "text/") || str_starts_with(content_type, "application/"))) { |
1057 |
char * content_type_with_charset = NULL; |
1058 |
xasprintf(&content_type_with_charset, "%s; charset=%s", content_type, specified_encoding); |
1059 |
HTTPExchange_set_response_header(exchange, HTTP_CONTENT_TYPE, content_type_with_charset); |
1060 |
free(content_type_with_charset); |
1061 |
} |
1062 |
else { |
1063 |
HTTPExchange_set_response_header(exchange, HTTP_CONTENT_TYPE, content_type); |
1064 |
} |
1065 |
|
1066 |
char buffer[8192]; |
1067 |
size_t bytes_read; |
1068 |
while ((bytes_read = fread(buffer, 1, 8192, f)) > 0) { |
1069 |
if (HTTPExchange_write_response(exchange, buffer, bytes_read) != 0) { |
1070 |
HTTPServer_log_err("Warning: error writing to client\n"); |
1071 |
} |
1072 |
} |
1073 |
} |
1074 |
fclose(f); |
1075 |
} |
1076 |
else { |
1077 |
send_response(exchange, 404, "Not found\n"); |
1078 |
goto done; |
1079 |
} |
1080 |
|
1081 |
done: |
1082 |
free(filesystem_path); |
1083 |
free(decoded_path); |
1084 |
} |
1085 |
|
1086 |
static void handler(HTTPExchange * exchange) { |
1087 |
if (verbose) { |
1088 |
HTTPServer_log_out("%s", HTTPExchange_get_request_line(exchange)); |
1089 |
} |
1090 |
|
1091 |
if (proxy) { |
1092 |
handle_proxy_request(exchange); |
1093 |
} |
1094 |
else { |
1095 |
handle_local_request(exchange); |
1096 |
} |
1097 |
} |
1098 |
|
1099 |
int main(int argc, char ** argv) { |
1100 |
program = "jscoverage-server"; |
1101 |
|
1102 |
const char * ip_address = "127.0.0.1"; |
1103 |
const char * port = "8080"; |
1104 |
int shutdown = 0; |
1105 |
|
1106 |
no_instrument = xnew(const char *, argc - 1); |
1107 |
|
1108 |
for (int i = 1; i < argc; i++) { |
1109 |
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) { |
1110 |
copy_resource_to_stream("jscoverage-server-help.txt", stdout); |
1111 |
exit(EXIT_SUCCESS); |
1112 |
} |
1113 |
else if (strcmp(argv[i], "-V") == 0 || strcmp(argv[i], "--version") == 0) { |
1114 |
version(); |
1115 |
} |
1116 |
else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--verbose") == 0) { |
1117 |
verbose = 1; |
1118 |
} |
1119 |
|
1120 |
else if (strcmp(argv[i], "--report-dir") == 0) { |
1121 |
i++; |
1122 |
if (i == argc) { |
1123 |
fatal_command_line("--report-dir: option requires an argument"); |
1124 |
} |
1125 |
report_directory = argv[i]; |
1126 |
} |
1127 |
else if (strncmp(argv[i], "--report-dir=", 13) == 0) { |
1128 |
report_directory = argv[i] + 13; |
1129 |
} |
1130 |
|
1131 |
else if (strcmp(argv[i], "--document-root") == 0) { |
1132 |
i++; |
1133 |
if (i == argc) { |
1134 |
fatal_command_line("--document-root: option requires an argument"); |
1135 |
} |
1136 |
document_root = argv[i]; |
1137 |
} |
1138 |
else if (strncmp(argv[i], "--document-root=", 16) == 0) { |
1139 |
document_root = argv[i] + 16; |
1140 |
} |
1141 |
|
1142 |
else if (strcmp(argv[i], "--encoding") == 0) { |
1143 |
i++; |
1144 |
if (i == argc) { |
1145 |
fatal_command_line("--encoding: option requires an argument"); |
1146 |
} |
1147 |
jscoverage_encoding = argv[i]; |
1148 |
specified_encoding = jscoverage_encoding; |
1149 |
} |
1150 |
else if (strncmp(argv[i], "--encoding=", 11) == 0) { |
1151 |
jscoverage_encoding = argv[i] + 11; |
1152 |
specified_encoding = jscoverage_encoding; |
1153 |
} |
1154 |
|
1155 |
else if (strcmp(argv[i], "--ip-address") == 0) { |
1156 |
i++; |
1157 |
if (i == argc) { |
1158 |
fatal_command_line("--ip-address: option requires an argument"); |
1159 |
} |
1160 |
ip_address = argv[i]; |
1161 |
} |
1162 |
else if (strncmp(argv[i], "--ip-address=", 13) == 0) { |
1163 |
ip_address = argv[i] + 13; |
1164 |
} |
1165 |
|
1166 |
else if (strcmp(argv[i], "--no-highlight") == 0) { |
1167 |
jscoverage_highlight = false; |
1168 |
} |
1169 |
|
1170 |
else if (strcmp(argv[i], "--no-instrument") == 0) { |
1171 |
i++; |
1172 |
if (i == argc) { |
1173 |
fatal_command_line("--no-instrument: option requires an argument"); |
1174 |
} |
1175 |
no_instrument[num_no_instrument] = argv[i]; |
1176 |
num_no_instrument++; |
1177 |
} |
1178 |
else if (strncmp(argv[i], "--no-instrument=", 16) == 0) { |
1179 |
no_instrument[num_no_instrument] = argv[i] + 16; |
1180 |
num_no_instrument++; |
1181 |
} |
1182 |
|
1183 |
else if (strcmp(argv[i], "--port") == 0) { |
1184 |
i++; |
1185 |
if (i == argc) { |
1186 |
fatal_command_line("--port: option requires an argument"); |
1187 |
} |
1188 |
port = argv[i]; |
1189 |
} |
1190 |
else if (strncmp(argv[i], "--port=", 7) == 0) { |
1191 |
port = argv[i] + 7; |
1192 |
} |
1193 |
|
1194 |
else if (strcmp(argv[i], "--proxy") == 0) { |
1195 |
proxy = 1; |
1196 |
} |
1197 |
|
1198 |
else if (strcmp(argv[i], "--shutdown") == 0) { |
1199 |
shutdown = 1; |
1200 |
} |
1201 |
|
1202 |
else if (strncmp(argv[i], "-", 1) == 0) { |
1203 |
fatal_command_line("unrecognized option `%s'", argv[i]); |
1204 |
} |
1205 |
else { |
1206 |
fatal_command_line("too many arguments"); |
1207 |
} |
1208 |
} |
1209 |
|
1210 |
/* check the port */ |
1211 |
char * end; |
1212 |
unsigned long numeric_port = strtoul(port, &end, 10); |
1213 |
if (*end != '\0') { |
1214 |
fatal_command_line("--port: option must be an integer"); |
1215 |
} |
1216 |
if (numeric_port > UINT16_MAX) { |
1217 |
fatal_command_line("--port: option must be 16 bits"); |
1218 |
} |
1219 |
|
1220 |
/* is this a shutdown? */ |
1221 |
if (shutdown) { |
1222 |
#ifdef __MINGW32__ |
1223 |
WSADATA data; |
1224 |
if (WSAStartup(MAKEWORD(1, 1), &data) != 0) { |
1225 |
fatal("could not start Winsock"); |
1226 |
} |
1227 |
#endif |
1228 |
|
1229 |
/* INADDR_LOOPBACK */ |
1230 |
HTTPConnection * connection = HTTPConnection_new_client("127.0.0.1", numeric_port); |
1231 |
if (connection == NULL) { |
1232 |
fatal("could not connect to server"); |
1233 |
} |
1234 |
HTTPExchange * exchange = HTTPExchange_new(connection); |
1235 |
HTTPExchange_set_method(exchange, "POST"); |
1236 |
HTTPExchange_set_request_uri(exchange, "/jscoverage-shutdown"); |
1237 |
if (HTTPExchange_write_request_headers(exchange) != 0) { |
1238 |
fatal("could not write request headers to server"); |
1239 |
} |
1240 |
if (HTTPExchange_read_response_headers(exchange) != 0) { |
1241 |
fatal("could not read response headers from server"); |
1242 |
} |
1243 |
Stream * stream = Stream_new(0); |
1244 |
if (HTTPExchange_read_entire_response_entity_body(exchange, stream) != 0) { |
1245 |
fatal("could not read response body from server"); |
1246 |
} |
1247 |
fwrite(stream->data, 1, stream->length, stdout); |
1248 |
Stream_delete(stream); |
1249 |
HTTPExchange_delete(exchange); |
1250 |
if (HTTPConnection_delete(connection) != 0) { |
1251 |
fatal("could not close connection with server"); |
1252 |
} |
1253 |
exit(EXIT_SUCCESS); |
1254 |
} |
1255 |
|
1256 |
jscoverage_init(); |
1257 |
|
1258 |
#ifndef __MINGW32__ |
1259 |
/* handle broken pipe */ |
1260 |
signal(SIGPIPE, SIG_IGN); |
1261 |
#endif |
1262 |
|
1263 |
#ifdef __MINGW32__ |
1264 |
InitializeCriticalSection(&javascript_mutex); |
1265 |
InitializeCriticalSection(&source_cache_mutex); |
1266 |
#endif |
1267 |
|
1268 |
if (verbose) { |
1269 |
printf("Starting HTTP server on %s:%lu\n", ip_address, numeric_port); |
1270 |
fflush(stdout); |
1271 |
} |
1272 |
HTTPServer_run(ip_address, (uint16_t) numeric_port, handler); |
1273 |
if (verbose) { |
1274 |
printf("Stopping HTTP server\n"); |
1275 |
fflush(stdout); |
1276 |
} |
1277 |
|
1278 |
jscoverage_cleanup(); |
1279 |
|
1280 |
free(no_instrument); |
1281 |
|
1282 |
LOCK(&source_cache_mutex); |
1283 |
while (source_cache != NULL) { |
1284 |
SourceCache * p = source_cache; |
1285 |
source_cache = source_cache->next; |
1286 |
free(p->url); |
1287 |
free(p->characters); |
1288 |
free(p); |
1289 |
} |
1290 |
UNLOCK(&source_cache_mutex); |
1291 |
|
1292 |
return 0; |
1293 |
} |