So guys let us discuss the code walk of our HTTP web server, so let us
so here you can see that as
Usual, I have a main function and inside that main function. I have called the function
setup TCP server communication
So inside that function I implement the entire functionality of TCP webserver
So as we have discussed that our TCP webserver maintains our database of students
So here you can see that I have defined
the data structure of student type
here we have the student name, student roll number, the student hobby and the department to which the student belongs and
I have just maintained the array of these structures and
This array contains the information about five students
So this array simulates as a database that is residing on HTTP web server
When a server receives a request then our server will search this database and pick up the particular student
based on roll number
So let us come back to a function setup TCP server communication as I said that our
HTTP webserver is exactly same as a normal TCP server
So following are the steps that you have already learnt regarding how to implement a TCP server
There are absolutely no difference
So you can see that in the step one its initialization phase and the step two you have created a master socket file descriptor
Then in the step three you have filled up the server information
Regarding the port number so our web server is listening one port number 2000
Then you have bind the socket
Right, then listen system call. So all the steps are exactly same as normal TCP server that you have already learned
Then
Then you are calling a select system call and now waiting for the client connection request
Right in this case. You are waiting for the request from the browser
Then as soon as you receive the request from the browser, you create a communication file descriptor using accept system call
again, exactly the same steps
And
when you receive the
connection request from the client
Your TCP server now waits for the actual data from the client
So considering that the client that is the browser has sent the request to our server
our server will get unblocked from the receive from system call and
Now we come here
So in this portion, we implement the HTTP web server specific functionality
Right
So you can see that as soon as our server receives any data from the client our
Server prints the data that is received from the client as it is that is we are printing the entire buffer here
So you can see in the demonstration part
This highlighted portion is the actual message that the server received the moment you
Enter the URL in the browser and press Enter
So you can see that the message received the first line that your server has received is the request line
Right. And since it's a get request therefore first word is a get. The second part is the URL and the third part is the
HTTP version number
So we have already discussed the format of the get request and the highlighted portion on the screen is the message
That our server receives from the client
And as I said that the rest of the lines in this request are added automatically by the browser
So the second step is that our server will going to parse this request line and
Try to identify the roll number of the student
So the first thing to be done is to extract the request line out of the entire message that is received
From the client that is the server has to extract out the first line. So the delimeter here will going to be
New line character that is \n
So if you split this entire string using \n
That is the newline character as a delimeter, Then you will have tokens and those tokens are nothing but one complete line
But one complete line of text present in this request
So you can see we have chosen a delimeter as a \n and we simply call this
strtok function of C on the data buffer and if you pass the delimeter
So here the delimeter is a \n, so what happens that after this call
The request line is actually the first line of the message that we have received from the client
so we have extracted the request line from the
client message
Because the server only needs the information that is present in the request line to process the data and return the result
So in this line, we have extracted the request line from the client message
Now we have changed our delimeter to space and now we will going to split the request line based on space
So our method will now be point
So our method is a string which says that whether the request is a get request or post request
So you can see that we have
Extracted out the first word that is present in the request line and that word is a get
So we know that the client has sent the get request and not post request
So the URL can be extracted by calling the strtok function again and
Pass the delimeter and just pass the null here. So for that now you have to understand that how strtok function works
I request you that
That Google and try to understand how strtok function works
So here we have extracted out the method which is a get and we have extracted out the URL
from the request line
so so the next step is that
Based on the method type. That is the method is a get or
The method is a post because the client can send you a get request or a post request
So in this example, the client has send us the get request. Therefore. This condition will be true and
We'll simply call the process get request function, which is the function written by us
And in this function, we pass the URL and we pass the response length
the pointer to the integer variable and
this response length will actually
Contains the length of the entire response that the server will be sending back to the client
So going inside the process get request function you can see that we have passed the URL here
so the URL is actually
So the URL string looks like this right remember in the browser you pass the URL
something like this in this format, so
Here you can see that you need to split this is string based on question mark first
because you
Because the server needs to know the roll number of the student
Right. So using strtok function and strtok function repeatedly
You can split this string and eventually you can extract out the value of the roll number
Now I am skipping all this discussion of strtok because
You really have to understand how strtok function works and how using strtok function you can split the strings
so in
so as you can see because the message format of
HTTP webs
So here you can see that the message format of HTTP protocol is of text nature
Therefore we need to rely on too much of string handling function here
right
So here we are iterating over the array that
Acts as a data base to the HTTP webserver and based on the roll number that comes in the URL
The server extract out the student
information from the database
right
Now simply the server has to prepare a response and send back to the client
Right
So to send back the response we need a buffer
Which will going to store the response message
So you can see in the next line. We are using again too many string handling functions like here we are using
Strcpy, and you can see that in this strcpy function
to the response buffer we are simply copying the HTML code
Right. So this HTML code does nothing but it just creates a table which have one column and one row and inside that
one column one row we
Fill up the student name. Which match with the row number
Right
so basically you are generating the HTML code and
You are storing that entire HTML code inside the response buffer
Now as we have discussed that when HTTP web server replies back to the client it has to reply back in the
in the response format
So here we take some more memory which we call it as header
right and
We assume that the header will be of size
248 bytes at least and
this header and
following the header
Will be the HTML code that we have just generated above
So we have already discussed this in the previous slide here
So here you can see that when HTTP web server sends back the response to the client
it has the header and
followed by the HTML code
So we have already discussed that this HTML code has been generated which contain a table
Which have one row and one column and that table contains only the name of the student. Which matched with the roll number
So the next thing that we are doing is to create this header
Right, so to create this header we have taken the memory space and now to this header we are copying the text
That is you can see the first line HTTP 1.1 200
Ok, so same thing here then second thing that you can copy here is the server name
So this could be anything server is a keyword
Whereas following after the colon you can pass any string, which should be a server name
The next is the content length, so here I have one mistake in this line
Should come here, right?
So I have taken a content length, so content length should be
The size of the entire message that you are sending back to the client
So I have just hard-coded this value to 2048
but it should be exactly the size of the message that you are sending back to the client and
Then because our HTTP web server
You can see that after returning from this function. We will going to close the connection
Therefore our HTTP web server is sending the connection status as close to the client
Right and then just add few more strings in this particular format
Like this content type text HTML and all this stuff
So
So after this line our HTTP response header has been prepared and
After that, you have to append the entire HTML code
So here you can see you can simply concatenate the header with the response that you have already
prepared above
so basically after this line you have completely prepared the
HTML response to be returned back to client
Right!! your HTML response contains two parts the header as well as the response
So after concatenating the header and the response you have actually prepared this entire message
Right
Now because strcat function will concatenate the response and store or it will copy the entire response
string into the header memory
Therefore you can free up the response memory here
Right so after returning from this function
So you can see that this function is actually returning a pointer to the entire
Response message that is supposed to be returned to the client
So after returning from this function we have the response here
Which is to be sent back to the client by our server
so you can see if we have got some response the server simply call a send to function call and
Send back the response to the client
Right and after sending back the response to the client, you can simply free up the memory that you have allocated
Right and then rest of the state machine or this server works as usual
So the entire thing here, is that how the server parse the
HTTP request from the client using a string handling function and
From the URL how it extract out
The parameters based on which the server has to prepare the response in this case
The parameter was the roll number
Right so you can see because the
HTTP protocol is actually a text-based protocol
That is the reason that you have to do so much of string manipulation in
Order to implement HTTP web servers
also the second thing to note here is that
the final response that you are sending back to the client is actually the
HTML code
So if you have if you had written this HTML code in the text file and executed on the browser
It would have run the same way
Right
So you can see that we are following the standards we are preparing the message
In compliance to the HTTP protocol
So on the client side the
Message that is the HTTP client request is prepared by the browser
Automatically and on the server side we have to do it manually something like this
So that's all with the discussion of the code walk of the HTTP web server
Because we have implemented a minimal functionality of the HTTP web server
Consider a scenario where you would want to return a complete web page to the client
So that on the client side the browser can display that complete web page
You can consider that instead of writing this code. This code could have been as big as generating
big enough web page
So it is this code that will grow in complexity depending on
How complex the web page the web server is returning to the client to be displayed by the client
Right, so now let us discuss certain observations regarding HTTP web server design and implementation