#include "http.h" #include #include #include #include #include #include const char * status_str[] = { [STATUS_OK] = "200 OK", [STATUS_BAD_REQUEST] = "400 Bad Request", [STATUS_METHOD_NOT_ALLOWED] = "405 Method Not Allowed", [STATUS_REQUEST_TIMEOUT] = "408 Request Timeout", [STATUS_INTERNAL_SERVER_ERROR] = "500 Internal Server Error", [STATUS_NOT_IMPLEMENTED] = "501 Not Implemented", [STATUS_VERSION_NOT_SUPPORTED] = "505 Version Not Supported", }; /// Allocates and initializes request structure. /// \return Pointer to initialized request /// \see /free_request struct request * new_request(void) { struct request * request = malloc(sizeof(struct request)); if (NULL == request) { return NULL; } memset(request, 0, sizeof(struct request)); request->step = parse_method; return request; } /// Releases memory used by the request and the request itself. /// \param request Request to free /// \see /new_request void free_request(struct request * request) { if (NULL != request->data) { // TODO: free(request->data); data lifetime is now managed by plop, move it here } if (NULL != request->headers) { free(request->headers); } free(request); } /// Sends a simple response only with a status to the client. /// \param fd File descriptor of the client socket /// \param status HTTP response status code /// \return Negative value if an error was encountered; numbers of bytes written otherwise int respond_only_status(const int fd, const enum status status) { static const char * pattern = "HTTP/1.1 %s\r\n" "Connection: close\r\n" "\r\n"; return dprintf(fd, pattern, status_str[status]); } /// Sends a response with a status and a body to the client. /// \param fd File descriptor of the client socket /// \param status HTTP response status code /// \param body Content that will be sent /// \param size Size of the content in bytes /// \return Negative value if an error was encountered; numbers of bytes written otherwise int respond_with_body(const int fd, const enum status status, const char * body, const int size) { static const char * pattern = "HTTP/1.1 %s\r\n" "Connection: close\r\n" "Content-Type: application/json\r\n" "Content-Size: %d\r\n" "\r\n"; if (0 > dprintf(fd, pattern, status_str[status], size)) { return -1; // TODO: Handle errors properly } return write(fd, body, size); } /// Collects request between calls to `poll`. /// \param fd Client socket /// \param request Buffer with the request content /// \return Number of bytes parsed, -1 if an error occured or 0 if expects more data int collect_request(const int fd, char ** request) { static const int size = 4096; if (NULL == *request) { *request = malloc(size); if (NULL == *request) { return -1; } } // TODO: Expand buffer until EAGAIN or arbitrary limit int length = read(fd, *request, size - 1); if (0 == length || (-1 == length && EWOULDBLOCK != errno && EAGAIN != errno)) { return -1; // TODO: Handle errors properly } // TODO: Use struct request in plop struct request * debug = new_request(); debug->data = *request; debug->length = length; debug->step(debug); printf( "(%.*s) [%.*s] %.*s\n", debug->version.length, debug->data + debug->version.start, debug->method.length, debug->data + debug->method.start, debug->path.length, debug->data + debug->path.start); free_request(debug); (*request)[length] = 0; return length; } /// Progresses the readout of the request until delimiter is found. /// \param request Request to process /// \param delimiter Character that marks the end of the readout /// \return Position of the delimiter or 0 if the deliminter could not be found static int read_until_char(struct request * request, const char delimiter) { char character; while (request->position < request->length) { character = request->data[request->position]; if (delimiter == character) { return request->position; } request->position++; } return 0; } /// Progresses the readout of the request until non-whitespace character is found. /// \param request Request to process /// \return Position of the first non-whitespace character or 0 if it could not be found /// \see isalpha(3) static int read_until_word(struct request * request) { char character; while (request->position < request->length) { character = request->data[request->position]; if (!isspace(character)) { return request->position; } request->position++; } return 0; } /// Progresses the readout of the request until line separator is found. /// \param request Request to process /// \return Position of the first byte of the separator or 0 if line separator could not be found static int read_rest_of_line(struct request * request) { char buffer[2] = {0, 0}; while (request->position < request->length - 1) { memcpy(buffer, &request->data[request->position], 2); if (0 == strncmp(buffer, "\r\n", 2)) { return request->position; } if (buffer[1] == '\r') { request->position += 1; continue; } request->position += 2; } return 0; } /// Parses method field of the request. /// \param request Request to process /// \return -1 if an error has occured, 0 if too little data available or total number of bytes processed int parse_method(struct request * request) { request->method.length = read_until_char(request, ' '); if (0 == request->method.length) { return 0; } request->step = parse_path; return parse_path(request); } /// Parses path field of the request. /// \param request Request to process /// \return -1 if an error has occured, 0 if too little data available or total number of bytes processed // TODO: Consider spliting path into an actual path and arguments in this stage int parse_path(struct request * request) { if (0 >= request->path.start) { request->path.start = read_until_word(request); if (0 == request->path.start) { return 0; } } const int result = read_until_char(request, ' '); if (0 == result) { return 0; } request->path.length = request->position - request->path.start; request->step = parse_version; return parse_version(request); } /// Parses and verifies http version field of the request. /// \param request Request to process /// \return -1 if an error has occured, 0 if too little data available or total number of bytes processed // TODO: Return -1 if version is unsupported, meaning other than HTTP/1.1 int parse_version(struct request * request) { if (0 >= request->version.start) { request->version.start = read_until_word(request); if (0 == request->version.start) { return 0; } } const int result = read_rest_of_line(request); if (0 == result) { return 0; } request->version.length = request->position - request->version.start; request->step = NULL; // TODO: Parse headers return request->position; }