#include <malloc.h>
#include <string.h>
+#include <time.h>
#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",
/* 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;
}
int error_400 (char **buffer)
{
-
+ add_status_line (buffer, c400);
+ add_general_header (buffer);
+ add_response_header (buffer, NULL);
+
+ char *response = "<html><head><title>Error 400</title></head><body><p>Bad Request</p></body></html>";
+ 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 */
type = post_e;
} else {
VERBOSE (WARNING, PRINT ("Unkown method: %s\n", method));
- } else {
free (saved_data);
return error_400 (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\n<html><head><title>Test</title></head><body><p>This a test</p></body></html>");
- return strlen (*pdata);
+ switch (type) {
+ case get_e:
+ len = response_html (pdata, "http://localhost/", "<html><head><title>Test</title></head><body><p>This a test</p></body></html>");
+ break;
+ case head_e:
+ break;
+ case post_e:
+ break;
+ case not_supported_e:
+ break;
+ }
+
+ return len;
}
/* vim: set ts=4 sw=4 et: */