sh2lib.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 == MBEDTLS_ERR_SSL_WANT_READ || rv == MBEDTLS_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 - 1)) {
  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) {
  59. /* If some data was xferred, send the number of bytes
  60. * xferred */
  61. rv = copy_offset;
  62. } else {
  63. /* If not, send the error code */
  64. rv = subrv;
  65. }
  66. break;
  67. }
  68. copy_offset += chunk_len;
  69. pending_data -= chunk_len;
  70. }
  71. return rv;
  72. }
  73. /*
  74. * The implementation of nghttp2_recv_callback type. Here we read data
  75. * from the network and write them in |buf|. The capacity of |buf| is
  76. * |length| bytes. Returns the number of bytes stored in |buf|. See
  77. * the documentation of nghttp2_recv_callback for the details.
  78. */
  79. static ssize_t callback_recv(nghttp2_session *session, uint8_t *buf,
  80. size_t length, int flags, void *user_data)
  81. {
  82. struct sh2lib_handle *hd = user_data;
  83. int rv;
  84. rv = esp_tls_conn_read(hd->http2_tls, (char *)buf, (int)length);
  85. if (rv < 0) {
  86. if (rv == MBEDTLS_ERR_SSL_WANT_READ || rv == MBEDTLS_ERR_SSL_WANT_WRITE) {
  87. rv = NGHTTP2_ERR_WOULDBLOCK;
  88. } else {
  89. rv = NGHTTP2_ERR_CALLBACK_FAILURE;
  90. }
  91. } else if (rv == 0) {
  92. rv = NGHTTP2_ERR_EOF;
  93. }
  94. return rv;
  95. }
  96. const char *sh2lib_frame_type_str(int type)
  97. {
  98. switch (type) {
  99. case NGHTTP2_HEADERS:
  100. return "HEADERS";
  101. break;
  102. case NGHTTP2_RST_STREAM:
  103. return "RST_STREAM";
  104. break;
  105. case NGHTTP2_GOAWAY:
  106. return "GOAWAY";
  107. break;
  108. case NGHTTP2_DATA:
  109. return "DATA";
  110. break;
  111. case NGHTTP2_SETTINGS:
  112. return "SETTINGS";
  113. break;
  114. case NGHTTP2_PUSH_PROMISE:
  115. return "PUSH_PROMISE";
  116. break;
  117. case NGHTTP2_PING:
  118. return "PING";
  119. break;
  120. default:
  121. return "other";
  122. break;
  123. }
  124. }
  125. static int callback_on_frame_send(nghttp2_session *session,
  126. const nghttp2_frame *frame, void *user_data)
  127. {
  128. ESP_LOGD(TAG, "[frame-send] frame type %s", sh2lib_frame_type_str(frame->hd.type));
  129. switch (frame->hd.type) {
  130. case NGHTTP2_HEADERS:
  131. if (nghttp2_session_get_stream_user_data(session, frame->hd.stream_id)) {
  132. ESP_LOGD(TAG, "[frame-send] C ----------------------------> S (HEADERS)");
  133. #if DBG_FRAME_SEND
  134. ESP_LOGD(TAG, "[frame-send] headers nv-len = %d", frame->headers.nvlen);
  135. const nghttp2_nv *nva = frame->headers.nva;
  136. size_t i;
  137. for (i = 0; i < frame->headers.nvlen; ++i) {
  138. ESP_LOGD(TAG, "[frame-send] %s : %s", nva[i].name, nva[i].value);
  139. }
  140. #endif
  141. }
  142. break;
  143. }
  144. return 0;
  145. }
  146. static int callback_on_frame_recv(nghttp2_session *session,
  147. const nghttp2_frame *frame, void *user_data)
  148. {
  149. ESP_LOGD(TAG, "[frame-recv][sid: %d] frame type %s", frame->hd.stream_id, sh2lib_frame_type_str(frame->hd.type));
  150. if (frame->hd.type != NGHTTP2_DATA) {
  151. return 0;
  152. }
  153. /* Subsequent processing only for data frame */
  154. sh2lib_frame_data_recv_cb_t data_recv_cb = nghttp2_session_get_stream_user_data(session, frame->hd.stream_id);
  155. if (data_recv_cb) {
  156. struct sh2lib_handle *h2 = user_data;
  157. (*data_recv_cb)(h2, NULL, 0, DATA_RECV_FRAME_COMPLETE);
  158. }
  159. return 0;
  160. }
  161. static int callback_on_stream_close(nghttp2_session *session, int32_t stream_id,
  162. uint32_t error_code, void *user_data)
  163. {
  164. ESP_LOGD(TAG, "[stream-close][sid %d]", stream_id);
  165. sh2lib_frame_data_recv_cb_t data_recv_cb = nghttp2_session_get_stream_user_data(session, stream_id);
  166. if (data_recv_cb) {
  167. struct sh2lib_handle *h2 = user_data;
  168. (*data_recv_cb)(h2, NULL, 0, DATA_RECV_RST_STREAM);
  169. }
  170. return 0;
  171. }
  172. static int callback_on_data_chunk_recv(nghttp2_session *session, uint8_t flags,
  173. int32_t stream_id, const uint8_t *data,
  174. size_t len, void *user_data)
  175. {
  176. sh2lib_frame_data_recv_cb_t data_recv_cb;
  177. ESP_LOGD(TAG, "[data-chunk][sid:%d]", stream_id);
  178. data_recv_cb = nghttp2_session_get_stream_user_data(session, stream_id);
  179. if (data_recv_cb) {
  180. ESP_LOGD(TAG, "[data-chunk] C <---------------------------- S (DATA chunk)"
  181. "%lu bytes",
  182. (unsigned long int)len);
  183. struct sh2lib_handle *h2 = user_data;
  184. (*data_recv_cb)(h2, (char *)data, len, 0);
  185. /* TODO: What to do with the return value: look for pause/abort */
  186. }
  187. return 0;
  188. }
  189. static int callback_on_header(nghttp2_session *session, const nghttp2_frame *frame,
  190. const uint8_t *name, size_t namelen, const uint8_t *value,
  191. size_t valuelen, uint8_t flags, void *user_data)
  192. {
  193. ESP_LOGD(TAG, "[hdr-recv][sid:%d] %s : %s", frame->hd.stream_id, name, value);
  194. return 0;
  195. }
  196. static int do_http2_connect(struct sh2lib_handle *hd)
  197. {
  198. int ret;
  199. nghttp2_session_callbacks *callbacks;
  200. nghttp2_session_callbacks_new(&callbacks);
  201. nghttp2_session_callbacks_set_send_callback(callbacks, callback_send);
  202. nghttp2_session_callbacks_set_recv_callback(callbacks, callback_recv);
  203. nghttp2_session_callbacks_set_on_frame_send_callback(callbacks, callback_on_frame_send);
  204. nghttp2_session_callbacks_set_on_frame_recv_callback(callbacks, callback_on_frame_recv);
  205. nghttp2_session_callbacks_set_on_stream_close_callback(callbacks, callback_on_stream_close);
  206. nghttp2_session_callbacks_set_on_data_chunk_recv_callback(callbacks, callback_on_data_chunk_recv);
  207. nghttp2_session_callbacks_set_on_header_callback(callbacks, callback_on_header);
  208. ret = nghttp2_session_client_new(&hd->http2_sess, callbacks, hd);
  209. if (ret != 0) {
  210. ESP_LOGE(TAG, "[sh2-connect] New http2 session failed");
  211. nghttp2_session_callbacks_del(callbacks);
  212. return -1;
  213. }
  214. nghttp2_session_callbacks_del(callbacks);
  215. /* Create the SETTINGS frame */
  216. ret = nghttp2_submit_settings(hd->http2_sess, NGHTTP2_FLAG_NONE, NULL, 0);
  217. if (ret != 0) {
  218. ESP_LOGE(TAG, "[sh2-connect] Submit settings failed");
  219. return -1;
  220. }
  221. return 0;
  222. }
  223. int sh2lib_connect(struct sh2lib_handle *hd, const char *uri)
  224. {
  225. memset(hd, 0, sizeof(*hd));
  226. const char *proto[] = {"h2", NULL};
  227. esp_tls_cfg_t tls_cfg = {
  228. .alpn_protos = proto,
  229. .non_block = true,
  230. };
  231. if ((hd->http2_tls = esp_tls_conn_http_new(uri, &tls_cfg)) == NULL) {
  232. ESP_LOGE(TAG, "[sh2-connect] esp-tls connection failed");
  233. goto error;
  234. }
  235. struct http_parser_url u;
  236. http_parser_url_init(&u);
  237. http_parser_parse_url(uri, strlen(uri), 0, &u);
  238. hd->hostname = strndup(&uri[u.field_data[UF_HOST].off], u.field_data[UF_HOST].len);
  239. /* HTTP/2 Connection */
  240. if (do_http2_connect(hd) != 0) {
  241. ESP_LOGE(TAG, "[sh2-connect] HTTP2 Connection failed with %s", uri);
  242. goto error;
  243. }
  244. return 0;
  245. error:
  246. sh2lib_free(hd);
  247. return -1;
  248. }
  249. void sh2lib_free(struct sh2lib_handle *hd)
  250. {
  251. if (hd->http2_sess) {
  252. nghttp2_session_del(hd->http2_sess);
  253. hd->http2_sess = NULL;
  254. }
  255. if (hd->http2_tls) {
  256. esp_tls_conn_delete(hd->http2_tls);
  257. hd->http2_tls = NULL;
  258. }
  259. if (hd->hostname) {
  260. free(hd->hostname);
  261. hd->hostname = NULL;
  262. }
  263. }
  264. int sh2lib_execute(struct sh2lib_handle *hd)
  265. {
  266. int ret;
  267. ret = nghttp2_session_send(hd->http2_sess);
  268. if (ret != 0) {
  269. ESP_LOGE(TAG, "[sh2-execute] HTTP2 session send failed %d", ret);
  270. return -1;
  271. }
  272. ret = nghttp2_session_recv(hd->http2_sess);
  273. if (ret != 0) {
  274. ESP_LOGE(TAG, "[sh2-execute] HTTP2 session recv failed %d", ret);
  275. return -1;
  276. }
  277. return 0;
  278. }
  279. 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)
  280. {
  281. int ret = nghttp2_submit_request(hd->http2_sess, NULL, nva, nvlen, NULL, recv_cb);
  282. if (ret < 0) {
  283. ESP_LOGE(TAG, "[sh2-do-get] HEADERS call failed");
  284. return -1;
  285. }
  286. return ret;
  287. }
  288. int sh2lib_do_get(struct sh2lib_handle *hd, const char *path, sh2lib_frame_data_recv_cb_t recv_cb)
  289. {
  290. const nghttp2_nv nva[] = { SH2LIB_MAKE_NV(":method", "GET"),
  291. SH2LIB_MAKE_NV(":scheme", "https"),
  292. SH2LIB_MAKE_NV(":authority", hd->hostname),
  293. SH2LIB_MAKE_NV(":path", path),
  294. };
  295. return sh2lib_do_get_with_nv(hd, nva, sizeof(nva) / sizeof(nva[0]), recv_cb);
  296. }
  297. ssize_t sh2lib_data_provider_cb(nghttp2_session *session, int32_t stream_id, uint8_t *buf,
  298. size_t length, uint32_t *data_flags,
  299. nghttp2_data_source *source, void *user_data)
  300. {
  301. struct sh2lib_handle *h2 = user_data;
  302. sh2lib_putpost_data_cb_t data_cb = source->ptr;
  303. return (*data_cb)(h2, (char *)buf, length, data_flags);
  304. }
  305. int sh2lib_do_putpost_with_nv(struct sh2lib_handle *hd, const nghttp2_nv *nva, size_t nvlen,
  306. sh2lib_putpost_data_cb_t send_cb,
  307. sh2lib_frame_data_recv_cb_t recv_cb)
  308. {
  309. nghttp2_data_provider sh2lib_data_provider;
  310. sh2lib_data_provider.read_callback = sh2lib_data_provider_cb;
  311. sh2lib_data_provider.source.ptr = send_cb;
  312. int ret = nghttp2_submit_request(hd->http2_sess, NULL, nva, nvlen, &sh2lib_data_provider, recv_cb);
  313. if (ret < 0) {
  314. ESP_LOGE(TAG, "[sh2-do-putpost] HEADERS call failed");
  315. return -1;
  316. }
  317. return ret;
  318. }
  319. int sh2lib_do_post(struct sh2lib_handle *hd, const char *path,
  320. sh2lib_putpost_data_cb_t send_cb,
  321. sh2lib_frame_data_recv_cb_t recv_cb)
  322. {
  323. const nghttp2_nv nva[] = { SH2LIB_MAKE_NV(":method", "POST"),
  324. SH2LIB_MAKE_NV(":scheme", "https"),
  325. SH2LIB_MAKE_NV(":authority", hd->hostname),
  326. SH2LIB_MAKE_NV(":path", path),
  327. };
  328. return sh2lib_do_putpost_with_nv(hd, nva, sizeof(nva) / sizeof(nva[0]), send_cb, recv_cb);
  329. }
  330. int sh2lib_do_put(struct sh2lib_handle *hd, const char *path,
  331. sh2lib_putpost_data_cb_t send_cb,
  332. sh2lib_frame_data_recv_cb_t recv_cb)
  333. {
  334. const nghttp2_nv nva[] = { SH2LIB_MAKE_NV(":method", "PUT"),
  335. SH2LIB_MAKE_NV(":scheme", "https"),
  336. SH2LIB_MAKE_NV(":authority", hd->hostname),
  337. SH2LIB_MAKE_NV(":path", path),
  338. };
  339. return sh2lib_do_putpost_with_nv(hd, nva, sizeof(nva) / sizeof(nva[0]), send_cb, recv_cb);
  340. }