Library for Firebase, PUT, PATCH, POST, GET, DELETE operations supported, (others are available, todo). Based on Mbed's https-example. Tested on STM32F767 using ETHERNET and ESP8266 WIFI interfaces and STM32F446 using ESP8266 WIFI interface.
source/http_parsed_url.h@0:768ae9838086, 2020-01-23 (annotated)
- Committer:
- star297
- Date:
- Thu Jan 23 22:18:11 2020 +0000
- Revision:
- 0:768ae9838086
Initial version.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
star297 | 0:768ae9838086 | 1 | /* |
star297 | 0:768ae9838086 | 2 | * PackageLicenseDeclared: Apache-2.0 |
star297 | 0:768ae9838086 | 3 | * Copyright (c) 2017 ARM Limited |
star297 | 0:768ae9838086 | 4 | * |
star297 | 0:768ae9838086 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
star297 | 0:768ae9838086 | 6 | * you may not use this file except in compliance with the License. |
star297 | 0:768ae9838086 | 7 | * You may obtain a copy of the License at |
star297 | 0:768ae9838086 | 8 | * |
star297 | 0:768ae9838086 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
star297 | 0:768ae9838086 | 10 | * |
star297 | 0:768ae9838086 | 11 | * Unless required by applicable law or agreed to in writing, software |
star297 | 0:768ae9838086 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
star297 | 0:768ae9838086 | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
star297 | 0:768ae9838086 | 14 | * See the License for the specific language governing permissions and |
star297 | 0:768ae9838086 | 15 | * limitations under the License. |
star297 | 0:768ae9838086 | 16 | */ |
star297 | 0:768ae9838086 | 17 | |
star297 | 0:768ae9838086 | 18 | #ifndef _MBED_HTTP_PARSED_URL_H_ |
star297 | 0:768ae9838086 | 19 | #define _MBED_HTTP_PARSED_URL_H_ |
star297 | 0:768ae9838086 | 20 | |
star297 | 0:768ae9838086 | 21 | #include "http_parser.h" |
star297 | 0:768ae9838086 | 22 | |
star297 | 0:768ae9838086 | 23 | class ParsedUrl { |
star297 | 0:768ae9838086 | 24 | public: |
star297 | 0:768ae9838086 | 25 | ParsedUrl(const char* url) { |
star297 | 0:768ae9838086 | 26 | struct http_parser_url parsed_url; |
star297 | 0:768ae9838086 | 27 | http_parser_parse_url(url, strlen(url), false, &parsed_url); |
star297 | 0:768ae9838086 | 28 | |
star297 | 0:768ae9838086 | 29 | for (size_t ix = 0; ix < UF_MAX; ix++) { |
star297 | 0:768ae9838086 | 30 | char* value; |
star297 | 0:768ae9838086 | 31 | if (parsed_url.field_set & (1 << ix)) { |
star297 | 0:768ae9838086 | 32 | value = (char*)calloc(parsed_url.field_data[ix].len + 1, 1); |
star297 | 0:768ae9838086 | 33 | memcpy(value, url + parsed_url.field_data[ix].off, |
star297 | 0:768ae9838086 | 34 | parsed_url.field_data[ix].len); |
star297 | 0:768ae9838086 | 35 | } |
star297 | 0:768ae9838086 | 36 | else { |
star297 | 0:768ae9838086 | 37 | value = (char*)calloc(1, 1); |
star297 | 0:768ae9838086 | 38 | } |
star297 | 0:768ae9838086 | 39 | |
star297 | 0:768ae9838086 | 40 | switch ((http_parser_url_fields)ix) { |
star297 | 0:768ae9838086 | 41 | case UF_SCHEMA: _schema = value; break; |
star297 | 0:768ae9838086 | 42 | case UF_HOST: _host = value; break; |
star297 | 0:768ae9838086 | 43 | case UF_PATH: _path = value; break; |
star297 | 0:768ae9838086 | 44 | case UF_QUERY: _query = value; break; |
star297 | 0:768ae9838086 | 45 | case UF_USERINFO: _userinfo = value; break; |
star297 | 0:768ae9838086 | 46 | default: |
star297 | 0:768ae9838086 | 47 | // PORT is already parsed, FRAGMENT is not relevant for HTTP requests |
star297 | 0:768ae9838086 | 48 | free(value); |
star297 | 0:768ae9838086 | 49 | break; |
star297 | 0:768ae9838086 | 50 | } |
star297 | 0:768ae9838086 | 51 | } |
star297 | 0:768ae9838086 | 52 | |
star297 | 0:768ae9838086 | 53 | _port = parsed_url.port; |
star297 | 0:768ae9838086 | 54 | if (!_port) { |
star297 | 0:768ae9838086 | 55 | if (strcmp(_schema, "https") == 0 || strcmp(_schema, "wss") == 0) { |
star297 | 0:768ae9838086 | 56 | _port = 443; |
star297 | 0:768ae9838086 | 57 | } |
star297 | 0:768ae9838086 | 58 | else { |
star297 | 0:768ae9838086 | 59 | _port = 80; |
star297 | 0:768ae9838086 | 60 | } |
star297 | 0:768ae9838086 | 61 | } |
star297 | 0:768ae9838086 | 62 | |
star297 | 0:768ae9838086 | 63 | if (strcmp(_path, "") == 0) { |
star297 | 0:768ae9838086 | 64 | free(_path); |
star297 | 0:768ae9838086 | 65 | _path = (char*)calloc(2, 1); |
star297 | 0:768ae9838086 | 66 | _path[0] = '/'; |
star297 | 0:768ae9838086 | 67 | } |
star297 | 0:768ae9838086 | 68 | } |
star297 | 0:768ae9838086 | 69 | |
star297 | 0:768ae9838086 | 70 | ~ParsedUrl() { |
star297 | 0:768ae9838086 | 71 | if (_schema) free(_schema); |
star297 | 0:768ae9838086 | 72 | if (_host) free(_host); |
star297 | 0:768ae9838086 | 73 | if (_path) free(_path); |
star297 | 0:768ae9838086 | 74 | if (_query) free(_query); |
star297 | 0:768ae9838086 | 75 | if (_userinfo) free(_userinfo); |
star297 | 0:768ae9838086 | 76 | } |
star297 | 0:768ae9838086 | 77 | |
star297 | 0:768ae9838086 | 78 | uint16_t port() const { return _port; } |
star297 | 0:768ae9838086 | 79 | char* schema() const { return _schema; } |
star297 | 0:768ae9838086 | 80 | char* host() const { return _host; } |
star297 | 0:768ae9838086 | 81 | char* path() const { return _path; } |
star297 | 0:768ae9838086 | 82 | char* query() const { return _query; } |
star297 | 0:768ae9838086 | 83 | char* userinfo() const { return _userinfo; } |
star297 | 0:768ae9838086 | 84 | |
star297 | 0:768ae9838086 | 85 | private: |
star297 | 0:768ae9838086 | 86 | uint16_t _port; |
star297 | 0:768ae9838086 | 87 | char* _schema; |
star297 | 0:768ae9838086 | 88 | char* _host; |
star297 | 0:768ae9838086 | 89 | char* _path; |
star297 | 0:768ae9838086 | 90 | char* _query; |
star297 | 0:768ae9838086 | 91 | char* _userinfo; |
star297 | 0:768ae9838086 | 92 | }; |
star297 | 0:768ae9838086 | 93 | |
star297 | 0:768ae9838086 | 94 | #endif // _MBED_HTTP_PARSED_URL_H_ |