2017-09-26 08:02:13 -04:00
|
|
|
/* HTTP2 GET Example using nghttp2
|
2020-11-10 02:40:01 -05:00
|
|
|
|
2022-01-05 22:59:42 -05:00
|
|
|
Contacts http2.github.io and executes the GET request. A thin API
|
2017-09-26 08:02:13 -04:00
|
|
|
wrapper on top of nghttp2, to properly demonstrate the interactions.
|
|
|
|
|
|
|
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, this
|
|
|
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
|
|
|
*/
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "esp_wifi.h"
|
2019-04-11 06:54:26 -04:00
|
|
|
#include "esp_event.h"
|
2018-09-06 07:43:08 -04:00
|
|
|
#include "lwip/apps/sntp.h"
|
2017-09-26 08:02:13 -04:00
|
|
|
#include "esp_system.h"
|
|
|
|
#include "nvs_flash.h"
|
2019-04-11 06:54:26 -04:00
|
|
|
#include "protocol_examples_common.h"
|
2019-08-31 10:19:21 -04:00
|
|
|
#include "esp_netif.h"
|
2017-09-26 08:02:13 -04:00
|
|
|
|
|
|
|
#include "sh2lib.h"
|
|
|
|
|
|
|
|
|
2022-01-05 22:59:42 -05:00
|
|
|
extern const uint8_t server_root_cert_pem_start[] asm("_binary_http2_github_io_root_cert_pem_start");
|
|
|
|
extern const uint8_t server_root_cert_pem_end[] asm("_binary_http2_github_io_root_cert_pem_end");
|
2021-04-08 02:38:51 -04:00
|
|
|
|
2017-09-26 08:02:13 -04:00
|
|
|
/* The HTTP/2 server to connect to */
|
2022-01-05 22:59:42 -05:00
|
|
|
#define HTTP2_SERVER_URI "https://http2.github.io"
|
2017-09-26 08:02:13 -04:00
|
|
|
/* A GET request that keeps streaming current time every second */
|
2022-01-05 22:59:42 -05:00
|
|
|
#define HTTP2_STREAMING_GET_PATH "/index.html"
|
2017-09-26 08:02:13 -04:00
|
|
|
|
2019-04-11 06:54:26 -04:00
|
|
|
|
2017-09-26 08:02:13 -04:00
|
|
|
int handle_get_response(struct sh2lib_handle *handle, const char *data, size_t len, int flags)
|
|
|
|
{
|
|
|
|
if (len) {
|
|
|
|
printf("[get-response] %.*s\n", len, data);
|
|
|
|
}
|
|
|
|
if (flags == DATA_RECV_FRAME_COMPLETE) {
|
|
|
|
printf("[get-response] Frame fully received\n");
|
|
|
|
}
|
|
|
|
if (flags == DATA_RECV_RST_STREAM) {
|
|
|
|
printf("[get-response] Stream Closed\n");
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int handle_echo_response(struct sh2lib_handle *handle, const char *data, size_t len, int flags)
|
|
|
|
{
|
|
|
|
if (len) {
|
|
|
|
printf("[echo-response] %.*s\n", len, data);
|
|
|
|
}
|
|
|
|
if (flags == DATA_RECV_FRAME_COMPLETE) {
|
|
|
|
printf("[echo-response] Frame fully received\n");
|
|
|
|
}
|
|
|
|
if (flags == DATA_RECV_RST_STREAM) {
|
|
|
|
printf("[echo-response] Stream Closed\n");
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int send_put_data(struct sh2lib_handle *handle, char *buf, size_t length, uint32_t *data_flags)
|
|
|
|
{
|
|
|
|
#define DATA_TO_SEND "Hello World"
|
|
|
|
int copylen = strlen(DATA_TO_SEND);
|
|
|
|
if (copylen < length) {
|
|
|
|
printf("[data-prvd] Sending %d bytes\n", copylen);
|
|
|
|
memcpy(buf, DATA_TO_SEND, copylen);
|
|
|
|
} else {
|
|
|
|
copylen = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
(*data_flags) |= NGHTTP2_DATA_FLAG_EOF;
|
|
|
|
return copylen;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_time(void)
|
|
|
|
{
|
|
|
|
struct timeval tv = {
|
|
|
|
.tv_sec = 1509449941,
|
|
|
|
};
|
|
|
|
struct timezone tz = {
|
|
|
|
0, 0
|
|
|
|
};
|
|
|
|
settimeofday(&tv, &tz);
|
|
|
|
|
|
|
|
/* Start SNTP service */
|
|
|
|
sntp_setoperatingmode(SNTP_OPMODE_POLL);
|
|
|
|
sntp_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void http2_task(void *args)
|
|
|
|
{
|
|
|
|
/* Set current time: proper system time is required for TLS based
|
|
|
|
* certificate verification.
|
|
|
|
*/
|
|
|
|
set_time();
|
|
|
|
|
|
|
|
/* HTTP2: one connection multiple requests. Do the TLS/TCP connection first */
|
|
|
|
printf("Connecting to server\n");
|
2021-04-08 02:38:51 -04:00
|
|
|
struct sh2lib_config_t cfg = {
|
|
|
|
.uri = HTTP2_SERVER_URI,
|
|
|
|
.cacert_buf = server_root_cert_pem_start,
|
|
|
|
.cacert_bytes = server_root_cert_pem_end - server_root_cert_pem_start,
|
|
|
|
};
|
2017-09-26 08:02:13 -04:00
|
|
|
struct sh2lib_handle hd;
|
2021-04-08 02:38:51 -04:00
|
|
|
|
|
|
|
if (sh2lib_connect(&cfg, &hd) != 0) {
|
2017-09-26 08:02:13 -04:00
|
|
|
printf("Failed to connect\n");
|
2019-04-11 06:54:26 -04:00
|
|
|
vTaskDelete(NULL);
|
2017-09-26 08:02:13 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
printf("Connection done\n");
|
|
|
|
|
|
|
|
/* HTTP GET */
|
|
|
|
sh2lib_do_get(&hd, HTTP2_STREAMING_GET_PATH, handle_get_response);
|
|
|
|
while (1) {
|
|
|
|
/* Process HTTP2 send/receive */
|
|
|
|
if (sh2lib_execute(&hd) < 0) {
|
|
|
|
printf("Error in send/receive\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
vTaskDelay(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
sh2lib_free(&hd);
|
2018-08-02 01:11:50 -04:00
|
|
|
vTaskDelete(NULL);
|
2017-09-26 08:02:13 -04:00
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
void app_main(void)
|
2017-09-26 08:02:13 -04:00
|
|
|
{
|
|
|
|
ESP_ERROR_CHECK( nvs_flash_init() );
|
2019-11-29 04:54:02 -05:00
|
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
2019-04-11 06:54:26 -04:00
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
|
|
|
|
|
|
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
|
|
|
|
* Read "Establishing Wi-Fi or Ethernet Connection" section in
|
|
|
|
* examples/protocols/README.md for more information about this function.
|
|
|
|
*/
|
|
|
|
ESP_ERROR_CHECK(example_connect());
|
2017-09-26 08:02:13 -04:00
|
|
|
|
|
|
|
xTaskCreate(&http2_task, "http2_task", (1024 * 32), NULL, 5, NULL);
|
|
|
|
}
|