{"id":19987241,"url":"https://github.com/chrkspln/http-server-cpp","last_synced_at":"2025-03-01T21:17:17.904Z","repository":{"id":241962448,"uuid":"808321769","full_name":"chrkspln/http-server-cpp","owner":"chrkspln","description":"HTTP/1.1 server that is capable of handling multiple clients with GET and POST requests","archived":false,"fork":false,"pushed_at":"2024-06-02T20:22:40.000Z","size":32,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-12T11:44:15.818Z","etag":null,"topics":["codecrafters-http-server","cpp20"],"latest_commit_sha":null,"homepage":"https://app.codecrafters.io/courses/http-server/introduction","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chrkspln.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-05-30T20:34:28.000Z","updated_at":"2024-06-02T20:22:43.000Z","dependencies_parsed_at":"2024-11-13T04:35:00.474Z","dependency_job_id":"3ac3b4d9-4be8-4ce0-a9a3-9708e521d88c","html_url":"https://github.com/chrkspln/http-server-cpp","commit_stats":null,"previous_names":["chrkspln/http-server-cpp"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrkspln%2Fhttp-server-cpp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrkspln%2Fhttp-server-cpp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrkspln%2Fhttp-server-cpp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrkspln%2Fhttp-server-cpp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chrkspln","download_url":"https://codeload.github.com/chrkspln/http-server-cpp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241424470,"owners_count":19960752,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["codecrafters-http-server","cpp20"],"created_at":"2024-11-13T04:34:51.208Z","updated_at":"2025-03-01T21:17:17.884Z","avatar_url":"https://github.com/chrkspln.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Simple HTTP Server\n\nThis project is a simple HTTP server written in C++. It handles basic HTTP requests including GET and POST methods and serves files from a specified directory. The server responds with appropriate HTTP status codes and can also echo messages back to the client.\n\n## Features\n\n- Handles basic HTTP GET and POST requests.\n- Serves files from a specified directory.\n- Echoes messages back to the client.\n- Responds with appropriate HTTP status codes (200 OK, 404 Not Found, 201 Created).\n- Supports `User-Agent` inspection.\n- Multi-threaded to handle multiple client connections simultaneously.\n\n## Requirements\n\n- A C++ compiler that supports C++11 or later.\n- POSIX-compliant operating system (e.g., Linux, macOS).\n\n## Usage\n\n1. **Compile the server:**\n\n    ```sh\n    g++ -std=c++11 -pthread -o server main.cpp\n    ```\n\n2. **Run the server:**\n\n    ```sh\n    ./server --directory \u003cpath_to_directory\u003e\n    ```\n\n    Replace `\u003cpath_to_directory\u003e` with the path to the directory you want to serve files from.\n\n## Code Overview\n\n### Functions\n\n- `sockaddr_in setting_server_addr()`\n  - Configures and returns the server address structure.\n\n- `int server_setup(const int \u0026server_fd, sockaddr_in \u0026server_addr)`\n  - Binds the server to a port and starts listening for incoming connections.\n\n- `void cutting_connection()`\n  - Signals the end of a connection.\n\n- `int create_socket()`\n  - Creates and returns a server socket.\n\n- `void default_success_response(const int \u0026client_fd)`\n  - Sends a default success (200 OK) response to the client.\n\n- `void default_fail_response(const int \u0026client_fd)`\n  - Sends a default failure (404 Not Found) response to the client.\n\n- `void get_echo_response(const int \u0026client_fd, const std::string \u0026client_msg)`\n  - Handles GET requests to `/echo/` by echoing back the message.\n\n- `std::string encoding_request(const std::string \u0026client_msg, int start_encoding_index)`\n  - Parses and returns the encoding type from the client's request.\n\n- `void get_user_ag_response(const int \u0026client_fd, const std::string \u0026client_msg)`\n  - Handles GET requests to `/user-agent` by returning the client's User-Agent.\n\n- `void get_files_response(const int \u0026client_fd, const std::string \u0026client_msg)`\n  - Handles GET requests to `/files/` by serving the requested file.\n\n- `void post_files_response(const int \u0026client_fd, const std::string \u0026client_msg)`\n  - Handles POST requests to `/files/` by saving the uploaded file.\n\n- `void processing_user_request(const int \u0026client_fd, const std::string \u0026client_msg)`\n  - Processes the client's request and routes it to the appropriate handler.\n\n- `void handle_client(const int \u0026client_fd)`\n  - Receives the client's message and processes the request.\n\n### Main Function\n\n- Sets up the server socket.\n- Binds to the port and starts listening.\n- Accepts client connections and spawns threads to handle each client.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchrkspln%2Fhttp-server-cpp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchrkspln%2Fhttp-server-cpp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchrkspln%2Fhttp-server-cpp/lists"}