summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornot-a-robot06 <72096472+not-a-robot06@users.noreply.github.com>2024-04-07 08:41:45 +0100
committernot-a-robot06 <72096472+not-a-robot06@users.noreply.github.com>2024-04-07 08:41:45 +0100
commitf52e10b10b9d39dabf6e057cd4ab6e980d97240b (patch)
tree5187a867c42984b239528011f80a74e94768eb50
downloadhttp-server-main.tar.gz
http-server-main.tar.bz2
http-server-main.zip
initial commitHEADmain
-rw-r--r--.gitignore1
-rw-r--r--Makefile29
-rw-r--r--http.c109
-rw-r--r--resources/hello.html3
-rw-r--r--resources/hello2.html3
5 files changed, 145 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..e660fd9
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+bin/
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..e5d5a72
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,29 @@
+ifneq (, $(shell which clang))
+ CC:=clang
+else ifneq (, $(shell which gcc))
+ CC:=gcc
+else ifneq (, $(shell which cc))
+ CC:=cc
+else
+ $(error "No C compiler found (!)")
+endif
+
+CFLAGS:=-Wall -Wextra -Wpedantic -std=c99 -D_POSIX_C_SOURCE=200112L -g3
+LDFLAGS:=
+DIRS:=bin
+BINS:=bin/http
+
+all: dirs $(BINS)
+
+run: dirs $(BINS)
+ bin/http
+
+bin/%: %.c
+ $(CC) $(CFLAGS) $(LDFLAGS) $< -o $@
+
+dirs: $(DIRS)
+$(DIRS):
+ @mkdir -p $(DIRS)
+
+clean:
+ rm bin/*
diff --git a/http.c b/http.c
new file mode 100644
index 0000000..f30fe53
--- /dev/null
+++ b/http.c
@@ -0,0 +1,109 @@
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netdb.h>
+#include <string.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <strings.h>
+
+#define BUF_LEN 512
+#define PATH_LEN 256
+#define PORT "3456"
+#define BACKLOG 10
+
+int
+main(void){
+ struct sockaddr_storage client_addr;
+ socklen_t addr_size = sizeof client_addr;
+ struct addrinfo hints, *serv_info;
+ int sockfd, acpt_fd, yes = 1, err, recv_len, file_len;
+ char buf[BUF_LEN], method[16], path[PATH_LEN], *file_buf;
+ FILE *file;
+
+ memset(&hints, 0, sizeof hints);
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_flags = AI_PASSIVE;
+ hints.ai_socktype = SOCK_STREAM;
+
+ if ((err = getaddrinfo(NULL, PORT, &hints, &serv_info)) != 0) {
+ fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(err));
+ return 1;
+ }
+ if ((sockfd = socket(serv_info->ai_family, serv_info->ai_socktype,
+ serv_info->ai_protocol)) < 0) {
+ perror("socket");
+ exit(1);
+ }
+ if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0) {
+ perror("setsockopt");
+ exit(1);
+ }
+ if (bind(sockfd, serv_info->ai_addr, serv_info->ai_addrlen) < 0) {
+ perror("bind");
+ exit(1);
+ }
+ freeaddrinfo(serv_info);
+
+ if (listen(sockfd, BACKLOG) < 0) {
+ perror("listen");
+ exit(1);
+ }
+
+ if ((acpt_fd =
+ accept(sockfd, (struct sockaddr *)&client_addr, &addr_size)) < 0) {
+ perror("accept");
+ exit(1);
+ }
+ chdir("./resources"); // folder where all files to send are
+
+ while (1) {
+ if ((recv_len = recv(acpt_fd, buf, BUF_LEN, 0)) < 0) {
+ perror("accept");
+ exit(1);
+ }
+ buf[recv_len] = '\0';
+
+ printf("got request: %s\n", buf);
+ sscanf(buf, "%s %s", method, path+1); //+1 as first char needs to be .
+ printf("method: %s\n", method);
+ if (strcmp(method, "GET") == 0) {
+ printf("requested: %s\n\n", path+1);
+ if (path[1] == '\0') {
+ printf("got empty request\n");
+ continue;
+ }
+ path[0] = '.';
+
+ if ((file = fopen(path, "r")) == NULL) {
+ printf("opening \"%s\" didn't work (file nonexistent?)\n\n", path);
+ continue;
+ }
+ fseek(file, 0, SEEK_END);
+ file_len = ftell(file);
+ rewind(file);
+
+ file_buf = malloc(file_len);
+ fread(file_buf, file_len, 1, file);
+ fclose(file);
+ file_buf[file_len] = '\0';
+
+ if (send(acpt_fd, file_buf, file_len, 0) < 0) {
+ perror("send");
+ exit(1);
+ }
+
+ free(file_buf);
+ // prevent data leak by resetting path (a second, empty GET request will
+ // otherwise send previous file
+ path[0] = path[1] = '\0';
+ }
+ else
+ printf("invalid method\n");
+ }
+ close(acpt_fd);
+ close(sockfd);
+
+ return 0;
+}
diff --git a/resources/hello.html b/resources/hello.html
new file mode 100644
index 0000000..a46cd33
--- /dev/null
+++ b/resources/hello.html
@@ -0,0 +1,3 @@
+<html>
+ hello network!
+</html>
diff --git a/resources/hello2.html b/resources/hello2.html
new file mode 100644
index 0000000..931af2a
--- /dev/null
+++ b/resources/hello2.html
@@ -0,0 +1,3 @@
+<html>
+ hello network! this is a second message
+</html>