winnie-http/src/server.c

120 lines
3.7 KiB
C

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#include <time.h>
#include "server.h"
int send_response(int fd, const char *header, char *content_type, void *body, int content_length) {
const int max_response_size = MAX_RESPONSE_SIZE;
char response[max_response_size];
time_t current = time(NULL);
char date_time[30];
strftime(date_time, sizeof(date_time), "%a, %d %b %Y %H:%M:%S %Z", localtime(&current));
int header_length = sprintf(response,
"%s\n"
"Server: %s\n"
"Date: %s\n"
"Connection: close\n"
"Content-Length: %d\n"
"Content-Type: %s\n"
"\n",
header, SERVER_VERSION, date_time, content_length, content_type);
memcpy(response + header_length, body, content_length);
int write_res = write(fd, response, header_length + content_length);
if (write_res < 0) {
perror("webserver (write)");
return write_res;
}
return write_res;
}
int main() {
char buffer[BUFFER_SIZE];
// create a socket
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
perror("webserver (socket)");
return 1;
}
printf("[Winnie-HTTP]: Socket created successfully\n");
// create the address to bind the socket to
struct sockaddr_in host_addr;
int host_addrlen = sizeof(host_addr);
host_addr.sin_family = AF_INET;
host_addr.sin_port = htons(PORT);
host_addr.sin_addr.s_addr = htonl(INADDR_ANY);
// create client address
struct sockaddr_in client_addr;
int client_addrlen = sizeof(client_addr);
// Bind the socket to the address
if (bind(sockfd, (struct sockaddr *) &host_addr, host_addrlen) != 0) {
perror("webserver (bind)");
return 1;
}
printf("[Winnie-HTTP]: Socket successfully bound to address\n");
// listen to incoming connections
if (listen(sockfd, SOMAXCONN) != 0) {
perror("webserver (listen)");
return 1;
}
printf("[Winnie-HTTP]: server listening for connections\n");
for (;;) {
// accept incoming connections
int client_sock_fd = accept(sockfd, (struct sockaddr *) &host_addr, (socklen_t *) &host_addrlen);
if (client_sock_fd < 0) {
perror("webserver (accept)");
continue;
}
printf("[Winnie-HTTP]: connection accepted\n");
// get client address
int sockn = getsockname(client_sock_fd, (struct sockaddr *) &client_addr, (socklen_t *) &client_addrlen);
if(sockn < 0) {
perror("webserver (getsockname)");
continue;
}
// read from the socket
int valread = read(client_sock_fd, buffer, BUFFER_SIZE);
if (valread < 0) {
perror("webserver (read)");
continue;
}
// read the request
char method[BUFFER_SIZE], uri[BUFFER_SIZE], version[BUFFER_SIZE];
sscanf(buffer, "%s %s %s", method, uri, version);
printf("[%s:%u] %s %s %s\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), method, version, uri);
if (strncmp("GET", method, 7) != 0) {
send_response(client_sock_fd, BAD_REQUEST_HEAD, "text/html", NULL, 0);
} else {
char body[100];
int body_size = snprintf(body, 100, "%s\r\n", "hello what up bruh");
send_response(client_sock_fd, OK_HEAD, "text/html", body, (body_size + 1));
}
close(client_sock_fd);
}
return 0;
}