diff --git a/common/socket_helper.h b/common/socket_helper.h index 0ecea63..a0b6667 100644 --- a/common/socket_helper.h +++ b/common/socket_helper.h @@ -9,6 +9,7 @@ #define HEADER_CONTENT_LENGTH "Content-Length" #define HEADER_TRANSFER_ENCODING "Transfer-Encoding" #define HEADER_CONNECTION "Connection" +#define HEADER_DATE "Date" enum line_type {RESULT_START_LINE, RESULT_HEADER, RESULT_BLANK_LINE, RESULT_NO_LINE}; enum socket_result {RESULT_OK, RESULT_MALFORMED, RESULT_READ_ERROR, RESULT_PROCESSING_ERROR, RESULT_WRITE_ERROR}; diff --git a/server/socket_server.c b/server/socket_server.c index bb8adc6..35303f1 100644 --- a/server/socket_server.c +++ b/server/socket_server.c @@ -11,6 +11,7 @@ #include #include #include +#include #include /** @@ -80,6 +81,21 @@ struct server_info setup(int port) { return info; } +/** + * Make an HTTP date for the date header + * + * @param buffer The buffer to write into + * @param buffer_size The max size of the buffer + * + * @return The number of bytes the format takes up, excluding the null term. + */ +static size_t make_http_date(char *buffer, size_t buffer_size) { + time_t current_time = time(NULL); + struct tm *utc_time = gmtime(¤t_time); + + return strftime(buffer, buffer_size, DATE_FORMAT, utc_time); +} + /** * Sends the headers and status line of the response * @@ -121,8 +137,15 @@ static int send_headers(int status_code, int client_socket, long content_length) free(content_length_string); } + // The 1024 is somewhat arbitrary, but everything should fit. + char *date_header_buffer = malloc(1024 * sizeof(char)); + header_buffer_size += make_http_date(date_header_buffer, 1024); + // +4 is ': \r\n' + header_buffer_size += strlen(HEADER_DATE) + 4; + char *header_buffer = malloc(header_buffer_size * sizeof(char)); - sprintf(header_buffer, "%s: close\r\n%s\r\n", HEADER_CONNECTION, content_length_header_buffer); + sprintf(header_buffer, "%s: close\r\n%s: %s\r\n%s\r\n", HEADER_CONNECTION, HEADER_DATE, date_header_buffer, content_length_header_buffer); + free(date_header_buffer); if (content_length > 0) { free(content_length_header_buffer); } diff --git a/server/socket_server.h b/server/socket_server.h index 0a109e3..84b9aa0 100644 --- a/server/socket_server.h +++ b/server/socket_server.h @@ -6,6 +6,7 @@ #define LISTEN_ADDR "::" #define BACKLOG_SIZE 8 #define HTTP_VERSION "HTTP/1.1" +#define DATE_FORMAT "%a, %d %b %Y %H:%M:%S %Z" #define FILE_BUFFER_SIZE 1024 enum server_status {STATUS_LISTENING, STATUS_CLOSED, STATUS_ERROR};