242 |
return c - '0'; |
return c - '0'; |
243 |
} |
} |
244 |
else if ('A' <= c && c <= 'F') { |
else if ('A' <= c && c <= 'F') { |
245 |
return c - 'A'; |
return c - 'A' + 10; |
246 |
} |
} |
247 |
else if ('a' <= c && c <= 'f') { |
else if ('a' <= c && c <= 'f') { |
248 |
return c - 'a'; |
return c - 'a' + 10; |
249 |
} |
} |
250 |
else { |
else { |
251 |
return 0; |
return 0; |
566 |
|
|
567 |
static int write_json(Coverage * coverage, const char * path) { |
static int write_json(Coverage * coverage, const char * path) { |
568 |
/* write the JSON */ |
/* write the JSON */ |
569 |
FILE * f = fopen(path, "w"); |
FILE * f = fopen(path, "wb"); |
570 |
if (f == NULL) { |
if (f == NULL) { |
571 |
return -1; |
return -1; |
572 |
} |
} |
630 |
struct stat buf; |
struct stat buf; |
631 |
if (stat(path, &buf) == 0) { |
if (stat(path, &buf) == 0) { |
632 |
/* it exists: merge */ |
/* it exists: merge */ |
633 |
FILE * f = fopen(path, "r"); |
FILE * f = fopen(path, "rb"); |
634 |
if (f == NULL) { |
if (f == NULL) { |
635 |
result = 1; |
result = 1; |
636 |
} |
} |
852 |
free(encoding); |
free(encoding); |
853 |
Stream_delete(input_stream); |
Stream_delete(input_stream); |
854 |
if (result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED) { |
if (result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED) { |
|
free(characters); |
|
855 |
send_response(client_exchange, 502, "Encoding not supported\n"); |
send_response(client_exchange, 502, "Encoding not supported\n"); |
856 |
goto done; |
goto done; |
857 |
} |
} |
858 |
else if (result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE) { |
else if (result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE) { |
|
free(characters); |
|
859 |
send_response(client_exchange, 502, "Error decoding response\n"); |
send_response(client_exchange, 502, "Error decoding response\n"); |
860 |
goto done; |
goto done; |
861 |
} |
} |
931 |
/* add the `Server' response-header (RFC 2616 14.38, 3.8) */ |
/* add the `Server' response-header (RFC 2616 14.38, 3.8) */ |
932 |
HTTPExchange_add_response_header(exchange, HTTP_SERVER, "jscoverage-server/" VERSION); |
HTTPExchange_add_response_header(exchange, HTTP_SERVER, "jscoverage-server/" VERSION); |
933 |
|
|
934 |
|
char * decoded_path = NULL; |
935 |
char * filesystem_path = NULL; |
char * filesystem_path = NULL; |
936 |
|
|
937 |
const char * abs_path = HTTPExchange_get_abs_path(exchange); |
const char * abs_path = HTTPExchange_get_abs_path(exchange); |
938 |
assert(*abs_path != '\0'); |
assert(*abs_path != '\0'); |
939 |
|
|
940 |
if (str_starts_with(abs_path, "/jscoverage")) { |
decoded_path = decode_uri_component(abs_path); |
941 |
|
|
942 |
|
if (str_starts_with(decoded_path, "/jscoverage")) { |
943 |
handle_jscoverage_request(exchange); |
handle_jscoverage_request(exchange); |
944 |
goto done; |
goto done; |
945 |
} |
} |
946 |
|
|
947 |
if (strstr(abs_path, "..") != NULL) { |
if (strstr(decoded_path, "..") != NULL) { |
948 |
send_response(exchange, 403, "Forbidden\n"); |
send_response(exchange, 403, "Forbidden\n"); |
949 |
goto done; |
goto done; |
950 |
} |
} |
951 |
|
|
952 |
filesystem_path = make_path(document_root, abs_path + 1); |
filesystem_path = make_path(document_root, decoded_path + 1); |
953 |
size_t filesystem_path_length = strlen(filesystem_path); |
size_t filesystem_path_length = strlen(filesystem_path); |
954 |
if (filesystem_path_length > 0 && filesystem_path[filesystem_path_length - 1] == '/') { |
if (filesystem_path_length > 0 && filesystem_path[filesystem_path_length - 1] == '/') { |
955 |
/* stat on Windows doesn't work with trailing slash */ |
/* stat on Windows doesn't work with trailing slash */ |
1016 |
Stream_delete(input_stream); |
Stream_delete(input_stream); |
1017 |
|
|
1018 |
if (result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED) { |
if (result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED) { |
|
free(characters); |
|
1019 |
send_response(exchange, 500, "Encoding not supported\n"); |
send_response(exchange, 500, "Encoding not supported\n"); |
1020 |
goto done; |
goto done; |
1021 |
} |
} |
1022 |
else if (result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE) { |
else if (result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE) { |
|
free(characters); |
|
1023 |
send_response(exchange, 500, "Error decoding JavaScript file\n"); |
send_response(exchange, 500, "Error decoding JavaScript file\n"); |
1024 |
goto done; |
goto done; |
1025 |
} |
} |
1051 |
|
|
1052 |
done: |
done: |
1053 |
free(filesystem_path); |
free(filesystem_path); |
1054 |
|
free(decoded_path); |
1055 |
} |
} |
1056 |
|
|
1057 |
static void handler(HTTPExchange * exchange) { |
static void handler(HTTPExchange * exchange) { |