Skip to content

Commit

Permalink
Merge pull request #1 from hermit-os/http-server
Browse files Browse the repository at this point in the history
feat: add blocking HTTP server
  • Loading branch information
mkroening authored Jan 3, 2025
2 parents 5ac8fd0 + e216ad5 commit c9a4547
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ jobs:
- name: Download loader
run: gh release download --repo hermit-os/loader --pattern hermit-loader-${{ matrix.arch }}
- run: cargo xtask ci c --arch ${{ matrix.arch }} --buildtype ${{ matrix.buildtype }} --target hello_world qemu ${{ matrix.flags }}
- run: cargo xtask ci c --arch ${{ matrix.arch }} --buildtype ${{ matrix.buildtype }} --target http_server qemu ${{ matrix.flags }} --netdev virtio-net-pci
- run: cargo xtask ci c --arch ${{ matrix.arch }} --buildtype ${{ matrix.buildtype }} --target math qemu ${{ matrix.flags }}
- run: cargo xtask ci c --arch ${{ matrix.arch }} --buildtype ${{ matrix.buildtype }} --target memory qemu ${{ matrix.flags }}
- run: cargo xtask ci c --arch ${{ matrix.arch }} --buildtype ${{ matrix.buildtype }} --target thread_local qemu ${{ matrix.flags }}
2 changes: 1 addition & 1 deletion kernel
Submodule kernel updated 133 files
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ else
endif

executable('hello_world', 'src/hello_world.c', link_whole: hermit)
executable('http_server', 'src/http_server.c', link_whole: hermit)
executable('math', 'src/math.c', link_whole: hermit)
executable('memory', 'src/memory.c', link_whole: hermit)
executable('thread_local', 'src/thread_local.c', link_whole: hermit)
64 changes: 64 additions & 0 deletions src/http_server.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <unistd.h>

enum { LISTEN_PORT = 9975 };

static const char reply[] = "HTTP/1.1 200 OK\n"
"Content-Type: text/plain\n"
"Content-Length: 14\n"
"\n"
"Hello, world!\n";

int main(void) {
int listener = socket(AF_INET, SOCK_STREAM, 0);
if (listener < 0) {
perror("socket() failed");
return EXIT_FAILURE;
}

struct sockaddr_in addr = {.sin_family = AF_INET,
.sin_port = htons(LISTEN_PORT),
.sin_addr = INADDR_ANY};

int bound = bind(listener, (struct sockaddr *)&addr, sizeof(addr));
if (bound < 0) {
perror("bind() failed");
return EXIT_FAILURE;
}

int listened = listen(listener, 1);
if (listened < 0) {
perror("listen() failed");
return EXIT_FAILURE;
}

void *read_buf = malloc(BUFSIZ);

fprintf(stderr, "Listening on port %d...\n", LISTEN_PORT);
for (;;) {
int connection = accept(listener, NULL, 0);
if (connection < 0) {
perror("accept() failed");
}

ssize_t readed = read(connection, read_buf, BUFSIZ);
if (readed < 0) {
perror("read() failed");
}

ssize_t written = write(connection, reply, sizeof(reply) - 1);
if (written < 0) {
perror("write() failed");
} else if ((size_t)written != sizeof(reply) - 1) {
fprintf(stderr, "partial write\n");
}

int closed = close(connection);
if (closed < 0) {
perror("close() failed");
}
}
}

0 comments on commit c9a4547

Please sign in to comment.