Skip to content

Commit

Permalink
init [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
codecrafters-bot committed Mar 16, 2024
0 parents commit 24df92b
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
61 changes: 61 additions & 0 deletions app/server.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

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;
}
11 changes: 11 additions & 0 deletions codecrafters.yml
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions your_server.sh
Original file line number Diff line number Diff line change
@@ -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" "$@"

0 comments on commit 24df92b

Please sign in to comment.