sh2lib.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. // Copyright 2017 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdlib.h>
  15. #include <stdint.h>
  16. #include <stddef.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include <ctype.h>
  21. #include <netdb.h>
  22. #include <esp_log.h>
  23. #include <http_parser.h>
  24. #include "sh2lib.h"
  25. static const char *TAG = "sh2lib";
  26. #define DBG_FRAME_SEND 1
  27. /*
  28. * The implementation of nghttp2_send_callback type. Here we write
  29. * |data| with size |length| to the network and return the number of
  30. * bytes actually written. See the documentation of
  31. * nghttp2_send_callback for the details.
  32. */
  33. static ssize_t callback_send_inner(struct sh2lib_handle *hd, const uint8_t *data,
  34. size_t length)
  35. {
  36. int rv = esp_tls_conn_write(hd->http2_tls, data, length);
  37. if (rv <= 0) {
  38. if (rv == ESP_TLS_ERR_SSL_WANT_READ || rv == ESP_TLS_ERR_SSL_WANT_WRITE) {
  39. rv = NGHTTP2_ERR_WOULDBLOCK;
  40. } else {
  41. rv = NGHTTP2_ERR_CALLBACK_FAILURE;
  42. }
  43. }
  44. return rv;
  45. }
  46. static ssize_t callback_send(nghttp2_session *session, const uint8_t *data,
  47. size_t length, int flags, void *user_data)
  48. {
  49. int rv = 0;
  50. struct sh2lib_handle *hd = user_data;
  51. int copy_offset = 0;
  52. int pending_data = length;
  53. /* Send data in 1000 byte chunks */
  54. while (copy_offset != length) {
  55. int chunk_len = pending_data > 1000 ? 1000 : pending_data;
  56. int subrv = callback_send_inner(hd, data + copy_offset, chunk_len);
  57. if (subrv <= 0) {
  58. if (copy_offset == 0) {
  59. /* If no data is transferred, send the error code */
  60. rv = subrv;
  61. }
  62. break;
  63. }
  64. copy_offset += subrv;
  65. pending_data -= subrv;
  66. rv += subrv;
  67. }
  68. return rv;
  69. }
  70. /*
  71. * The implementation of nghttp2_recv_callback type. Here we read data
  72. * from the network and write them in |buf|. The capacity of |buf| is
  73. * |length| bytes. Returns the number of bytes stored in |buf|. See
  74. * the documentation of nghttp2_recv_callback for the details.
  75. */
  76. static ssize_t callback_recv(nghttp2_session *session, uint8_t *buf,
  77. size_t length, int flags, void *user_data)
  78. {
  79. struct sh2lib_handle *hd = user_data;
  80. int rv;
  81. rv = esp_tls_conn_read(hd->http2_tls, (char *)buf, (int)length);
  82. if (rv < 0) {
  83. if (rv == ESP_TLS_ERR_SSL_WANT_READ || rv == ESP_TLS_ERR_SSL_WANT_WRITE) {
  84. rv = NGHTTP2_ERR_WOULDBLOCK;
  85. } else {
  86. rv = NGHTTP2_ERR_CALLBACK_FAILURE;
  87. }
  88. } else if (rv == 0) {
  89. rv = NGHTTP2_ERR_EOF;
  90. }
  91. return rv;
  92. }
  93. const char *sh2lib_frame_type_str(int type)
  94. {
  95. switch (type) {
  96. case NGHTTP2_HEADERS:
  97. return "HEADERS";
  98. break;
  99. case NGHTTP2_RST_STREAM:
  100. return "RST_STREAM";
  101. break;
  102. case NGHTTP2_GOAWAY:
  103. return "GOAWAY";
  104. break;
  105. case NGHTTP2_DATA:
  106. return "DATA";
  107. break;
  108. case NGHTTP2_SETTINGS:
  109. return "SETTINGS";
  110. break;
  111. case NGHTTP2_PUSH_PROMISE:
  112. return "PUSH_PROMISE";
  113. break;
  114. case NGHTTP2_PING:
  115. return "PING";
  116. break;
  117. default:
  118. return "other";
  119. break;
  120. }
  121. }
  122. static int callback_on_frame_send(nghttp2_session *session,
  123. const nghttp2_frame *frame, void *user_data)
  124. {
  125. ESP_LOGD(TAG, "[frame-send] frame type %s", sh2lib_frame_type_str(frame->hd.type));
  126. switch (frame->hd.type) {
  127. case NGHTTP2_HEADERS:
  128. if (nghttp2_session_get_stream_user_data(session, frame->hd.stream_id)) {
  129. ESP_LOGD(TAG, "[frame-send] C ----------------------------> S (HEADERS)");
  130. #if DBG_FRAME_SEND
  131. ESP_LOGD(TAG, "[frame-send] headers nv-len = %d", frame->headers.nvlen);
  132. const nghttp2_nv *nva = frame->headers.nva;
  133. size_t i;
  134. for (i = 0; i < frame->headers.nvlen; ++i) {
  135. ESP_LOGD(TAG, "[frame-send] %s : %s", nva[i].name, nva[i].value);
  136. }
  137. #endif
  138. }
  139. break;
  140. }
  141. return 0;
  142. }
  143. static int callback_on_frame_recv(nghttp2_session *session,
  144. const nghttp2_frame *frame, void *user_data)
  145. {
  146. ESP_LOGD(TAG, "[frame-recv][sid: %d] frame type %s", frame->hd.stream_id, sh2lib_frame_type_str(frame->hd.type));
  147. if (frame->hd.type != NGHTTP2_DATA) {
  148. return 0;
  149. }
  150. /* Subsequent processing only for data frame */
  151. sh2lib_frame_data_recv_cb_t data_recv_cb = nghttp2_session_get_stream_user_data(session, frame->hd.stream_id);
  152. if (data_recv_cb) {
  153. struct sh2lib_handle *h2 = user_data;
  154. (*data_recv_cb)(h2, NULL, 0, DATA_RECV_FRAME_COMPLETE);
  155. }
  156. return 0;
  157. }
  158. static int callback_on_stream_close(nghttp2_session *session, int32_t stream_id,
  159. uint32_t error_code, void *user_data)
  160. {
  161. ESP_LOGD(TAG, "[stream-close][sid %d]", stream_id);
  162. sh2lib_frame_data_recv_cb_t data_recv_cb = nghttp2_session_get_stream_user_data(session, stream_id);
  163. if (data_recv_cb) {
  164. struct sh2lib_handle *h2 = user_data;
  165. (*data_recv_cb)(h2, NULL, 0, DATA_RECV_RST_STREAM);
  166. }
  167. return 0;
  168. }
  169. static int callback_on_data_chunk_recv(nghttp2_session *session, uint8_t flags,
  170. int32_t stream_id, const uint8_t *data,
  171. size_t len, void *user_data)
  172. {
  173. sh2lib_frame_data_recv_cb_t data_recv_cb;
  174. ESP_LOGD(TAG, "[data-chunk][sid:%d]", stream_id);
  175. data_recv_cb = nghttp2_session_get_stream_user_data(session, stream_id);
  176. if (data_recv_cb) {
  177. ESP_LOGD(TAG, "[data-chunk] C <---------------------------- S (DATA chunk)"
  178. "%lu bytes",
  179. (unsigned long int)len);
  180. struct sh2lib_handle *h2 = user_data;
  181. (*data_recv_cb)(h2, (char *)data, len, 0);
  182. /* TODO: What to do with the return value: look for pause/abort */
  183. }
  184. return 0;
  185. }
  186. static int callback_on_header(nghttp2_session *session, const nghttp2_frame *frame,
  187. const uint8_t *name, size_t namelen, const uint8_t *value,
  188. size_t valuelen, uint8_t flags, void *user_data)
  189. {
  190. ESP_LOGD(TAG, "[hdr-recv][sid:%d] %s : %s", frame->hd.stream_id, name, value);
  191. return 0;
  192. }
  193. static int do_http2_connect(struct sh2lib_handle *hd)
  194. {
  195. int ret;
  196. nghttp2_session_callbacks *callbacks;
  197. nghttp2_session_callbacks_new(&callbacks);
  198. nghttp2_session_callbacks_set_send_callback(callbacks, callback_send);
  199. nghttp2_session_callbacks_set_recv_callback(callbacks, callback_recv);
  200. nghttp2_session_callbacks_set_on_frame_send_callback(callbacks, callback_on_frame_send);
  201. nghttp2_session_callbacks_set_on_frame_recv_callback(callbacks, callback_on_frame_recv);
  202. nghttp2_session_callbacks_set_on_stream_close_callback(callbacks, callback_on_stream_close);
  203. nghttp2_session_callbacks_set_on_data_chunk_recv_callback(callbacks, callback_on_data_chunk_recv);
  204. nghttp2_session_callbacks_set_on_header_callback(callbacks, callback_on_header);
  205. ret = nghttp2_session_client_new(&hd->http2_sess, callbacks, hd);
  206. if (ret != 0) {
  207. ESP_LOGE(TAG, "[sh2-connect] New http2 session failed");
  208. nghttp2_session_callbacks_del(callbacks);
  209. return -1;
  210. }
  211. nghttp2_session_callbacks_del(callbacks);
  212. /* Create the SETTINGS frame */
  213. ret = nghttp2_submit_settings(hd->http2_sess, NGHTTP2_FLAG_NONE, NULL, 0);
  214. if (ret != 0) {
  215. ESP_LOGE(TAG, "[sh2-connect] Submit settings failed");
  216. return -1;
  217. }
  218. return 0;
  219. }
  220. int sh2lib_connect(struct sh2lib_handle *hd, const char *uri)
  221. {
  222. memset(hd, 0, sizeof(*hd));
  223. const char *proto[] = {"h2", NULL};
  224. esp_tls_cfg_t tls_cfg = {
  225. .alpn_protos = proto,
  226. .non_block = true,
  227. .timeout_ms = 10 * 1000,
  228. };
  229. if ((hd->http2_tls = esp_tls_conn_http_new(uri, &tls_cfg)) == NULL) {
  230. ESP_LOGE(TAG, "[sh2-connect] esp-tls connection failed");
  231. goto error;
  232. }
  233. struct http_parser_url u;
  234. http_parser_url_init(&u);
  235. http_parser_parse_url(uri, strlen(uri), 0, &u);
  236. hd->hostname = strndup(&uri[u.field_data[UF_HOST].off], u.field_data[UF_HOST].len);
  237. /* HTTP/2 Connection */
  238. if (do_http2_connect(hd) != 0) {
  239. ESP_LOGE(TAG, "[sh2-connect] HTTP2 Connection failed with %s", uri);
  240. goto error;
  241. }
  242. return 0;
  243. error:
  244. sh2lib_free(hd);
  245. return -1;
  246. }
  247. void sh2lib_free(struct sh2lib_handle *hd)
  248. {
  249. if (hd->http2_sess) {
  250. nghttp2_session_del(hd->http2_sess);
  251. hd->http2_sess = NULL;
  252. }
  253. if (hd->http2_tls) {
  254. esp_tls_conn_delete(hd->http2_tls);
  255. hd->http2_tls = NULL;
  256. }
  257. if (hd->hostname) {
  258. free(hd->hostname);
  259. hd->hostname = NULL;
  260. }
  261. }
  262. int sh2lib_execute(struct sh2lib_handle *hd)
  263. {
  264. int ret;
  265. ret = nghttp2_session_send(hd->http2_sess);
  266. if (ret != 0) {
  267. ESP_LOGE(TAG, "[sh2-execute] HTTP2 session send failed %d", ret);
  268. return -1;
  269. }
  270. ret = nghttp2_session_recv(hd->http2_sess);
  271. if (ret != 0) {
  272. ESP_LOGE(TAG, "[sh2-execute] HTTP2 session recv failed %d", ret);
  273. return -1;
  274. }
  275. return 0;
  276. }
  277. int sh2lib_do_get_with_nv(struct sh2lib_handle *hd, const nghttp2_nv *nva, size_t nvlen, sh2lib_frame_data_recv_cb_t recv_cb)
  278. {
  279. int ret = nghttp2_submit_request(hd->http2_sess, NULL, nva, nvlen, NULL, recv_cb);
  280. if (ret < 0) {
  281. ESP_LOGE(TAG, "[sh2-do-get] HEADERS call failed");
  282. return -1;
  283. }
  284. return ret;
  285. }
  286. int sh2lib_do_get(struct sh2lib_handle *hd, const char *path, sh2lib_frame_data_recv_cb_t recv_cb)
  287. {
  288. const nghttp2_nv nva[] = { SH2LIB_MAKE_NV(":method", "GET"),
  289. SH2LIB_MAKE_NV(":scheme", "https"),
  290. SH2LIB_MAKE_NV(":authority", hd->hostname),
  291. SH2LIB_MAKE_NV(":path", path),
  292. };
  293. return sh2lib_do_get_with_nv(hd, nva, sizeof(nva) / sizeof(nva[0]), recv_cb);
  294. }
  295. ssize_t sh2lib_data_provider_cb(nghttp2_session *session, int32_t stream_id, uint8_t *buf,
  296. size_t length, uint32_t *data_flags,
  297. nghttp2_data_source *source, void *user_data)
  298. {
  299. struct sh2lib_handle *h2 = user_data;
  300. sh2lib_putpost_data_cb_t data_cb = source->ptr;
  301. return (*data_cb)(h2, (char *)buf, length, data_flags);
  302. }
  303. int sh2lib_do_putpost_with_nv(struct sh2lib_handle *hd, const nghttp2_nv *nva, size_t nvlen,
  304. sh2lib_putpost_data_cb_t send_cb,
  305. sh2lib_frame_data_recv_cb_t recv_cb)
  306. {
  307. nghttp2_data_provider sh2lib_data_provider;
  308. sh2lib_data_provider.read_callback = sh2lib_data_provider_cb;
  309. sh2lib_data_provider.source.ptr = send_cb;
  310. int ret = nghttp2_submit_request(hd->http2_sess, NULL, nva, nvlen, &sh2lib_data_provider, recv_cb);
  311. if (ret < 0) {
  312. ESP_LOGE(TAG, "[sh2-do-putpost] HEADERS call failed");
  313. return -1;
  314. }
  315. return ret;
  316. }
  317. int sh2lib_do_post(struct sh2lib_handle *hd, const char *path,
  318. sh2lib_putpost_data_cb_t send_cb,
  319. sh2lib_frame_data_recv_cb_t recv_cb)
  320. {
  321. const nghttp2_nv nva[] = { SH2LIB_MAKE_NV(":method", "POST"),
  322. SH2LIB_MAKE_NV(":scheme", "https"),
  323. SH2LIB_MAKE_NV(":authority", hd->hostname),
  324. SH2LIB_MAKE_NV(":path", path),
  325. };
  326. return sh2lib_do_putpost_with_nv(hd, nva, sizeof(nva) / sizeof(nva[0]), send_cb, recv_cb);
  327. }
  328. int sh2lib_do_put(struct sh2lib_handle *hd, const char *path,
  329. sh2lib_putpost_data_cb_t send_cb,
  330. sh2lib_frame_data_recv_cb_t recv_cb)
  331. {
  332. const nghttp2_nv nva[] = { SH2LIB_MAKE_NV(":method", "PUT"),
  333. SH2LIB_MAKE_NV(":scheme", "https"),
  334. SH2LIB_MAKE_NV(":authority", hd->hostname),
  335. SH2LIB_MAKE_NV(":path", path),
  336. };
  337. return sh2lib_do_putpost_with_nv(hd, nva, sizeof(nva) / sizeof(nva[0]), send_cb, recv_cb);
  338. }