Skip to content

Commit

Permalink
http keep alive
Browse files Browse the repository at this point in the history
Give an example of how to implement http keep alive. This requires a
modified version of the lwip http client.
  • Loading branch information
peterharperuk committed Jan 16, 2025
1 parent b6ac07f commit da703d4
Show file tree
Hide file tree
Showing 11 changed files with 1,428 additions and 38 deletions.
4 changes: 3 additions & 1 deletion pico_w/wifi/http_client/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
add_subdirectory(http_client_mod)

pico_add_library(example_lwip_http_util NOFLAG)
target_sources(example_lwip_http_util INTERFACE
${CMAKE_CURRENT_LIST_DIR}/example_http_client_util.c
)
pico_mirrored_target_link_libraries(example_lwip_http_util INTERFACE
pico_lwip_http
pico_lwip_http_client_mod
pico_lwip_mbedtls
pico_mbedtls
)
Expand Down
20 changes: 13 additions & 7 deletions pico_w/wifi/http_client/example_http_client_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

#ifndef HTTP_DEBUG
#ifdef NDEBUG
#define HTTP_DEBUG
#define HTTP_DEBUG(...)
#else
#define HTTP_DEBUG printf
#endif
Expand All @@ -48,7 +48,7 @@ err_t http_client_header_print_fn(__unused httpc_state_t *connection, __unused v

// Print body to stdout
err_t http_client_receive_print_fn(__unused void *arg, __unused struct altcp_pcb *conn, struct pbuf *p, err_t err) {
HTTP_INFO("\ncontent err %d\n", err);
HTTP_INFO("\ncontent err %d len %u\n", err, p->tot_len);
u16_t offset = 0;
while (offset < p->tot_len) {
char c = (char)pbuf_get_at(p, offset++);
Expand All @@ -71,20 +71,26 @@ static err_t internal_recv_fn(void *arg, struct altcp_pcb *conn, struct pbuf *p,
assert(arg);
EXAMPLE_HTTP_REQUEST_T *req = (EXAMPLE_HTTP_REQUEST_T*)arg;
if (req->recv_fn) {
return req->recv_fn(req->callback_arg, conn, p, err);
req->recv_fn(req->callback_arg, conn, p, err);
}
altcp_recved(conn, p->tot_len);
pbuf_free(p);
return ERR_OK;
}

static void internal_result_fn(void *arg, httpc_result_t httpc_result, u32_t rx_content_len, u32_t srv_res, err_t err) {
static int internal_result_fn(void *arg, httpc_result_t httpc_result, u32_t rx_content_len, u32_t srv_res, err_t err) {
int more = 0;
assert(arg);
EXAMPLE_HTTP_REQUEST_T *req = (EXAMPLE_HTTP_REQUEST_T*)arg;
HTTP_DEBUG("result %d len %u server_response %u err %d\n", httpc_result, rx_content_len, srv_res, err);
req->complete = true;
req->result = httpc_result;
if (req->result_fn) {
req->result_fn(req->callback_arg, httpc_result, rx_content_len, srv_res, err);
more = req->result_fn(req->callback_arg, httpc_result, rx_content_len, srv_res, err);
}
if (!more) {
req->complete = true;
}
return more;
}

// Override altcp_tls_alloc to set sni
Expand Down Expand Up @@ -118,7 +124,7 @@ int http_client_request_async(async_context_t *context, EXAMPLE_HTTP_REQUEST_T *
req->settings.headers_done_fn = req->headers_fn ? internal_header_fn : NULL;
req->settings.result_fn = internal_result_fn;
async_context_acquire_lock_blocking(context);
err_t ret = httpc_get_file_dns(req->hostname, req->port ? req->port : default_port, req->url, &req->settings, internal_recv_fn, req, NULL);
err_t ret = httpc_get_file_dns(req->hostname, req->port ? req->port : default_port, req->url, &req->settings, internal_recv_fn, req, &req->connection);
async_context_release_lock(context);
if (ret != ERR_OK) {
HTTP_ERROR("http request failed: %d", ret);
Expand Down
12 changes: 8 additions & 4 deletions pico_w/wifi/http_client/example_http_client_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#ifndef EXAMPLE_HTTP_CLIENT_UTIL_H
#define EXAMPLE_HTTP_CLIENT_UTIL_H

#include "lwip/apps/http_client.h"
#include "http_client_mod.h"

/*! \brief Parameters used to make HTTP request
* \ingroup pico_lwip
Expand All @@ -33,6 +33,7 @@ typedef struct EXAMPLE_HTTP_REQUEST {
altcp_recv_fn recv_fn;
/*!
* Function to callback with final results of the request, can be null
* @return true to keep the connection alive and make more requests
* @see httpc_result_fn
*/
httpc_result_fn result_fn;
Expand All @@ -51,7 +52,7 @@ typedef struct EXAMPLE_HTTP_REQUEST {
*/
struct altcp_tls_config *tls_config;
/*!
* TLS allocator, used internall for setting TLS server name indication
* TLS allocator, used internally for setting TLS server name indication
*/
altcp_allocator_t tls_allocator;
#endif
Expand All @@ -64,10 +65,13 @@ typedef struct EXAMPLE_HTTP_REQUEST {
*/
int complete;
/*!
* Overall result of http request, only valid when complete is set
* Overall result of http request
*/
httpc_result_t result;

/*!
* Gets a pointer to internal http client
*/
httpc_state_t *connection;
} EXAMPLE_HTTP_REQUEST_T;

struct async_context;
Expand Down
9 changes: 9 additions & 0 deletions pico_w/wifi/http_client/http_client_mod/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Modified version of LwIP http client
pico_add_library(pico_lwip_http_client_mod NOFLAG)
target_include_directories(pico_lwip_http_client_mod INTERFACE
${CMAKE_CURRENT_LIST_DIR}
)
target_sources(pico_lwip_http_client_mod INTERFACE
${PICO_LWIP_PATH}/src/apps/http/altcp_proxyconnect.c
${CMAKE_CURRENT_LIST_DIR}/http_client_mod.c
)
Loading

0 comments on commit da703d4

Please sign in to comment.