coreHTTP v3.1.2
HTTP/1.1 Client Library
 
Loading...
Searching...
No Matches
core_http_client.h
Go to the documentation of this file.
1/*
2 * coreHTTP
3 * Copyright (C) 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
30#ifndef CORE_HTTP_CLIENT_H_
31#define CORE_HTTP_CLIENT_H_
32
33#include <stdint.h>
34#include <stddef.h>
35
36/* *INDENT-OFF* */
37#ifdef __cplusplus
38 extern "C" {
39#endif
40/* *INDENT-ON* */
41
49#define HTTP_LIBRARY_VERSION "v3.1.2"
52/* HTTP_DO_NOT_USE_CUSTOM_CONFIG allows building the HTTP Client library
53 * without a config file. If a config file is provided, the
54 * HTTP_DO_NOT_USE_CUSTOM_CONFIG macro must not be defined.
55 */
56#ifndef HTTP_DO_NOT_USE_CUSTOM_CONFIG
57 #include "core_http_config.h"
58#endif
59
60/* Include config defaults header to get default values of configurations not
61 * defined in core_http_config.h file. */
63
64/* Transport interface include. */
65#include "transport_interface.h"
66
67/* Convenience macros for some HTTP request methods. */
68
72#define HTTP_METHOD_GET "GET"
73#define HTTP_METHOD_PUT "PUT"
74#define HTTP_METHOD_POST "POST"
75#define HTTP_METHOD_HEAD "HEAD"
83#define HTTP_MAX_CONTENT_LENGTH_HEADER_LENGTH sizeof( "Content-Length: 4294967295" ) - 1U
84
102#define HTTP_SEND_DISABLE_CONTENT_LENGTH_FLAG 0x1U
103
124#define HTTP_REQUEST_KEEP_ALIVE_FLAG 0x1U
125
135#define HTTP_REQUEST_NO_USER_AGENT_FLAG 0x2U
136
156#define HTTP_RESPONSE_CONNECTION_CLOSE_FLAG 0x1U
157
164#define HTTP_RESPONSE_CONNECTION_KEEP_ALIVE_FLAG 0x2U
165
185#define HTTP_RESPONSE_DO_NOT_PARSE_BODY_FLAG 0x1U
186
199#define HTTP_RANGE_REQUEST_END_OF_FILE -1
200
205typedef enum HTTPStatus
206{
218
230
238
246
257
269
278
286
295
304
314
324
335
344
352
362
374typedef struct HTTPRequestHeaders
375{
390 uint8_t * pBuffer;
391 size_t bufferLen;
400
405typedef struct HTTPRequestInfo
406{
410 const char * pMethod;
411 size_t methodLen;
416 const char * pPath;
417 size_t pathLen;
424 const char * pHost;
425 size_t hostLen;
432 uint32_t reqFlags;
434
435
436
442typedef struct HTTPClient_ResponseHeaderParsingCallback
443{
453 void ( * onHeaderCallback )( void * pContext,
454 const char * fieldLoc,
455 size_t fieldLen,
456 const char * valueLoc,
457 size_t valueLen,
458 uint16_t statusCode );
459
463 void * pContext;
465
473typedef uint32_t (* HTTPClient_GetCurrentTimeFunc_t )( void );
474
479typedef struct HTTPResponse
480{
496 uint8_t * pBuffer;
497 size_t bufferLen;
505
521
527 const uint8_t * pHeaders;
528
535
541 const uint8_t * pBody;
542
548 size_t bodyLen;
549
550 /* Useful HTTP header values found. */
551
557 uint16_t statusCode;
558
565
572
579
586
593 uint32_t respFlags;
595
647/* @[declare_httpclient_initializerequestheaders] */
649 const HTTPRequestInfo_t * pRequestInfo );
650/* @[declare_httpclient_initializerequestheaders] */
651
701/* @[declare_httpclient_addheader] */
703 const char * pField,
704 size_t fieldLen,
705 const char * pValue,
706 size_t valueLen );
707/* @[declare_httpclient_addheader] */
708
783/* @[declare_httpclient_addrangeheader] */
785 int32_t rangeStartOrlastNbytes,
786 int32_t rangeEnd );
787/* @[declare_httpclient_addrangeheader] */
788
824/* @[declare_httpclient_sendhttpheaders] */
826 HTTPClient_GetCurrentTimeFunc_t getTimestampMs,
827 HTTPRequestHeaders_t * pRequestHeaders,
828 size_t reqBodyLen,
829 uint32_t sendFlags );
830/* @[declare_httpclient_sendhttpheaders] */
831
845/* @[declare_httpclient_sendhttpdata] */
847 HTTPClient_GetCurrentTimeFunc_t getTimestampMs,
848 const uint8_t * pData,
849 size_t dataLen );
850/* @[declare_httpclient_sendhttpdata] */
851
945/* @[declare_httpclient_send] */
947 HTTPRequestHeaders_t * pRequestHeaders,
948 const uint8_t * pRequestBodyBuf,
949 size_t reqBodyBufLen,
950 HTTPResponse_t * pResponse,
951 uint32_t sendFlags );
952/* @[declare_httpclient_send] */
953
969/* @[declare_httpclient_receiveandparsehttpresponse] */
971 HTTPResponse_t * pResponse,
972 const HTTPRequestHeaders_t * pRequestHeaders );
973/* @[declare_httpclient_receiveandparsehttpresponse] */
974
1023/* @[declare_httpclient_readheader] */
1025 const char * pField,
1026 size_t fieldLen,
1027 const char ** pValueLoc,
1028 size_t * pValueLen );
1029/* @[declare_httpclient_readheader] */
1030
1040/* @[declare_httpclient_strerror] */
1041const char * HTTPClient_strerror( HTTPStatus_t status );
1042/* @[declare_httpclient_strerror] */
1043
1044/* *INDENT-OFF* */
1045#ifdef __cplusplus
1046 }
1047#endif
1048/* *INDENT-ON* */
1049
1050#endif /* ifndef CORE_HTTP_CLIENT_H_ */
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:1689
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:2214
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:1589
HTTPStatus_t HTTPClient_ReceiveAndParseHttpResponse(const TransportInterface_t *pTransport, HTTPResponse_t *pResponse, const HTTPRequestHeaders_t *pRequestHeaders)
Receive the HTTP response from the network and parse it.
Definition: core_http_client.c:2037
HTTPStatus_t HTTPClient_SendHttpHeaders(const TransportInterface_t *pTransport, HTTPClient_GetCurrentTimeFunc_t getTimestampMs, HTTPRequestHeaders_t *pRequestHeaders, size_t reqBodyLen, uint32_t sendFlags)
Send the request headers in pRequestHeaders over the transport.
Definition: core_http_client.c:1926
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:2558
const char * HTTPClient_strerror(HTTPStatus_t status)
Error code to string conversion utility for HTTP Client library.
Definition: core_http_client.c:2623
HTTPStatus_t HTTPClient_SendHttpData(const TransportInterface_t *pTransport, HTTPClient_GetCurrentTimeFunc_t getTimestampMs, const uint8_t *pData, size_t dataLen)
Send the request body in pRequestBodyBuf over the transport.
Definition: core_http_client.c:1814
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:1749
The default values for the configuration macros for the HTTP Client library.
uint32_t(* HTTPClient_GetCurrentTimeFunc_t)(void)
Application provided function to query the current time in milliseconds.
Definition: core_http_client.h:473
HTTPStatus_t
The HTTP Client library return status.
Definition: core_http_client.h:206
@ HTTPSecurityAlertInvalidCharacter
An invalid character was found in the HTTP response message or in the HTTP request header.
Definition: core_http_client.h:313
@ HTTPPartialResponse
Part of the HTTP response was received from the network.
Definition: core_http_client.h:245
@ HTTPInsufficientMemory
The application buffer was not large enough for the HTTP request headers or the HTTP response message...
Definition: core_http_client.h:268
@ HTTPParserInternalError
An error occurred in the third-party parsing library.
Definition: core_http_client.h:343
@ HTTPNetworkError
A network error was returned from the transport interface.
Definition: core_http_client.h:237
@ HTTPSuccess
The HTTP Client library function completed successfully.
Definition: core_http_client.h:217
@ HTTPInvalidResponse
The HTTP response, provided for parsing, is either corrupt or incomplete.
Definition: core_http_client.h:360
@ HTTPNoResponse
No HTTP response was received from the network.
Definition: core_http_client.h:256
@ HTTPSecurityAlertInvalidProtocolVersion
The server sent a response with an invalid character in the HTTP protocol version.
Definition: core_http_client.h:294
@ HTTPSecurityAlertInvalidContentLength
The response contains either an invalid character in the Content-Length header or a Content-Length he...
Definition: core_http_client.h:323
@ HTTPParserPaused
Represents the paused state of the HTTP parser.
Definition: core_http_client.h:334
@ HTTPHeaderNotFound
The requested header field was not found in the response buffer.
Definition: core_http_client.h:351
@ HTTPInvalidParameter
The HTTP Client library function input an invalid parameter.
Definition: core_http_client.h:229
@ HTTPSecurityAlertInvalidChunkHeader
The server sent a chunk header containing an invalid character.
Definition: core_http_client.h:285
@ 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:303
@ HTTPSecurityAlertExtraneousResponseData
A response contained the "Connection: close" header, but there was more data at the end of the comple...
Definition: core_http_client.h:277
Callback to intercept headers during the first parse through of the response as it is received from t...
Definition: core_http_client.h:443
void * pContext
Private context for the application.
Definition: core_http_client.h:463
Represents header data that will be sent in an HTTP request.
Definition: core_http_client.h:375
size_t bufferLen
Definition: core_http_client.h:391
uint8_t * pBuffer
Buffer to hold the raw HTTP request headers.
Definition: core_http_client.h:390
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:398
Configurations of the initial request headers.
Definition: core_http_client.h:406
uint32_t reqFlags
Flags to activate other request header configurations.
Definition: core_http_client.h:432
size_t methodLen
Definition: core_http_client.h:411
const char * pMethod
The HTTP request method e.g. "GET", "POST", "PUT", or "HEAD".
Definition: core_http_client.h:410
const char * pPath
The Request-URI to the objects of interest, e.g. "/path/to/item.txt".
Definition: core_http_client.h:416
const char * pHost
The server's host name, e.g. "my-storage.my-cloud.com".
Definition: core_http_client.h:424
size_t pathLen
Definition: core_http_client.h:417
size_t hostLen
Definition: core_http_client.h:425
Represents an HTTP response.
Definition: core_http_client.h:480
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:504
size_t bodyLen
Byte length of the body in pBuffer.
Definition: core_http_client.h:548
size_t headerCount
Count of the headers sent by the server.
Definition: core_http_client.h:571
uint16_t statusCode
The HTTP response Status-Code.
Definition: core_http_client.h:557
uint32_t respFlags
Flags of useful headers found in the response.
Definition: core_http_client.h:593
size_t bufferLen
Definition: core_http_client.h:497
uint32_t respOptionFlags
Flags to control the behavior of response parsing.
Definition: core_http_client.h:585
size_t headersLen
Byte length of the response headers in pBuffer.
Definition: core_http_client.h:534
uint8_t * pBuffer
Buffer for both the raw HTTP header and body.
Definition: core_http_client.h:496
const uint8_t * pHeaders
The starting location of the response headers in pBuffer.
Definition: core_http_client.h:527
uint8_t areHeadersComplete
Indicates whether the HTTP response headers have been fully received.
Definition: core_http_client.h:578
const uint8_t * pBody
The starting location of the response body in pBuffer.
Definition: core_http_client.h:541
size_t contentLength
The value in the "Content-Length" header is returned here.
Definition: core_http_client.h:564
HTTPClient_GetCurrentTimeFunc_t getTime
Optional callback for getting the system time.
Definition: core_http_client.h:520
The transport layer interface.
Definition: transport_interface.h:303
Transport interface definitions to send and receive data over the network.