Skip to content

Commit

Permalink
rg_network: Improved HTTP client
Browse files Browse the repository at this point in the history
- Can now set max_redirections
- Can now set request timeout
- Can now perform POST
  • Loading branch information
ducalex committed Oct 3, 2024
1 parent a7f7435 commit e4588f7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
42 changes: 30 additions & 12 deletions components/retro-go/rg_network.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,39 +287,57 @@ rg_http_req_t *rg_network_http_open(const char *url, const rg_http_cfg_t *cfg)
{
RG_ASSERT_ARG(url != NULL);
#ifdef RG_ENABLE_NETWORKING
esp_http_client_config_t http_config = {.url = url, .buffer_size = 1024, .buffer_size_tx = 1024};
esp_http_client_handle_t http_client = esp_http_client_init(&http_config);
rg_http_req_t *req = calloc(1, sizeof(rg_http_req_t));
if (!req)
{
RG_LOGE("Out of memory");
return NULL;
}

if (!http_client || !req)
req->config = cfg ? *cfg : (rg_http_cfg_t)RG_HTTP_DEFAULT_CONFIG();
req->client = esp_http_client_init(&(esp_http_client_config_t){
.url = url,
.buffer_size = 1024,
.buffer_size_tx = 1024,
.method = req->config.post_data ? HTTP_METHOD_POST : HTTP_METHOD_GET,
.timeout_ms = req->config.timeout_ms,
});

if (!req->client)
{
RG_LOGE("Error creating client");
goto fail;
}

try_again:
if (esp_http_client_open(http_client, 0) != ESP_OK)
if (esp_http_client_open(req->client, req->config.post_len) != ESP_OK)
{
RG_LOGE("Error opening connection");
goto fail;
}

if (esp_http_client_fetch_headers(http_client) < 0)
if (req->config.post_data)
{
esp_http_client_write(req->client, req->config.post_data, req->config.post_len);
// Check for errors?
}

if (esp_http_client_fetch_headers(req->client) < 0)
{
RG_LOGE("Error fetching headers");
goto fail;
}

req->status_code = esp_http_client_get_status_code(http_client);
req->content_length = esp_http_client_get_content_length(http_client);
req->client = (void *)http_client;
req->status_code = esp_http_client_get_status_code(req->client);
req->content_length = esp_http_client_get_content_length(req->client);

// We must handle redirections manually because we're not using esp_http_client_perform
if (req->status_code == 301 || req->status_code == 302)
{
if (req->redirections < 5)
if (req->redirections < req->config.max_redirections)
{
esp_http_client_set_redirection(http_client);
esp_http_client_close(http_client);
esp_http_client_set_redirection(req->client);
esp_http_client_close(req->client);
req->redirections++;
goto try_again;
}
Expand All @@ -328,7 +346,7 @@ rg_http_req_t *rg_network_http_open(const char *url, const rg_http_cfg_t *cfg)
return req;

fail:
esp_http_client_cleanup(http_client);
esp_http_client_cleanup(req->client);
free(req);
#endif
return NULL;
Expand Down
14 changes: 13 additions & 1 deletion components/retro-go/rg_network.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,21 @@ rg_network_t rg_network_get_info(void);

typedef struct
{

int max_redirections;
int timeout_ms;
// Perform POST request
const void *post_data;
int post_len;
} rg_http_cfg_t;

#define RG_HTTP_DEFAULT_CONFIG() \
{ \
.max_redirections = 5, \
.timeout_ms = 30000, \
.post_data = NULL, \
.post_len = 0, \
}

typedef struct
{
rg_http_cfg_t config;
Expand Down

0 comments on commit e4588f7

Please sign in to comment.