1+ /*
2+ * PackageLicenseDeclared: Apache-2.0
3+ * Copyright (c) 2017 ARM Limited
4+ *
5+ * Licensed under the Apache License, Version 2.0 (the "License");
6+ * you may not use this file except in compliance with the License.
7+ * You may obtain a copy of the License at
8+ *
9+ * http://www.apache.org/licenses/LICENSE-2.0
10+ *
11+ * Unless required by applicable law or agreed to in writing, software
12+ * distributed under the License is distributed on an "AS IS" BASIS,
13+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+ * See the License for the specific language governing permissions and
15+ * limitations under the License.
16+ */
17+ 18+ /*
19+ * The following class is defined in mbed libraries, in case of STM32H7 include the original library
20+ */
21+ #if defined __has_include
22+ # if __has_include(<utility/http_parsed_url.h>)
23+ # include < utility/http_parsed_url.h>
24+ # else
25+ # define NO_HTTP_PARSED
26+ # endif
27+ #endif
28+ 29+ #ifdef NO_HTTP_PARSED
30+ #ifndef _MBED_HTTP_PARSED_URL_H_
31+ #define _MBED_HTTP_PARSED_URL_H_
32+ 33+ #include " utility/URLParser/http_parser.h"
34+ 35+ class ParsedUrl {
36+ public:
37+ ParsedUrl (const char * url) {
38+ struct http_parser_url parsed_url;
39+ http_parser_parse_url (url, strlen (url), false , &parsed_url);
40+ 41+ for (size_t ix = 0 ; ix < UF_MAX; ix++) {
42+ char * value;
43+ if (parsed_url.field_set & (1 << ix)) {
44+ value = (char *)calloc (parsed_url.field_data [ix].len + 1 , 1 );
45+ memcpy (value, url + parsed_url.field_data [ix].off ,
46+ parsed_url.field_data [ix].len );
47+ }
48+ else {
49+ value = (char *)calloc (1 , 1 );
50+ }
51+ 52+ switch ((http_parser_url_fields)ix) {
53+ case UF_SCHEMA: _schema = value; break ;
54+ case UF_HOST: _host = value; break ;
55+ case UF_PATH: _path = value; break ;
56+ case UF_QUERY: _query = value; break ;
57+ case UF_USERINFO: _userinfo = value; break ;
58+ default :
59+ // PORT is already parsed, FRAGMENT is not relevant for HTTP requests
60+ free (value);
61+ break ;
62+ }
63+ }
64+ 65+ _port = parsed_url.port ;
66+ if (!_port) {
67+ if (strcmp (_schema, " https" ) == 0 || strcmp (_schema, " wss" ) == 0 ) {
68+ _port = 443 ;
69+ }
70+ else {
71+ _port = 80 ;
72+ }
73+ }
74+ 75+ if (strcmp (_path, " " ) == 0 ) {
76+ free (_path);
77+ _path = (char *)calloc (2 , 1 );
78+ _path[0 ] = ' /' ;
79+ }
80+ }
81+ 82+ ~ParsedUrl () {
83+ if (_schema) free (_schema);
84+ if (_host) free (_host);
85+ if (_path) free (_path);
86+ if (_query) free (_query);
87+ if (_userinfo) free (_userinfo);
88+ }
89+ 90+ uint16_t port () const { return _port; }
91+ char * schema () const { return _schema; }
92+ char * host () const { return _host; }
93+ char * path () const { return _path; }
94+ char * query () const { return _query; }
95+ char * userinfo () const { return _userinfo; }
96+ 97+ private:
98+ uint16_t _port;
99+ char * _schema;
100+ char * _host;
101+ char * _path;
102+ char * _query;
103+ char * _userinfo;
104+ };
105+ 106+ #endif // _MBED_HTTP_PARSED_URL_H_
107+ #endif // NO_HTTP_PARSED
108+ #undef NO_HTTP_PARSED
0 commit comments