pages.dox 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /**
  2. @mainpage Overview
  3. @anchor http
  4. @brief HTTP Client library
  5. This HTTP Client library implements a subset of the HTTP/1.1 protocol. Features
  6. of this library include:
  7. - Fully synchronous API, to allow applications to completely manage their concurrency and multi-threading.
  8. - Operations on user supplied buffers, so that applications have complete control of their memory allocation strategy.
  9. - Integration with [llhttp](https://github.com/nodejs/llhttp) to handle chunked encoding.
  10. Feature of HTTP/1.1 not supported in this library:
  11. - Streaming uploads and downloads. Range requests for partial content responses are highly encouraged with this API.
  12. - Pipelining requests. There may be only one request outgoing and one response incoming, at a time, on a connection.
  13. - Automatic redirection. The user application owns their connection and must handle redirection status codes.
  14. @section http_memory_requirements Memory Requirements
  15. @brief Memory requirements of the HTTP Client library.
  16. @include{doc} size_table.md
  17. */
  18. /**
  19. @page http_design Design
  20. HTTP Client Library Architecture and Design
  21. This HTTP client library implements a subset of the HTTP/1.1 protocol. It is
  22. optimized for resource constrained devices and does not dynamically allocate any
  23. memory.
  24. @section http_transport_interface_blurb Transport Interface
  25. For decoupling from the user platform, the HTTP client library uses a
  26. transport interface. The transport interface allows the HTTP client library to
  27. send and receive data over the user's transport layer. The user platform must
  28. implement a @ref TransportInterface_t to use in @ref HTTPClient_Send.
  29. @see The [transport interface documentation](@ref http_transport_interface) for
  30. more information.
  31. @section http_request_serialization Building an HTTP Request
  32. The HTTP client library provides the following API to serialize request headers.
  33. - @ref HTTPClient_InitializeRequestHeaders
  34. - @ref HTTPClient_AddHeader
  35. - @ref HTTPClient_AddRangeHeader
  36. An application is expected to create and populate an @ref HTTPRequestInfo_t and
  37. set a buffer to use for the headers in @ref HTTPRequestHeaders_t.pBuffer. The
  38. application may first call @ref HTTPClient_InitializeRequestHeaders to populate
  39. the @ref HTTPRequestHeaders_t with the method, the path, and the host. The
  40. HTTP request will be serialized to the following when HTTPRequestInfo_t.reqFlags
  41. is zero.
  42. @code
  43. <METHOD> <PATH> HTTP/1.1\r\n
  44. User-Agent: <MY-PLATFORM>\r\n
  45. Host: <SERVER-URL>\r\n\r\n
  46. @endcode
  47. When the @ref HTTPRequestInfo_t.reqFlags has @ref HTTP_REQUEST_KEEP_ALIVE_FLAG
  48. set, then the HTTP request will be serialized to the following:
  49. @code
  50. <METHOD> <PATH> HTTP/1.1\r\n
  51. User-Agent: <MY-PLATFORM>\r\n
  52. Host: <SERVER-URL>\r\n
  53. Connection: keep-alive\r\n\r\n
  54. @endcode
  55. The user application may add more headers using @ref HTTPClient_AddHeader or
  56. @ref HTTPClient_AddRangeHeader. New headers will be appended to the end of the
  57. existing headers. Please see the following example:
  58. @code
  59. <METHOD> <PATH> HTTP/1.1\r\n
  60. User-Agent: <MY-PLATFORM>\r\n
  61. Host: <SERVER-URL>\r\n
  62. Connection: keep-alive\r\n
  63. Another-Header1: another-value1\r\n
  64. Another-Header2: another-value2\r\n
  65. Another-Header3: another-value3\r\n\r\n
  66. @endcode
  67. The user application may pass a request body into the @ref HTTPClient_Send
  68. function when the request is ready to be sent.
  69. @section http_range_support HTTP Range Requests and Partial Content Responses
  70. Range Requests are strongly encouraged for downloading a large file. Large is
  71. defined here to be a file whose total size cannot fit into the space currently
  72. available in RAM. By downloading a large file using range requests the user
  73. application can spend time processing that part of the file (for example writing
  74. to flash), then request the next part of the file. If the user application were
  75. to request the entire file at once and process it in sections from the network,
  76. the system is at a high risk for dropping packets. Dropped packets cause
  77. retransmissions in the system's transport layer. With many, there can be a
  78. negative impact on the overall system throughput, network bandwidth, and battery
  79. life of the device.
  80. Range requests are supported using @ref HTTPClient_AddRangeHeader. Please see
  81. the function documentation for more information.
  82. @section http_response_deserialization Receiving and Parsing an HTTP Response
  83. After the request headers are serialized, the user application must set a buffer
  84. to receive the HTTP response in @ref HTTPResponse_t.pBuffer.
  85. @ref HTTPClient_Send is then used to send the request and receive the response.
  86. If the request has a body it is passed as a parameter to @ref HTTPClient_Send.
  87. As soon as the response is received from the network it is parsed. The final
  88. parsed response is represented by the @ref HTTPResponse_t returned from
  89. @ref HTTPClient_Send. Parsing the HTTP response is done using
  90. [llhttp](https://github.com/nodejs/llhttp). llhttp invokes
  91. callbacks for each section in the HTTP response it finds. Using these callbacks
  92. the HTTP client library sets the members of @ref HTTPResponse_t to return from
  93. @ref HTTPClient_Send. The overall flow of @ref HTTPClient_Send is
  94. shown in the activity diagram below:
  95. @image html httpclient_send_activity_diagram.png width=50%
  96. @section http_response_headers Reading the HTTP Response Headers
  97. Upon a successful return from @ref HTTPClient_Send, the HTTP Response headers
  98. can be read from the headers found in @ref HTTPResponse_t.pHeaders. The function
  99. @ref HTTPClient_ReadHeader reads the headers from an @ref HTTPResponse_t.
  100. @ref HTTPClient_ReadHeader will re-parse the response in
  101. @ref HTTPResponse_t.pBuffer, looking for the header field of interest.
  102. Re-parsing involves using llhttp to look at each character starting from
  103. the beginning of @ref HTTPResponse_t.pBuffer until the header field of interest
  104. is found.
  105. If the user application wants to avoid re-parsing @ref HTTPResponse_t.pBuffer,
  106. then the user application may register a callback in
  107. @ref HTTPResponse_t.pHeaderParsingCallback. When the HTTP response message is
  108. first received from the network, in @ref HTTPClient_Send, llhttp is invoked
  109. to parse the response. This first parsing in @ref HTTPClient_Send will invoke
  110. @ref HTTPResponse_t.pHeaderParsingCallback for each header that is found
  111. in response. Please see the sequence diagram below for an illustration of when
  112. @ref HTTPClient_ResponseHeaderParsingCallback_t.onHeaderCallback is invoked
  113. during the operation of @ref HTTPClient_Send.
  114. @image html httpclient_send_sequence_diagram.png width=50%
  115. */
  116. /**
  117. @page http_porting Porting Guide
  118. @brief Guide for porting the HTTP client library to a new platform.
  119. To use the HTTP client library, a platform must implement the following
  120. components:
  121. 1. [Configuration Macros](@ref http_porting_config)
  122. 2. [Transport Interface](@ref http_porting_transport)
  123. @section http_porting_config Configuration Macros
  124. @brief Settings that can be set as macros in the config header
  125. `core_http_config.h`, or passed in as compiler options.
  126. @note If the custom configuration header `core_http_config.h` is not provided,
  127. then the @ref HTTP_DO_NOT_USE_CUSTOM_CONFIG macro must be defined.
  128. @see [Configurations](@ref http_config)
  129. The following macros can be configured for this library:
  130. - @ref HTTP_USER_AGENT_VALUE
  131. - @ref HTTP_SEND_RETRY_TIMEOUT_MS
  132. - @ref HTTP_RECV_RETRY_TIMEOUT_MS
  133. In addition, the following logging macros are used throughout this library:
  134. - @ref LogError
  135. - @ref LogWarn
  136. - @ref LogInfo
  137. - @ref LogDebug
  138. @section http_porting_transport Transport Interface
  139. @brief The HTTP client library relies on transport interface callbacks
  140. that must be implemented in order to send and receive packets on a network.
  141. @see The [Transport Interface](@ref http_transport_interface) documentation for
  142. more information.
  143. The transport interface API used by the HTTP client is defined in
  144. @ref transport_interface.h. A port must implement functions corresponding to the
  145. following functions pointers:
  146. - [Transport Receive](@ref TransportRecv_t): A function to receive bytes from a network.
  147. @code
  148. int32_t (* TransportRecv_t )(
  149. NetworkContext_t * pNetworkContext, void * pBuffer, size_t bytesToRecv
  150. );
  151. @endcode
  152. - [Transport Send](@ref TransportSend_t): A function to send bytes over a network.
  153. @code
  154. int32_t (* TransportSend_t )(
  155. NetworkContext_t * pNetworkContext, const void * pBuffer, size_t bytesToSend
  156. );
  157. @endcode
  158. The above two functions take in a pointer to a @ref NetworkContext_t, the type
  159. name of a `struct NetworkContext`. The NetworkContext struct must also be
  160. defined by the user's implementation, and ought to contain any information
  161. necessary to send and receive data with the @ref TransportSend_t and
  162. @ref TransportRecv_t implementations, respectively:
  163. @code
  164. struct NetworkContext {
  165. // Fields necessary for the transport implementations, e.g. a TCP socket descriptor.
  166. };
  167. @endcode
  168. @section http_porting_time Time Function
  169. @brief The HTTP library optionally relies on a function to generate millisecond
  170. timestamps, for the purpose of calculating the elapsed time when no data has
  171. been sent or received.
  172. @see @ref HTTPClient_GetCurrentTimeFunc_t
  173. Applications can supply their platform-specific function capable of generating
  174. 32-bit timestamps of millisecond resolution. These timestamps need not correspond
  175. with any real world clock; the only requirement is that the difference between two
  176. timestamps must be an accurate representation of the duration between them, in
  177. milliseconds.
  178. This function is used in conjunction with macros @ref HTTP_SEND_RETRY_TIMEOUT_MS
  179. and @ref HTTP_RECV_RETRY_TIMEOUT_MS.
  180. */
  181. /**
  182. @page http_config Configurations
  183. @brief Configurations of the HTTP Client library.
  184. <!-- @par configpagestyle allows the @section titles to be styled according to style.css -->
  185. @par configpagestyle
  186. Configuration settings are C pre-processor constants. They can be set with a \#define in the config file (core_http_config.h) or by using a compiler option such as -D in gcc.
  187. @section HTTP_DO_NOT_USE_CUSTOM_CONFIG
  188. @brief Define this macro to build the HTTP client library without the custom
  189. config file core_http_config.h.
  190. Without the custom config, the HTTP client library builds with default values of
  191. config macros defined in core_http_config_defaults.h file.
  192. If a custom config is provided, then HTTP_DO_NOT_USE_CUSTOM_CONFIG should not be
  193. defined.
  194. @section HTTP_USER_AGENT_VALUE
  195. @copydoc HTTP_USER_AGENT_VALUE
  196. @section HTTP_SEND_RETRY_TIMEOUT_MS
  197. @copydoc HTTP_SEND_RETRY_TIMEOUT_MS
  198. @section HTTP_RECV_RETRY_TIMEOUT_MS
  199. @copydoc HTTP_RECV_RETRY_TIMEOUT_MS
  200. @section http_logerror LogError
  201. @copydoc LogError
  202. @section http_logwarn LogWarn
  203. @copydoc LogWarn
  204. @section http_loginfo LogInfo
  205. @copydoc LogInfo
  206. @section http_logdebug LogDebug
  207. @copydoc LogDebug
  208. */
  209. /**
  210. @page http_functions Functions
  211. @brief Primary functions of the HTTP Client library:<br><br>
  212. @subpage httpclient_initializerequestheaders_function <br>
  213. @subpage httpclient_addheader_function <br>
  214. @subpage httpclient_addrangeheader_function <br>
  215. @subpage httpclient_send_function <br>
  216. @subpage httpclient_readheader_function <br>
  217. @subpage httpclient_strerror_function <br>
  218. @page httpclient_initializerequestheaders_function HTTPClient_InitializeRequestHeaders
  219. @snippet core_http_client.h declare_httpclient_initializerequestheaders
  220. @copydoc HTTPClient_InitializeRequestHeaders
  221. @page httpclient_addheader_function HTTPClient_AddHeader
  222. @snippet core_http_client.h declare_httpclient_addheader
  223. @copydoc HTTPClient_AddHeader
  224. @page httpclient_addrangeheader_function HTTPClient_AddRangeHeader
  225. @snippet core_http_client.h declare_httpclient_addrangeheader
  226. @copydoc HTTPClient_AddRangeHeader
  227. @page httpclient_send_function HTTPClient_Send
  228. @snippet core_http_client.h declare_httpclient_send
  229. @copydoc HTTPClient_Send
  230. @page httpclient_readheader_function HTTPClient_ReadHeader
  231. @snippet core_http_client.h declare_httpclient_readheader
  232. @copydoc HTTPClient_ReadHeader
  233. @page httpclient_strerror_function HTTPClient_strerror
  234. @snippet core_http_client.h declare_httpclient_strerror
  235. @copydoc HTTPClient_strerror
  236. */
  237. <!-- We do not use doxygen ALIASes here because there have been issues in the past versions with "^^" newlines within the alias definition. -->
  238. /**
  239. @defgroup http_enum_types Enumerated Types
  240. @brief Enumerated types of the HTTP Client library
  241. */
  242. /**
  243. @defgroup http_callback_types Callback Types
  244. @brief Callback function pointer types of the HTTP Client library
  245. */
  246. /**
  247. @defgroup http_struct_types Parameter Structures
  248. @brief Structures passed as parameters to [HTTP Client library functions](@ref http_functions)
  249. These structures are passed as parameters to library functions. Documentation for these structures will state the functions associated with each parameter structure and the purpose of each member.
  250. */
  251. /**
  252. @defgroup http_basic_types Basic Types
  253. @brief Primitive types of the HTTP Client library.
  254. */
  255. /**
  256. @defgroup http_constants Constants
  257. @brief Constants defined in the HTTP Client library
  258. */