Skip to content

Android Tutorial Station

HTTP protocol for developers (part I)

This is the first article in a series of articles that will show all the basic concepts that a developer should know about the HTTP protocol . We will try to make it an article that adapts to all levels and can be understood by webmasters that start in the world of development.

HTTP protocol basics

HTTP is the acronym for Hypertext Transfer Protocol . It is a protocol focused on transactions and follows the request-response scheme between a client and a server. It is a stateless protocol, that is, it does not save any information about the connections made previously.

Many of you will be wondering how this last true statement can be, since when for example we access a forum with our username / password this is remembered in future connections. This is due to the use of cookies, these cookies are nothing more than information that the server can store on the client.

The operation of the HTTP protocol is very extensive and is specified in the RFC 2616 standard, this standard is not exactly a pleasant read, but after reading our series of articles we hope to ensure that the reader can consult RFC 2616 if necessary and not get lost .

HTTP transactions

HTTP protocol transactions are made up of a request header (data can optionally be added). This request header is sent by the client to the server. The server then interprets the request header sent by the client and returns a response header.

Request headers can be modified by the client to interact with the server in different ways. Later we will analyze in detail the headers, both those of the client’s request and those of the server’s response, for the moment it is sufficient so you know that they exist to give you an idea of ??how the HTTP protocol works.

Parts of a URL

Knowing the structure of a URL is something fundamental and that you have to be clear, it is even useful when programming in PHP since certain functions talk about the different parts of a URL.

URL is the acronym for Uniform Resource Locators . URLs are used to name resources on the Internet, to locate or identify them.

The best way to understand the structure of a URL is to see the following image:

  • Protocol : In general, the HTTP protocol is used, but we can also see URLs with the HTTPS protocol for encrypted communications.
  • Host (domain) : the host is a way of identifying the host (the server) with which our client will establish communication.
  • Port : This is a network port number, since the HTTP protocol is based on the use of sockets. The port for web pages is not specified when we use our browser (FireFox, Chrome, etc.), but the default port for web pages is 80. We can try the following URL in our browser to check it:
  • Resource Path : is the path where the resource is located.
  • Query : is the part of the URL that contains data that is passed to a web application. We can see that the query goes after the resource path, and is identified because it always begins with the character “?”. In the case of the image, two variables are sent:
    • variable “a” whose value is “b”.
    • variable “x” whose value is “y”.

    We can appreciate that each variable-value pair is separated by the & character.

HTTP protocol workshop for developers (part I)

All the previous theory is very good, but we believe that the best way to strengthen knowledge is to practice with what has been learned. That is why we propose this workshop where we will show a series of simple codes that allow us to analyze the concepts we have learned before.

To analyze the different requests with their headers in FireFox (the browser I use) we can use Firebug (activating the Network option) or any other similar program. Another add-on for FireFox that I like very much and allows us to analyze the request / response headers is Live HTTP Headers .

Code that shows the request headers sent by our client.

Eye! This code is only compatible with Apache servers.

  <?php  foreach (getallheaders() as $nombre => $valor) {   echo "$nombre: $valorn"; }  ?> 

Code that allows us to add a line to the response header of the server.

This can be very useful, for example to make a permanent redirection. The code would be as follows:

  <?php  header ('HTTP/1.1 301 Moved Permanently'); header ('Location:');  ?> 

Code that shows us the different parts of the URL.

  <?php  //Definimos el salto de linea HTML para mostrar los datos en el navegador $salto_de_linea_html = "</br>";  //Obtener el Host de la URL echo "Host: " . $_SERVER['HTTP_HOST'] . $salto_de_linea_html;  //Obtener el puerto de la URL echo "Puerto: " . $_SERVER['SERVER_PORT'] . $salto_de_linea_html;  //Obtener el resource path y la query de la URL echo "Resource path y query: " . $_SERVER['REQUEST_URI'] . $salto_de_linea_html;  //Obtener el resource path de la URL echo "Resource path: " . $_SERVER['SCRIPT_NAME'] . $salto_de_linea_html;  //Obtener la query de la URL echo "Query: " . $_SERVER['QUERY_STRING'] . $salto_de_linea_html;  ?> 

No comments yet.

Leave a Reply

Your email address will not be published.

Comments (0)