add response (with html + css)

This commit is contained in:
2024-11-27 11:33:14 +01:00
parent dc4ef7c7d5
commit 3a6c8ad9a3
11 changed files with 91 additions and 48 deletions

View File

@@ -1,5 +1,10 @@
#include "http/http.h"
#include <stdio.h> /* Debug */
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include "utils/colors.h"
http_t *http_parse(char *request_str) {
@@ -16,6 +21,15 @@ http_t *http_parse(char *request_str) {
printf("[http] location: %s\n", pch);
strncpy(request->location, pch, LOCATION_LEN);
/* Parse location path */
/* TODO: fix warnings */
if (strcmp(request->location, "/") == 0) {
snprintf(request->location_path, LOCATION_LEN, "%s/index.html", WWW);
} else {
snprintf(request->location_path, LOCATION_LEN, "%s%s", WWW, request->location);
}
fprintf(stdout, "[http] location path: %s\n", request->location_path);
/* Parse HTTP version */
pch = strtok(NULL, " \r\n");
printf("[http] version: %s\n", pch);
@@ -39,6 +53,58 @@ void http_parse_method(http_t *request, const char *method) {
}
}
void http_send_response(http_t *request) { /* TODO */ }
void http_send_response(http_t *request, int sockfd) {
FILE *file = fopen(request->location_path, "r");
if (file == NULL) {
/* 404 */
/* TODO: improve error handling */
char response[1024] =
"HTTP/1.1 404 Not Found\r\n"
"Content-Type: text/html\r\n"
"Content-Length: 216\r\n"
"\r\n"
"<html>\n"
"<head>\n"
" <title>Resource Not Found</title>\n"
"</head>\n"
"<body>\n"
"<p>Resource not found.</p>\n"
"</body>\n"
"</html>";
send(sockfd, response, 1024, 0);
return;
}
char content_type[1024];
http_get_content_type(request, content_type);
/* Don't care about numbers, they are random */
char line[1024] = {0};
char html_code[32000] = {0};
char response[65535] = {0};
while (fgets(line, 1024, file)) {
strncat(html_code, line, 32000);
}
fclose(file);
const size_t content_length = strlen(html_code);
snprintf(response, sizeof response,
"%s 200 OK\r\n"
"Content-Type: %s\r\n"
"Content-Length: %zu\r\n"
"Connection: close\r\n"
"\r\n"
"%s",
request->http_version, content_type, content_length, html_code);
send(sockfd, response, strlen(response), 0);
}
void http_get_content_type(http_t *request, char *content_type) {
char *ptr = strrchr(request->location_path, '.');
/* TODO: improve content_type (used to test) */
snprintf(content_type, 1024, "text/%s", ptr + 1);
}
void http_free(http_t *request) { free(request); }