Skip to content

Shoyeb45/http-server

Repository files navigation

HTTP Server

  • 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.

Run the server

  1. Ensure you have cmake installed locally, also make sure to configure vcpkg
  2. Run ./your_program.sh to run your program, which is implemented in src/main.cpp.(first make it executablechmod +x your_program.sh)
  3. Use curl to make request.
curl -i http://localhost:4221/

Current features

  1. Concurrent Connection

  2. Persisten Connection, if sent Connection: close, then it will shut the connection.

  3. Allowed compression using gzip algorithm(implemented using zlib)

  4. Route you can hit and get desired output:

    1. /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
    1. /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
    1. 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
    
    1. POST /files/{path}, Body : {file content}
    • It will write to file with given path.
    1. GET /
    • It will return 200 Ok.

Files

  1. main
  • 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.
  1. Utils
  • Utitlity function to parse and format the strings.

About

A simple http server written from scratch using CPP. I learned this from the codecrafters.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •