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 |
} |
} |
933 |
/* add the `Server' response-header (RFC 2616 14.38, 3.8) */ |
/* add the `Server' response-header (RFC 2616 14.38, 3.8) */ |
934 |
HTTPExchange_add_response_header(exchange, HTTP_SERVER, "jscoverage-server/" VERSION); |
HTTPExchange_add_response_header(exchange, HTTP_SERVER, "jscoverage-server/" VERSION); |
935 |
|
|
936 |
|
char * decoded_path = NULL; |
937 |
char * filesystem_path = NULL; |
char * filesystem_path = NULL; |
938 |
|
|
939 |
const char * abs_path = HTTPExchange_get_abs_path(exchange); |
const char * abs_path = HTTPExchange_get_abs_path(exchange); |
940 |
assert(*abs_path != '\0'); |
assert(*abs_path != '\0'); |
941 |
|
|
942 |
if (str_starts_with(abs_path, "/jscoverage")) { |
decoded_path = decode_uri_component(abs_path); |
943 |
|
|
944 |
|
if (str_starts_with(decoded_path, "/jscoverage")) { |
945 |
handle_jscoverage_request(exchange); |
handle_jscoverage_request(exchange); |
946 |
goto done; |
goto done; |
947 |
} |
} |
948 |
|
|
949 |
if (strstr(abs_path, "..") != NULL) { |
if (strstr(decoded_path, "..") != NULL) { |
950 |
send_response(exchange, 403, "Forbidden\n"); |
send_response(exchange, 403, "Forbidden\n"); |
951 |
goto done; |
goto done; |
952 |
} |
} |
953 |
|
|
954 |
filesystem_path = make_path(document_root, abs_path + 1); |
filesystem_path = make_path(document_root, decoded_path + 1); |
955 |
size_t filesystem_path_length = strlen(filesystem_path); |
size_t filesystem_path_length = strlen(filesystem_path); |
956 |
if (filesystem_path_length > 0 && filesystem_path[filesystem_path_length - 1] == '/') { |
if (filesystem_path_length > 0 && filesystem_path[filesystem_path_length - 1] == '/') { |
957 |
/* stat on Windows doesn't work with trailing slash */ |
/* stat on Windows doesn't work with trailing slash */ |
1055 |
|
|
1056 |
done: |
done: |
1057 |
free(filesystem_path); |
free(filesystem_path); |
1058 |
|
free(decoded_path); |
1059 |
} |
} |
1060 |
|
|
1061 |
static void handler(HTTPExchange * exchange) { |
static void handler(HTTPExchange * exchange) { |