progress?

This commit is contained in:
SuperNovaa41 2025-02-07 13:54:59 -05:00
parent 8d810650e5
commit 04abd8b152
4 changed files with 67 additions and 168 deletions

View File

@ -1,5 +1,6 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "include/file.h"
@ -7,8 +8,10 @@ void get_file_from_uri(char** uri, char** buffer)
{
char* tok;
FILE* f;
int err;
int err, i;
size_t bufsize, newlen;
char* filepath;
@ -20,10 +23,16 @@ void get_file_from_uri(char** uri, char** buffer)
/**
* TODO: if no file is specified, append index.html
*/
f = fopen(*uri, "r");
asprintf(&filepath, "./%s", *uri);
if (filepath[strlen(filepath) - 1] == '/')
asprintf(&filepath, "%sindex.html", filepath);
f = fopen(filepath, "r");
if (f == NULL) {
perror("fopen");
exit(EXIT_FAILURE);
return; // TODO: 404
}
err = fseek(f, 0L, SEEK_END); // go to the end of the file

View File

@ -290,6 +290,8 @@ void create_http_response(http_request* request, http_response* response)
// TODO: obviouslly add something to determine the status code to use
response->status.status_code = HTTP200;
response->status.reason_phrase = http_status_code_to_str(response->status.status_code);
response->response_headers.content_type = "text/html";
}

View File

@ -171,6 +171,13 @@ struct response_headers {
* required for 401
*/
const char* www_authenticate;
/**
* Content-Type: ...
*
* type of the content
*/
const char* content_type;
};
struct request_headers {

211
src/tcp.c
View File

@ -89,6 +89,22 @@ int recv_message(char** buffer, int socket)
return 0;
}
void add_header_to_string(char** msg_to_send, const char* headers[][2], size_t len)
{
int i;
for (i = 0; i < len; i++) {
if (strcmp(headers[i][0], "") == 0) // if its empty we don't want this header
continue;
(*msg_to_send) = realloc(*msg_to_send, sizeof(char) *
(strlen(*msg_to_send) +
strlen(headers[i][0]) +
strlen(headers[i][1])
));
strcat(*msg_to_send, headers[i][1]);
strcat(*msg_to_send, headers[i][0]);
}
}
void send_message(int socket, http_response* response)
{
int err;
@ -100,177 +116,39 @@ void send_message(int socket, http_response* response)
response->status.version.minor, response->status.status_code,
response->status.reason_phrase);
if (strcmp(response->general_headers.cache_control, "") != 0) {
msg_to_send = realloc(msg_to_send, sizeof(char) *
(strlen(msg_to_send) +
strlen(response->general_headers.cache_control) +
strlen("Cache-Control: ") + 1));
const char* general_headers[][2] = {
{response->general_headers.cache_control, "\nCache-Control: "},
{response->general_headers.connection, "\nConnection: "},
{response->general_headers.date, "\nDate: "},
{response->general_headers.pragma, "\nPragma: "},
{response->general_headers.trailer, "\nTrailer: "},
{response->general_headers.transfer_encoding, "\nTransfer-Encoding: "},
{response->general_headers.upgrade, "\nUpgrade: "},
{response->general_headers.via, "\nVia: "},
{response->general_headers.warning, "\nWarning: "},
};
strcat(msg_to_send, "Cache-Control: ");
strcat(msg_to_send, response->general_headers.cache_control);
}
add_header_to_string(&msg_to_send, general_headers, 9);
if (strcmp(response->general_headers.connection, "") != 0) {
msg_to_send = realloc(msg_to_send, sizeof(char) *
(strlen(msg_to_send) +
strlen(response->general_headers.connection) +
strlen("Connection: ") + 1));
const char* response_headers[][2] = {
{response->response_headers.accept_ranges, "\nAccept-Ranges: "},
{response->response_headers.age, "\nAge: "},
{response->response_headers.etag, "\nETag: "},
{response->response_headers.location, "\nLocation: "},
{response->response_headers.proxy_authenticate, "\nProxy-Authenticate: "},
{response->response_headers.retry_after, "\nRetry-After: "},
{response->response_headers.server, "\nServer: "},
{response->response_headers.vary, "\nVary: "},
{response->response_headers.www_authenticate, "\nWWW-Authenticate: "},
{response->response_headers.content_type, "\nContent-Type: "},
strcat(msg_to_send, "Connection: ");
strcat(msg_to_send, response->general_headers.connection);
}
if (strcmp(response->general_headers.date, "") != 0) {
msg_to_send = realloc(msg_to_send, sizeof(char) *
(strlen(msg_to_send) +
strlen(response->general_headers.date) +
strlen("Date: ") + 1));
};
strcat(msg_to_send, "Date: ");
strcat(msg_to_send, response->general_headers.date);
}
if (strcmp(response->general_headers.pragma, "") != 0) {
msg_to_send = realloc(msg_to_send, sizeof(char) *
(strlen(msg_to_send) +
strlen(response->general_headers.pragma) +
strlen("Pragma: ") + 1));
add_header_to_string(&msg_to_send, response_headers, 9);
strcat(msg_to_send, "Pragma: ");
strcat(msg_to_send, response->general_headers.pragma);
}
if (strcmp(response->general_headers.trailer, "") != 0) {
msg_to_send = realloc(msg_to_send, sizeof(char) *
(strlen(msg_to_send) +
strlen(response->general_headers.trailer) +
strlen("Trailer: ") + 1));
asprintf(&msg_to_send, "%s\nContent-Length: %d\n\n", msg_to_send, response->response_headers.content_length);
asprintf(&msg_to_send, "%s%s\n", msg_to_send, response->message_body);
strcat(msg_to_send, "Trailer: ");
strcat(msg_to_send, response->general_headers.trailer);
}
if (strcmp(response->general_headers.transfer_encoding, "") != 0) {
msg_to_send = realloc(msg_to_send, sizeof(char) *
(strlen(msg_to_send) +
strlen(response->general_headers.transfer_encoding) +
strlen("Transfer-Encoding: ") + 1));
strcat(msg_to_send, "Transfer-Encoding: ");
strcat(msg_to_send, response->general_headers.transfer_encoding);
}
if (strcmp(response->general_headers.upgrade, "") != 0) {
msg_to_send = realloc(msg_to_send, sizeof(char) *
(strlen(msg_to_send) +
strlen(response->general_headers.upgrade) +
strlen("Upgrade: ") + 1));
strcat(msg_to_send, "Upgrade: ");
strcat(msg_to_send, response->general_headers.upgrade);
}
if (strcmp(response->general_headers.via, "") != 0) {
msg_to_send = realloc(msg_to_send, sizeof(char) *
(strlen(msg_to_send) +
strlen(response->general_headers.via) +
strlen("Via: ") + 1));
strcat(msg_to_send, "Via: ");
strcat(msg_to_send, response->general_headers.via);
}
if (strcmp(response->general_headers.warning, "") != 0) {
msg_to_send = realloc(msg_to_send, sizeof(char) *
(strlen(msg_to_send) +
strlen(response->general_headers.warning) +
strlen("Warning: ") + 1));
strcat(msg_to_send, "Warning: ");
strcat(msg_to_send, response->general_headers.warning);
}
if (strcmp(response->response_headers.accept_ranges, "") != 0) {
msg_to_send = realloc(msg_to_send, sizeof(char) *
(strlen(msg_to_send) +
strlen(response->response_headers.accept_ranges) +
strlen("Accept-Ranges: ") + 1));
strcat(msg_to_send, "Accept-Ranges: ");
strcat(msg_to_send, response->response_headers.accept_ranges);
}
if (strcmp(response->response_headers.age, "") != 0) {
msg_to_send = realloc(msg_to_send, sizeof(char) *
(strlen(msg_to_send) +
strlen(response->response_headers.age) +
strlen("Age: ") + 1));
strcat(msg_to_send, "Age: ");
strcat(msg_to_send, response->response_headers.age);
}
if (strcmp(response->response_headers.etag, "") != 0) {
msg_to_send = realloc(msg_to_send, sizeof(char) *
(strlen(msg_to_send) +
strlen(response->response_headers.etag) +
strlen("ETag: ") + 1));
strcat(msg_to_send, "ETag: ");
strcat(msg_to_send, response->response_headers.etag);
}
if (strcmp(response->response_headers.location, "") != 0) {
msg_to_send = realloc(msg_to_send, sizeof(char) *
(strlen(msg_to_send) +
strlen(response->response_headers.location) +
strlen("Location: ") + 1));
strcat(msg_to_send, "Location: ");
strcat(msg_to_send, response->response_headers.location);
}
if (strcmp(response->response_headers.proxy_authenticate, "") != 0) {
msg_to_send = realloc(msg_to_send, sizeof(char) *
(strlen(msg_to_send) +
strlen(response->response_headers.proxy_authenticate) +
strlen("Proxy-Authenticate: ") + 1));
strcat(msg_to_send, "Proxy-Authenticate: ");
strcat(msg_to_send, response->response_headers.proxy_authenticate);
}
if (strcmp(response->response_headers.retry_after, "") != 0) {
msg_to_send = realloc(msg_to_send, sizeof(char) *
(strlen(msg_to_send) +
strlen(response->response_headers.retry_after) +
strlen("Retry-After: ") + 1));
strcat(msg_to_send, "Retry-After: ");
strcat(msg_to_send, response->response_headers.retry_after);
}
if (strcmp(response->response_headers.server, "") != 0) {
msg_to_send = realloc(msg_to_send, sizeof(char) *
(strlen(msg_to_send) +
strlen(response->response_headers.server) +
strlen("Server: ") + 1));
strcat(msg_to_send, "Server: ");
strcat(msg_to_send, response->response_headers.server);
}
if (strcmp(response->response_headers.vary, "") != 0) {
msg_to_send = realloc(msg_to_send, sizeof(char) *
(strlen(msg_to_send) +
strlen(response->response_headers.vary) +
strlen("Vary: ") + 1));
strcat(msg_to_send, "Vary: ");
strcat(msg_to_send, response->response_headers.vary);
}
if (strcmp(response->response_headers.www_authenticate, "") != 0) {
msg_to_send = realloc(msg_to_send, sizeof(char) *
(strlen(msg_to_send) +
strlen(response->response_headers.www_authenticate) +
strlen("WWW-Authenticate: ") + 1));
strcat(msg_to_send, "WWW-Authenticate: ");
strcat(msg_to_send, response->response_headers.www_authenticate);
}
/* add the content-length thing here
msg_to_send = realloc(msg_to_send, sizeof(char) *
(strlen(msg_to_send) +
strlen(response->response_headers.www_authenticate) +
strlen("WWW-Authenticate: ") + 1));
*/
msg_len = strlen(msg_to_send);
err = send(socket, msg_to_send, msg_len, 0);
@ -319,6 +197,9 @@ void* connection_handler(void* arg)
// send payload
send_message(client_socket, &response);
free_http_response(&response);
free(buffer);
close(client_socket);