Part 1
요약·해설
Socket ownership, request structure, http_client_req()와 HTTP_DATA_MORE/FINAL callback 흐름을 설명합니다.
TCP · TLS
http_client_req
Chunk callback
Part 2
접을 수 있는 영어 원문 전체
영어 원문 전체 펼치기
5F7A9F44F3F1020446E84BB15CBB5613D62ACD5AD01EAEE6F54B61FED6082B59
.. _http_client_interface:
HTTP Client
###########
.. contents::
:local:
:depth: 2
Overview
********
The HTTP client library allows you to send HTTP requests and
parse HTTP responses. The library communicates over the sockets
API but it does not create sockets on its own.
It can be enabled with :kconfig:option:`CONFIG_HTTP_CLIENT` Kconfig option.
The application must be responsible for creating a socket and passing it to the library.
Therefore, depending on the application's needs, the library can communicate over
either a plain TCP socket (HTTP) or a TLS socket (HTTPS).
Sample Usage
************
The API of the HTTP client library has a single function.
The following is an example of a request structure created correctly:
.. code-block:: c
struct http_request req = { 0 };
static uint8_t recv_buf[512];
req.method = HTTP_GET;
req.url = "/";
req.host = "localhost";
req.protocol = "HTTP/1.1";
req.response = response_cb;
req.recv_buf = recv_buf;
req.recv_buf_len = sizeof(recv_buf);
/* sock is a file descriptor referencing a socket that has been connected
* to the HTTP server.
*/
ret = http_client_req(sock, &req, 5000, NULL);
If the server responds to the request, the library provides the response to the
application through the response callback registered in the request structure.
As the library can provide the response in chunks, the application must be able
to process these.
Together with the structure containing the response data, the callback function
also provides information about whether the library expects to receive more data.
The following is an example of a very simple response handling function:
.. code-block:: c
static void response_cb(struct http_response *rsp,
enum http_final_call final_data,
void *user_data)
{
if (final_data == HTTP_DATA_MORE) {
LOG_INF("Partial data received (%zd bytes)", rsp->data_len);
} else if (final_data == HTTP_DATA_FINAL) {
LOG_INF("All the data received (%zd bytes)", rsp->data_len);
}
LOG_INF("Response status %s", rsp->http_status);
}
See :zephyr:code-sample:`HTTP client sample application <sockets-http-client>` for
more information about the library usage.
API Reference
*************
.. doxygengroup:: http_client
Part 3
한국어 전문 번역
HTTP Client
Overview
HTTP client library는 HTTP request를 전송하고 HTTP response를 parse합니다. Socket API를 통해 통신하지만 socket 자체를 만들지는 않습니다. CONFIG_HTTP_CLIENT Kconfig option으로 활성화할 수 있습니다.
Application이 socket을 생성한 뒤 library에 전달해야 합니다. 따라서 application 요구에 따라 plain TCP socket을 사용해 HTTP로 통신하거나 TLS socket을 사용해 HTTPS로 통신할 수 있습니다.
Sample Usage
HTTP client library API는 함수 하나로 구성됩니다. 다음은 올바르게 만든 request structure 예입니다.
struct http_request req = { 0 };
static uint8_t recv_buf[512];
req.method = HTTP_GET;
req.url = "/";
req.host = "localhost";
req.protocol = "HTTP/1.1";
req.response = response_cb;
req.recv_buf = recv_buf;
req.recv_buf_len = sizeof(recv_buf);
/* sock is a file descriptor referencing a socket that has been connected
* to the HTTP server.
*/
ret = http_client_req(sock, &req, 5000, NULL);http_request에서 method, URL, host, protocol, response callback과 receive buffer를 지정합니다. 이미 HTTP server에 연결된 socket file descriptor를 http_client_req()에 전달하고 예제에서는 timeout을 5,000ms로 지정합니다.
Server가 request에 응답하면 library는 request structure에 등록한 response callback을 통해 application에 response를 전달합니다. Library가 response를 여러 chunk로 제공할 수 있으므로 application은 분할된 데이터를 처리할 수 있어야 합니다.
Callback은 response data가 든 structure와 함께 library가 데이터를 더 받을 예정인지 알려주는 정보도 제공합니다. 다음은 아주 단순한 response 처리 함수 예입니다.
static void response_cb(struct http_response *rsp,
enum http_final_call final_data,
void *user_data)
{
if (final_data == HTTP_DATA_MORE) {
LOG_INF("Partial data received (%zd bytes)", rsp->data_len);
} else if (final_data == HTTP_DATA_FINAL) {
LOG_INF("All the data received (%zd bytes)", rsp->data_len);
}
LOG_INF("Response status %s", rsp->http_status);
}HTTP_DATA_MORE이면 일부 data가 도착한 상태이고 HTTP_DATA_FINAL이면 모든 data를 받은 상태입니다. 예제 callback은 각 경우의 byte 수와 HTTP status를 log로 남깁니다.
sockets-http-client HTTP client sample application에서 library 사용법을 더 확인할 수 있습니다.
API Reference
Doxygen group http_client가 API reference를 제공합니다.
Source
출처
영어 원문에는 문단, 문서 지시문, 표, 코드, 코드 심벌, 경로를 그대로 보존했습니다.