From: Laurent Mazet Date: Mon, 22 May 2023 13:39:02 +0000 (+0200) Subject: corrections X-Git-Url: https://secure.softndesign.org/git/?a=commitdiff_plain;h=8a1d9e4a8a87e85bbc4521b41a23235bdc8fa230;p=webserver.git corrections --- diff --git a/http.c b/http.c index 614d934..20301d6 100644 --- a/http.c +++ b/http.c @@ -1,14 +1,17 @@ #include #include +#include #include "debug.h" #include "http.h" +#define BUFFER_SIZE 128 + #define HTTP_VERSION "HTTP/1.0" #define SERVER_NAME "Webserver/0.0.1" -char **codes = { +char *codes[15] = { "200 OK", "201 Created", "202 Accepted", @@ -100,23 +103,90 @@ char *find_sequence (char *data, int len, char *seq, char **pdata) /* response entity */ -int add_status_line (char **buffer, int len) +int add_line (char **buffer, char *str) { + int len = strlen (*buffer) + strlen (str) + 1; + *buffer = realloc (*buffer, len); + strcat (*buffer, str); return len; } -int add_general_header (char **buffer, int len) +int add_status_line (char **buffer, code_t code) { - return len; + char tmp[BUFFER_SIZE] = {0}; + + /* Status */ + sprintf (tmp, "%s %s\r\n", HTTP_VERSION, codes[code]); + add_line (buffer, tmp); + + return strlen (*buffer); } -int add_response_header (char **buffer, int len) +int add_general_header (char **buffer) { - return len; + char tmp[BUFFER_SIZE] = {0}; + + /* Date */ + time_t ts = time (NULL); + sprintf (tmp, "Date: %s\r\n", ctime (&ts)); + add_line (buffer, tmp); + + /* Pragma */ + + return strlen (*buffer); +} + +int add_response_header (char **buffer, char *uri) +{ + char tmp[BUFFER_SIZE] = {0}; + + /* Location */ + sprintf (tmp, "Location: %s\r\n", uri); + add_line (buffer, tmp); + + /* Server */ + sprintf (tmp, "Server: %s\r\n", SERVER_NAME); + add_line (buffer, tmp); + + /* WWW-Authentificate */ + + return strlen (*buffer); } -int add_entity (char **buffer, int len, unsigned char *entity, int size, char *type) +int add_entity (char **buffer, unsigned char *entity, int size, char *type, char *encoding) { + char tmp[BUFFER_SIZE] = {0}; + int len = strlen (*buffer); + + /* Allow */ + /* Expires */ + /* Last-Modified */ + + if (entity != NULL) { + + /* Content-Encoding */ + if (encoding != NULL) { + sprintf (tmp, "Content-Encoding: %s\r\n", encoding); + add_line (buffer, tmp); + } + + /* Content-Length */ + sprintf (tmp, "Content-Length: %d\r\n", size); + add_line (buffer, tmp); + + /* Content-Type */ + sprintf (tmp, "Content-Type: %s\r\n", type); + add_line (buffer, tmp); + + add_line (buffer, "\r\n"); + + /* Entity */ + len = strlen (*buffer); + *buffer = realloc (*buffer, len + size); + memcpy (*buffer + len, entity, size); + len += size; + } + return len; } @@ -124,7 +194,23 @@ int add_entity (char **buffer, int len, unsigned char *entity, int size, char *t int error_400 (char **buffer) { - + add_status_line (buffer, c400); + add_general_header (buffer); + add_response_header (buffer, NULL); + + char *response = "Error 400

Bad Request

"; + return add_entity (buffer, (unsigned char *)response, strlen (response), "text/html", "iso-8859-1"); +} + +/* response html */ + +int response_html (char **buffer, char *location, char *response) +{ + add_status_line (buffer, c200); + add_general_header (buffer); + add_response_header (buffer, location); + + return add_entity (buffer, (unsigned char *)response, strlen (response), "text/html", "iso-8859-1"); } /* trim string */ @@ -169,7 +255,6 @@ int processing (char *data, int len, char **pdata) type = post_e; } else { VERBOSE (WARNING, PRINT ("Unkown method: %s\n", method)); - } else { free (saved_data); return error_400 (pdata); } @@ -234,8 +319,19 @@ int processing (char *data, int len, char **pdata) free (saved_data); /* response */ - *pdata = strdup ("HTTP/1.0 200 OK\r\nDate: Sat, 20 May 2023 16:37:46 GMT\r\nServer: Webserver/0.0.1 (Debian)\r\nLocation: http://localhost/\r\nContent-Length: 77\r\nContent-Type: text/html; charset=iso-8859-1\r\n\r\nTest

This a test

"); - return strlen (*pdata); + switch (type) { + case get_e: + len = response_html (pdata, "http://localhost/", "Test

This a test

"); + break; + case head_e: + break; + case post_e: + break; + case not_supported_e: + break; + } + + return len; } /* vim: set ts=4 sw=4 et: */