http/https
1. Introduction to HTTP/HTTPSHTTP (Hyper Text Transfer Protocol) is one of the most widely used network protocols on the Internet. It serves as a standard for client-server request and response, facilitating the transfer of hypertext from WWW servers to local clients.
HTTPS (Hyper Text Transfer Protocol over Secure Socket Layer) is a secure-oriented HTTP channel. Based on the original HTTP, it ensures transmission security through encryption and identity authentication (i.e., integrating SSL/TLS security protocols).
1.1 Differences Between HTTP and HTTPSThe key differences between HTTP and HTTPS are summarized in the following table:
A typical HTTP/HTTPS communication is applied between a browser and a web server. The browser acts as the client, responsible for sending requests, receiving messages, and rendering the display. The web server acts as the server, responsible for responding to requests, managing server files, and handling specific tasks. In some cases, HTTP can serve as a communication protocol for information exchange.
When a client needs resources, the message it sends out is a request message. After receiving the request, the server returns a response message to the client. HTTP specifies the formats of request messages and response messages.
The format of the request message is as follows.
For the purpose of this article, we only need to focus on the request method and URL.
A response message consists of four main parts:
- Status Line: Contains protocol version, status code, and status phrase.
- Response Headers: Key-value pairs (e.g., timestamp, data type, content length).
- Empty Line: Separates headers from the response body.
- Response Body: For web pages, this is typically HTML code; it can also be audio, video, or images (specified by the
Content-Typeheader).
Suppose we deploy an environmental monitoring server (www.huanjin.com) that collects data from multiple observation points. The server stores temperature data for two devices across different dates. To retrieve specific data, the client must send a unique path—this path is a URL.
URL (Uniform Resource Locator) is included in the request line of a request message, acting as a "roadmap." When browsing the web, clicking on different components triggers the browser to automatically generate corresponding URLs. A complete URL consists of three parts: protocol, domain name, and file path.
The specific content is as follows.
For example, if we want to obtain the data of Device 1 in the environmental monitoring server on December 25th, the request message to be filled in is a GET request with the URL "http://www.huanjin.com/Device-1/DateData/20211225".
A URL can also carry variable information, which is called a URL query string, in the form of:
https://www.Adblock.com/s?a=123&b=234Variables are stored at the end of the URL, starting with a "?", in the form of key-value pairs "parameter name=value", and multiple parameters are separated by "&".
1.4 Key Points for HTTPS UsageWhen using HTTPS, adhere to the following guidelines:
- Use a CA certificate (HTTPS relies on SSL/TLS).
- Use POST requests to send data (server address in URL format).
- Use GET requests to receive data/files (server address in URL format).
The EASY-EAI SDK's easyeai-api open-source library encapsulates complex message formatting and data transmission, providing a simple interface for HTTP/HTTPS communication.
If you are new to the EASY-EAI SDK, refer to 《Getting Started Guide / Development Environment Preparation / EASY-EAI Compilation Environment Setup and Update》, and follow the instructions to deploy the compilation environment.
To enter the EASY-EAI compilation environment on a PC running Ubuntu:
cd ~/develop_environment
./run.shCreate a management directory for storing source code repositories in the EASY-EAI compilation environment:
cd /opt
mkdir EASY-EAI-Toolkit
cd EASY-EAI-ToolkitUsing the git tool, clone the remote repository in the management directory.
git clone https://github.com/EASY-EAI/EASY-EAI-Toolkit-1126B.gitNote:* There may be delays here due to network issues, please wait patiently.* If you really need to download from the GitHub webpage, you must download the entire repository; you cannot download the directory corresponding to this example alone.
Enter the corresponding routine directory and perform the compilation operation. The specific commands are as follows:
cd EASY-EAI-Toolkit-1126B/Demos/common-http/
./build.shNote:
* Since the dependent libraries are deployed on the board, the /mnt mount must be maintained during the cross-compilation process.
Access the target board via Serial port debugging or ssh debugging,then navigate to the sample deployment directory:
cd /userdata/Demo/common-httpThe command to run the routine is as follows:
./test-http_server &
./test-http_client2.4 Expected ResultsRefer to the on-screen output for successful execution status.
This section describes the usage of EASY-EAI's HTTPS library functions.
3.1 Integration MethodsThe EASY-EAI API library is located in the easyeai-api directory of the repository. To integrate it into your project, use the following configuration:
The API source code path is EASY-EAI-Toolkit-1126B/easyeai-api/common/http/. Users can understand the interface implementation through the source code and even modify the source code.
3.2 Set CertificateThe function prototype for setting the certificate is:
void set_customer_crt(const char *crt_file);The specific content is as follows.
The function prototype for clearing the cache is:
void clear_multipart();The specific introduction is as follows.
The function prototype for populating the cache is as follows, and the transmitted data is transferred in the form of key-value pairs.
void add_multipart_form_data(const char *key, const char *value);The specific content is as follows.
Separate functions are provided for HTTP and HTTPS (consistent parameters):
int32_t send_data_to_Http(const char *server, const char *func, char *result, uint32_t result_lenth);
int32_t send_data_to_Https(const char *server, const char *func, char *result, uint32_t result_lenth);The specific content is as follows.
Due to the difference in encryption methods between HTTP and HTTPS, two functions are defined separately, but their parameters are consistent. The function prototypes are as follows.
int32_t send_json_to_Http(const char *server, const char *func, const char *json, char *result, uint32_t result_length);
int32_t send_json_to_Https(const char *server, const char *func, const char *json, char *result, uint32_t result_length);The specific content is as follows.
Separate functions are provided for HTTP and HTTPS (consistent parameters):
int32_t get_file_from_https(const char *url, const char *func, const char *filePath);
int32_t get_file_from_http(const char *url, const char *func, const char *filePath);The specific content is as follows.
Sample code path: EASY-EAI-Toolkit-1126B/Demos/common-http/test-http_client.c
The operation to set the key file is as follows.
set_customer_crt("/userdata/customer.crt");4.2 Send form-data4.2.1 Populating DataClear the form_data data buffer, then fill the form_data buffer with data.
clear_multipart();
add_multipart_form_data("name", "gzlmo");
add_multipart_form_data("id", "888888");
add_multipart_form_data("pwd", "123456");4.2.2 Sending DataThe sending operation is as follows.
send_data_to_Http("192.168.3.191:50000", "/add", res, sizeof(res));4.3 Sending json dataThe sending operation is as follows.
send_json_to_Http("192.168.3.191:50000", "/add", "{name : \"gzlmo\"}", res, sizeof(res));



Comments