--- trunk/jscoverage-server.c 2008/05/31 21:42:36 116 +++ trunk/jscoverage-server.c 2008/09/23 03:48:37 189 @@ -22,20 +22,29 @@ #include #include #include +#include #include #include +#ifdef HAVE_PTHREAD_H #include +#endif +#include "encoding.h" +#include "global.h" #include "http-server.h" #include "instrument-js.h" #include "resource-manager.h" #include "stream.h" #include "util.h" +const char * jscoverage_encoding = "ISO-8859-1"; +bool jscoverage_highlight = true; + typedef struct SourceCache { char * url; - Stream * source; + uint16_t * characters; + size_t num_characters; struct SourceCache * next; } SourceCache; @@ -64,41 +73,52 @@ static const char ** no_instrument; static size_t num_no_instrument = 0; +#ifdef __MINGW32__ +CRITICAL_SECTION javascript_mutex; +CRITICAL_SECTION source_cache_mutex; +#define LOCK EnterCriticalSection +#define UNLOCK LeaveCriticalSection +#else pthread_mutex_t javascript_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t source_cache_mutex = PTHREAD_MUTEX_INITIALIZER; - -static Stream * find_cached_source(const char * url) { - Stream * result = NULL; - pthread_mutex_lock(&source_cache_mutex); +#define LOCK pthread_mutex_lock +#define UNLOCK pthread_mutex_unlock +#endif + +static const SourceCache * find_cached_source(const char * url) { + SourceCache * result = NULL; + LOCK(&source_cache_mutex); for (SourceCache * p = source_cache; p != NULL; p = p->next) { if (strcmp(url, p->url) == 0) { - result = p->source; + result = p; break; } } - pthread_mutex_unlock(&source_cache_mutex); + UNLOCK(&source_cache_mutex); return result; } -static void add_cached_source(const char * url, Stream * source) { +static void add_cached_source(const char * url, uint16_t * characters, size_t num_characters) { SourceCache * new_source_cache = xmalloc(sizeof(SourceCache)); new_source_cache->url = xstrdup(url); - new_source_cache->source = source; - pthread_mutex_lock(&source_cache_mutex); + new_source_cache->characters = characters; + new_source_cache->num_characters = num_characters; + LOCK(&source_cache_mutex); new_source_cache->next = source_cache; source_cache = new_source_cache; - pthread_mutex_unlock(&source_cache_mutex); + UNLOCK(&source_cache_mutex); } -static int get(const char * url, Stream * stream) __attribute__((warn_unused_result)); +static int get(const char * url, uint16_t ** characters, size_t * num_characters) __attribute__((warn_unused_result)); -static int get(const char * url, Stream * stream) { +static int get(const char * url, uint16_t ** characters, size_t * num_characters) { char * host = NULL; uint16_t port; char * abs_path = NULL; char * query = NULL; HTTPConnection * connection = NULL; HTTPExchange * exchange = NULL; + Stream * stream = NULL; int result = URL_parse(url, &host, &port, &abs_path, &query); if (result != 0) { @@ -123,14 +143,27 @@ goto done; } + stream = Stream_new(0); result = HTTPExchange_read_entire_response_entity_body(exchange, stream); if (result != 0) { goto done; } + char * encoding = HTTPMessage_get_charset(HTTPExchange_get_response_message(exchange)); + if (encoding == NULL) { + encoding = xstrdup(jscoverage_encoding); + } + result = jscoverage_bytes_to_characters(encoding, stream->data, stream->length, characters, num_characters); + free(encoding); + if (result != 0) { + goto done; + } result = 0; done: + if (stream != NULL) { + Stream_delete(stream); + } if (exchange != NULL) { HTTPExchange_delete(exchange); } @@ -204,6 +237,44 @@ return result; } +static unsigned int hex_value(char c) { + if ('0' <= c && c <= '9') { + return c - '0'; + } + else if ('A' <= c && c <= 'F') { + return c - 'A'; + } + else if ('a' <= c && c <= 'f') { + return c - 'a'; + } + else { + return 0; + } +} + +static char * decode_uri_component(const char * s) { + size_t length = strlen(s); + char * result = xmalloc(length + 1); + char * p = result; + while (*s != '\0') { + if (*s == '%') { + if (s[1] == '\0' || s[2] == '\0') { + *p = '\0'; + return result; + } + *p = hex_value(s[1]) * 16 + hex_value(s[2]); + s += 2; + } + else { + *p = *s; + } + p++; + s++; + } + *p = '\0'; + return result; +} + static const char * get_entity(char c) { switch(c) { case '<': @@ -338,26 +409,17 @@ return true; } -static int merge(Coverage * coverage, const char * path, size_t size) __attribute__((warn_unused_result)); +static int merge(Coverage * coverage, FILE * f) __attribute__((warn_unused_result)); -static int merge(Coverage * coverage, const char * path, size_t size) { - FILE * f = fopen(path, "r"); - if (f == NULL) { - return -1; - } - uint8_t * buffer = xmalloc(size); - if (fread(buffer, 1, size, f) != size) { - fclose(f); - free(buffer); - return -1; - } - fclose(f); +static int merge(Coverage * coverage, FILE * f) { + Stream * stream = Stream_new(0); + Stream_write_file_contents(stream, f); - pthread_mutex_lock(&javascript_mutex); - int result = jscoverage_parse_json(coverage, buffer, size); - pthread_mutex_unlock(&javascript_mutex); + LOCK(&javascript_mutex); + int result = jscoverage_parse_json(coverage, stream->data, stream->length); + UNLOCK(&javascript_mutex); - free(buffer); + Stream_delete(stream); return result; } @@ -398,6 +460,13 @@ putc('"', f); } +static void write_source(const char * id, const uint16_t * characters, size_t num_characters, FILE * f) { + Stream * output = Stream_new(num_characters); + jscoverage_write_source(id, characters, num_characters, output); + fwrite(output->data, 1, output->length, f); + Stream_delete(output); +} + static void write_json_for_file(const FileCoverage * file_coverage, int i, void * p) { FILE * f = p; @@ -408,11 +477,11 @@ write_js_quoted_string(f, file_coverage->id, strlen(file_coverage->id)); fputs(":{\"coverage\":[", f); - for (uint32_t i = 0; i <= file_coverage->num_lines; i++) { + for (uint32_t i = 0; i < file_coverage->num_coverage_lines; i++) { if (i > 0) { putc(',', f); } - int timesExecuted = file_coverage->lines[i]; + int timesExecuted = file_coverage->coverage_lines[i]; if (timesExecuted < 0) { fputs("null", f); } @@ -421,52 +490,74 @@ } } fputs("],\"source\":", f); - if (file_coverage->source == NULL) { + if (file_coverage->source_lines == NULL) { if (proxy) { - Stream * stream = find_cached_source(file_coverage->id); - if (stream == NULL) { - stream = Stream_new(0); - if (get(file_coverage->id, stream) == 0) { - write_js_quoted_string(f, stream->data, stream->length); - add_cached_source(file_coverage->id, stream); + const SourceCache * cached = find_cached_source(file_coverage->id); + if (cached == NULL) { + uint16_t * characters; + size_t num_characters; + if (get(file_coverage->id, &characters, &num_characters) == 0) { + write_source(file_coverage->id, characters, num_characters, f); + add_cached_source(file_coverage->id, characters, num_characters); } else { - fputs("\"\"", f); + fputs("[]", f); HTTPServer_log_err("Warning: cannot retrieve URL: %s\n", file_coverage->id); - Stream_delete(stream); } } else { - write_js_quoted_string(f, stream->data, stream->length); + write_source(file_coverage->id, cached->characters, cached->num_characters, f); } } else { /* check that the path begins with / */ if (file_coverage->id[0] == '/') { char * source_path = make_path(document_root, file_coverage->id + 1); - FILE * source_file = fopen(source_path, "r"); + FILE * source_file = fopen(source_path, "rb"); free(source_path); if (source_file == NULL) { - fputs("\"\"", f); + fputs("[]", f); HTTPServer_log_err("Warning: cannot open file: %s\n", file_coverage->id); } else { Stream * stream = Stream_new(0); Stream_write_file_contents(stream, source_file); fclose(source_file); - write_js_quoted_string(f, stream->data, stream->length); + uint16_t * characters; + size_t num_characters; + int result = jscoverage_bytes_to_characters(jscoverage_encoding, stream->data, stream->length, &characters, &num_characters); Stream_delete(stream); + if (result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED) { + fputs("[]", f); + HTTPServer_log_err("Warning: encoding %s not supported\n", jscoverage_encoding); + } + else if (result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE) { + fputs("[]", f); + HTTPServer_log_err("Warning: error decoding %s in file %s\n", jscoverage_encoding, file_coverage->id); + } + else { + write_source(file_coverage->id, characters, num_characters, f); + free(characters); + } } } else { /* path does not begin with / */ - fputs("\"\"", f); + fputs("[]", f); HTTPServer_log_err("Warning: invalid source path: %s\n", file_coverage->id); } } } else { - write_js_quoted_string(f, file_coverage->source, strlen(file_coverage->source)); + fputc('[', f); + for (uint32_t i = 0; i < file_coverage->num_source_lines; i++) { + if (i > 0) { + fputc(',', f); + } + char * source_line = file_coverage->source_lines[i]; + write_js_quoted_string(f, source_line, strlen(source_line)); + } + fputc(']', f); } fputc('}', f); } @@ -511,9 +602,9 @@ } Coverage * coverage = Coverage_new(); - pthread_mutex_lock(&javascript_mutex); + LOCK(&javascript_mutex); int result = jscoverage_parse_json(coverage, json->data, json->length); - pthread_mutex_unlock(&javascript_mutex); + UNLOCK(&javascript_mutex); Stream_delete(json); if (result != 0) { @@ -523,12 +614,34 @@ } mkdir_if_necessary(report_directory); - char * path = make_path(report_directory, "jscoverage.json"); + char * current_report_directory; + if (str_starts_with(abs_path, "/jscoverage-store/") && abs_path[18] != '\0') { + char * dir = decode_uri_component(abs_path + 18); + current_report_directory = make_path(report_directory, dir); + free(dir); + } + else { + current_report_directory = xstrdup(report_directory); + } + mkdir_if_necessary(current_report_directory); + char * path = make_path(current_report_directory, "jscoverage.json"); + + /* check if the JSON file exists */ struct stat buf; if (stat(path, &buf) == 0) { /* it exists: merge */ - result = merge(coverage, path, buf.st_size); + FILE * f = fopen(path, "r"); + if (f == NULL) { + result = 1; + } + else { + result = merge(coverage, f); + if (fclose(f) == EOF) { + result = 1; + } + } if (result != 0) { + free(current_report_directory); free(path); Coverage_delete(coverage); send_response(exchange, 500, "Could not merge with existing coverage data\n"); @@ -540,13 +653,15 @@ free(path); Coverage_delete(coverage); if (result != 0) { + free(current_report_directory); send_response(exchange, 500, "Could not write coverage data\n"); return; } /* copy other files */ - jscoverage_copy_resources(report_directory); - path = make_path(report_directory, "jscoverage.js"); + jscoverage_copy_resources(current_report_directory); + path = make_path(current_report_directory, "jscoverage.js"); + free(current_report_directory); FILE * f = fopen(path, "ab"); free(path); if (f == NULL) { @@ -603,13 +718,13 @@ } } -static void instrument_js(const char * id, Stream * input_stream, Stream * output_stream) { - pthread_mutex_lock(&javascript_mutex); - jscoverage_instrument_js(id, input_stream, output_stream); - pthread_mutex_unlock(&javascript_mutex); - +static void instrument_js(const char * id, const uint16_t * characters, size_t num_characters, Stream * output_stream) { const struct Resource * resource = get_resource("report.js"); Stream_write(output_stream, resource->data, resource->length); + + LOCK(&javascript_mutex); + jscoverage_instrument_js(id, characters, num_characters, output_stream); + UNLOCK(&javascript_mutex); } static bool is_hop_by_hop_header(const char * h) { @@ -727,8 +842,28 @@ } const char * request_uri = HTTPExchange_get_request_uri(client_exchange); + char * encoding = HTTPMessage_get_charset(HTTPExchange_get_response_message(server_exchange)); + if (encoding == NULL) { + encoding = xstrdup(jscoverage_encoding); + } + uint16_t * characters; + size_t num_characters; + int result = jscoverage_bytes_to_characters(encoding, input_stream->data, input_stream->length, &characters, &num_characters); + free(encoding); + Stream_delete(input_stream); + if (result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED) { + free(characters); + send_response(client_exchange, 502, "Encoding not supported\n"); + goto done; + } + else if (result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE) { + free(characters); + send_response(client_exchange, 502, "Error decoding response\n"); + goto done; + } + Stream * output_stream = Stream_new(0); - instrument_js(request_uri, input_stream, output_stream); + instrument_js(request_uri, characters, num_characters, output_stream); /* send the headers to the client */ for (const HTTPHeader * h = HTTPExchange_get_response_headers(server_exchange); h != NULL; h = h->next) { @@ -745,12 +880,12 @@ HTTPServer_log_err("Warning: error writing to client\n"); } - /* input_stream goes on the cache */ + /* characters go on the cache */ /* - Stream_delete(input_stream); + free(characters); */ Stream_delete(output_stream); - add_cached_source(request_uri, input_stream); + add_cached_source(request_uri, characters, num_characters); } else { /* does not need instrumentation */ @@ -814,6 +949,11 @@ } filesystem_path = make_path(document_root, abs_path + 1); + size_t filesystem_path_length = strlen(filesystem_path); + if (filesystem_path_length > 0 && filesystem_path[filesystem_path_length - 1] == '/') { + /* stat on Windows doesn't work with trailing slash */ + filesystem_path[filesystem_path_length - 1] = '\0'; + } struct stat buf; if (stat(filesystem_path, &buf) == -1) { @@ -822,7 +962,7 @@ } if (S_ISDIR(buf.st_mode)) { - if (filesystem_path[strlen(filesystem_path) - 1] != '/') { + if (abs_path[strlen(abs_path) - 1] != '/') { const char * request_uri = HTTPExchange_get_request_uri(exchange); char * uri = xmalloc(strlen(request_uri) + 2); strcpy(uri, request_uri); @@ -857,7 +997,7 @@ closedir(d); } else if (S_ISREG(buf.st_mode)) { - FILE * f = fopen(filesystem_path, "r"); + FILE * f = fopen(filesystem_path, "rb"); if (f == NULL) { send_response(exchange, 404, "Not found\n"); goto done; @@ -865,20 +1005,33 @@ const char * content_type = get_content_type(filesystem_path); HTTPExchange_set_response_header(exchange, HTTP_CONTENT_TYPE, content_type); - const char * request_uri = HTTPExchange_get_request_uri(exchange); - if (strcmp(content_type, "text/javascript") == 0 && ! is_no_instrument(request_uri)) { + if (strcmp(content_type, "text/javascript") == 0 && ! is_no_instrument(abs_path)) { Stream * input_stream = Stream_new(0); - Stream * output_stream = Stream_new(0); - Stream_write_file_contents(input_stream, f); - instrument_js(request_uri, input_stream, output_stream); + uint16_t * characters; + size_t num_characters; + int result = jscoverage_bytes_to_characters(jscoverage_encoding, input_stream->data, input_stream->length, &characters, &num_characters); + Stream_delete(input_stream); + + if (result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED) { + free(characters); + send_response(exchange, 500, "Encoding not supported\n"); + goto done; + } + else if (result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE) { + free(characters); + send_response(exchange, 500, "Error decoding JavaScript file\n"); + goto done; + } + + Stream * output_stream = Stream_new(0); + instrument_js(abs_path, characters, num_characters, output_stream); + free(characters); if (HTTPExchange_write_response(exchange, output_stream->data, output_stream->length) != 0) { HTTPServer_log_err("Warning: error writing to client\n"); } - - Stream_delete(input_stream); Stream_delete(output_stream); } else { @@ -958,6 +1111,17 @@ document_root = argv[i] + 16; } + else if (strcmp(argv[i], "--encoding") == 0) { + i++; + if (i == argc) { + fatal("--encoding: option requires an argument"); + } + jscoverage_encoding = argv[i]; + } + else if (strncmp(argv[i], "--encoding=", 11) == 0) { + jscoverage_encoding = argv[i] + 11; + } + else if (strcmp(argv[i], "--ip-address") == 0) { i++; if (i == argc) { @@ -969,6 +1133,10 @@ ip_address = argv[i] + 13; } + else if (strcmp(argv[i], "--no-highlight") == 0) { + jscoverage_highlight = false; + } + else if (strcmp(argv[i], "--no-instrument") == 0) { i++; if (i == argc) { @@ -1021,6 +1189,13 @@ /* is this a shutdown? */ if (shutdown) { +#ifdef __MINGW32__ + WSADATA data; + if (WSAStartup(MAKEWORD(1, 1), &data) != 0) { + fatal("could not start Winsock"); + } +#endif + /* INADDR_LOOPBACK */ HTTPConnection * connection = HTTPConnection_new_client("127.0.0.1", numeric_port); if (connection == NULL) { @@ -1050,30 +1225,39 @@ jscoverage_init(); +#ifndef __MINGW32__ /* handle broken pipe */ signal(SIGPIPE, SIG_IGN); +#endif + +#ifdef __MINGW32__ +InitializeCriticalSection(&javascript_mutex); +InitializeCriticalSection(&source_cache_mutex); +#endif if (verbose) { printf("Starting HTTP server on %s:%lu\n", ip_address, numeric_port); + fflush(stdout); } HTTPServer_run(ip_address, (uint16_t) numeric_port, handler); if (verbose) { printf("Stopping HTTP server\n"); + fflush(stdout); } jscoverage_cleanup(); free(no_instrument); - pthread_mutex_lock(&source_cache_mutex); + LOCK(&source_cache_mutex); while (source_cache != NULL) { SourceCache * p = source_cache; source_cache = source_cache->next; free(p->url); - Stream_delete(p->source); + free(p->characters); free(p); } - pthread_mutex_unlock(&source_cache_mutex); + UNLOCK(&source_cache_mutex); return 0; }