diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..176a458 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/README.md b/README.md new file mode 100644 index 0000000..bb33511 --- /dev/null +++ b/README.md @@ -0,0 +1,38 @@ +[![progress-banner](https://backend.codecrafters.io/progress/http-server/1102e432-3197-490f-94aa-a42bc1bd9516)](https://app.codecrafters.io/users/codecrafters-bot?r=2qF) + +This is a starting point for C solutions to the +["Build Your Own HTTP server" Challenge](https://app.codecrafters.io/courses/http-server/overview). + +[HTTP](https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol) is the +protocol that powers the web. In this challenge, you'll build a HTTP/1.1 server +that is capable of serving multiple clients. + +Along the way you'll learn about TCP servers, +[HTTP request syntax](https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html), +and more. + +**Note**: If you're viewing this repo on GitHub, head over to +[codecrafters.io](https://codecrafters.io) to try the challenge. + +# Passing the first stage + +The entry point for your HTTP server implementation is in `app/server.c`. Study +and uncomment the relevant code, and push your changes to pass the first stage: + +```sh +git add . +git commit -m "pass 1st stage" # any msg +git push origin master +``` + +Time to move on to the next stage! + +# Stage 2 & beyond + +Note: This section is for stages 2 and beyond. + +1. Ensure you have `gcc` installed locally +1. Run `./your_server.sh` to run your program, which is implemented in + `app/server.c`. +1. Commit your changes and run `git push origin master` to submit your solution + to CodeCrafters. Test output will be streamed to your terminal. diff --git a/app/server.c b/app/server.c new file mode 100644 index 0000000..c32b198 --- /dev/null +++ b/app/server.c @@ -0,0 +1,61 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +int main() { + // Disable output buffering + setbuf(stdout, NULL); + + // You can use print statements as follows for debugging, they'll be visible when running tests. + printf("Logs from your program will appear here!\n"); + + // Uncomment this block to pass the first stage + // + // int server_fd, client_addr_len; + // struct sockaddr_in client_addr; + // + // server_fd = socket(AF_INET, SOCK_STREAM, 0); + // if (server_fd == -1) { + // printf("Socket creation failed: %s...\n", strerror(errno)); + // return 1; + // } + // + // // Since the tester restarts your program quite often, setting REUSE_PORT + // // ensures that we don't run into 'Address already in use' errors + // int reuse = 1; + // if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEPORT, &reuse, sizeof(reuse)) < 0) { + // printf("SO_REUSEPORT failed: %s \n", strerror(errno)); + // return 1; + // } + // + // struct sockaddr_in serv_addr = { .sin_family = AF_INET , + // .sin_port = htons(4221), + // .sin_addr = { htonl(INADDR_ANY) }, + // }; + // + // if (bind(server_fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) != 0) { + // printf("Bind failed: %s \n", strerror(errno)); + // return 1; + // } + // + // int connection_backlog = 5; + // if (listen(server_fd, connection_backlog) != 0) { + // printf("Listen failed: %s \n", strerror(errno)); + // return 1; + // } + // + // printf("Waiting for a client to connect...\n"); + // client_addr_len = sizeof(client_addr); + // + // accept(server_fd, (struct sockaddr *) &client_addr, &client_addr_len); + // printf("Client connected\n"); + // + // close(server_fd); + + return 0; +} diff --git a/codecrafters.yml b/codecrafters.yml new file mode 100644 index 0000000..4de7cd9 --- /dev/null +++ b/codecrafters.yml @@ -0,0 +1,11 @@ +# Set this to true if you want debug logs. +# +# These can be VERY verbose, so we suggest turning them off +# unless you really need them. +debug: false + +# Use this to change the C version used to run your code +# on Codecrafters. +# +# Available versions: c-9.2 +language_pack: c-9.2 diff --git a/your_server.sh b/your_server.sh new file mode 100755 index 0000000..bc5d895 --- /dev/null +++ b/your_server.sh @@ -0,0 +1,11 @@ +#!/bin/sh +# +# DON'T EDIT THIS! +# +# CodeCrafters uses this file to test your code. Don't make any changes here! +# +# DON'T EDIT THIS! +set -e +tmpFile=$(mktemp) +gcc -lcurl app/*.c -o $tmpFile +exec "$tmpFile" "$@"