deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef RTC_BASE_HTTPCOMMON_INL_H_ |
| 12 | #define RTC_BASE_HTTPCOMMON_INL_H_ |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 13 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "rtc_base/arraysize.h" |
| 15 | #include "rtc_base/checks.h" |
| 16 | #include "rtc_base/httpcommon.h" |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 17 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 18 | namespace rtc { |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 19 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 20 | /////////////////////////////////////////////////////////////////////////////// |
| 21 | // Url |
| 22 | /////////////////////////////////////////////////////////////////////////////// |
| 23 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 24 | template <class CTYPE> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 25 | void Url<CTYPE>::do_set_url(const CTYPE* val, size_t len) { |
| 26 | if (ascnicmp(val, "http://", 7) == 0) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 27 | val += 7; |
| 28 | len -= 7; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 29 | secure_ = false; |
| 30 | } else if (ascnicmp(val, "https://", 8) == 0) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 31 | val += 8; |
| 32 | len -= 8; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 33 | secure_ = true; |
| 34 | } else { |
| 35 | clear(); |
| 36 | return; |
| 37 | } |
| 38 | const CTYPE* path = strchrn(val, len, static_cast<CTYPE>('/')); |
| 39 | if (!path) { |
| 40 | path = val + len; |
| 41 | } |
| 42 | size_t address_length = (path - val); |
| 43 | do_set_address(val, address_length); |
| 44 | do_set_full_path(path, len - address_length); |
| 45 | } |
| 46 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 47 | template <class CTYPE> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 48 | void Url<CTYPE>::do_set_address(const CTYPE* val, size_t len) { |
| 49 | if (const CTYPE* at = strchrn(val, len, static_cast<CTYPE>('@'))) { |
| 50 | // Everything before the @ is a user:password combo, so skip it. |
| 51 | len -= at - val + 1; |
| 52 | val = at + 1; |
| 53 | } |
| 54 | if (const CTYPE* colon = strchrn(val, len, static_cast<CTYPE>(':'))) { |
| 55 | host_.assign(val, colon - val); |
| 56 | // Note: In every case, we're guaranteed that colon is followed by a null, |
| 57 | // or non-numeric character. |
| 58 | port_ = static_cast<uint16_t>(::strtoul(colon + 1, nullptr, 10)); |
| 59 | // TODO: Consider checking for invalid data following port number. |
| 60 | } else { |
| 61 | host_.assign(val, len); |
| 62 | port_ = HttpDefaultPort(secure_); |
| 63 | } |
| 64 | } |
| 65 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 66 | template <class CTYPE> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 67 | void Url<CTYPE>::do_set_full_path(const CTYPE* val, size_t len) { |
| 68 | const CTYPE* query = strchrn(val, len, static_cast<CTYPE>('?')); |
| 69 | if (!query) { |
| 70 | query = val + len; |
| 71 | } |
| 72 | size_t path_length = (query - val); |
| 73 | if (0 == path_length) { |
| 74 | // TODO: consider failing in this case. |
| 75 | path_.assign(1, static_cast<CTYPE>('/')); |
| 76 | } else { |
| 77 | RTC_DCHECK(val[0] == static_cast<CTYPE>('/')); |
| 78 | path_.assign(val, path_length); |
| 79 | } |
| 80 | query_.assign(query, len - path_length); |
| 81 | } |
| 82 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 83 | template <class CTYPE> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 84 | void Url<CTYPE>::do_get_url(string* val) const { |
| 85 | CTYPE protocol[9]; |
| 86 | asccpyn(protocol, arraysize(protocol), secure_ ? "https://" : "http://"); |
| 87 | val->append(protocol); |
| 88 | do_get_address(val); |
| 89 | do_get_full_path(val); |
| 90 | } |
| 91 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 92 | template <class CTYPE> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 93 | void Url<CTYPE>::do_get_address(string* val) const { |
| 94 | val->append(host_); |
| 95 | if (port_ != HttpDefaultPort(secure_)) { |
| 96 | CTYPE format[5], port[32]; |
| 97 | asccpyn(format, arraysize(format), ":%hu"); |
| 98 | sprintfn(port, arraysize(port), format, port_); |
| 99 | val->append(port); |
| 100 | } |
| 101 | } |
| 102 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 103 | template <class CTYPE> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 104 | void Url<CTYPE>::do_get_full_path(string* val) const { |
| 105 | val->append(path_); |
| 106 | val->append(query_); |
| 107 | } |
| 108 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 109 | template <class CTYPE> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 110 | bool Url<CTYPE>::get_attribute(const string& name, string* value) const { |
| 111 | if (query_.empty()) |
| 112 | return false; |
| 113 | |
| 114 | std::string::size_type pos = query_.find(name, 1); |
| 115 | if (std::string::npos == pos) |
| 116 | return false; |
| 117 | |
| 118 | pos += name.length() + 1; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 119 | if ((pos > query_.length()) || (static_cast<CTYPE>('=') != query_[pos - 1])) |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 120 | return false; |
| 121 | |
| 122 | std::string::size_type end = query_.find(static_cast<CTYPE>('&'), pos); |
| 123 | if (std::string::npos == end) { |
| 124 | end = query_.length(); |
| 125 | } |
| 126 | value->assign(query_.substr(pos, end - pos)); |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | /////////////////////////////////////////////////////////////////////////////// |
| 131 | |
| 132 | } // namespace rtc |
| 133 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 134 | #endif // RTC_BASE_HTTPCOMMON_INL_H_ |