-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 24df92b
Showing
5 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* text=auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
[](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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" "$@" |