sh2lib.h 11 KB

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