blob: bb72357c801a1d87506e6d11c6640840e559d016 [file] [log] [blame]
deadbeeff137e972017-03-23 15:45:49 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_HTTPCOMMON_INL_H_
12#define RTC_BASE_HTTPCOMMON_INL_H_
deadbeeff137e972017-03-23 15:45:49 -070013
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "rtc_base/arraysize.h"
15#include "rtc_base/checks.h"
16#include "rtc_base/httpcommon.h"
deadbeeff137e972017-03-23 15:45:49 -070017
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020018namespace rtc {
deadbeeff137e972017-03-23 15:45:49 -070019
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020020///////////////////////////////////////////////////////////////////////////////
21// Url
22///////////////////////////////////////////////////////////////////////////////
23
Yves Gerey665174f2018-06-19 15:03:05 +020024template <class CTYPE>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020025void Url<CTYPE>::do_set_url(const CTYPE* val, size_t len) {
26 if (ascnicmp(val, "http://", 7) == 0) {
Yves Gerey665174f2018-06-19 15:03:05 +020027 val += 7;
28 len -= 7;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020029 secure_ = false;
30 } else if (ascnicmp(val, "https://", 8) == 0) {
Yves Gerey665174f2018-06-19 15:03:05 +020031 val += 8;
32 len -= 8;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020033 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 Gerey665174f2018-06-19 15:03:05 +020047template <class CTYPE>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020048void 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 Gerey665174f2018-06-19 15:03:05 +020066template <class CTYPE>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020067void 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 Gerey665174f2018-06-19 15:03:05 +020083template <class CTYPE>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020084void 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 Gerey665174f2018-06-19 15:03:05 +020092template <class CTYPE>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020093void 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 Gerey665174f2018-06-19 15:03:05 +0200103template <class CTYPE>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200104void Url<CTYPE>::do_get_full_path(string* val) const {
105 val->append(path_);
106 val->append(query_);
107}
108
Yves Gerey665174f2018-06-19 15:03:05 +0200109template <class CTYPE>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200110bool 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 Gerey665174f2018-06-19 15:03:05 +0200119 if ((pos > query_.length()) || (static_cast<CTYPE>('=') != query_[pos - 1]))
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200120 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 Bonadei92ea95e2017-09-15 06:47:31 +0200134#endif // RTC_BASE_HTTPCOMMON_INL_H_