/[jscoverage]/tags/jscoverage-0.4/jscoverage-server.c
ViewVC logotype

Diff of /tags/jscoverage-0.4/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 300 by siliconforks, Mon Oct 13 03:07:24 2008 UTC
# 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 566  Line 567 
567    
568  static int write_json(Coverage * coverage, const char * path) {  static int write_json(Coverage * coverage, const char * path) {
569    /* write the JSON */    /* write the JSON */
570    FILE * f = fopen(path, "w");    FILE * f = fopen(path, "wb");
571    if (f == NULL) {    if (f == NULL) {
572      return -1;      return -1;
573    }    }
# Line 630  Line 631 
631      struct stat buf;      struct stat buf;
632      if (stat(path, &buf) == 0) {      if (stat(path, &buf) == 0) {
633        /* it exists: merge */        /* it exists: merge */
634        FILE * f = fopen(path, "r");        FILE * f = fopen(path, "rb");
635        if (f == NULL) {        if (f == NULL) {
636          result = 1;          result = 1;
637        }        }
# Line 852  Line 853 
853      free(encoding);      free(encoding);
854      Stream_delete(input_stream);      Stream_delete(input_stream);
855      if (result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED) {      if (result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED) {
       free(characters);  
856        send_response(client_exchange, 502, "Encoding not supported\n");        send_response(client_exchange, 502, "Encoding not supported\n");
857        goto done;        goto done;
858      }      }
859      else if (result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE) {      else if (result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE) {
       free(characters);  
860        send_response(client_exchange, 502, "Error decoding response\n");        send_response(client_exchange, 502, "Error decoding response\n");
861        goto done;        goto done;
862      }      }
# Line 870  Line 869 
869        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) {
870          continue;          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);        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));      add_via_header(HTTPExchange_get_response_message(client_exchange), HTTPExchange_get_response_http_version(server_exchange));
# Line 933  Line 936 
936    /* add the `Server' response-header (RFC 2616 14.38, 3.8) */    /* add the `Server' response-header (RFC 2616 14.38, 3.8) */
937    HTTPExchange_add_response_header(exchange, HTTP_SERVER, "jscoverage-server/" VERSION);    HTTPExchange_add_response_header(exchange, HTTP_SERVER, "jscoverage-server/" VERSION);
938    
939      char * decoded_path = NULL;
940    char * filesystem_path = NULL;    char * filesystem_path = NULL;
941    
942    const char * abs_path = HTTPExchange_get_abs_path(exchange);    const char * abs_path = HTTPExchange_get_abs_path(exchange);
943    assert(*abs_path != '\0');    assert(*abs_path != '\0');
944    
945    if (str_starts_with(abs_path, "/jscoverage")) {    decoded_path = decode_uri_component(abs_path);
946    
947      if (str_starts_with(decoded_path, "/jscoverage")) {
948      handle_jscoverage_request(exchange);      handle_jscoverage_request(exchange);
949      goto done;      goto done;
950    }    }
951    
952    if (strstr(abs_path, "..") != NULL) {    if (strstr(decoded_path, "..") != NULL) {
953      send_response(exchange, 403, "Forbidden\n");      send_response(exchange, 403, "Forbidden\n");
954      goto done;      goto done;
955    }    }
956    
957    filesystem_path = make_path(document_root, abs_path + 1);    filesystem_path = make_path(document_root, decoded_path + 1);
958    size_t filesystem_path_length = strlen(filesystem_path);    size_t filesystem_path_length = strlen(filesystem_path);
959    if (filesystem_path_length > 0 && filesystem_path[filesystem_path_length - 1] == '/') {    if (filesystem_path_length > 0 && filesystem_path[filesystem_path_length - 1] == '/') {
960      /* stat on Windows doesn't work with trailing slash */      /* stat on Windows doesn't work with trailing slash */
# Line 1003  Line 1009 
1009        goto done;        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);      const char * content_type = get_content_type(filesystem_path);
     HTTPExchange_set_response_header(exchange, HTTP_CONTENT_TYPE, content_type);  
1025      if (strcmp(content_type, "text/javascript") == 0 && ! is_no_instrument(abs_path)) {      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);        Stream * input_stream = Stream_new(0);
1029        Stream_write_file_contents(input_stream, f);        Stream_write_file_contents(input_stream, f);
1030    
# Line 1015  Line 1034 
1034        Stream_delete(input_stream);        Stream_delete(input_stream);
1035    
1036        if (result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED) {        if (result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED) {
         free(characters);  
1037          send_response(exchange, 500, "Encoding not supported\n");          send_response(exchange, 500, "Encoding not supported\n");
1038          goto done;          goto done;
1039        }        }
1040        else if (result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE) {        else if (result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE) {
         free(characters);  
1041          send_response(exchange, 500, "Error decoding JavaScript file\n");          send_response(exchange, 500, "Error decoding JavaScript file\n");
1042          goto done;          goto done;
1043        }        }
# Line 1035  Line 1052 
1052        Stream_delete(output_stream);        Stream_delete(output_stream);
1053      }      }
1054      else {      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];        char buffer[8192];
1067        size_t bytes_read;        size_t bytes_read;
1068        while ((bytes_read = fread(buffer, 1, 8192, f)) > 0) {        while ((bytes_read = fread(buffer, 1, 8192, f)) > 0) {
# Line 1052  Line 1080 
1080    
1081  done:  done:
1082    free(filesystem_path);    free(filesystem_path);
1083      free(decoded_path);
1084  }  }
1085    
1086  static void handler(HTTPExchange * exchange) {  static void handler(HTTPExchange * exchange) {
# Line 1092  Line 1121 
1121      else if (strcmp(argv[i], "--report-dir") == 0) {      else if (strcmp(argv[i], "--report-dir") == 0) {
1122        i++;        i++;
1123        if (i == argc) {        if (i == argc) {
1124          fatal("--report-dir: option requires an argument");          fatal_command_line("--report-dir: option requires an argument");
1125        }        }
1126        report_directory = argv[i];        report_directory = argv[i];
1127      }      }
# Line 1103  Line 1132 
1132      else if (strcmp(argv[i], "--document-root") == 0) {      else if (strcmp(argv[i], "--document-root") == 0) {
1133        i++;        i++;
1134        if (i == argc) {        if (i == argc) {
1135          fatal("--document-root: option requires an argument");          fatal_command_line("--document-root: option requires an argument");
1136        }        }
1137        document_root = argv[i];        document_root = argv[i];
1138      }      }
# Line 1114  Line 1143 
1143      else if (strcmp(argv[i], "--encoding") == 0) {      else if (strcmp(argv[i], "--encoding") == 0) {
1144        i++;        i++;
1145        if (i == argc) {        if (i == argc) {
1146          fatal("--encoding: option requires an argument");          fatal_command_line("--encoding: option requires an argument");
1147        }        }
1148        jscoverage_encoding = argv[i];        jscoverage_encoding = argv[i];
1149          specified_encoding = jscoverage_encoding;
1150      }      }
1151      else if (strncmp(argv[i], "--encoding=", 11) == 0) {      else if (strncmp(argv[i], "--encoding=", 11) == 0) {
1152        jscoverage_encoding = argv[i] + 11;        jscoverage_encoding = argv[i] + 11;
1153          specified_encoding = jscoverage_encoding;
1154      }      }
1155    
1156      else if (strcmp(argv[i], "--ip-address") == 0) {      else if (strcmp(argv[i], "--ip-address") == 0) {
1157        i++;        i++;
1158        if (i == argc) {        if (i == argc) {
1159          fatal("--ip-address: option requires an argument");          fatal_command_line("--ip-address: option requires an argument");
1160        }        }
1161        ip_address = argv[i];        ip_address = argv[i];
1162      }      }
# Line 1140  Line 1171 
1171      else if (strcmp(argv[i], "--no-instrument") == 0) {      else if (strcmp(argv[i], "--no-instrument") == 0) {
1172        i++;        i++;
1173        if (i == argc) {        if (i == argc) {
1174          fatal("--no-instrument: option requires an argument");          fatal_command_line("--no-instrument: option requires an argument");
1175        }        }
1176        no_instrument[num_no_instrument] = argv[i];        no_instrument[num_no_instrument] = argv[i];
1177        num_no_instrument++;        num_no_instrument++;
# Line 1153  Line 1184 
1184      else if (strcmp(argv[i], "--port") == 0) {      else if (strcmp(argv[i], "--port") == 0) {
1185        i++;        i++;
1186        if (i == argc) {        if (i == argc) {
1187          fatal("--port: option requires an argument");          fatal_command_line("--port: option requires an argument");
1188        }        }
1189        port = argv[i];        port = argv[i];
1190      }      }
# Line 1170  Line 1201 
1201      }      }
1202    
1203      else if (strncmp(argv[i], "-", 1) == 0) {      else if (strncmp(argv[i], "-", 1) == 0) {
1204        fatal("unrecognized option `%s'", argv[i]);        fatal_command_line("unrecognized option `%s'", argv[i]);
1205      }      }
1206      else {      else {
1207        fatal("too many arguments");        fatal_command_line("too many arguments");
1208      }      }
1209    }    }
1210    
# Line 1181  Line 1212 
1212    char * end;    char * end;
1213    unsigned long numeric_port = strtoul(port, &end, 10);    unsigned long numeric_port = strtoul(port, &end, 10);
1214    if (*end != '\0') {    if (*end != '\0') {
1215      fatal("--port: option must be an integer");      fatal_command_line("--port: option must be an integer");
1216    }    }
1217    if (numeric_port > UINT16_MAX) {    if (numeric_port > UINT16_MAX) {
1218      fatal("--port: option must be 16 bits");      fatal_command_line("--port: option must be 16 bits");
1219    }    }
1220    
1221    /* is this a shutdown? */    /* is this a shutdown? */

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

  ViewVC Help
Powered by ViewVC 1.1.24