sh2lib.h 11 KB

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