2 * Amazon FreeRTOS HTTPS Client V1.1.0
\r
3 * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://aws.amazon.com/freertos
\r
23 * http://www.FreeRTOS.org
\r
27 * @file iot_https_utils.c
\r
28 * @brief Implements functions for HTTPS Client library utilities.
\r
31 /* The config header is always included first. */
\r
32 #include "iot_config.h"
\r
34 /* iot_https_includes */
\r
35 #include "iot_https_utils.h"
\r
36 #include "http_parser.h"
\r
37 #include "private/iot_https_internal.h"
\r
39 /*-----------------------------------------------------------*/
\r
41 IotHttpsReturnCode_t IotHttpsClient_GetUrlPath( const char * pUrl,
\r
43 const char ** pPath,
\r
46 /* http-parser status. Initialized to 0 to signify success. */
\r
47 int parserStatus = 0;
\r
48 struct http_parser_url urlParser;
\r
49 IotHttpsReturnCode_t returnStatus = IOT_HTTPS_OK;
\r
51 /* Sets all members in urlParser to 0. */
\r
52 http_parser_url_init( &urlParser );
\r
54 if( ( pUrl == NULL ) || ( pPath == NULL ) || ( pPathLen == NULL ) )
\r
56 IotLogError( "NULL parameter passed to IotHttpsClient_GetUrlPath()." );
\r
57 returnStatus = IOT_HTTPS_INVALID_PARAMETER;
\r
60 if( returnStatus == IOT_HTTPS_OK )
\r
62 parserStatus = http_parser_parse_url( pUrl, urlLen, 0, &urlParser );
\r
64 if( parserStatus != 0 )
\r
66 IotLogError( "Error parsing the input URL %.*s. Error code: %d.", urlLen, pUrl, parserStatus );
\r
67 returnStatus = IOT_HTTPS_PARSING_ERROR;
\r
71 if( returnStatus == IOT_HTTPS_OK )
\r
73 *pPathLen = ( size_t ) ( urlParser.field_data[ UF_PATH ].len );
\r
75 if( *pPathLen == 0 )
\r
77 returnStatus = IOT_HTTPS_NOT_FOUND;
\r
82 *pPath = &pUrl[ urlParser.field_data[ UF_PATH ].off ];
\r
86 return returnStatus;
\r
89 /*-----------------------------------------------------------*/
\r
91 IotHttpsReturnCode_t IotHttpsClient_GetUrlAddress( const char * pUrl,
\r
93 const char ** pAddress,
\r
94 size_t * pAddressLen )
\r
96 /* http-parser status. Initialized to 0 to signify success. */
\r
97 int parserStatus = 0;
\r
98 struct http_parser_url urlParser;
\r
99 IotHttpsReturnCode_t returnStatus = IOT_HTTPS_OK;
\r
101 /* Sets all members in urlParser to 0. */
\r
102 http_parser_url_init( &urlParser );
\r
104 if( ( pUrl == NULL ) || ( pAddress == NULL ) || ( pAddressLen == NULL ) )
\r
106 IotLogError( "NULL parameter passed to IotHttpsClient_GetUrlAddress()." );
\r
107 returnStatus = IOT_HTTPS_INVALID_PARAMETER;
\r
110 if( returnStatus == IOT_HTTPS_OK )
\r
112 parserStatus = http_parser_parse_url( pUrl, urlLen, 0, &urlParser );
\r
114 if( parserStatus != 0 )
\r
116 IotLogError( "Error parsing the input URL %.*s. Error code: %d.", urlLen, pUrl, parserStatus );
\r
117 returnStatus = IOT_HTTPS_PARSING_ERROR;
\r
121 if( returnStatus == IOT_HTTPS_OK )
\r
123 *pAddressLen = ( size_t ) ( urlParser.field_data[ UF_HOST ].len );
\r
125 if( *pAddressLen == 0 )
\r
127 returnStatus = IOT_HTTPS_NOT_FOUND;
\r
132 *pAddress = &pUrl[ urlParser.field_data[ UF_HOST ].off ];
\r
136 return returnStatus;
\r