progress?
This commit is contained in:
parent
8d810650e5
commit
04abd8b152
15
src/file.c
15
src/file.c
@ -1,5 +1,6 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "include/file.h"
|
#include "include/file.h"
|
||||||
|
|
||||||
@ -7,8 +8,10 @@ void get_file_from_uri(char** uri, char** buffer)
|
|||||||
{
|
{
|
||||||
char* tok;
|
char* tok;
|
||||||
FILE* f;
|
FILE* f;
|
||||||
int err;
|
int err, i;
|
||||||
size_t bufsize, newlen;
|
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
|
* 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) {
|
if (f == NULL) {
|
||||||
perror("fopen");
|
perror("fopen");
|
||||||
exit(EXIT_FAILURE);
|
return; // TODO: 404
|
||||||
}
|
}
|
||||||
|
|
||||||
err = fseek(f, 0L, SEEK_END); // go to the end of the file
|
err = fseek(f, 0L, SEEK_END); // go to the end of the 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
|
// TODO: obviouslly add something to determine the status code to use
|
||||||
response->status.status_code = HTTP200;
|
response->status.status_code = HTTP200;
|
||||||
response->status.reason_phrase = http_status_code_to_str(response->status.status_code);
|
response->status.reason_phrase = http_status_code_to_str(response->status.status_code);
|
||||||
|
|
||||||
|
response->response_headers.content_type = "text/html";
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -171,6 +171,13 @@ struct response_headers {
|
|||||||
* required for 401
|
* required for 401
|
||||||
*/
|
*/
|
||||||
const char* www_authenticate;
|
const char* www_authenticate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Content-Type: ...
|
||||||
|
*
|
||||||
|
* type of the content
|
||||||
|
*/
|
||||||
|
const char* content_type;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct request_headers {
|
struct request_headers {
|
||||||
|
211
src/tcp.c
211
src/tcp.c
@ -89,6 +89,22 @@ int recv_message(char** buffer, int socket)
|
|||||||
return 0;
|
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)
|
void send_message(int socket, http_response* response)
|
||||||
{
|
{
|
||||||
int err;
|
int err;
|
||||||
@ -100,177 +116,39 @@ void send_message(int socket, http_response* response)
|
|||||||
response->status.version.minor, response->status.status_code,
|
response->status.version.minor, response->status.status_code,
|
||||||
response->status.reason_phrase);
|
response->status.reason_phrase);
|
||||||
|
|
||||||
if (strcmp(response->general_headers.cache_control, "") != 0) {
|
const char* general_headers[][2] = {
|
||||||
msg_to_send = realloc(msg_to_send, sizeof(char) *
|
{response->general_headers.cache_control, "\nCache-Control: "},
|
||||||
(strlen(msg_to_send) +
|
{response->general_headers.connection, "\nConnection: "},
|
||||||
strlen(response->general_headers.cache_control) +
|
{response->general_headers.date, "\nDate: "},
|
||||||
strlen("Cache-Control: ") + 1));
|
{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: ");
|
add_header_to_string(&msg_to_send, general_headers, 9);
|
||||||
strcat(msg_to_send, response->general_headers.cache_control);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strcmp(response->general_headers.connection, "") != 0) {
|
const char* response_headers[][2] = {
|
||||||
msg_to_send = realloc(msg_to_send, sizeof(char) *
|
{response->response_headers.accept_ranges, "\nAccept-Ranges: "},
|
||||||
(strlen(msg_to_send) +
|
{response->response_headers.age, "\nAge: "},
|
||||||
strlen(response->general_headers.connection) +
|
{response->response_headers.etag, "\nETag: "},
|
||||||
strlen("Connection: ") + 1));
|
{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: ");
|
add_header_to_string(&msg_to_send, response_headers, 9);
|
||||||
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));
|
|
||||||
|
|
||||||
strcat(msg_to_send, "Pragma: ");
|
asprintf(&msg_to_send, "%s\nContent-Length: %d\n\n", msg_to_send, response->response_headers.content_length);
|
||||||
strcat(msg_to_send, response->general_headers.pragma);
|
asprintf(&msg_to_send, "%s%s\n", msg_to_send, response->message_body);
|
||||||
}
|
|
||||||
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));
|
|
||||||
|
|
||||||
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);
|
msg_len = strlen(msg_to_send);
|
||||||
err = send(socket, msg_to_send, msg_len, 0);
|
err = send(socket, msg_to_send, msg_len, 0);
|
||||||
@ -319,6 +197,9 @@ void* connection_handler(void* arg)
|
|||||||
|
|
||||||
// send payload
|
// send payload
|
||||||
|
|
||||||
|
send_message(client_socket, &response);
|
||||||
|
free_http_response(&response);
|
||||||
|
|
||||||
|
|
||||||
free(buffer);
|
free(buffer);
|
||||||
close(client_socket);
|
close(client_socket);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user