2024-05-14 18:34:20 +07:00
|
|
|
#include <stdio.h>
|
2024-05-15 14:52:38 +07:00
|
|
|
#include <string.h>
|
2024-05-15 20:47:03 +07:00
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
2024-05-14 18:34:20 +07:00
|
|
|
#include <sys/socket.h>
|
2024-05-15 14:52:38 +07:00
|
|
|
#include <unistd.h>
|
2024-05-14 18:34:20 +07:00
|
|
|
|
2024-05-16 15:54:17 +07:00
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "server.h"
|
|
|
|
|
|
|
|
int send_response(int fd, char *header, char *content_type, void *body, int content_length) {
|
|
|
|
const int max_response_size = 65536;
|
|
|
|
char response[max_response_size];
|
|
|
|
|
2024-05-23 19:31:49 +07:00
|
|
|
time_t current = time(NULL);
|
|
|
|
char date_time[30];
|
|
|
|
strftime(date_time, sizeof(date_time), "%a, %d %b %Y %H:%M:%S %Z", localtime(¤t));
|
|
|
|
|
|
|
|
int header_length = sprintf(response,
|
2024-05-16 15:54:17 +07:00
|
|
|
"%s\n"
|
2024-05-22 20:58:28 +07:00
|
|
|
"Server: %s\n"
|
|
|
|
"Date: %s\n"
|
2024-05-16 15:54:17 +07:00
|
|
|
"Connection: close\n"
|
|
|
|
"Content-Length: %d\n"
|
|
|
|
"Content-Type: %s\n"
|
|
|
|
"\n",
|
2024-05-23 19:31:49 +07:00
|
|
|
header, SERVER_VERSION, date_time, content_length, content_type);
|
2024-05-16 15:54:17 +07:00
|
|
|
|
2024-05-23 19:31:49 +07:00
|
|
|
printf("%s\n", (char *)body);
|
2024-05-16 15:54:17 +07:00
|
|
|
|
2024-05-23 19:31:49 +07:00
|
|
|
//printf("%p\n", (char *)response);
|
|
|
|
//printf("%p\n", &header_length);
|
|
|
|
//printf("%p\n", (void *)(response + header_length));
|
|
|
|
memcpy(response + header_length, body, content_length);
|
|
|
|
printf("%s\n", response);
|
2024-05-22 20:58:28 +07:00
|
|
|
|
2024-05-23 19:31:49 +07:00
|
|
|
int write_res = write(fd, response, header_length + content_length);
|
|
|
|
|
|
|
|
if (write_res < 0) {
|
|
|
|
perror("webserver (write)");
|
|
|
|
return write_res;
|
|
|
|
}
|
2024-05-22 20:58:28 +07:00
|
|
|
|
2024-05-23 19:31:49 +07:00
|
|
|
return write_res;
|
2024-05-22 20:58:28 +07:00
|
|
|
}
|
2024-05-17 23:12:56 +07:00
|
|
|
|
2024-05-14 18:34:20 +07:00
|
|
|
int main() {
|
2024-05-15 14:52:38 +07:00
|
|
|
char buffer[BUFFER_SIZE];
|
2024-05-17 23:12:56 +07:00
|
|
|
// char resp[] = "HTTP/1.0 200 OK\r\n"
|
|
|
|
// "Server: webserver-c\r\n"
|
|
|
|
// "Content-type: text/html\r\n\r\n"
|
|
|
|
// "<html>hello, world</html>\r\n";
|
2024-05-15 14:52:38 +07:00
|
|
|
|
|
|
|
// create a socket
|
2024-05-14 18:34:20 +07:00
|
|
|
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
2024-05-15 14:52:38 +07:00
|
|
|
if (sockfd == -1) {
|
2024-05-15 15:59:13 +07:00
|
|
|
perror("webserver (socket)");
|
2024-05-14 18:34:20 +07:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
printf("socket created successfully\n");
|
|
|
|
|
2024-05-15 14:52:38 +07:00
|
|
|
// create the address to bind the socket to
|
2024-05-14 20:44:39 +07:00
|
|
|
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);
|
|
|
|
|
2024-05-15 15:59:13 +07:00
|
|
|
// create client address
|
|
|
|
struct sockaddr_in client_addr;
|
|
|
|
int client_addrlen = sizeof(client_addr);
|
|
|
|
|
2024-05-15 14:52:38 +07:00
|
|
|
// Bind the socket to the address
|
2024-05-15 15:59:13 +07:00
|
|
|
if (bind(sockfd, (struct sockaddr *) &host_addr, host_addrlen) != 0) {
|
2024-05-14 20:44:39 +07:00
|
|
|
perror("webserver (bind)");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
printf("socket successfully bound to address\n");
|
2024-05-15 14:52:38 +07:00
|
|
|
|
|
|
|
// listen to incoming connections
|
|
|
|
if (listen(sockfd, SOMAXCONN) != 0) {
|
|
|
|
perror("webserver (listen)");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
printf("server listening for connections\n");
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
// accept incoming connections
|
2024-05-15 15:59:13 +07:00
|
|
|
int newsockfd = accept(sockfd, (struct sockaddr *) &host_addr, (socklen_t *) &host_addrlen);
|
2024-05-15 14:52:38 +07:00
|
|
|
if (newsockfd < 0) {
|
|
|
|
perror("webserver (accept)");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
printf("connection accepted\n");
|
|
|
|
|
2024-05-15 15:59:13 +07:00
|
|
|
// get client address
|
|
|
|
int sockn = getsockname(newsockfd, (struct sockaddr *) &client_addr, (socklen_t *) &client_addrlen);
|
|
|
|
if(sockn < 0) {
|
|
|
|
perror("webserver (getsockname)");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-05-15 14:52:38 +07:00
|
|
|
// read from the socket
|
|
|
|
int valread = read(newsockfd, buffer, BUFFER_SIZE);
|
|
|
|
if (valread < 0) {
|
|
|
|
perror("webserver (read)");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-05-15 15:59:13 +07:00
|
|
|
// 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);
|
|
|
|
|
2024-05-15 20:47:03 +07:00
|
|
|
// check if request is GET request
|
|
|
|
if (strncmp("GET", method, 7) != 0) {
|
2024-05-17 23:12:56 +07:00
|
|
|
// const char resp[] = "HTTP/1.0 400 Bad Request\r\n"
|
|
|
|
// "Server: webserver-c\r\n"
|
|
|
|
// "Content-Type: text/html\r\n\r\n";
|
|
|
|
|
|
|
|
char resp[100];
|
|
|
|
int resp_size = snprintf(resp, 100,
|
|
|
|
"%s\r\n"
|
|
|
|
"Server: %s\r\n"
|
|
|
|
"Content-Type: %s\r\n"
|
|
|
|
"\r\n",
|
|
|
|
BAD_REQUEST_HEAD, SERVER_VERSION, "text/html");
|
|
|
|
|
|
|
|
int valwrite = write(newsockfd, resp, resp_size);
|
|
|
|
if (valwrite < 0) {
|
|
|
|
perror("webserver (write)");
|
|
|
|
continue;
|
|
|
|
}
|
2024-05-15 20:47:03 +07:00
|
|
|
} else {
|
2024-05-22 20:58:28 +07:00
|
|
|
// char resp[100];
|
|
|
|
// int resp_size = snprintf(resp, 100,
|
|
|
|
// "%s\r\n"
|
|
|
|
// "Server: %s\r\n"
|
|
|
|
// "Content-Type: %s\r\n"
|
|
|
|
// "\r\n",
|
|
|
|
// OK_HEAD, SERVER_VERSION, "text/html");
|
|
|
|
//
|
|
|
|
// int valwrite = write(newsockfd, resp, resp_size);
|
|
|
|
// printf("%s\n", OK_HEAD);
|
|
|
|
// if (valwrite < 0) {
|
|
|
|
// perror("webserver (write)");
|
|
|
|
// continue;
|
|
|
|
// }
|
|
|
|
|
|
|
|
char body[100];
|
2024-05-23 19:31:49 +07:00
|
|
|
int body_size = snprintf(body, 100, "%s\r\n", "hello what up bruh");
|
|
|
|
printf("%d\n", body_size);
|
|
|
|
send_response(newsockfd, "HTTP/1.1 200 OK", "text/html", body, (body_size+1));
|
2024-05-15 20:47:03 +07:00
|
|
|
}
|
|
|
|
|
2024-05-15 14:52:38 +07:00
|
|
|
// write to the socket
|
2024-05-17 23:12:56 +07:00
|
|
|
// int valwrite = write(newsockfd, resp, strlen(resp));
|
|
|
|
// if (valwrite < 0) {
|
|
|
|
// perror("webserver (write)");
|
|
|
|
// continue;
|
|
|
|
// }
|
2024-05-15 14:52:38 +07:00
|
|
|
|
|
|
|
close(newsockfd);
|
|
|
|
}
|
2024-05-14 20:44:39 +07:00
|
|
|
|
2024-05-14 18:34:20 +07:00
|
|
|
return 0;
|
|
|
|
}
|