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 
11 #ifndef WEBRTC_BASE_HTTPCOMMON_INL_H__
12 #define WEBRTC_BASE_HTTPCOMMON_INL_H__
13 
14 #include "webrtc/base/arraysize.h"
15 #include "webrtc/base/checks.h"
16 #include "webrtc/base/common.h"
17 #include "webrtc/base/httpcommon.h"
18 
19 namespace rtc {
20 
21 ///////////////////////////////////////////////////////////////////////////////
22 // Url
23 ///////////////////////////////////////////////////////////////////////////////
24 
25 template<class CTYPE>
do_set_url(const CTYPE * val,size_t len)26 void Url<CTYPE>::do_set_url(const CTYPE* val, size_t len) {
27   if (ascnicmp(val, "http://", 7) == 0) {
28     val += 7; len -= 7;
29     secure_ = false;
30   } else if (ascnicmp(val, "https://", 8) == 0) {
31     val += 8; len -= 8;
32     secure_ = true;
33   } else {
34     clear();
35     return;
36   }
37   const CTYPE* path = strchrn(val, len, static_cast<CTYPE>('/'));
38   if (!path) {
39     path = val + len;
40   }
41   size_t address_length = (path - val);
42   do_set_address(val, address_length);
43   do_set_full_path(path, len - address_length);
44 }
45 
46 template<class CTYPE>
do_set_address(const CTYPE * val,size_t len)47 void Url<CTYPE>::do_set_address(const CTYPE* val, size_t len) {
48   if (const CTYPE* at = strchrn(val, len, static_cast<CTYPE>('@'))) {
49     // Everything before the @ is a user:password combo, so skip it.
50     len -= at - val + 1;
51     val = at + 1;
52   }
53   if (const CTYPE* colon = strchrn(val, len, static_cast<CTYPE>(':'))) {
54     host_.assign(val, colon - val);
55     // Note: In every case, we're guaranteed that colon is followed by a null,
56     // or non-numeric character.
57     port_ = static_cast<uint16_t>(::strtoul(colon + 1, NULL, 10));
58     // TODO: Consider checking for invalid data following port number.
59   } else {
60     host_.assign(val, len);
61     port_ = HttpDefaultPort(secure_);
62   }
63 }
64 
65 template<class CTYPE>
do_set_full_path(const CTYPE * val,size_t len)66 void Url<CTYPE>::do_set_full_path(const CTYPE* val, size_t len) {
67   const CTYPE* query = strchrn(val, len, static_cast<CTYPE>('?'));
68   if (!query) {
69     query = val + len;
70   }
71   size_t path_length = (query - val);
72   if (0 == path_length) {
73     // TODO: consider failing in this case.
74     path_.assign(1, static_cast<CTYPE>('/'));
75   } else {
76     RTC_DCHECK(val[0] == static_cast<CTYPE>('/'));
77     path_.assign(val, path_length);
78   }
79   query_.assign(query, len - path_length);
80 }
81 
82 template<class CTYPE>
do_get_url(string * val)83 void Url<CTYPE>::do_get_url(string* val) const {
84   CTYPE protocol[9];
85   asccpyn(protocol, arraysize(protocol), secure_ ? "https://" : "http://");
86   val->append(protocol);
87   do_get_address(val);
88   do_get_full_path(val);
89 }
90 
91 template<class CTYPE>
do_get_address(string * val)92 void Url<CTYPE>::do_get_address(string* val) const {
93   val->append(host_);
94   if (port_ != HttpDefaultPort(secure_)) {
95     CTYPE format[5], port[32];
96     asccpyn(format, arraysize(format), ":%hu");
97     sprintfn(port, arraysize(port), format, port_);
98     val->append(port);
99   }
100 }
101 
102 template<class CTYPE>
do_get_full_path(string * val)103 void Url<CTYPE>::do_get_full_path(string* val) const {
104   val->append(path_);
105   val->append(query_);
106 }
107 
108 template<class CTYPE>
get_attribute(const string & name,string * value)109 bool Url<CTYPE>::get_attribute(const string& name, string* value) const {
110   if (query_.empty())
111     return false;
112 
113   std::string::size_type pos = query_.find(name, 1);
114   if (std::string::npos == pos)
115     return false;
116 
117   pos += name.length() + 1;
118   if ((pos > query_.length()) || (static_cast<CTYPE>('=') != query_[pos-1]))
119     return false;
120 
121   std::string::size_type end = query_.find(static_cast<CTYPE>('&'), pos);
122   if (std::string::npos == end) {
123     end = query_.length();
124   }
125   value->assign(query_.substr(pos, end - pos));
126   return true;
127 }
128 
129 ///////////////////////////////////////////////////////////////////////////////
130 
131 }  // namespace rtc
132 
133 #endif  // WEBRTC_BASE_HTTPCOMMON_INL_H__
134