examples/protocols: Added URI encoding/decoding feature

- http_server/simple: Decoding received query
  - esp_http_client: Sending encoded query
This commit is contained in:
Laukik Hase
2023-01-25 16:45:01 +05:30
parent d825753387
commit 167618d6a4
7 changed files with 500 additions and 11 deletions

View File

@@ -10,11 +10,13 @@
#include <string.h>
#include <sys/param.h>
#include <stdlib.h>
#include <ctype.h>
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_event.h"
#include "esp_netif.h"
#include "protocol_examples_common.h"
#include "protocol_examples_utils.h"
#include "esp_tls.h"
#if CONFIG_MBEDTLS_CERTIFICATE_BUNDLE
#include "esp_crt_bundle.h"
@@ -431,6 +433,34 @@ static void https_with_hostname_path(void)
esp_http_client_cleanup(client);
}
static void http_encoded_query(void)
{
esp_http_client_config_t config = {
.host = "httpbin.org",
.path = "/get",
.event_handler = _http_event_handler,
};
static const char query_val[] = "ABC xyz!012@#%&";
char query_val_enc[64] = {0};
uint32_t enc_len = example_uri_encode(query_val_enc, query_val, strlen(query_val));
if (enc_len > 0) {
ESP_LOG_BUFFER_HEXDUMP(TAG, query_val_enc, enc_len, ESP_LOG_DEBUG);
config.query = query_val_enc;
}
esp_http_client_handle_t client = esp_http_client_init(&config);
esp_err_t err = esp_http_client_perform(client);
if (err == ESP_OK) {
ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %"PRIu64,
esp_http_client_get_status_code(client),
esp_http_client_get_content_length(client));
} else {
ESP_LOGE(TAG, "HTTP GET request failed: %s", esp_err_to_name(err));
}
}
static void http_relative_redirect(void)
{
esp_http_client_config_t config = {
@@ -743,6 +773,7 @@ static void http_test_task(void *pvParameters)
#if CONFIG_ESP_HTTP_CLIENT_ENABLE_DIGEST_AUTH
http_auth_digest();
#endif
http_encoded_query();
http_relative_redirect();
http_absolute_redirect();
http_absolute_redirect_manual();

View File

@@ -17,9 +17,12 @@
#include "esp_netif.h"
#include "esp_eth.h"
#include "protocol_examples_common.h"
#include "protocol_examples_utils.h"
#include "esp_tls_crypto.h"
#include <esp_http_server.h>
#define EXAMPLE_HTTP_QUERY_KEY_MAX_LEN (64)
/* A simple example that demonstrates how to create GET and POST
* handlers for the web server.
*/
@@ -188,16 +191,22 @@ static esp_err_t hello_get_handler(httpd_req_t *req)
buf = malloc(buf_len);
if (httpd_req_get_url_query_str(req, buf, buf_len) == ESP_OK) {
ESP_LOGI(TAG, "Found URL query => %s", buf);
char param[32];
char param[EXAMPLE_HTTP_QUERY_KEY_MAX_LEN], dec_param[EXAMPLE_HTTP_QUERY_KEY_MAX_LEN] = {0};
/* Get value of expected key from query string */
if (httpd_query_key_value(buf, "query1", param, sizeof(param)) == ESP_OK) {
ESP_LOGI(TAG, "Found URL query parameter => query1=%s", param);
example_uri_decode(dec_param, param, strnlen(param, EXAMPLE_HTTP_QUERY_KEY_MAX_LEN));
ESP_LOGI(TAG, "Decoded query parameter => %s", dec_param);
}
if (httpd_query_key_value(buf, "query3", param, sizeof(param)) == ESP_OK) {
ESP_LOGI(TAG, "Found URL query parameter => query3=%s", param);
example_uri_decode(dec_param, param, strnlen(param, EXAMPLE_HTTP_QUERY_KEY_MAX_LEN));
ESP_LOGI(TAG, "Decoded query parameter => %s", dec_param);
}
if (httpd_query_key_value(buf, "query2", param, sizeof(param)) == ESP_OK) {
ESP_LOGI(TAG, "Found URL query parameter => query2=%s", param);
example_uri_decode(dec_param, param, strnlen(param, EXAMPLE_HTTP_QUERY_KEY_MAX_LEN));
ESP_LOGI(TAG, "Decoded query parameter => %s", dec_param);
}
}
free(buf);

View File

@@ -115,17 +115,18 @@ def test_examples_protocol_http_server_simple(dut: Dut) -> None:
if not client.test_post_handler(got_ip, got_port, random_data):
raise RuntimeError
query = 'http://foobar'
logging.info('Test /hello with custom query : {}'.format(query))
if not client.test_custom_uri_query(got_ip, got_port, query):
queries = 'query1=http%3A%2F%2Ffoobar&query3=abcd%2B1234%20xyz&query2=Esp%21%40%20%23%2471'
logging.info('Test /hello with custom query')
if not client.test_custom_uri_query(got_ip, got_port, queries):
raise RuntimeError
dut.expect('Found URL query => ' + query, timeout=30)
query = 'abcd+1234%20xyz'
logging.info('Test /hello with custom query : {}'.format(query))
if not client.test_custom_uri_query(got_ip, got_port, query):
raise RuntimeError
dut.expect_exact('Found URL query => ' + query, timeout=30)
dut.expect_exact('Found URL query => query1=http%3A%2F%2Ffoobar&query3=abcd%2B1234%20xyz&query2=Esp%21%40%20%23%2471', timeout=30)
dut.expect_exact('Found URL query parameter => query1=http%3A%2F%2Ffoobar', timeout=30)
dut.expect_exact('Decoded query parameter => http://foobar', timeout=30)
dut.expect_exact('Found URL query parameter => query3=abcd%2B1234%20xyz', timeout=30)
dut.expect_exact('Decoded query parameter => abcd+1234 xyz', timeout=30)
dut.expect_exact('Found URL query parameter => query2=Esp%21%40%20%23%2471', timeout=30)
dut.expect_exact('Decoded query parameter => Esp!@ #$71', timeout=30)
@pytest.mark.esp32