http

Committer:
DuaaAbusharkh
Date:
Mon Apr 15 08:30:22 2019 +0000
Revision:
0:a49e37a83a7a
blink

Who changed what in which revision?

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