diff --git a/Dockerfile b/Dockerfile index 6d4e901..5adb2b5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,33 +1,18 @@ -FROM rust:1.85-slim-bookworm AS build +FROM gcc 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 +COPY ./main.c . +COPY ./libstrops.a . -# this build step will cache your dependencies -RUN cargo build --release -RUN rm src/*.rs +RUN gcc -I . -L. -static -ansi -O2 -o http_server main.c -lstrops -# 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 +RUN strip ./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 --from=build /http_server / COPY ./www /www COPY ./internal /internal diff --git a/main.c b/main.c index e98958f..4ecc194 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,62 @@ #include #include +#include +#include + +#include +#include +#include + +#define PORT 8080 + +void handle_client(int client) { + ssize_t bytes_read; + size_t bufsize = 1024; + char buf[bufsize]; + + bytes_read = read(client, buf, bufsize - 1); + if (bytes_read == -1) { + fprintf(stderr, "Failed to read data. Error = %s\n", strerror(errno)); + } + printf("%s\n", buf); +} int main() { + int ret; + int server = socket(AF_INET, SOCK_STREAM, 0); + if (server == -1) { + fprintf(stderr, "Couldn't create socket. Error = %s\n", strerror(errno)); + return 1; + } + + struct sockaddr_in addr; + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = INADDR_ANY; + addr.sin_port = htons(PORT); + + ret = bind(server, (struct sockaddr*)&addr, sizeof(addr)); + if (ret == -1) { + fprintf(stderr, "Couldn't bind socket. Error = %s\n", strerror(errno)); + return 1; + } + + ret = listen(server, 3); + if (ret == -1) { + fprintf(stderr, "Cannot listen on socket. Error = %s", strerror(errno)); + return 1; + } + + int client; + while(1) { + client = accept(server, (struct sockaddr*)&addr, sizeof(addr)); + if (client == -1) { + fprintf(stderr, "Couldn't connect to client. Error = %s\n", strerror(errno)); + } + handle_client(client); + close(client); + } + + /* Unreacheable Code */ + close(server); return 0; }