Read data as binary rather than strings

master
Nick Krichevsky 2018-09-11 15:04:47 -04:00
parent 3582cc8b0d
commit 67ce81fe03
2 changed files with 3 additions and 3 deletions

View File

@ -124,7 +124,7 @@ enum file_result get_file_from_uri(const char *uri, struct requested_file *req_f
return RESULT_NOT_FILE;
}
FILE *open_file = fopen(resource, "r");
FILE *open_file = fopen(resource, "rb");
if (open_file == NULL) {
return RESULT_IO_ERROR;
}

View File

@ -178,8 +178,8 @@ static int send_headers(int status_code, int client_socket, long content_length)
*/
static int send_file(int client_fd, struct requested_file file) {
char *buffer = malloc(FILE_BUFFER_SIZE * sizeof(char));
while (fgets(buffer, FILE_BUFFER_SIZE, file.file) != NULL) {
int bytes_read = strlen(buffer);
int bytes_read;
while ((bytes_read = fread(buffer, sizeof(char), FILE_BUFFER_SIZE, file.file)) != 0) {
int send_result = send(client_fd, buffer, bytes_read, 0);
if (send_result == -1) {
free(buffer);