sh2lib.h 11 KB

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