Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2143747

Browse files
Merge pull request #173 from andreagilardoni/url-parser
Added Url parser
2 parents 9101851 + 9ea6ace commit 2143747

File tree

7 files changed

+853
-1
lines changed

7 files changed

+853
-1
lines changed

‎.codespellrc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
ignore-words-list = ,
55
check-filenames =
66
check-hidden =
7-
skip = ./.git
7+
skip = ./.git,./src/utility/URLParser

‎examples/ParseURL/ParseURL.ino‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "URLParser.h"
2+
3+
void setup() {
4+
5+
Serial.begin(9600);
6+
7+
while(!Serial);
8+
9+
Serial.println("starting");
10+
11+
ParsedUrl url(
12+
"https://www.google.com/search?q=arduino"
13+
);
14+
15+
Serial.print("parsed URL schema: \"");
16+
Serial.print(url.schema());
17+
Serial.print("\"\nparsed URL host: \"");
18+
Serial.print(url.host());
19+
Serial.print("\"\nparsed URL path: \"");
20+
Serial.print(url.path());
21+
Serial.print("\"\nparsed URL query: \"");
22+
Serial.print(url.query());
23+
Serial.print("\"\nparsed URL userinfo: \"");
24+
Serial.print(url.userinfo());
25+
Serial.println("\"");
26+
27+
}
28+
29+
void loop() { }

‎src/URLParser.h‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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

‎src/utility/URLParser/LICENSE‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
http_parser.c is based on src/http/ngx_http_parse.c from NGINX copyright
2+
Igor Sysoev.
3+
4+
Additional changes are licensed under the same terms as NGINX and
5+
copyright Joyent, Inc. and other Node contributors. All rights reserved.
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to
9+
deal in the Software without restriction, including without limitation the
10+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11+
sell copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all 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,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23+
IN THE SOFTWARE.

‎src/utility/URLParser/README.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# http_parser library
2+
3+
This code is imported from: https://github.com/arduino/ArduinoCore-mbed/tree/4.1.1/libraries/SocketWrapper/src/utility/http_parser
4+
5+
The code is shrinked in size by deleting all the unrelated code to url parse.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /