blob: c9eaffccb64f908d4bbe5991dd76350e32413d2f [file] [log] [blame]
henrike@webrtc.org0e118e72013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004--2005, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef TALK_BASE_HTTPCOMMON_INL_H__
29#define TALK_BASE_HTTPCOMMON_INL_H__
30
31#include "talk/base/common.h"
32#include "talk/base/httpcommon.h"
33
34namespace talk_base {
35
36///////////////////////////////////////////////////////////////////////////////
37// Url
38///////////////////////////////////////////////////////////////////////////////
39
40template<class CTYPE>
41void Url<CTYPE>::do_set_url(const CTYPE* val, size_t len) {
42 if (ascnicmp(val, "http://", 7) == 0) {
43 val += 7; len -= 7;
44 secure_ = false;
45 } else if (ascnicmp(val, "https://", 8) == 0) {
46 val += 8; len -= 8;
47 secure_ = true;
48 } else {
49 clear();
50 return;
51 }
52 const CTYPE* path = strchrn(val, len, static_cast<CTYPE>('/'));
53 if (!path) {
54 path = val + len;
55 }
56 size_t address_length = (path - val);
57 do_set_address(val, address_length);
58 do_set_full_path(path, len - address_length);
59}
60
61template<class CTYPE>
62void Url<CTYPE>::do_set_address(const CTYPE* val, size_t len) {
63 if (const CTYPE* at = strchrn(val, len, static_cast<CTYPE>('@'))) {
64 // Everything before the @ is a user:password combo, so skip it.
65 len -= at - val + 1;
66 val = at + 1;
67 }
68 if (const CTYPE* colon = strchrn(val, len, static_cast<CTYPE>(':'))) {
69 host_.assign(val, colon - val);
70 // Note: In every case, we're guaranteed that colon is followed by a null,
71 // or non-numeric character.
72 port_ = static_cast<uint16>(::strtoul(colon + 1, NULL, 10));
73 // TODO: Consider checking for invalid data following port number.
74 } else {
75 host_.assign(val, len);
76 port_ = HttpDefaultPort(secure_);
77 }
78}
79
80template<class CTYPE>
81void Url<CTYPE>::do_set_full_path(const CTYPE* val, size_t len) {
82 const CTYPE* query = strchrn(val, len, static_cast<CTYPE>('?'));
83 if (!query) {
84 query = val + len;
85 }
86 size_t path_length = (query - val);
87 if (0 == path_length) {
88 // TODO: consider failing in this case.
89 path_.assign(1, static_cast<CTYPE>('/'));
90 } else {
91 ASSERT(val[0] == static_cast<CTYPE>('/'));
92 path_.assign(val, path_length);
93 }
94 query_.assign(query, len - path_length);
95}
96
97template<class CTYPE>
98void Url<CTYPE>::do_get_url(string* val) const {
99 CTYPE protocol[9];
100 asccpyn(protocol, ARRAY_SIZE(protocol), secure_ ? "https://" : "http://");
101 val->append(protocol);
102 do_get_address(val);
103 do_get_full_path(val);
104}
105
106template<class CTYPE>
107void Url<CTYPE>::do_get_address(string* val) const {
108 val->append(host_);
109 if (port_ != HttpDefaultPort(secure_)) {
110 CTYPE format[5], port[32];
111 asccpyn(format, ARRAY_SIZE(format), ":%hu");
112 sprintfn(port, ARRAY_SIZE(port), format, port_);
113 val->append(port);
114 }
115}
116
117template<class CTYPE>
118void Url<CTYPE>::do_get_full_path(string* val) const {
119 val->append(path_);
120 val->append(query_);
121}
122
123template<class CTYPE>
124bool Url<CTYPE>::get_attribute(const string& name, string* value) const {
125 if (query_.empty())
126 return false;
127
128 std::string::size_type pos = query_.find(name, 1);
129 if (std::string::npos == pos)
130 return false;
131
132 pos += name.length() + 1;
133 if ((pos > query_.length()) || (static_cast<CTYPE>('=') != query_[pos-1]))
134 return false;
135
136 std::string::size_type end = query_.find(static_cast<CTYPE>('&'), pos);
137 if (std::string::npos == end) {
138 end = query_.length();
139 }
140 value->assign(query_.substr(pos, end - pos));
141 return true;
142}
143
144///////////////////////////////////////////////////////////////////////////////
145
146} // namespace talk_base
147
148#endif // TALK_BASE_HTTPCOMMON_INL_H__