adds a bunch of shit
This commit is contained in:
parent
71a197c9cb
commit
8d810650e5
18
src/http.c
18
src/http.c
@ -160,8 +160,10 @@ void init_http_response(http_response* response)
|
||||
{
|
||||
init_general_headers(&(response->general_headers));
|
||||
|
||||
response->response_headers.content_length = 0;
|
||||
|
||||
response->response_headers.accept_ranges = "";
|
||||
response->response_headers.age = 0;
|
||||
response->response_headers.age = "";
|
||||
response->response_headers.etag = "";
|
||||
response->response_headers.location = "";
|
||||
response->response_headers.proxy_authenticate = "";
|
||||
@ -277,5 +279,17 @@ void create_http_response(http_request* request, http_response* response)
|
||||
{
|
||||
get_file_from_uri(&(request->request_line.request_uri), &(response->message_body));
|
||||
|
||||
response->message_len = strlen(response->message_body);
|
||||
init_http_response(response);
|
||||
|
||||
response->response_headers.content_length = strlen(response->message_body);
|
||||
|
||||
// HTTP/1.1
|
||||
response->status.version.major = 1;
|
||||
response->status.version.minor = 1;
|
||||
|
||||
// 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);
|
||||
|
||||
|
||||
}
|
||||
|
@ -93,6 +93,13 @@ struct general_headers {
|
||||
};
|
||||
|
||||
struct response_headers {
|
||||
/**
|
||||
* Content-Length: ...
|
||||
*
|
||||
* length of the content
|
||||
*/
|
||||
uint content_length;
|
||||
|
||||
/**
|
||||
* Accept-Ranges: ...
|
||||
*
|
||||
@ -110,7 +117,7 @@ struct response_headers {
|
||||
* time since the response was generated, this is for cached responses,
|
||||
* if we are cacheing, this header is REQUIRED
|
||||
*/
|
||||
uint age;
|
||||
const char* age;
|
||||
|
||||
/**
|
||||
* ETag: ...
|
||||
|
@ -58,7 +58,6 @@ typedef struct {
|
||||
http_status_line status;
|
||||
struct general_headers general_headers;
|
||||
struct response_headers response_headers;
|
||||
size_t message_len;
|
||||
char* message_body;
|
||||
} http_response;
|
||||
|
||||
|
194
src/tcp.c
194
src/tcp.c
@ -5,6 +5,7 @@
|
||||
#include <netinet/in.h>
|
||||
#include <poll.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "include/tcp.h"
|
||||
#include "include/http.h"
|
||||
@ -88,6 +89,198 @@ int recv_message(char** buffer, int socket)
|
||||
return 0;
|
||||
}
|
||||
|
||||
void send_message(int socket, http_response* response)
|
||||
{
|
||||
int err;
|
||||
size_t msg_len;
|
||||
|
||||
char* msg_to_send = NULL;
|
||||
|
||||
asprintf(&msg_to_send, "HTTP/%d.%d %d %s", response->status.version.major,
|
||||
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));
|
||||
|
||||
strcat(msg_to_send, "Cache-Control: ");
|
||||
strcat(msg_to_send, response->general_headers.cache_control);
|
||||
}
|
||||
|
||||
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));
|
||||
|
||||
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));
|
||||
|
||||
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));
|
||||
|
||||
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);
|
||||
if (err < 0)
|
||||
perror("send");
|
||||
|
||||
free(msg_to_send);
|
||||
|
||||
}
|
||||
|
||||
void* connection_handler(void* arg)
|
||||
{
|
||||
int client_socket = *(int*) arg;
|
||||
@ -125,6 +318,7 @@ void* connection_handler(void* arg)
|
||||
|
||||
|
||||
// send payload
|
||||
|
||||
|
||||
free(buffer);
|
||||
close(client_socket);
|
||||
|
Loading…
x
Reference in New Issue
Block a user