/[jscoverage]/trunk/jscoverage-server.c
ViewVC logotype

Diff of /trunk/jscoverage-server.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 189 by siliconforks, Tue Sep 23 03:48:37 2008 UTC revision 447 by siliconforks, Sun Aug 9 16:21:27 2009 UTC
# Line 1  Line 1 
1  /*  /*
2      jscoverage-server.c - JSCoverage server main routine      jscoverage-server.c - JSCoverage server main routine
3      Copyright (C) 2008 siliconforks.com      Copyright (C) 2008, 2009 siliconforks.com
4    
5      This program is free software; you can redistribute it and/or modify      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      it under the terms of the GNU General Public License as published by
# Line 38  Line 38 
38  #include "stream.h"  #include "stream.h"
39  #include "util.h"  #include "util.h"
40    
41    static const char * specified_encoding = NULL;
42  const char * jscoverage_encoding = "ISO-8859-1";  const char * jscoverage_encoding = "ISO-8859-1";
43  bool jscoverage_highlight = true;  bool jscoverage_highlight = true;
44    
# Line 242  Line 243 
243      return c - '0';      return c - '0';
244    }    }
245    else if ('A' <= c && c <= 'F') {    else if ('A' <= c && c <= 'F') {
246      return c - 'A';      return c - 'A' + 10;
247    }    }
248    else if ('a' <= c && c <= 'f') {    else if ('a' <= c && c <= 'f') {
249      return c - 'a';      return c - 'a' + 10;
250    }    }
251    else {    else {
252      return 0;      return 0;
# Line 443  Line 444 
444      case '\t':      case '\t':
445        fputs("\\t", f);        fputs("\\t", f);
446        break;        break;
447        /* IE doesn't support this */
448        /*
449      case '\v':      case '\v':
450        fputs("\\v", f);        fputs("\\v", f);
451        break;        break;
452        */
453      case '"':      case '"':
454        fputs("\\\"", f);        fputs("\\\"", f);
455        break;        break;
# Line 512  Line 516 
516      else {      else {
517        /* check that the path begins with / */        /* check that the path begins with / */
518        if (file_coverage->id[0] == '/') {        if (file_coverage->id[0] == '/') {
519          char * source_path = make_path(document_root, file_coverage->id + 1);          char * decoded_path = decode_uri_component(file_coverage->id);
520            if (strstr(decoded_path, "..") != NULL) {
521              free(decoded_path);
522              fputs("[]", f);
523              HTTPServer_log_err("Warning: invalid source path: %s\n", file_coverage->id);
524              goto done;
525            }
526            char * source_path = make_path(document_root, decoded_path + 1);
527            free(decoded_path);
528          FILE * source_file = fopen(source_path, "rb");          FILE * source_file = fopen(source_path, "rb");
529          free(source_path);          free(source_path);
530          if (source_file == NULL) {          if (source_file == NULL) {
# Line 559  Line 571 
571      }      }
572      fputc(']', f);      fputc(']', f);
573    }    }
574    done:
575    fputc('}', f);    fputc('}', f);
576  }  }
577    
# Line 566  Line 579 
579    
580  static int write_json(Coverage * coverage, const char * path) {  static int write_json(Coverage * coverage, const char * path) {
581    /* write the JSON */    /* write the JSON */
582    FILE * f = fopen(path, "w");    FILE * f = fopen(path, "wb");
583    if (f == NULL) {    if (f == NULL) {
584      return -1;      return -1;
585    }    }
# Line 630  Line 643 
643      struct stat buf;      struct stat buf;
644      if (stat(path, &buf) == 0) {      if (stat(path, &buf) == 0) {
645        /* it exists: merge */        /* it exists: merge */
646        FILE * f = fopen(path, "r");        FILE * f = fopen(path, "rb");
647        if (f == NULL) {        if (f == NULL) {
648          result = 1;          result = 1;
649        }        }
# Line 852  Line 865 
865      free(encoding);      free(encoding);
866      Stream_delete(input_stream);      Stream_delete(input_stream);
867      if (result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED) {      if (result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED) {
868        free(characters);        send_response(client_exchange, 500, "Encoding not supported\n");
       send_response(client_exchange, 502, "Encoding not supported\n");  
869        goto done;        goto done;
870      }      }
871      else if (result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE) {      else if (result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE) {
       free(characters);  
872        send_response(client_exchange, 502, "Error decoding response\n");        send_response(client_exchange, 502, "Error decoding response\n");
873        goto done;        goto done;
874      }      }
# Line 870  Line 881 
881        if (is_hop_by_hop_header(h->name) || strcasecmp(h->name, HTTP_CONTENT_LENGTH) == 0) {        if (is_hop_by_hop_header(h->name) || strcasecmp(h->name, HTTP_CONTENT_LENGTH) == 0) {
882          continue;          continue;
883        }        }
884          else if (strcasecmp(h->name, HTTP_CONTENT_TYPE) == 0) {
885            HTTPExchange_add_response_header(client_exchange, HTTP_CONTENT_TYPE, "text/javascript; charset=ISO-8859-1");
886            continue;
887          }
888        HTTPExchange_add_response_header(client_exchange, h->name, h->value);        HTTPExchange_add_response_header(client_exchange, h->name, h->value);
889      }      }
890      add_via_header(HTTPExchange_get_response_message(client_exchange), HTTPExchange_get_response_http_version(server_exchange));      add_via_header(HTTPExchange_get_response_message(client_exchange), HTTPExchange_get_response_http_version(server_exchange));
# Line 933  Line 948 
948    /* add the `Server' response-header (RFC 2616 14.38, 3.8) */    /* add the `Server' response-header (RFC 2616 14.38, 3.8) */
949    HTTPExchange_add_response_header(exchange, HTTP_SERVER, "jscoverage-server/" VERSION);    HTTPExchange_add_response_header(exchange, HTTP_SERVER, "jscoverage-server/" VERSION);
950    
951      char * decoded_path = NULL;
952    char * filesystem_path = NULL;    char * filesystem_path = NULL;
953    
954    const char * abs_path = HTTPExchange_get_abs_path(exchange);    const char * abs_path = HTTPExchange_get_abs_path(exchange);
955    assert(*abs_path != '\0');    assert(*abs_path != '\0');
956    
957    if (str_starts_with(abs_path, "/jscoverage")) {    decoded_path = decode_uri_component(abs_path);
958    
959      if (str_starts_with(decoded_path, "/jscoverage")) {
960      handle_jscoverage_request(exchange);      handle_jscoverage_request(exchange);
961      goto done;      goto done;
962    }    }
963    
964    if (strstr(abs_path, "..") != NULL) {    if (strstr(decoded_path, "..") != NULL) {
965      send_response(exchange, 403, "Forbidden\n");      send_response(exchange, 403, "Forbidden\n");
966      goto done;      goto done;
967    }    }
968    
969    filesystem_path = make_path(document_root, abs_path + 1);    filesystem_path = make_path(document_root, decoded_path + 1);
970    size_t filesystem_path_length = strlen(filesystem_path);    size_t filesystem_path_length = strlen(filesystem_path);
971    if (filesystem_path_length > 0 && filesystem_path[filesystem_path_length - 1] == '/') {    if (filesystem_path_length > 0 && filesystem_path[filesystem_path_length - 1] == '/') {
972      /* stat on Windows doesn't work with trailing slash */      /* stat on Windows doesn't work with trailing slash */
# Line 1003  Line 1021 
1021        goto done;        goto done;
1022      }      }
1023    
1024        /*
1025        When do we send a charset with Content-Type?
1026        if Content-Type is "text" or "application"
1027          if instrumented JavaScript
1028            use Content-Type: application/javascript; charset=ISO-8859-1
1029          else if --encoding is given
1030            use that encoding
1031          else
1032            send no charset
1033        else
1034          send no charset
1035        */
1036      const char * content_type = get_content_type(filesystem_path);      const char * content_type = get_content_type(filesystem_path);
     HTTPExchange_set_response_header(exchange, HTTP_CONTENT_TYPE, content_type);  
1037      if (strcmp(content_type, "text/javascript") == 0 && ! is_no_instrument(abs_path)) {      if (strcmp(content_type, "text/javascript") == 0 && ! is_no_instrument(abs_path)) {
1038          HTTPExchange_set_response_header(exchange, HTTP_CONTENT_TYPE, "text/javascript; charset=ISO-8859-1");
1039    
1040        Stream * input_stream = Stream_new(0);        Stream * input_stream = Stream_new(0);
1041        Stream_write_file_contents(input_stream, f);        Stream_write_file_contents(input_stream, f);
1042    
# Line 1015  Line 1046 
1046        Stream_delete(input_stream);        Stream_delete(input_stream);
1047    
1048        if (result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED) {        if (result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED) {
         free(characters);  
1049          send_response(exchange, 500, "Encoding not supported\n");          send_response(exchange, 500, "Encoding not supported\n");
1050          goto done;          goto done;
1051        }        }
1052        else if (result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE) {        else if (result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE) {
         free(characters);  
1053          send_response(exchange, 500, "Error decoding JavaScript file\n");          send_response(exchange, 500, "Error decoding JavaScript file\n");
1054          goto done;          goto done;
1055        }        }
# Line 1035  Line 1064 
1064        Stream_delete(output_stream);        Stream_delete(output_stream);
1065      }      }
1066      else {      else {
1067          /* send the Content-Type with charset if necessary */
1068          if (specified_encoding != NULL && (str_starts_with(content_type, "text/") || str_starts_with(content_type, "application/"))) {
1069            char * content_type_with_charset = NULL;
1070            xasprintf(&content_type_with_charset, "%s; charset=%s", content_type, specified_encoding);
1071            HTTPExchange_set_response_header(exchange, HTTP_CONTENT_TYPE, content_type_with_charset);
1072            free(content_type_with_charset);
1073          }
1074          else {
1075            HTTPExchange_set_response_header(exchange, HTTP_CONTENT_TYPE, content_type);
1076          }
1077    
1078        char buffer[8192];        char buffer[8192];
1079        size_t bytes_read;        size_t bytes_read;
1080        while ((bytes_read = fread(buffer, 1, 8192, f)) > 0) {        while ((bytes_read = fread(buffer, 1, 8192, f)) > 0) {
# Line 1052  Line 1092 
1092    
1093  done:  done:
1094    free(filesystem_path);    free(filesystem_path);
1095      free(decoded_path);
1096  }  }
1097    
1098  static void handler(HTTPExchange * exchange) {  static void handler(HTTPExchange * exchange) {
# Line 1082  Line 1123 
1123        exit(EXIT_SUCCESS);        exit(EXIT_SUCCESS);
1124      }      }
1125      else if (strcmp(argv[i], "-V") == 0 || strcmp(argv[i], "--version") == 0) {      else if (strcmp(argv[i], "-V") == 0 || strcmp(argv[i], "--version") == 0) {
1126        printf("jscoverage-server %s\n", VERSION);        version();
       exit(EXIT_SUCCESS);  
1127      }      }
1128      else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--verbose") == 0) {      else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--verbose") == 0) {
1129        verbose = 1;        verbose = 1;
# Line 1092  Line 1132 
1132      else if (strcmp(argv[i], "--report-dir") == 0) {      else if (strcmp(argv[i], "--report-dir") == 0) {
1133        i++;        i++;
1134        if (i == argc) {        if (i == argc) {
1135          fatal("--report-dir: option requires an argument");          fatal_command_line("--report-dir: option requires an argument");
1136        }        }
1137        report_directory = argv[i];        report_directory = argv[i];
1138      }      }
# Line 1103  Line 1143 
1143      else if (strcmp(argv[i], "--document-root") == 0) {      else if (strcmp(argv[i], "--document-root") == 0) {
1144        i++;        i++;
1145        if (i == argc) {        if (i == argc) {
1146          fatal("--document-root: option requires an argument");          fatal_command_line("--document-root: option requires an argument");
1147        }        }
1148        document_root = argv[i];        document_root = argv[i];
1149      }      }
# Line 1114  Line 1154 
1154      else if (strcmp(argv[i], "--encoding") == 0) {      else if (strcmp(argv[i], "--encoding") == 0) {
1155        i++;        i++;
1156        if (i == argc) {        if (i == argc) {
1157          fatal("--encoding: option requires an argument");          fatal_command_line("--encoding: option requires an argument");
1158        }        }
1159        jscoverage_encoding = argv[i];        jscoverage_encoding = argv[i];
1160          specified_encoding = jscoverage_encoding;
1161      }      }
1162      else if (strncmp(argv[i], "--encoding=", 11) == 0) {      else if (strncmp(argv[i], "--encoding=", 11) == 0) {
1163        jscoverage_encoding = argv[i] + 11;        jscoverage_encoding = argv[i] + 11;
1164          specified_encoding = jscoverage_encoding;
1165      }      }
1166    
1167      else if (strcmp(argv[i], "--ip-address") == 0) {      else if (strcmp(argv[i], "--ip-address") == 0) {
1168        i++;        i++;
1169        if (i == argc) {        if (i == argc) {
1170          fatal("--ip-address: option requires an argument");          fatal_command_line("--ip-address: option requires an argument");
1171        }        }
1172        ip_address = argv[i];        ip_address = argv[i];
1173      }      }
# Line 1133  Line 1175 
1175        ip_address = argv[i] + 13;        ip_address = argv[i] + 13;
1176      }      }
1177    
1178        else if (strcmp(argv[i], "--js-version") == 0) {
1179          i++;
1180          if (i == argc) {
1181            fatal_command_line("--js-version: option requires an argument");
1182          }
1183          jscoverage_set_js_version(argv[i]);
1184        }
1185        else if (strncmp(argv[i], "--js-version=", 13) == 0) {
1186          jscoverage_set_js_version(argv[i] + 13);
1187        }
1188    
1189      else if (strcmp(argv[i], "--no-highlight") == 0) {      else if (strcmp(argv[i], "--no-highlight") == 0) {
1190        jscoverage_highlight = false;        jscoverage_highlight = false;
1191      }      }
# Line 1140  Line 1193 
1193      else if (strcmp(argv[i], "--no-instrument") == 0) {      else if (strcmp(argv[i], "--no-instrument") == 0) {
1194        i++;        i++;
1195        if (i == argc) {        if (i == argc) {
1196          fatal("--no-instrument: option requires an argument");          fatal_command_line("--no-instrument: option requires an argument");
1197        }        }
1198        no_instrument[num_no_instrument] = argv[i];        no_instrument[num_no_instrument] = argv[i];
1199        num_no_instrument++;        num_no_instrument++;
# Line 1153  Line 1206 
1206      else if (strcmp(argv[i], "--port") == 0) {      else if (strcmp(argv[i], "--port") == 0) {
1207        i++;        i++;
1208        if (i == argc) {        if (i == argc) {
1209          fatal("--port: option requires an argument");          fatal_command_line("--port: option requires an argument");
1210        }        }
1211        port = argv[i];        port = argv[i];
1212      }      }
# Line 1170  Line 1223 
1223      }      }
1224    
1225      else if (strncmp(argv[i], "-", 1) == 0) {      else if (strncmp(argv[i], "-", 1) == 0) {
1226        fatal("unrecognized option `%s'", argv[i]);        fatal_command_line("unrecognized option `%s'", argv[i]);
1227      }      }
1228      else {      else {
1229        fatal("too many arguments");        fatal_command_line("too many arguments");
1230      }      }
1231    }    }
1232    
# Line 1181  Line 1234 
1234    char * end;    char * end;
1235    unsigned long numeric_port = strtoul(port, &end, 10);    unsigned long numeric_port = strtoul(port, &end, 10);
1236    if (*end != '\0') {    if (*end != '\0') {
1237      fatal("--port: option must be an integer");      fatal_command_line("--port: option must be an integer");
1238    }    }
1239    if (numeric_port > UINT16_MAX) {    if (numeric_port > UINT16_MAX) {
1240      fatal("--port: option must be 16 bits");      fatal_command_line("--port: option must be 16 bits");
1241    }    }
1242    
1243    /* is this a shutdown? */    /* is this a shutdown? */

Legend:
Removed from v.189  
changed lines
  Added in v.447

  ViewVC Help
Powered by ViewVC 1.1.24