sh2lib.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. #ifndef __ESP_EXAMPLE_SH2_LIB_H_
  15. #define __ESP_EXAMPLE_SH2_LIB_H_
  16. #include "esp_tls.h"
  17. #include <nghttp2/nghttp2.h>
  18. /*
  19. * This is a thin API wrapper over nghttp2 that offers simplified APIs for usage
  20. * in the application. The intention of this wrapper is to act as a stepping
  21. * stone to quickly get started with using the HTTP/2 client. Since the focus is
  22. * on simplicity, not all the features of HTTP/2 are supported through this
  23. * wrapper. Once you are fairly comfortable with nghttp2, feel free to directly
  24. * use nghttp2 APIs or make changes to sh2lib.c for realising your objectives.
  25. *
  26. * TODO:
  27. * - Allowing to query response code, content-type etc in the receive callback
  28. * - A simple function for per-stream header callback
  29. */
  30. /**
  31. * @brief Handle for working with sh2lib APIs
  32. */
  33. struct sh2lib_handle {
  34. nghttp2_session *http2_sess; /*!< Pointer to the HTTP2 session handle */
  35. char *hostname; /*!< The hostname we are connected to */
  36. struct esp_tls *http2_tls; /*!< Pointer to the TLS session handle */
  37. };
  38. /** Flag indicating receive stream is reset */
  39. #define DATA_RECV_RST_STREAM 1
  40. /** Flag indicating frame is completely received */
  41. #define DATA_RECV_FRAME_COMPLETE 2
  42. /**
  43. * @brief Function Prototype for data receive callback
  44. *
  45. * This function gets called whenever data is received on any stream. The
  46. * function is also called for indicating events like frame receive complete, or
  47. * end of stream. The function may get called multiple times as long as data is
  48. * received on the stream.
  49. *
  50. * @param[in] handle Pointer to the sh2lib handle.
  51. * @param[in] data Pointer to a buffer that contains the data received.
  52. * @param[in] len The length of valid data stored at the 'data' pointer.
  53. * @param[in] flags Flags indicating whether the stream is reset (DATA_RECV_RST_STREAM) or
  54. * this particularly frame is completely received
  55. * DATA_RECV_FRAME_COMPLETE).
  56. *
  57. * @return The function should return 0
  58. */
  59. typedef int (*sh2lib_frame_data_recv_cb_t)(struct sh2lib_handle *handle, const char *data, size_t len, int flags);
  60. /**
  61. * @brief Function Prototype for callback to send data in PUT/POST
  62. *
  63. * This function gets called whenever nghttp2 wishes to send data, like for
  64. * PUT/POST requests to the server. The function keeps getting called until this
  65. * function sets the flag NGHTTP2_DATA_FLAG_EOF to indicate end of data.
  66. *
  67. * @param[in] handle Pointer to the sh2lib handle.
  68. * @param[out] data Pointer to a buffer that should contain the data to send.
  69. * @param[in] len The maximum length of data that can be sent out by this function.
  70. * @param[out] data_flags Pointer to the data flags. The NGHTTP2_DATA_FLAG_EOF
  71. * should be set in the data flags to indicate end of new data.
  72. *
  73. * @return The function should return the number of valid bytes stored in the
  74. * data pointer
  75. */
  76. typedef int (*sh2lib_putpost_data_cb_t)(struct sh2lib_handle *handle, char *data, size_t len, uint32_t *data_flags);
  77. /**
  78. * @brief Connect to a URI using HTTP/2
  79. *
  80. * This API opens an HTTP/2 connection with the provided URI. If successful, the
  81. * hd pointer is populated with a valid handle for subsequent communication.
  82. *
  83. * Only 'https' URIs are supported.
  84. *
  85. * @param[out] hd Pointer to a variable of the type 'struct sh2lib_handle'.
  86. * @param[in] uri Pointer to the URI that should be connected to.
  87. *
  88. * @return
  89. * - ESP_OK if the connection was successful
  90. * - ESP_FAIL if the connection fails
  91. */
  92. int sh2lib_connect(struct sh2lib_handle *hd, const char *uri);
  93. /**
  94. * @brief Free a sh2lib handle
  95. *
  96. * This API frees-up an sh2lib handle, thus closing any open connections that
  97. * may be associated with this handle, and freeing up any resources.
  98. *
  99. * @param[in] hd Pointer to a variable of the type 'struct sh2lib_handle'.
  100. *
  101. */
  102. void sh2lib_free(struct sh2lib_handle *hd);
  103. /**
  104. * @brief Setup an HTTP GET request stream
  105. *
  106. * This API sets up an HTTP GET request to be sent out to the server. A new
  107. * stream is created for handling the request. Once the request is setup, the
  108. * API sh2lib_execute() must be called to actually perform the socket I/O with
  109. * the server.
  110. *
  111. * @param[in] hd Pointer to a variable of the type 'struct sh2lib_handle'.
  112. * @param[in] path Pointer to the string that contains the resource to
  113. * perform the HTTP GET operation on (for example, /users).
  114. * @param[in] recv_cb The callback function that should be called for
  115. * processing the request's response
  116. *
  117. * @return
  118. * - ESP_OK if request setup is successful
  119. * - ESP_FAIL if the request setup fails
  120. */
  121. int sh2lib_do_get(struct sh2lib_handle *hd, const char *path, sh2lib_frame_data_recv_cb_t recv_cb);
  122. /**
  123. * @brief Setup an HTTP POST request stream
  124. *
  125. * This API sets up an HTTP POST request to be sent out to the server. A new
  126. * stream is created for handling the request. Once the request is setup, the
  127. * API sh2lib_execute() must be called to actually perform the socket I/O with
  128. * the server.
  129. *
  130. * @param[in] hd Pointer to a variable of the type 'struct sh2lib_handle'.
  131. * @param[in] path Pointer to the string that contains the resource to
  132. * perform the HTTP POST operation on (for example, /users).
  133. * @param[in] send_cb The callback function that should be called for
  134. * sending data as part of this request.
  135. * @param[in] recv_cb The callback function that should be called for
  136. * processing the request's response
  137. *
  138. * @return
  139. * - ESP_OK if request setup is successful
  140. * - ESP_FAIL if the request setup fails
  141. */
  142. int sh2lib_do_post(struct sh2lib_handle *hd, const char *path,
  143. sh2lib_putpost_data_cb_t send_cb,
  144. sh2lib_frame_data_recv_cb_t recv_cb);
  145. /**
  146. * @brief Setup an HTTP PUT request stream
  147. *
  148. * This API sets up an HTTP PUT request to be sent out to the server. A new
  149. * stream is created for handling the request. Once the request is setup, the
  150. * API sh2lib_execute() must be called to actually perform the socket I/O with
  151. * the server.
  152. *
  153. * @param[in] hd Pointer to a variable of the type 'struct sh2lib_handle'.
  154. * @param[in] path Pointer to the string that contains the resource to
  155. * perform the HTTP PUT operation on (for example, /users).
  156. * @param[in] send_cb The callback function that should be called for
  157. * sending data as part of this request.
  158. * @param[in] recv_cb The callback function that should be called for
  159. * processing the request's response
  160. *
  161. * @return
  162. * - ESP_OK if request setup is successful
  163. * - ESP_FAIL if the request setup fails
  164. */
  165. int sh2lib_do_put(struct sh2lib_handle *hd, const char *path,
  166. sh2lib_putpost_data_cb_t send_cb,
  167. sh2lib_frame_data_recv_cb_t recv_cb);
  168. /**
  169. * @brief Execute send/receive on an HTTP/2 connection
  170. *
  171. * While the API sh2lib_do_get(), sh2lib_do_post() setup the requests to be
  172. * initiated with the server, this API performs the actual data send/receive
  173. * operations on the HTTP/2 connection. The callback functions are accordingly
  174. * called during the processing of these requests.
  175. *
  176. * @param[in] hd Pointer to a variable of the type 'struct sh2lib_handle'
  177. *
  178. * @return
  179. * - ESP_OK if the connection was successful
  180. * - ESP_FAIL if the connection fails
  181. */
  182. int sh2lib_execute(struct sh2lib_handle *hd);
  183. #define SH2LIB_MAKE_NV(NAME, VALUE) \
  184. { \
  185. (uint8_t *)NAME, (uint8_t *)VALUE, strlen(NAME), strlen(VALUE), \
  186. NGHTTP2_NV_FLAG_NONE \
  187. }
  188. /**
  189. * @brief Setup an HTTP GET request stream with custom name-value pairs
  190. *
  191. * For a simpler version of the API, please refer to sh2lib_do_get().
  192. *
  193. * This API sets up an HTTP GET request to be sent out to the server. A new
  194. * stream is created for handling the request. Once the request is setup, the
  195. * API sh2lib_execute() must be called to actually perform the socket I/O with
  196. * the server.
  197. *
  198. * Please note that the following name value pairs MUST be a part of the request
  199. * - name:value
  200. * - ":method":"GET"
  201. * - ":scheme":"https"
  202. * - ":path":<the-path-for-the-GET-operation> (for example, /users)
  203. *
  204. * @param[in] hd Pointer to a variable of the type 'struct sh2lib_handle'.
  205. * @param[in] nva An array of name-value pairs that should be part of the request.
  206. * @param[in] nvlen The number of elements in the array pointed to by 'nva'.
  207. * @param[in] recv_cb The callback function that should be called for
  208. * processing the request's response
  209. *
  210. * @return
  211. * - ESP_OK if request setup is successful
  212. * - ESP_FAIL if the request setup fails
  213. */
  214. 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);
  215. /**
  216. * @brief Setup an HTTP PUT/POST request stream with custom name-value pairs
  217. *
  218. * For a simpler version of the API, please refer to sh2lib_do_put() or
  219. * sh2lib_do_post().
  220. *
  221. * This API sets up an HTTP PUT/POST request to be sent out to the server. A new
  222. * stream is created for handling the request. Once the request is setup, the
  223. * API sh2lib_execute() must be called to actually perform the socket I/O with
  224. * the server.
  225. *
  226. * Please note that the following name value pairs MUST be a part of the request
  227. * - name:value
  228. * - ":method":"PUT" (or POST)
  229. * - ":scheme":"https"
  230. * - ":path":<the-path-for-the-PUT-operation> (for example, /users)
  231. *
  232. * @param[in] hd Pointer to a variable of the type 'struct sh2lib_handle'.
  233. * @param[in] nva An array of name-value pairs that should be part of the request.
  234. * @param[in] nvlen The number of elements in the array pointed to by 'nva'.
  235. * @param[in] send_cb The callback function that should be called for
  236. * sending data as part of this request.
  237. * @param[in] recv_cb The callback function that should be called for
  238. * processing the request's response
  239. *
  240. * @return
  241. * - ESP_OK if request setup is successful
  242. * - ESP_FAIL if the request setup fails
  243. */
  244. int sh2lib_do_putpost_with_nv(struct sh2lib_handle *hd, const nghttp2_nv *nva, size_t nvlen,
  245. sh2lib_putpost_data_cb_t send_cb,
  246. sh2lib_frame_data_recv_cb_t recv_cb);
  247. #endif /* ! __ESP_EXAMPLE_SH2_LIB_H_ */