{"id":21340696,"url":"https://github.com/artainmo/webserv","last_synced_at":"2026-02-07T09:32:52.138Z","repository":{"id":93053094,"uuid":"326493717","full_name":"artainmo/webserv","owner":"artainmo","description":"42 school project. Create a webserver in c++ able to handle HTTP/1.1 requests following a specific configuration file.","archived":false,"fork":false,"pushed_at":"2024-10-28T15:37:19.000Z","size":14634,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-07-12T14:55:07.288Z","etag":null,"topics":["42school","cpp","http","web"],"latest_commit_sha":null,"homepage":"","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/artainmo.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,"zenodo":null}},"created_at":"2021-01-03T20:21:59.000Z","updated_at":"2024-10-28T15:37:22.000Z","dependencies_parsed_at":null,"dependency_job_id":"3495496c-c214-4062-b2af-c0bc74458ea6","html_url":"https://github.com/artainmo/webserv","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/artainmo/webserv","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artainmo%2Fwebserv","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artainmo%2Fwebserv/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artainmo%2Fwebserv/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artainmo%2Fwebserv/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/artainmo","download_url":"https://codeload.github.com/artainmo/webserv/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artainmo%2Fwebserv/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29191407,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-07T07:37:03.739Z","status":"ssl_error","status_checked_at":"2026-02-07T07:37:03.029Z","response_time":63,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["42school","cpp","http","web"],"created_at":"2024-11-22T00:52:34.947Z","updated_at":"2026-02-07T09:32:52.121Z","avatar_url":"https://github.com/artainmo.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# webserv\n\nCoding a webserver in c++ able to handle HTTP/1.1 requests following a specific configuration file.\n\n**Subject**: [Webserv - 42](https://cdn.intra.42.fr/pdf/pdf/68475/en.subject.pdf)\n\n**Members**: :last_quarter_moon_with_face: [Artainmo](https://github.com/artainmo) - [Ymanzi](https://github.com/ymanzi) :first_quarter_moon_with_face:\n\n![myImage](https://media.giphy.com/media/XRB1uf2F9bGOA/giphy.gif)\n\n## Resources\n\n![myImage](https://media.giphy.com/media/VcizxCUIgaKpa/giphy.gif)\n\n### Server setup\n* [Build a simple HTTP server from scratch](https://medium.com/from-the-scratch/http-server-what-do-you-need-to-know-to-build-a-simple-http-server-from-scratch-d1ef8945e4fa)\n* [Server - Client example code](https://gist.github.com/Alexey-N-Chernyshov/4634731)\n* [Simple server with C++](https://ncona.com/2019/04/building-a-simple-server-with-cpp/)\n* [C++ Web Programming](https://www.tutorialspoint.com/cplusplus/cpp_web_programming.htm)\n* [Youtube - Program your own webserv in C](https://www.youtube.com/watch?v=esXw4bdaZkc)\n* [Youtube - Socket Programming in C](https://youtu.be/LtXEMwSG5-8)\n\n### HTTP1.1 RFC\n* [HTTP/1.1 (RFC 2616)](https://www.rfc-editor.org/rfc/rfc2616.html)\n* [HTTP/1.1 : Message Syntax and Routing (RFC 7230)](https://www.rfc-editor.org/rfc/rfc7230.html)\n* [HTTP/1.1 : Semantics and Content (RFC 7231)](https://www.rfc-editor.org/rfc/rfc7231.html)\n* [HTTP/1.1 : Conditional Requests (RFC 7232)](https://www.rfc-editor.org/rfc/rfc7232.html)\n* [HTTP/1.1 : Range Requests (RFC 7233)](https://www.rfc-editor.org/rfc/rfc7233.html)\n* [HTTP/1.1 : Caching (RFC 7234)](https://www.rfc-editor.org/rfc/rfc7234.html)\n* [HTTP/1.1 : Authentication (RFC 7235)](https://www.rfc-editor.org/rfc/rfc7235.html)\n\n### HTTP Header Syntax\n* [HTTP Request Methods](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)\n* [HTTP Status Codes](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)\n* [HTTP Header Break Style](https://stackoverflow.com/questions/5757290/http-header-line-break-style)\n\n### Select / non-blocking fd / Signals\n* [Select](https://www.lowtek.com/sockets/select.html)\n* [Youtube - Slect](https://www.youtube.com/watch?v=Y6pFtgRdUts)\n* [Non-blocking fd](https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_72/rzab6/xnonblock.htm)\n* [Handle Signal](https://stackoverflow.com/questions/108183/how-to-prevent-sigpipes-or-handle-them-properly)\n\n### CGI\n* [Env CGI variables](https://fr.wikipedia.org/wiki/Variables_d%27environnement_CGI)\n* [CGI : Getting Started](http://www.mnuwer.dbasedeveloper.co.uk/dlearn/web/session01.htm)\n* [CGI 1.1 Documentation](http://www.wijata.com/cgi/cgispec.html#4.0)\n\n![myImage](https://media.giphy.com/media/xT5LMTff7lN1riPLXi/giphy.gif)\n\n## Notes\n### Understand subject\nWorld Wide Web (www), is a grouping of all documents on the web accessible through URLs (uniform resource locator).\n\nA server is a computer connected to the www, it contains an IP address to identify itself, this IP address can be associated with an URL which is a non-random name for easier identification.\n\nWeb browser, is a software application that allows access to the world wide web via URLs. The web browser transforms the URL into an IP address using the DNS (domain name system), it sends http requests towards that ip address or the server machine for the requested content.\n\nWebservers serve an application/platform/website on the web. They receive web browser requests and send back responses. They often run on an external server, which is a computer with no visual display, waiting for client-servers to make requests.\n\nWebserv project consists of writing an http web server. Meaning the web server is able to take and answer http requests.\u003cbr\u003e\nA real webserver thus at least contains HTML files and an http server. But it can also contain a database to provide dynamic content, meaning the html files take variables from the database to produce dynamic content.\n\nhttp requests are made by a web browser and consist of different parts, they can request content from the webserver or even post content on the webserver, allowing the client to interact with the webserver.\nhttp stands for hypertext transfer protocol, hypertext are documents that include links to other documents.\n\nAn http server is able to accept connections with a client, to be able to receive and answer http requests.\n\nA web platform consists of a back-end and front-end.\u003cbr\u003e\nBack-end acts as a web-server (answer HTTP request) and data-base-manager.\u003cbr\u003e\nFront-end refers to the pages send by the web-server to the client-server, that the client's web browser will visually display, creating a user interface (UI).\u003cbr\u003e\nDifferent programming languages can be used for the back-end and front-end development.\n\n### HTTP\nThe language used between servers to communicate.\u003cbr\u003e\nhttp requests are made by a web browser and follow a specific syntax, they can request content from the webserver or even post content on the webserver, allowing the client to interact with the webserver.\u003cbr\u003e\nhttp responses are made by the web server and follow a specific syntax, they can send HTML pages for the web browser to display.\u003cbr\u003e\nhttp stands for hypertext transfer protocol, hypertext are documents that include links to other documents.\n\nStructure of a request:\u003cbr\u003e\nFirst line: method + request targer (URL) + HTTP version (HTTP/1.1)\u003cbr\u003e\nHeader fields: Optional extra information about request\u003cbr\u003e\nSeparation empty space\u003cbr\u003e\nBody: Content of the request\n\nStructure of a response:\u003cbr\u003e\nStatus line: HTTP version (HTTP/1.1) + status code + status short description\u003cbr\u003e\nHeader fields: Optional extra information about response\u003cbr\u003e\nSeparation empty space\u003cbr\u003e\nBody: Content of the response\n\nThe different HTTP methods for requests are:\u003cbr\u003e\nGET: requests representation of specified resource, specified resource is indicated by URL.\u003cbr\u003e\nHEAD: asks for a response identical to that of a GET request, but without the response body, is used to verify if any erros.\u003cbr\u003e\nPOST: used to submit an entity to the specified resource, often causing a change in state on the server.\u003cbr\u003e\nPUT: replaces all current representations of the specified resource with the submitted request entity.\u003cbr\u003e\nDELETE: deletes the specified resource.\n\nThe different status codes for responses are:\u003cbr\u003e\n200: OK, successful request\u003cbr\u003e\n201: Created, for put and post methods\u003cbr\u003e\n400: Bad request, invalid syntax\u003cbr\u003e\n404: Not found, the URL is not recognized\u003cbr\u003e\n405: Not allowed, the resource does not accept the method\u003cbr\u003e\n500: Internal server error, situation the server does not know how to handle\n\nExample http request:\n```\nstd::string http_request_example = \"GET / HTTP/1.1\\r\\n\"\n\t\t\t\"Accept-Charsets: dwdwdq dwqdwqdwqdwq\\r\\n\"\n      \"Accept-Language: en-us\\r\\n\"\n\t\t\t\"Allow: xwdwdw dwqdwqdwq\\r\\n\"\n\t\t\t\"Authorization: dwdwqd dwqdwqdwq\\r\\n\"\n\t\t\t\"Content-Language: dfeefe efewf\\r\\n\"\n\t\t\t\"Content-Length: dwdwdw wdwdqw\\r\\n\"\n\t\t\t\"Content-Location: sdwdwqdwdwd\\r\\n\"\n\t\t\t\"Content-Type: ddw dwqdwqd\\r\\n\"\n\t\t\t\"Date: 09-9w2-1w1\\r\\n\"\n      \"Host: localhost:8080\\r\\n\"\n\t\t\t\"Last-Modified: 424443 3424324\\r\\n\"\n\t\t\t\"Location: c;d';e\\r\\n\"\n\t\t\t\"Referer: dwwdwqd dwqdqwdwq\\r\\n\"\n\t\t\t\"Retry-After: dwdqwdw wdwqdqwdw\\r\\n\"\n\t\t\t\"Server: dwdwdwdwdwdwd\\r\\n\"\n\t\t\t\"Transfer-Encoding: fdsfsf34343\\r\\n\"\n      \"User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/604.5.6 (KHTML, like Gecko) Version/11.0.3 Safari/604.5.6\\r\\n\"\n\t\t\t\"WWW_Authenticate: 2323wwdwdw\\r\\n\"\n      \"\\r\\n\"\n      \"Usually GET requests don\\'t have a body\\r\\n\"\n      \"But I don\\'t care in this case :)\";\n```\n\n### New functions allowed in this project\nstat - Information about a file\u003cbr\u003e\nlstat - Information about a symbolic link\u003cbr\u003e\nfstat - Same as stat but takes fd as argument\u003cbr\u003e\n\nmkdir - creates a new directory\u003cbr\u003e\nopendir - creates a directory stream\u003cbr\u003e\nreaddir - allowd to read the directory stream\u003cbr\u003e\nclosedir - closes the directory stream\u003cbr\u003e\n\ngettimeofday - number of seconds and miniseconds since epoch\u003cbr\u003e\nstrptime - transforms time string in numerical structure\n\n### Socket programming\nSocket programming allows two sockets (IP address with specific port) to communicate. The server contains the listen socket, and client, the client socket.\n\n*Create Socket*\u003cbr\u003e\nsocket - used to create a socket\u003cbr\u003e\nsetsockopt - helps manipulate the socket for re-use\u003cbr\u003e\nbind - binds the socket to a specific ip address and port number\n\n*Server Socket*\u003cbr\u003e\nlisten - listen socket waits for the client socket/sockets to connect\u003cbr\u003e\naccept - extract first connection request in queue and returns newly connected socket\n\n*Client Socket*\u003cbr\u003e\nconnect - client socket demands connection with server socket\n\n*Listen to multiple clients with one thread using select*\u003cbr\u003e\nselect - Allows the webserver to handle multiple clients. It waits until one of fds become active. Also allows for selection between TCP and UDP socket, http server normally uses TCP\n\n*Other*\u003cbr\u003e\nsend - allows sending a message to another socket\u003cbr\u003e\nrecv - used to reveive a message from a socket\u003cbr\u003e\ninet_addr - Used to convert an IPv4 address string notation into an int notation\u003cbr\u003e\ngetsockname - returns address of socket\u003cbr\u003e\nfcntl - funcion able to perform a range of different commands on file descriptors, here can only be used to set a fd as non-blocking\n\nPOST sets received body as env var, that can be used by the URI that is called with cgi or not to produce an upload file. The upload file should be returned in body.\u003cbr\u003e\n*Set env var\u003cbr\u003e\n*Call upload file with cgi (or not)\u003cbr\u003e\n*Produced upload file should be returned in body\n\n### Parsing body\nIf Transfer-encoding header is set to chunked, the content will be sent in multiple requests.\u003cbr\u003e\nIf header-lenght is specified, the content will be send in one request and the lenght of it is specified.\n\n### CGI\nWhen sending content to a browser it has to be in HTML.\u003cbr\u003e\nIf file is not written in HTML it can be transformed to HTML using CGI, an interface that enables to execute an external program that transfomrs the non-HTML file to HTML.\n\n### Server host name and port\nPort 1024 and above are not privileged and free to use and will not fail at binding. localhost can bind to ports under 1024.\u003cbr\u003e\nNot all host addresses can be assigned, locally only network interfaces can be used. All available network addresses can be found using `ifconfig -a`, to parse the useful information use : `ifconfig -a | grep \"inet \"`.\n\n### Steps\n-Take file path as argument\u003cbr\u003e\n-Parse the file\u003cbr\u003e\n-Socket connection / routing\u003cbr\u003e\n-Parse client socket messages\u003cbr\u003e\n--method\u003cbr\u003e\n--URI / URL\u003cbr\u003e\n--protocol version (IPv4 | IPv6)\u003cbr\u003e\n--Header fields\u003cbr\u003e\n---Request modifiers\u003cbr\u003e\n---client info and representation\u003cbr\u003e\n---metadata\u003cbr\u003e\n--empty line to indicate end of header\u003cbr\u003e\n--Message body containing payload body\u003cbr\u003e\n-http answers\u003cbr\u003e\n--status line protocol version\u003cbr\u003e\n--success or error code\u003cbr\u003e\n--textual reason phrase\u003cbr\u003e\n--header fields\u003cbr\u003e\n---server information\u003cbr\u003e\n---resource metadata\u003cbr\u003e\n---representational metadata\u003cbr\u003e\n---Empty line to indicate end of the header\u003cbr\u003e\n---Message body that contains the payload body\n\nRFC\u003cbr\u003e\n-Message Syntax And Routing\u003cbr\u003e\n-Semantics And Content\u003cbr\u003e\n-Conditional Requests\u003cbr\u003e\n-Range Requests\u003cbr\u003e\n-Caching\u003cbr\u003e\n-Authentification\n\nVERIF\u003cbr\u003e\n-Check leaks, hangs, select, .....\n\n### Links\n[How to make a basic Tcp server Video](https://youtu.be/LtXEMwSG5-8)\u003cbr\u003e\n[How to make a basic http server video](https://youtu.be/mStnzIEprH8)\u003cbr\u003e\n[HTTP request parsing https](https://codereview.stackexchange.com/questions/188384/http-request-parser-in-c)\n\n[NGINX workings](http://nginx.org/en/docs/beginners_guide.html)\n\n### Git collaboration\nNever work on same files\u003cbr\u003e\nWork on separate branches:\u003cbr\u003e\ngit branch \\\u003cbranch name\\\u003e -\u003e create branch\u003cbr\u003e\ngit checkout \\\u003cbranch name\\\u003e -\u003e go on branch\u003cbr\u003e\ngit push -u origin \\\u003cbranch name\\\u003e -\u003e push on your branch\u003cbr\u003e\ngit merge \\\u003cbranch name\\\u003e -\u003e from main branch, merge a sub-branch\u003cbr\u003e\ngit branch -d \\\u003cbranch name\\\u003e -\u003e delete your branch\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fartainmo%2Fwebserv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fartainmo%2Fwebserv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fartainmo%2Fwebserv/lists"}