--- trunk/jscoverage-server.c 2008/10/11 22:38:05 275 +++ trunk/jscoverage-server.c 2009/08/09 16:21:27 447 @@ -1,6 +1,6 @@ /* jscoverage-server.c - JSCoverage server main routine - Copyright (C) 2008 siliconforks.com + Copyright (C) 2008, 2009 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 @@ -38,6 +38,7 @@ #include "stream.h" #include "util.h" +static const char * specified_encoding = NULL; const char * jscoverage_encoding = "ISO-8859-1"; bool jscoverage_highlight = true; @@ -443,9 +444,12 @@ case '\t': fputs("\\t", f); break; + /* IE doesn't support this */ + /* case '\v': fputs("\\v", f); break; + */ case '"': fputs("\\\"", f); break; @@ -512,7 +516,15 @@ else { /* check that the path begins with / */ if (file_coverage->id[0] == '/') { - char * source_path = make_path(document_root, file_coverage->id + 1); + char * decoded_path = decode_uri_component(file_coverage->id); + if (strstr(decoded_path, "..") != NULL) { + free(decoded_path); + fputs("[]", f); + HTTPServer_log_err("Warning: invalid source path: %s\n", file_coverage->id); + goto done; + } + char * source_path = make_path(document_root, decoded_path + 1); + free(decoded_path); FILE * source_file = fopen(source_path, "rb"); free(source_path); if (source_file == NULL) { @@ -559,6 +571,7 @@ } fputc(']', f); } +done: fputc('}', f); } @@ -852,7 +865,7 @@ free(encoding); Stream_delete(input_stream); if (result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED) { - send_response(client_exchange, 502, "Encoding not supported\n"); + send_response(client_exchange, 500, "Encoding not supported\n"); goto done; } else if (result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE) { @@ -868,6 +881,10 @@ if (is_hop_by_hop_header(h->name) || strcasecmp(h->name, HTTP_CONTENT_LENGTH) == 0) { continue; } + else if (strcasecmp(h->name, HTTP_CONTENT_TYPE) == 0) { + HTTPExchange_add_response_header(client_exchange, HTTP_CONTENT_TYPE, "text/javascript; charset=ISO-8859-1"); + continue; + } HTTPExchange_add_response_header(client_exchange, h->name, h->value); } add_via_header(HTTPExchange_get_response_message(client_exchange), HTTPExchange_get_response_http_version(server_exchange)); @@ -1004,9 +1021,22 @@ goto done; } + /* + When do we send a charset with Content-Type? + if Content-Type is "text" or "application" + if instrumented JavaScript + use Content-Type: application/javascript; charset=ISO-8859-1 + else if --encoding is given + use that encoding + else + send no charset + else + send no charset + */ const char * content_type = get_content_type(filesystem_path); - HTTPExchange_set_response_header(exchange, HTTP_CONTENT_TYPE, content_type); if (strcmp(content_type, "text/javascript") == 0 && ! is_no_instrument(abs_path)) { + HTTPExchange_set_response_header(exchange, HTTP_CONTENT_TYPE, "text/javascript; charset=ISO-8859-1"); + Stream * input_stream = Stream_new(0); Stream_write_file_contents(input_stream, f); @@ -1034,6 +1064,17 @@ Stream_delete(output_stream); } else { + /* send the Content-Type with charset if necessary */ + if (specified_encoding != NULL && (str_starts_with(content_type, "text/") || str_starts_with(content_type, "application/"))) { + char * content_type_with_charset = NULL; + xasprintf(&content_type_with_charset, "%s; charset=%s", content_type, specified_encoding); + HTTPExchange_set_response_header(exchange, HTTP_CONTENT_TYPE, content_type_with_charset); + free(content_type_with_charset); + } + else { + HTTPExchange_set_response_header(exchange, HTTP_CONTENT_TYPE, content_type); + } + char buffer[8192]; size_t bytes_read; while ((bytes_read = fread(buffer, 1, 8192, f)) > 0) { @@ -1082,8 +1123,7 @@ exit(EXIT_SUCCESS); } else if (strcmp(argv[i], "-V") == 0 || strcmp(argv[i], "--version") == 0) { - printf("jscoverage-server %s\n", VERSION); - exit(EXIT_SUCCESS); + version(); } else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--verbose") == 0) { verbose = 1; @@ -1117,9 +1157,11 @@ fatal_command_line("--encoding: option requires an argument"); } jscoverage_encoding = argv[i]; + specified_encoding = jscoverage_encoding; } else if (strncmp(argv[i], "--encoding=", 11) == 0) { jscoverage_encoding = argv[i] + 11; + specified_encoding = jscoverage_encoding; } else if (strcmp(argv[i], "--ip-address") == 0) { @@ -1133,6 +1175,17 @@ ip_address = argv[i] + 13; } + else if (strcmp(argv[i], "--js-version") == 0) { + i++; + if (i == argc) { + fatal_command_line("--js-version: option requires an argument"); + } + jscoverage_set_js_version(argv[i]); + } + else if (strncmp(argv[i], "--js-version=", 13) == 0) { + jscoverage_set_js_version(argv[i] + 13); + } + else if (strcmp(argv[i], "--no-highlight") == 0) { jscoverage_highlight = false; }