sh2lib.h 12 KB

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