final_data: Indicates whether data bufer contains all the data, can be either HTTP_DATA_MORE or HTTP_DATA_FINAL
user_data: User specified data, specified when calling http_client_req()
The HTTP response from the server has many members so let’s focus on the most important ones.
http_status: Textual description of the numeric HTTP status code, 3-digit integer code if valid HTTP response is given, 0 if null response
recv_buf: Where the HTTP response is stored, provided by the user
body_frag_start: Start address of the body contained in the HTTP response in recv_buf
body_frag_len: Length of the body contained in recv_buf
In the following response callback, we print which HTTP request the HTTP response is a response to, through the user_data field used when sending the HTTP request. Then the status of the HTTP response and if the reponse has a body, we want to print that as well.
As opposed to the MQTT Library we covered in Lesson 4, the HTTP client library does not natively support implementing a TLS connection. We will do this by creating and configuring a TLS socket and passing it to the library.
After creating the socket, configure some TLS relevant configurations on the socket, using setsockopt(), which has the following signature
We want to configure the socket with the security tag used to store the certificate and the hostname of the server we are connecting to. These are the options TLS_SEC_TAG_LIST and TLS_HOSTNAME, which reside at the protocol level SOL_TLS.
At the time being, there is no dedicated HTTP server library. However, much of the HTTP client library APIs can be used to implement an HTTP server.
For an HTTP server, we do not need to create an HTTP request, however, we need to define the struct that holds HTTP requests. This struct will be passed on the function that responds to HTTP requests, by changing the state of an LED for example.
The HTTP request struct can be define as shown in the code snippet below.
To parse incoming HTTP requests, we will need to use the HTTP parser library and link it to different functions corresponding to each part of the HTTP request being parsed. The parser function implementation would look like this: