]> git.sur5r.net Git - freertos/blob - FreeRTOS-Labs/Source/FreeRTOS-IoT-Libraries/c_sdk/standard/https/src/iot_https_utils.c
Add the Labs projects provided in the V10.2.1_191129 zip file.
[freertos] / FreeRTOS-Labs / Source / FreeRTOS-IoT-Libraries / c_sdk / standard / https / src / iot_https_utils.c
1 /*\r
2  * Amazon FreeRTOS HTTPS Client V1.1.0\r
3  * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\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
11  *\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
14  *\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
21  *\r
22  * http://aws.amazon.com/freertos\r
23  * http://www.FreeRTOS.org\r
24  */\r
25 \r
26 /**\r
27  * @file iot_https_utils.c\r
28  * @brief Implements functions for HTTPS Client library utilities.\r
29  */\r
30 \r
31 /* The config header is always included first. */\r
32 #include "iot_config.h"\r
33 \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
38 \r
39 /*-----------------------------------------------------------*/\r
40 \r
41 IotHttpsReturnCode_t IotHttpsClient_GetUrlPath( const char * pUrl,\r
42                                                 size_t urlLen,\r
43                                                 const char ** pPath,\r
44                                                 size_t * pPathLen )\r
45 {\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
50 \r
51     /* Sets all members in urlParser to 0. */\r
52     http_parser_url_init( &urlParser );\r
53 \r
54     if( ( pUrl == NULL ) || ( pPath == NULL ) || ( pPathLen == NULL ) )\r
55     {\r
56         IotLogError( "NULL parameter passed to IotHttpsClient_GetUrlPath()." );\r
57         returnStatus = IOT_HTTPS_INVALID_PARAMETER;\r
58     }\r
59 \r
60     if( returnStatus == IOT_HTTPS_OK )\r
61     {\r
62         parserStatus = http_parser_parse_url( pUrl, urlLen, 0, &urlParser );\r
63 \r
64         if( parserStatus != 0 )\r
65         {\r
66             IotLogError( "Error parsing the input URL %.*s. Error code: %d.", urlLen, pUrl, parserStatus );\r
67             returnStatus = IOT_HTTPS_PARSING_ERROR;\r
68         }\r
69     }\r
70 \r
71     if( returnStatus == IOT_HTTPS_OK )\r
72     {\r
73         *pPathLen = ( size_t ) ( urlParser.field_data[ UF_PATH ].len );\r
74 \r
75         if( *pPathLen == 0 )\r
76         {\r
77             returnStatus = IOT_HTTPS_NOT_FOUND;\r
78             *pPath = NULL;\r
79         }\r
80         else\r
81         {\r
82             *pPath = &pUrl[ urlParser.field_data[ UF_PATH ].off ];\r
83         }\r
84     }\r
85 \r
86     return returnStatus;\r
87 }\r
88 \r
89 /*-----------------------------------------------------------*/\r
90 \r
91 IotHttpsReturnCode_t IotHttpsClient_GetUrlAddress( const char * pUrl,\r
92                                                    size_t urlLen,\r
93                                                    const char ** pAddress,\r
94                                                    size_t * pAddressLen )\r
95 {\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
100 \r
101     /* Sets all members in urlParser to 0. */\r
102     http_parser_url_init( &urlParser );\r
103 \r
104     if( ( pUrl == NULL ) || ( pAddress == NULL ) || ( pAddressLen == NULL ) )\r
105     {\r
106         IotLogError( "NULL parameter passed to IotHttpsClient_GetUrlAddress()." );\r
107         returnStatus = IOT_HTTPS_INVALID_PARAMETER;\r
108     }\r
109 \r
110     if( returnStatus == IOT_HTTPS_OK )\r
111     {\r
112         parserStatus = http_parser_parse_url( pUrl, urlLen, 0, &urlParser );\r
113 \r
114         if( parserStatus != 0 )\r
115         {\r
116             IotLogError( "Error parsing the input URL %.*s. Error code: %d.", urlLen, pUrl, parserStatus );\r
117             returnStatus = IOT_HTTPS_PARSING_ERROR;\r
118         }\r
119     }\r
120 \r
121     if( returnStatus == IOT_HTTPS_OK )\r
122     {\r
123         *pAddressLen = ( size_t ) ( urlParser.field_data[ UF_HOST ].len );\r
124 \r
125         if( *pAddressLen == 0 )\r
126         {\r
127             returnStatus = IOT_HTTPS_NOT_FOUND;\r
128             *pAddress = NULL;\r
129         }\r
130         else\r
131         {\r
132             *pAddress = &pUrl[ urlParser.field_data[ UF_HOST ].off ];\r
133         }\r
134     }\r
135 \r
136     return returnStatus;\r
137 }\r