coreHTTP  v2.0.2
HTTP/1.1 Client Library
core_http_client.h
Go to the documentation of this file.
1 /*
2  * coreHTTP v2.0.2
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
28 #ifndef CORE_HTTP_CLIENT_H_
29 #define CORE_HTTP_CLIENT_H_
30 
31 #include <stdint.h>
32 #include <stddef.h>
33 
34 /* *INDENT-OFF* */
35 #ifdef __cplusplus
36  extern "C" {
37 #endif
38 /* *INDENT-ON* */
39 
40 /* HTTP_DO_NOT_USE_CUSTOM_CONFIG allows building the HTTP Client library
41  * without a config file. If a config file is provided, the
42  * HTTP_DO_NOT_USE_CUSTOM_CONFIG macro must not be defined.
43  */
44 #ifndef HTTP_DO_NOT_USE_CUSTOM_CONFIG
45  #include "core_http_config.h"
46 #endif
47 
48 /* Include config defaults header to get default values of configurations not
49  * defined in core_http_config.h file. */
51 
52 /* Transport interface include. */
53 #include "transport_interface.h"
54 
55 /* Convenience macros for some HTTP request methods. */
56 
60 #define HTTP_METHOD_GET "GET"
61 #define HTTP_METHOD_PUT "PUT"
62 #define HTTP_METHOD_POST "POST"
63 #define HTTP_METHOD_HEAD "HEAD"
71 #define HTTP_MAX_CONTENT_LENGTH_HEADER_LENGTH sizeof( "Content-Length: 4294967295" ) - 1U
72 
90 #define HTTP_SEND_DISABLE_CONTENT_LENGTH_FLAG 0x1U
91 
112 #define HTTP_REQUEST_KEEP_ALIVE_FLAG 0x1U
113 
133 #define HTTP_RESPONSE_CONNECTION_CLOSE_FLAG 0x1U
134 
141 #define HTTP_RESPONSE_CONNECTION_KEEP_ALIVE_FLAG 0x2U
142 
155 #define HTTP_RANGE_REQUEST_END_OF_FILE -1
156 
161 typedef enum HTTPStatus
162 {
174 
186 
194 
202 
213 
225 
234 
243 
251 
260 
269 
279 
289 
298 
306 
316 
328 typedef struct HTTPRequestHeaders
329 {
344  uint8_t * pBuffer;
345  size_t bufferLen;
352  size_t headersLen;
354 
359 typedef struct HTTPRequestInfo
360 {
364  const char * pMethod;
365  size_t methodLen;
370  const char * pPath;
371  size_t pathLen;
378  const char * pHost;
379  size_t hostLen;
386  uint32_t reqFlags;
388 
389 
390 
396 typedef struct HTTPClient_ResponseHeaderParsingCallback
397 {
407  void ( * onHeaderCallback )( void * pContext,
408  const char * fieldLoc,
409  size_t fieldLen,
410  const char * valueLoc,
411  size_t valueLen,
412  uint16_t statusCode );
413 
417  void * pContext;
419 
427 typedef uint32_t (* HTTPClient_GetCurrentTimeFunc_t )( void );
428 
433 typedef struct HTTPResponse
434 {
450  uint8_t * pBuffer;
451  size_t bufferLen;
459 
475 
481  const uint8_t * pHeaders;
482 
488  size_t headersLen;
489 
495  const uint8_t * pBody;
496 
502  size_t bodyLen;
503 
504  /* Useful HTTP header values found. */
505 
511  uint16_t statusCode;
512 
519 
525  size_t headerCount;
526 
533  uint32_t respFlags;
535 
587 /* @[declare_httpclient_initializerequestheaders] */
589  const HTTPRequestInfo_t * pRequestInfo );
590 /* @[declare_httpclient_initializerequestheaders] */
591 
641 /* @[declare_httpclient_addheader] */
643  const char * pField,
644  size_t fieldLen,
645  const char * pValue,
646  size_t valueLen );
647 /* @[declare_httpclient_addheader] */
648 
723 /* @[declare_httpclient_addrangeheader] */
725  int32_t rangeStartOrlastNbytes,
726  int32_t rangeEnd );
727 /* @[declare_httpclient_addrangeheader] */
728 
824 /* @[declare_httpclient_send] */
826  HTTPRequestHeaders_t * pRequestHeaders,
827  const uint8_t * pRequestBodyBuf,
828  size_t reqBodyBufLen,
829  HTTPResponse_t * pResponse,
830  uint32_t sendFlags );
831 /* @[declare_httpclient_send] */
832 
881 /* @[declare_httpclient_readheader] */
883  const char * pField,
884  size_t fieldLen,
885  const char ** pValueLoc,
886  size_t * pValueLen );
887 /* @[declare_httpclient_readheader] */
888 
898 /* @[declare_httpclient_strerror] */
899 const char * HTTPClient_strerror( HTTPStatus_t status );
900 /* @[declare_httpclient_strerror] */
901 
902 /* *INDENT-OFF* */
903 #ifdef __cplusplus
904  }
905 #endif
906 /* *INDENT-ON* */
907 
908 #endif /* ifndef CORE_HTTP_CLIENT_H_ */
HTTPSecurityAlertInvalidStatusCode
@ HTTPSecurityAlertInvalidStatusCode
The server sent a response with an invalid character in the HTTP status-code or the HTTP status code ...
Definition: core_http_client.h:268
HTTPClient_ReadHeader
HTTPStatus_t HTTPClient_ReadHeader(const HTTPResponse_t *pResponse, const char *pField, size_t fieldLen, const char **pValueLoc, size_t *pValueLen)
Read a header from a buffer containing a complete HTTP response. This will return the location of the...
Definition: core_http_client.c:2496
HTTPNetworkError
@ HTTPNetworkError
A network error was returned from the transport interface.
Definition: core_http_client.h:193
HTTPHeaderNotFound
@ HTTPHeaderNotFound
The requested header field was not found in the response buffer.
Definition: core_http_client.h:305
HTTPResponse_t::contentLength
size_t contentLength
The value in the "Content-Length" header is returned here.
Definition: core_http_client.h:518
HTTPResponse_t::bodyLen
size_t bodyLen
Byte length of the body in pBuffer.
Definition: core_http_client.h:502
HTTPInsufficientMemory
@ HTTPInsufficientMemory
The application buffer was not large enough for the HTTP request headers or the HTTP response message...
Definition: core_http_client.h:224
HTTPResponse_t::pHeaders
const uint8_t * pHeaders
The starting location of the response headers in pBuffer.
Definition: core_http_client.h:481
HTTPSuccess
@ HTTPSuccess
The HTTP Client library function completed successfully.
Definition: core_http_client.h:173
HTTPNoResponse
@ HTTPNoResponse
No HTTP response was received from the network.
Definition: core_http_client.h:212
HTTPSecurityAlertInvalidProtocolVersion
@ HTTPSecurityAlertInvalidProtocolVersion
The server sent a response with an invalid character in the HTTP protocol version.
Definition: core_http_client.h:259
HTTPClient_InitializeRequestHeaders
HTTPStatus_t HTTPClient_InitializeRequestHeaders(HTTPRequestHeaders_t *pRequestHeaders, const HTTPRequestInfo_t *pRequestInfo)
Initialize the request headers, stored in HTTPRequestHeaders_t.pBuffer, with initial configurations f...
Definition: core_http_client.c:1557
HTTPRequestHeaders_t
Represents header data that will be sent in an HTTP request.
Definition: core_http_client.h:329
HTTPInvalidParameter
@ HTTPInvalidParameter
The HTTP Client library function input an invalid parameter.
Definition: core_http_client.h:185
HTTPResponse_t::pBody
const uint8_t * pBody
The starting location of the response body in pBuffer.
Definition: core_http_client.h:495
HTTPRequestInfo_t::pPath
const char * pPath
The Request-URI to the objects of interest, e.g. "/path/to/item.txt".
Definition: core_http_client.h:370
HTTPSecurityAlertResponseHeadersSizeLimitExceeded
@ HTTPSecurityAlertResponseHeadersSizeLimitExceeded
The server sent more headers than the configured HTTP_MAX_RESPONSE_HEADERS_SIZE_BYTES.
Definition: core_http_client.h:233
HTTPClient_AddRangeHeader
HTTPStatus_t HTTPClient_AddRangeHeader(HTTPRequestHeaders_t *pRequestHeaders, int32_t rangeStartOrlastNbytes, int32_t rangeEnd)
Add the byte range request header to the request headers store in HTTPRequestHeaders_t....
Definition: core_http_client.c:1714
HTTPClient_ResponseHeaderParsingCallback_t
Callback to intercept headers during the first parse through of the response as it is received from t...
Definition: core_http_client.h:397
HTTPRequestInfo_t::pMethod
const char * pMethod
The HTTP request method e.g. "GET", "POST", "PUT", or "HEAD".
Definition: core_http_client.h:364
HTTPRequestInfo_t::reqFlags
uint32_t reqFlags
Flags to activate other request header configurations.
Definition: core_http_client.h:386
HTTPRequestHeaders_t::bufferLen
size_t bufferLen
Definition: core_http_client.h:345
HTTPResponse_t::respFlags
uint32_t respFlags
Flags of useful headers found in the response.
Definition: core_http_client.h:533
core_http_config_defaults.h
The default values for the configuration macros for the HTTP Client library.
HTTPResponse_t
Represents an HTTP response.
Definition: core_http_client.h:434
HTTPResponse_t::bufferLen
size_t bufferLen
Definition: core_http_client.h:451
HTTPClient_AddHeader
HTTPStatus_t HTTPClient_AddHeader(HTTPRequestHeaders_t *pRequestHeaders, const char *pField, size_t fieldLen, const char *pValue, size_t valueLen)
Add a header to the request headers stored in HTTPRequestHeaders_t.pBuffer.
Definition: core_http_client.c:1654
HTTPResponse_t::pBuffer
uint8_t * pBuffer
Buffer for both the raw HTTP header and body.
Definition: core_http_client.h:450
HTTPSecurityAlertExtraneousResponseData
@ HTTPSecurityAlertExtraneousResponseData
A response contained the "Connection: close" header, but there was more data at the end of the comple...
Definition: core_http_client.h:242
HTTPResponse_t::headersLen
size_t headersLen
Byte length of the response headers in pBuffer.
Definition: core_http_client.h:488
HTTPRequestInfo_t::hostLen
size_t hostLen
Definition: core_http_client.h:379
HTTPClient_strerror
const char * HTTPClient_strerror(HTTPStatus_t status)
Error code to string conversion utility for HTTP Client library.
Definition: core_http_client.c:2561
HTTPStatus_t
HTTPStatus_t
The HTTP Client library return status.
Definition: core_http_client.h:162
HTTPResponse_t::getTime
HTTPClient_GetCurrentTimeFunc_t getTime
Optional callback for getting the system time.
Definition: core_http_client.h:474
HTTPParserInternalError
@ HTTPParserInternalError
An error occurred in the third-party parsing library.
Definition: core_http_client.h:297
HTTPResponse_t::pHeaderParsingCallback
HTTPClient_ResponseHeaderParsingCallback_t * pHeaderParsingCallback
Optional callback for intercepting the header during the first parse through of the response as is it...
Definition: core_http_client.h:458
HTTPSecurityAlertInvalidCharacter
@ HTTPSecurityAlertInvalidCharacter
An invalid character was found in the HTTP response message or in the HTTP request header.
Definition: core_http_client.h:278
HTTPRequestInfo_t::pHost
const char * pHost
The server's host name, e.g. "my-storage.my-cloud.com".
Definition: core_http_client.h:378
HTTPRequestInfo_t
Configurations of the initial request headers.
Definition: core_http_client.h:360
HTTPSecurityAlertInvalidContentLength
@ HTTPSecurityAlertInvalidContentLength
The response contains either an invalid character in the Content-Length header or a Content-Length he...
Definition: core_http_client.h:288
HTTPClient_ResponseHeaderParsingCallback_t::pContext
void * pContext
Private context for the application.
Definition: core_http_client.h:417
HTTPRequestHeaders_t::headersLen
size_t headersLen
The actual size in bytes of headers in the buffer. This field is updated by the HTTP Client library f...
Definition: core_http_client.h:352
HTTPResponse_t::headerCount
size_t headerCount
Count of the headers sent by the server.
Definition: core_http_client.h:525
HTTPClient_Send
HTTPStatus_t HTTPClient_Send(const TransportInterface_t *pTransport, HTTPRequestHeaders_t *pRequestHeaders, const uint8_t *pRequestBodyBuf, size_t reqBodyBufLen, HTTPResponse_t *pResponse, uint32_t sendFlags)
Send the request headers in HTTPRequestHeaders_t.pBuffer and request body in pRequestBodyBuf over the...
Definition: core_http_client.c:2154
HTTPSecurityAlertInvalidChunkHeader
@ HTTPSecurityAlertInvalidChunkHeader
The server sent a chunk header containing an invalid character.
Definition: core_http_client.h:250
HTTPInvalidResponse
@ HTTPInvalidResponse
The HTTP response, provided for parsing, is either corrupt or incomplete.
Definition: core_http_client.h:314
HTTPPartialResponse
@ HTTPPartialResponse
Part of the HTTP response was received from the network.
Definition: core_http_client.h:201
HTTPRequestInfo_t::methodLen
size_t methodLen
Definition: core_http_client.h:365
transport_interface.h
Transport interface definitions to send and receive data over the network.
HTTPRequestHeaders_t::pBuffer
uint8_t * pBuffer
Buffer to hold the raw HTTP request headers.
Definition: core_http_client.h:344
HTTPRequestInfo_t::pathLen
size_t pathLen
Definition: core_http_client.h:371
HTTPResponse_t::statusCode
uint16_t statusCode
The HTTP response Status-Code.
Definition: core_http_client.h:511
HTTPClient_GetCurrentTimeFunc_t
uint32_t(* HTTPClient_GetCurrentTimeFunc_t)(void)
Application provided function to query the current time in milliseconds.
Definition: core_http_client.h:427
TransportInterface_t
The transport layer interface.
Definition: transport_interface.h:252