- HTTP protocol is one of the most important techonology which allows the communication over network.
- This was one of the challenge of codecrafters and I really enjoyed building it and I will make it like a mini express soon.
- Ensure you have
cmakeinstalled locally, also make sure to configurevcpkg - Run
./your_program.shto run your program, which is implemented insrc/main.cpp.(first make it executablechmod +x your_program.sh) - Use curl to make request.
curl -i http://localhost:4221/-
Concurrent Connection
-
Persisten Connection, if sent
Connection: close, then it will shut the connection. -
Allowed compression using
gzipalgorithm(implemented usingzlib) -
Route you can hit and get desired output:
/echo/{str}
- Send desired string in params, and you will get output like:
curl -i http://localhost:4221/echo/http-protocol Output: HTTP/1.1 200 OK Content-Type: text/plain Content-Length: 13 http-protocol
/user-agent
- It will return User agent(The client used to send a request)
curl -i http://localhost:4221/user-agent Ouput: HTTP/1.1 200 OK Content-Type: text/plain Content-Length: 10 curl/8.5.0
- GET
/files/{file_path}
- Read from the file in desired location. You need to give the location in command line argument. (
./your-program.sh --directory /tmp/) - It will read from the given file path if the file exits, else it will return with
404.
1. File exists curl -i http://localhost:4221/files/hello.txt HTTP/1.1 200 OK Content-Type: application/octet-stream Content-Length: 12 Hello, World 2. File does not exist curl -i http://localhost:4221/files/world.txt HTTP/1.1 404 Not Found
- POST
/files/{path}, Body : {file content}
- It will write to file with given
path.
- GET
/
- It will return
200 Ok.
- Initialises a TCP connection and attach to the port
- Using multithreading we can connect to different client and handle each of them separatley while keeping the connection persistent.
- Utitlity function to parse and format the strings.