coreHTTP v3.0.0
HTTP/1.1 Client Library
core_http_client.h
Go to the documentation of this file.
1/*
2 * coreHTTP v3.0.0
3 * Copyright (C) 2022 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
42/* HTTP_DO_NOT_USE_CUSTOM_CONFIG allows building the HTTP Client library
43 * without a config file. If a config file is provided, the
44 * HTTP_DO_NOT_USE_CUSTOM_CONFIG macro must not be defined.
45 */
46#ifndef HTTP_DO_NOT_USE_CUSTOM_CONFIG
47 #include "core_http_config.h"
48#endif
49
50/* Include config defaults header to get default values of configurations not
51 * defined in core_http_config.h file. */
53
54/* Transport interface include. */
55#include "transport_interface.h"
56
57/* Convenience macros for some HTTP request methods. */
58
62#define HTTP_METHOD_GET "GET"
63#define HTTP_METHOD_PUT "PUT"
64#define HTTP_METHOD_POST "POST"
65#define HTTP_METHOD_HEAD "HEAD"
73#define HTTP_MAX_CONTENT_LENGTH_HEADER_LENGTH sizeof( "Content-Length: 4294967295" ) - 1U
74
92#define HTTP_SEND_DISABLE_CONTENT_LENGTH_FLAG 0x1U
93
114#define HTTP_REQUEST_KEEP_ALIVE_FLAG 0x1U
115
135#define HTTP_RESPONSE_CONNECTION_CLOSE_FLAG 0x1U
136
143#define HTTP_RESPONSE_CONNECTION_KEEP_ALIVE_FLAG 0x2U
144
157#define HTTP_RANGE_REQUEST_END_OF_FILE -1
158
163typedef enum HTTPStatus
164{
176
188
196
204
215
227
236
244
253
262
272
282
291
299
309
321typedef struct HTTPRequestHeaders
322{
337 uint8_t * pBuffer;
338 size_t bufferLen;
347
352typedef struct HTTPRequestInfo
353{
357 const char * pMethod;
358 size_t methodLen;
363 const char * pPath;
364 size_t pathLen;
371 const char * pHost;
372 size_t hostLen;
379 uint32_t reqFlags;
381
382
383
389typedef struct HTTPClient_ResponseHeaderParsingCallback
390{
400 void ( * onHeaderCallback )( void * pContext,
401 const char * fieldLoc,
402 size_t fieldLen,
403 const char * valueLoc,
404 size_t valueLen,
405 uint16_t statusCode );
406
410 void * pContext;
412
420typedef uint32_t (* HTTPClient_GetCurrentTimeFunc_t )( void );
421
426typedef struct HTTPResponse
427{
443 uint8_t * pBuffer;
444 size_t bufferLen;
452
468
474 const uint8_t * pHeaders;
475
482
488 const uint8_t * pBody;
489
495 size_t bodyLen;
496
497 /* Useful HTTP header values found. */
498
504 uint16_t statusCode;
505
512
519
526 uint32_t respFlags;
528
580/* @[declare_httpclient_initializerequestheaders] */
582 const HTTPRequestInfo_t * pRequestInfo );
583/* @[declare_httpclient_initializerequestheaders] */
584
634/* @[declare_httpclient_addheader] */
636 const char * pField,
637 size_t fieldLen,
638 const char * pValue,
639 size_t valueLen );
640/* @[declare_httpclient_addheader] */
641
716/* @[declare_httpclient_addrangeheader] */
718 int32_t rangeStartOrlastNbytes,
719 int32_t rangeEnd );
720/* @[declare_httpclient_addrangeheader] */
721
815/* @[declare_httpclient_send] */
817 HTTPRequestHeaders_t * pRequestHeaders,
818 const uint8_t * pRequestBodyBuf,
819 size_t reqBodyBufLen,
820 HTTPResponse_t * pResponse,
821 uint32_t sendFlags );
822/* @[declare_httpclient_send] */
823
872/* @[declare_httpclient_readheader] */
874 const char * pField,
875 size_t fieldLen,
876 const char ** pValueLoc,
877 size_t * pValueLen );
878/* @[declare_httpclient_readheader] */
879
889/* @[declare_httpclient_strerror] */
890const char * HTTPClient_strerror( HTTPStatus_t status );
891/* @[declare_httpclient_strerror] */
892
893/* *INDENT-OFF* */
894#ifdef __cplusplus
895 }
896#endif
897/* *INDENT-ON* */
898
899#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:1664
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:2169
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:1567
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:2513
const char * HTTPClient_strerror(HTTPStatus_t status)
Error code to string conversion utility for HTTP Client library.
Definition: core_http_client.c:2578
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:1724
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:420
HTTPStatus_t
The HTTP Client library return status.
Definition: core_http_client.h:164
@ HTTPSecurityAlertInvalidCharacter
An invalid character was found in the HTTP response message or in the HTTP request header.
Definition: core_http_client.h:271
@ HTTPPartialResponse
Part of the HTTP response was received from the network.
Definition: core_http_client.h:203
@ HTTPInsufficientMemory
The application buffer was not large enough for the HTTP request headers or the HTTP response message...
Definition: core_http_client.h:226
@ HTTPParserInternalError
An error occurred in the third-party parsing library.
Definition: core_http_client.h:290
@ HTTPNetworkError
A network error was returned from the transport interface.
Definition: core_http_client.h:195
@ HTTPSuccess
The HTTP Client library function completed successfully.
Definition: core_http_client.h:175
@ HTTPInvalidResponse
The HTTP response, provided for parsing, is either corrupt or incomplete.
Definition: core_http_client.h:307
@ HTTPNoResponse
No HTTP response was received from the network.
Definition: core_http_client.h:214
@ HTTPSecurityAlertInvalidProtocolVersion
The server sent a response with an invalid character in the HTTP protocol version.
Definition: core_http_client.h:252
@ HTTPSecurityAlertInvalidContentLength
The response contains either an invalid character in the Content-Length header or a Content-Length he...
Definition: core_http_client.h:281
@ HTTPHeaderNotFound
The requested header field was not found in the response buffer.
Definition: core_http_client.h:298
@ HTTPInvalidParameter
The HTTP Client library function input an invalid parameter.
Definition: core_http_client.h:187
@ HTTPSecurityAlertInvalidChunkHeader
The server sent a chunk header containing an invalid character.
Definition: core_http_client.h:243
@ 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:261
@ HTTPSecurityAlertExtraneousResponseData
A response contained the "Connection: close" header, but there was more data at the end of the comple...
Definition: core_http_client.h:235
Callback to intercept headers during the first parse through of the response as it is received from t...
Definition: core_http_client.h:390
void * pContext
Private context for the application.
Definition: core_http_client.h:410
Represents header data that will be sent in an HTTP request.
Definition: core_http_client.h:322
size_t bufferLen
Definition: core_http_client.h:338
uint8_t * pBuffer
Buffer to hold the raw HTTP request headers.
Definition: core_http_client.h:337
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:345
Configurations of the initial request headers.
Definition: core_http_client.h:353
uint32_t reqFlags
Flags to activate other request header configurations.
Definition: core_http_client.h:379
size_t methodLen
Definition: core_http_client.h:358
const char * pMethod
The HTTP request method e.g. "GET", "POST", "PUT", or "HEAD".
Definition: core_http_client.h:357
const char * pPath
The Request-URI to the objects of interest, e.g. "/path/to/item.txt".
Definition: core_http_client.h:363
const char * pHost
The server's host name, e.g. "my-storage.my-cloud.com".
Definition: core_http_client.h:371
size_t pathLen
Definition: core_http_client.h:364
size_t hostLen
Definition: core_http_client.h:372
Represents an HTTP response.
Definition: core_http_client.h:427
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:451
size_t bodyLen
Byte length of the body in pBuffer.
Definition: core_http_client.h:495
size_t headerCount
Count of the headers sent by the server.
Definition: core_http_client.h:518
uint16_t statusCode
The HTTP response Status-Code.
Definition: core_http_client.h:504
uint32_t respFlags
Flags of useful headers found in the response.
Definition: core_http_client.h:526
size_t bufferLen
Definition: core_http_client.h:444
size_t headersLen
Byte length of the response headers in pBuffer.
Definition: core_http_client.h:481
uint8_t * pBuffer
Buffer for both the raw HTTP header and body.
Definition: core_http_client.h:443
const uint8_t * pHeaders
The starting location of the response headers in pBuffer.
Definition: core_http_client.h:474
const uint8_t * pBody
The starting location of the response body in pBuffer.
Definition: core_http_client.h:488
size_t contentLength
The value in the "Content-Length" header is returned here.
Definition: core_http_client.h:511
HTTPClient_GetCurrentTimeFunc_t getTime
Optional callback for getting the system time.
Definition: core_http_client.h:467
The transport layer interface.
Definition: transport_interface.h:302
Transport interface definitions to send and receive data over the network.