fix(http): fix serving large file
This commit is contained in:
+1
-1
@@ -8,6 +8,6 @@ typedef struct mimetype {
|
|||||||
const char *type;
|
const char *type;
|
||||||
} mimetype;
|
} mimetype;
|
||||||
|
|
||||||
int http_get_content_type(char *location_path, char *content_type);
|
int http_get_content_type(const char *location_path, char *content_type);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -2,12 +2,6 @@
|
|||||||
#define CWS_RESPONSE_H
|
#define CWS_RESPONSE_H
|
||||||
|
|
||||||
#include "http/request.h"
|
#include "http/request.h"
|
||||||
#include <sys/types.h>
|
|
||||||
|
|
||||||
size_t http_simple_html(char **response, cws_http_status_e status, char *title, char *description);
|
|
||||||
|
|
||||||
size_t http_response_builder(char **response, cws_http_status_e status, char *content_type,
|
|
||||||
char *body, size_t body_len_bytes);
|
|
||||||
|
|
||||||
void cws_http_send_response(cws_request_s *request, cws_http_status_e status);
|
void cws_http_send_response(cws_request_s *request, cws_http_status_e status);
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -9,7 +9,7 @@ static mimetype mimetypes[] = {
|
|||||||
{"html", "text/html"}, {"css", "text/css"}, {"js", "application/javascript"},
|
{"html", "text/html"}, {"css", "text/css"}, {"js", "application/javascript"},
|
||||||
{"jpg", "image/jpeg"}, {"png", "image/png"}, {"ico", "image/x-icon"}};
|
{"jpg", "image/jpeg"}, {"png", "image/png"}, {"ico", "image/x-icon"}};
|
||||||
|
|
||||||
int http_get_content_type(char *location_path, char *content_type) {
|
int http_get_content_type(const char *location_path, char *content_type) {
|
||||||
/* Find last occurrence of a string */
|
/* Find last occurrence of a string */
|
||||||
char *ptr = strrchr(location_path, '.');
|
char *ptr = strrchr(location_path, '.');
|
||||||
if (ptr == NULL) {
|
if (ptr == NULL) {
|
||||||
|
|||||||
+78
-97
@@ -4,32 +4,16 @@
|
|||||||
#include "http/request.h"
|
#include "http/request.h"
|
||||||
#include "utils/debug.h"
|
#include "utils/debug.h"
|
||||||
#include <core/socket.h>
|
#include <core/socket.h>
|
||||||
#include <errno.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
size_t http_simple_html(char **response, cws_http_status_e status, char *title, char *description) {
|
/* 8 KB reading buffer length */
|
||||||
char body[512] = {0};
|
#define CHUNK_SIZE 8192
|
||||||
|
#define HEADERS_LEN 512
|
||||||
|
|
||||||
snprintf(body, sizeof(body),
|
static size_t http_header_len(const char *status_code, char *content_type, size_t body_len) {
|
||||||
"<html>\n"
|
|
||||||
"<head>\n"
|
|
||||||
" <title>%s</title>\n"
|
|
||||||
"</head>\n"
|
|
||||||
"<body>\n"
|
|
||||||
"<p>%s</p>\n"
|
|
||||||
"</body>"
|
|
||||||
"</html>",
|
|
||||||
title, description);
|
|
||||||
size_t body_len = strlen(body);
|
|
||||||
|
|
||||||
size_t response_len = http_response_builder(response, status, "text/html", body, body_len);
|
|
||||||
|
|
||||||
return response_len;
|
|
||||||
}
|
|
||||||
|
|
||||||
static size_t http_header_len(char *status_code, char *content_type, size_t body_len) {
|
|
||||||
size_t len = snprintf(NULL, 0,
|
size_t len = snprintf(NULL, 0,
|
||||||
"HTTP/1.1 %s\r\n"
|
"HTTP/1.1 %s\r\n"
|
||||||
"Content-Type: %s\r\n"
|
"Content-Type: %s\r\n"
|
||||||
@@ -41,94 +25,97 @@ static size_t http_header_len(char *status_code, char *content_type, size_t body
|
|||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *http_status_string(cws_http_status_e status) {
|
static const char *http_status_string(cws_http_status_e status) {
|
||||||
switch (status) {
|
switch (status) {
|
||||||
case HTTP_OK: {
|
case HTTP_OK: {
|
||||||
return "200 OK";
|
return "200 OK";
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
case HTTP_NOT_FOUND: {
|
case HTTP_NOT_FOUND: {
|
||||||
return "404 Not Found";
|
return "404 Not Found";
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
case HTTP_NOT_IMPLEMENTED: {
|
case HTTP_NOT_IMPLEMENTED: {
|
||||||
return "501 Not Implemented";
|
return "501 Not Implemented";
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return "?";
|
return "?";
|
||||||
}
|
}
|
||||||
|
|
||||||
static size_t file_data(const char *path, char **data) {
|
static void http_send_headers(int sockfd, const char *status_str, const char *content_type, size_t content_length) {
|
||||||
FILE *file = fopen(path, "rb");
|
char headers[HEADERS_LEN];
|
||||||
if (!file) {
|
int len = snprintf(headers, sizeof headers,
|
||||||
return 0;
|
"HTTP/1.1 %s\r\n"
|
||||||
|
"Content-Type: %s\r\n"
|
||||||
|
"Content-Length: %zu\r\n"
|
||||||
|
"Connection: close\r\n"
|
||||||
|
"\r\n",
|
||||||
|
status_str, content_type, content_length);
|
||||||
|
|
||||||
|
if (len > 0) {
|
||||||
|
cws_send_data(sockfd, headers, len, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Retrieve file size */
|
|
||||||
fseek(file, 0, SEEK_END);
|
|
||||||
const size_t content_length = ftell(file);
|
|
||||||
errno = 0;
|
|
||||||
rewind(file);
|
|
||||||
if (errno != 0) {
|
|
||||||
fclose(file);
|
|
||||||
CWS_LOG_ERROR("Unable to rewind");
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Retrieve file data */
|
|
||||||
*data = malloc(content_length);
|
|
||||||
if (!*data) {
|
|
||||||
fclose(file);
|
|
||||||
CWS_LOG_ERROR("Unable to allocate file data");
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Read file data */
|
|
||||||
size_t read_bytes = fread(*data, 1, content_length, file);
|
|
||||||
fclose(file);
|
|
||||||
|
|
||||||
if (read_bytes != content_length) {
|
|
||||||
CWS_LOG_ERROR("Partial read from file");
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return content_length;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void http_send_resource(cws_request_s *request) {
|
static void http_send_file(cws_request_s *request) {
|
||||||
char *data = NULL;
|
const char *path = request->location_path->data;
|
||||||
size_t content_length = file_data(request->location_path->data, &data);
|
FILE *fp = fopen(path, "rb");
|
||||||
if (content_length == 0) {
|
if (!fp) {
|
||||||
/* File not found */
|
CWS_LOG_ERROR("Cannot open file: %s", path);
|
||||||
cws_http_send_response(request, HTTP_NOT_FOUND);
|
cws_http_send_response(request, HTTP_NOT_FOUND);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
char content_type[CWS_HTTP_CONTENT_TYPE];
|
/* Get file size */
|
||||||
http_get_content_type(request->location_path->data, content_type);
|
struct stat st;
|
||||||
|
if (stat(path, &st) != 0) {
|
||||||
|
fclose(fp);
|
||||||
|
cws_http_send_response(request, HTTP_NOT_FOUND);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
size_t file_size = st.st_size;
|
||||||
|
|
||||||
/* TODO: Check for keep-alive */
|
char content_tye[CWS_HTTP_CONTENT_TYPE];
|
||||||
|
http_get_content_type(path, content_tye);
|
||||||
|
|
||||||
char *response = NULL;
|
/* Send headers */
|
||||||
|
http_send_headers(request->sockfd, http_status_string(HTTP_OK), content_tye, file_size);
|
||||||
|
|
||||||
size_t response_len =
|
/* Send the file in chunks */
|
||||||
http_response_builder(&response, HTTP_OK, content_type, data, content_length);
|
char buffer[CHUNK_SIZE];
|
||||||
|
size_t bytes_read = 0;
|
||||||
|
|
||||||
ssize_t sent = cws_send_data(request->sockfd, response, response_len, 0);
|
while ((bytes_read = fread(buffer, 1, sizeof buffer, fp)) > 0) {
|
||||||
CWS_LOG_DEBUG("Sent %zd bytes", sent);
|
/* TODO: check return */
|
||||||
|
cws_send_data(request->sockfd, buffer, bytes_read, 0);
|
||||||
|
}
|
||||||
|
|
||||||
free(response);
|
fclose(fp);
|
||||||
free(data);
|
CWS_LOG_DEBUG("Served file: %s (%zu bytes)", path, file_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t http_response_builder(char **response, cws_http_status_e status, char *content_type,
|
void http_send_simple_html(cws_request_s *request, cws_http_status_e status, const char *title, const char *desc) {
|
||||||
char *body, size_t body_len_bytes) {
|
const char *fmt = "<html>\n"
|
||||||
char *status_code = http_status_string(status);
|
"<head><title>%s</title></head>\n"
|
||||||
|
"<body><p>%s</p></body>\n"
|
||||||
|
"</html>";
|
||||||
|
|
||||||
|
int body_len = snprintf(NULL, 0, fmt, title, desc);
|
||||||
|
if (body_len < 0)
|
||||||
|
body_len = 0;
|
||||||
|
|
||||||
|
http_send_headers(request->sockfd, http_status_string(status), "text/html", body_len);
|
||||||
|
|
||||||
|
char *body = malloc(body_len + 1);
|
||||||
|
if (body) {
|
||||||
|
snprintf(body, body_len + 1, fmt, title, desc);
|
||||||
|
cws_send_data(request->sockfd, body, body_len, 0);
|
||||||
|
free(body);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t http_response_builder(char **response, cws_http_status_e status, char *content_type, char *body,
|
||||||
|
size_t body_len_bytes) {
|
||||||
|
const char *status_code = http_status_string(status);
|
||||||
|
|
||||||
size_t header_len = http_header_len(status_code, content_type, body_len_bytes);
|
size_t header_len = http_header_len(status_code, content_type, body_len_bytes);
|
||||||
size_t total_len = header_len + body_len_bytes;
|
size_t total_len = header_len + body_len_bytes;
|
||||||
@@ -139,8 +126,8 @@ size_t http_response_builder(char **response, cws_http_status_e status, char *co
|
|||||||
}
|
}
|
||||||
|
|
||||||
snprintf(*response, header_len + 1,
|
snprintf(*response, header_len + 1,
|
||||||
"HTTP/1.1 %s\r\nContent-Type: %s\r\nContent-Length: %zu\r\nConnection: close\r\n\r\n",
|
"HTTP/1.1 %s\r\nContent-Type: %s\r\nContent-Length: %zu\r\nConnection: close\r\n\r\n", status_code,
|
||||||
status_code, content_type, body_len_bytes);
|
content_type, body_len_bytes);
|
||||||
|
|
||||||
/* Only append body if we have it */
|
/* Only append body if we have it */
|
||||||
if (body && body_len_bytes > 0) {
|
if (body && body_len_bytes > 0) {
|
||||||
@@ -153,27 +140,21 @@ size_t http_response_builder(char **response, cws_http_status_e status, char *co
|
|||||||
}
|
}
|
||||||
|
|
||||||
void cws_http_send_response(cws_request_s *request, cws_http_status_e status) {
|
void cws_http_send_response(cws_request_s *request, cws_http_status_e status) {
|
||||||
char *response = NULL;
|
|
||||||
|
|
||||||
switch (status) {
|
switch (status) {
|
||||||
case HTTP_OK:
|
case HTTP_OK:
|
||||||
http_send_resource(request);
|
http_send_file(request);
|
||||||
break;
|
break;
|
||||||
case HTTP_NOT_FOUND: {
|
|
||||||
size_t len = http_simple_html(&response, HTTP_NOT_FOUND, "404 Not Found",
|
|
||||||
"Resource not found, 404.");
|
|
||||||
cws_send_data(request->sockfd, response, len, 0);
|
|
||||||
|
|
||||||
|
case HTTP_NOT_FOUND:
|
||||||
|
http_send_simple_html(request, HTTP_NOT_FOUND, "404 Not Found", "The requested resource was not found.");
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
case HTTP_NOT_IMPLEMENTED: {
|
|
||||||
size_t len = http_simple_html(&response, HTTP_NOT_IMPLEMENTED, "501 Not Implemented",
|
|
||||||
"Method not implemented, 501.");
|
|
||||||
cws_send_data(request->sockfd, response, len, 0);
|
|
||||||
|
|
||||||
|
case HTTP_NOT_IMPLEMENTED:
|
||||||
|
http_send_simple_html(request, HTTP_NOT_IMPLEMENTED, "501 Not Implemented", "Method not implemented.");
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
http_send_simple_html(request, status, "Error", "An unexpected error occurred.");
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
free(response);
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user