Note Concerning the implementation of our own http server, it's a huge project and requires a lot of time. Thus i implemented it with two of my friends; this is why in this
sub-repo
i will only be responsible of explaining the part of project i was responsible of (which is parsing the request that comes from the client through the server and generate the response and send it back to the server so that it can be shown in the preview of the client). However we made a repo of the whole code The root repo of this project
- HTTP is everywhere! Every website we visit is ran on HTTP server. You may think that then what about HTTPS servers? Technically HTTPS is same HTTP with more security.
- Many programmers at some point or the other may get curious about how HTTP servers work and how to build one from scratch without any external libraries.
And I'm one of that many programmers.
- In this project we will build an http server using CPP 98... pretty old version of C++ but don't worry it's more than enough to implement an awesome http server ;)
- HTTP is a protocol for fetching resources such as HTML documents. It is the foundation of any data exchange on the Web and it is a client-server protocol, which means requests are initialed by the recipient, usually the Web browser. A complete document is reconstructed from the different sub-documents fetched, for instance, text, layout description, images, videos, scripts and more.
- As a
request-response
protocol, HTTP gives users a way to interact with web resources such as HTML files by transmitting hypertext messages between clients and servers. - HTTP utilizes
specific request methods
in order to perform various tasks. All HTTP servers use the GET and HEAD methods, but not all support the rest of these request methods... However in our project we must implement at least the three neccessary methods (GET-POST-DELETE). - Clients and servers communicate by exchanging individual messages (as opposed to a stream of data). The messages sent by the client, usually a Web browser, are called
requests
and the messages sent by the server as an answer are calledresponses
and this is exactly what i am responsible of.
- Support for HTTP/1.1 protocol
- Parsing incoming requests to extract relevant information.
- The ability to match the URL/URI with the locations of the servers in the configue file either to match it by
exact location
dynamic location in case we have CGI
or usingprefix location
. - Generation of responses based on the parsed requests.
- The server can handle multi requests in same time and generate responses as well in same time.
- Implementation of GET, POST, DELETE.
- It handle CGI (php and python)
- Concerning GET method, it handles files, index, autoindex and CGI as well.
- Concerning POST method, it handles CGI, form-data, x-www-form-urlencoded (this one handles both CGI and decode files).
- Error handling and appropriate status code responses.
- To use our code, follow with me these steps:
1 - Clone this repository to your local machine.
2 - Ensure you have a compatible C++ compiler installed. (compile with make && ./webserv /Users/aoumad/Desktop/(project name)/prs_rsc/server.conf
just replace the location part based on the location of the project you cloned.
3 - Run the compiled server executable.
4 - Go to POSTMAN or to the browser and test like 'http://localhost:8001/upload/' (just check the port the server is listening to and choose any of the locations in the server in the configuration file or modify it as you want)
PS: check the ports and the IP of the host if you got an error such as 'Bind system...'
- When a client (browser) sends an HTTP request to the server; it get delivered to me throught the server as a string, so in my request folder my aim goals are the following:
1- To parse each line of the request and put them in my setters
of my class so that i can access them in my response class such as uri
, query
, host
, content length
, content type
, boundary
, body of the request
...etc.
2- to call an assignement constructor of the response that sent back to the server a bad request
with 400
in the status code in case of a unwanted request such as different version of an HTTP that we working for or in case of sending the content length
and the encoding-type
such as chunked
... and so on
- The Request-Line begins with a method token, followed by the Request-URI and the protocol version, and ending with CRLF. The elements are separated by space SP characters.
- The request method indicates the method to be performed on the resource identified by the given Request-URI. The method is case-sensitive and should always be mentioned in uppercase.
- In our project we are obligated to implement
GET
,POST
,DELETE
. Therefore, if you found a method that you haven't implemented you need to to get back a response with the status code501
Not implemented
.
- The GET method is used to retrieve information from the given server using a given URI. Requests using GET should only retrieve data and should have no other effect on the data.
- A POST request is used to send data to the server, for example, customer information, file upload, etc. using HTML forms.
- There are several
post types
such asform-data
,raw
,x-www-form-urlencoded
(decode files),binary
...etc. PS: I personally implementedform-data
andx-www-form-urlencoded
but if you implemented the first one is enough.
- Removes all the current representations of the target resource given by URI.
- The Request-URI is a Uniform Resource Identifier and identifies the resource upon which to apply the request. For example :
/cgi-bin/process.py
or/upload/video.mp4
Note Concerning the body request, it must exists when the http method is
POST
, otherwise you can ignore or pass a bad request in case you got a request body inDELETE
or inGET
(Something depends on your logic, i personally ignored it), Otherwise it is neccessary to get the request body inPOST
as well as to haveContent-Type
&&Content-Length
headers in this method too.