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
@@ -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);
@@ -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