Compare commits

...

5 Commits

9 changed files with 187 additions and 61 deletions

View File

@ -1,3 +0,0 @@
target/
debug/
**/*.rs.bk

2
.gitignore vendored
View File

@ -1,2 +1,2 @@
libstrops.a
libhttp.a
http_server

View File

@ -1,36 +0,0 @@
FROM rust:1.85-slim-bookworm AS build
# get x86_64-unknown-linux-musl as a build target
RUN rustup target add x86_64-unknown-linux-musl
# create a new empty shell project
RUN USER=root cargo new --bin http_server
WORKDIR /http_server
# copy over your manifests
COPY ./Cargo.lock ./Cargo.lock
COPY ./Cargo.toml ./Cargo.toml
# this build step will cache your dependencies
RUN cargo build --release
RUN rm src/*.rs
# copy your source tree
COPY ./src ./src
# build for release as a static-pie linked binary
RUN rm ./target/release/deps/http_server*
ENV RUSTFLAGS='-C target-feature=+crt-static'
RUN cargo build --release --target x86_64-unknown-linux-musl
RUN strip /http_server/target/x86_64-unknown-linux-musl/release/http_server
FROM scratch
# copy the build artifact from the build stage
COPY --from=build /http_server/target/x86_64-unknown-linux-musl/release/http_server /
COPY ./www /www
COPY ./internal /internal
EXPOSE 8080
CMD ["/http_server"]

8
Makefile Normal file
View File

@ -0,0 +1,8 @@
.POSIX:
server: server.c libhttp.a
gcc -I . -L. -ansi -ggdb -o http_server server.c -lstrops -Wl,-Bstatic -lhttp -Wl,-Bdynamic
libhttp.a: http.c
gcc -c -ansi -ggdb -o http.o http.c -lstrops
ar cr libhttp.a http.o
rm http.o

View File

@ -1,15 +0,0 @@
networks:
http-network:
name: http-network
driver: bridge
services:
http-server:
container_name: http-server
build: .
image: http-server:latest
restart: always
ports:
- 80:8080
networks:
- http-network

104
http.c Normal file
View File

@ -0,0 +1,104 @@
#include "http.h"
#include <strops.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* TODO: https://blog.netherlabs.nl/articles/2009/01/18/the-ultimate-so_linger-page-or-why-is-my-tcp-not-reliable */
int http_init(int port, int connection_amount) {
int ret;
int server_sock = socket(AF_INET, SOCK_STREAM, 0);
if (server_sock == -1) {
perror("Couldn't create socket");
return -1;
}
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(port);
if (bind(server_sock, (struct sockaddr*)&server_addr, sizeof server_addr) == -1) {
perror("Couldn't bind socket");
return -1;
}
if (listen(server_sock, connection_amount) == -1) {
perror("Cannot listen on socket");
return -1;
}
return server_sock;
}
HTTP_Request* http_accept(int server) {
int client_sock = accept(server, NULL, NULL);
if (client_sock == -1) {
perror("Couldn't connect to client");
return NULL;
}
HTTP_Request *request = malloc(sizeof(HTTP_Request));
request->client_sock = client_sock;
/* TODO: Read entire message and parse into request struct */
ssize_t bytes_read;
size_t bufsize = 4096;
char buf[bufsize];
bytes_read = read(request->client_sock, buf, bufsize - 1);
if (bytes_read == -1) {
perror("Failed to read data");
}
/* TODO: read tmp line by line */
char *tmp = strops_trim_left_string(buf, "\r\n");
size_t lines_count = 0;
size_t lines_capacity = 10;
char **lines = malloc(lines_capacity * sizeof (char*));
size_t i;
while (strops_length(tmp) > 0) {
char *line = malloc(strops_length(tmp));
memset(line, 0, strops_length(tmp));
for (i = 0; i < strops_length(tmp); i++) {
if (tmp[i] == '\r' && tmp[i + 1] == '\n') {
i += 2;
break;
}
line[i] = tmp[i];
}
for (; i > 0; i--) {
strops_remove_at_pos_char_inplace(tmp, 0);
}
if (lines_count >= lines_capacity) {
lines_capacity *= 2;
lines = realloc(lines, lines_capacity * sizeof (char*));
}
lines[lines_count] = line;
lines_count++;
}
free(tmp);
/* TODO: Parse lines */
/* TODO: find suitable data structure for field-lines */
/*
request-line => method target version
seperated by whitespace (SP)
field-line => field-name:field-value
seperated by a single colon => ':'
field-value may have leading and trailing optionial whitespace (OWS)
*/
for (i = 0; i < lines_count; i++) {
printf("%s\n", lines[i]);
}
/* TODO: remeber to delete this code after finishing parsing code */
for (i = lines_count; i > 0; i--) {
free(lines[i]);
}
free(lines);
return request;
}

26
http.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef HTTP_H
#define HTTP_H
#include <netinet/in.h>
#include <sys/socket.h>
typedef struct {
int client_sock;
char *method;
char *target;
char *version;
/* Field_Lines (Header) */
char *body;
} HTTP_Request;
typedef struct {
char *version;
unsigned int status;
/* Field_Lines (Header) */
char *body;
} HTTP_Response;
int http_init(int port, int connection_amount);
HTTP_Request* http_accept(int server);
#endif /* HTTP_H */

6
main.c
View File

@ -1,6 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main() {
return 0;
}

48
server.c Normal file
View File

@ -0,0 +1,48 @@
#include <strops.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include "http.h"
#include <unistd.h>
#define PORT 8080
HTTP_Response* handle_client(HTTP_Request *request) {
ssize_t bytes_written;
char *response = "HTTP/1.1 200 \r\n\r\n";
bytes_written = write(request->client_sock, response, strops_length(response));
if (bytes_written != strops_length(response)) {
fprintf(stderr, "Incomplete write\n");
}
return NULL;
}
/*
TODO: implement signals
TODO: graceful server shutdown
*/
int main() {
int ret;
int server;
server = http_init(PORT, 3);
if (server == -1) {
return 1;
}
HTTP_Request *request;
while(1) {
request = http_accept(server);
if (request == NULL) {
break;
}
handle_client(request);
close(request->client_sock);
free(request);
}
close(server);
return 0;
}