Add support for date header
parent
a9706cc6ac
commit
8d8c4f0097
|
@ -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};
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/**
|
||||
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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};
|
||||
|
|
Loading…
Reference in New Issue