ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/proj/trunk/Search/URL.cpp
Revision: 355
Committed: 2004-06-04T04:08:28-07:00 (21 years ago) by Douglas Thrift
File size: 5746 byte(s)
Log Message:
I missed some C++ifying!

File Contents

# Content
1 /* ============================================================================
2 * Douglas Thrift's Search Engine License
3 *
4 * Copyright (C) 2002-2004, Douglas Thrift. All Rights Reserved.
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 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * 3. The end-user documentation included with the redistribution, if any, must
16 * include the following acknowledgment:
17 *
18 * "This product includes software developed by Douglas Thrift
19 * (http://computers.douglasthrift.net/searchengine/)."
20 *
21 * Alternately, this acknowledgment may appear in the software itself, if
22 * and wherever such third-party acknowledgments normally appear.
23 *
24 * 4. The names "Douglas Thrift" and "Douglas Thrift's Search Engine" must not
25 * be used to endorse or promote products derived from this software without
26 * specific prior written permission. For written permission, please visit
27 * http://www.douglasthrift.net/contact.cgi for contact information.
28 *
29 * 5. Products derived from this software may not be called "Douglas Thrift's
30 * Search Engine", nor may "Douglas Thrift's Search Engine" appear in their
31 * name, without prior written permission.
32 *
33 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
34 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
35 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
36 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
37 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
39 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
40 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
41 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
42 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 * ============================================================================
44 */
45 // Douglas Thrift's Search Engine URL
46 //
47 // Douglas Thrift
48 //
49 // $Id$
50
51 #include "URL.hpp"
52
53 string URL::getURL()
54 {
55 ostringstream url;
56
57 #ifndef _OpenSSL_
58 url << "http://" << address;
59
60 if (port != 80)
61 #else
62 url << (tls ? "https://" : "http://") << address;
63
64 if (port != 80 && !tls || port != 443 && tls)
65 #endif
66 {
67 url << ":" << port;
68 }
69
70 url << path;
71
72 return url.str();
73 }
74
75 void URL::setURL(const URL& url)
76 {
77 this->address = url.address;
78 this->port = url.port;
79 this->path = url.path;
80 #ifdef _OpenSSL_
81 this->tls = url.tls;
82 #endif
83 }
84
85 void URL::setURL(const string& url)
86 {
87 #ifndef _OpenSSL_
88 if (url.find("http://") != 0 || url.length() <= 7)
89 {
90 cerr << program << ": Malformed URL: " << url << "\n";
91 exit(1);
92 }
93
94 unsigned begin(7);
95
96 #else
97 tls = false;
98
99 if (url.find("https://") == 0 && url.length() > 8)
100 {
101 tls = true;
102 }
103 else if (url.find("http://") != 0 || url.length() <= 7)
104 {
105 cerr << program << ": Malformed URL: " << url << "\n";
106
107 exit(1);
108 }
109
110 unsigned begin(tls ? 8 : 7);
111 #endif
112
113 unsigned colon(url.find(':', begin)), end(url.find('/', begin));
114
115 if (colon != string::npos && colon < end)
116 {
117 address = url.substr(begin, colon - begin);
118
119 istringstream number((url.substr(colon + 1, end - colon - 1)));
120
121 number >> port;
122 }
123 else
124 {
125 address = url.substr(begin, end - begin);
126 #ifndef _OpenSSL_
127 port = 80;
128 #else
129 port = tls ? 443 : 80;
130 #endif
131 }
132
133 if (end == string::npos)
134 {
135 path = "/";
136 }
137 else
138 {
139 path = url.substr(end);
140 }
141 }
142
143 void URL::setPath(const string& path)
144 {
145 if (path.find('/') != 0)
146 {
147 this->path = "/" + path;
148 }
149 else
150 {
151 this->path = path;
152 }
153 }
154
155 ostream& operator<<(ostream& os, URL& data)
156 {
157 os << data.getURL();
158
159 return os;
160 }
161
162 string getLink(string link, URL& url)
163 {
164 string hyperlink;
165
166 if (link.find('#') != string::npos)
167 {
168 unsigned pound(link.find('#'));
169
170 link.erase(pound);
171 }
172
173 if (link.find("://") != string::npos)
174 {
175 #ifndef _OpenSSL_
176 if (link.find("http://") == 0 && link.length() > 7) hyperlink = link;
177 #else
178 if (link.find("http://") == 0 && link.length() > 7 ||
179 link.find("https://") == 0 && link.length() > 8) hyperlink = link;
180 #endif
181 }
182 else if (link.find("mailto:") == 0)
183 {
184 // do nothing we are not evil spammers!
185 }
186 else if (link.find("news:") == 0)
187 {
188 // do nothing this isn't Google Groups
189 }
190 else if (link.find("aim:") == 0)
191 {
192 // do nothing we don't do AIM
193 }
194 else if (link.find("//") == 0)
195 {
196 #ifndef _OpenSSL_
197 hyperlink = "http:" + link;
198 #else
199 hyperlink = (url.getTls() ? "https:" : "http:") + link;
200 #endif
201 }
202 else if (link.find('/') == 0)
203 {
204 hyperlink = url.getURL();
205
206 #ifndef _OpenSSL_
207 unsigned path(hyperlink.find('/', 7));
208 #else
209 unsigned path(hyperlink.find('/', url.getTls() ? 8 : 7));
210 #endif
211
212 hyperlink.erase(path);
213
214 hyperlink += link;
215 }
216 else if (link.empty())
217 {
218 // a blank link is useless
219 }
220 else
221 {
222 hyperlink = url.getURL();
223
224 string path(url.getPath());
225 unsigned cutoff(hyperlink.rfind(path));
226
227 hyperlink.erase(cutoff);
228
229 unsigned dir(path.rfind('/') + 1);
230
231 path.erase(dir);
232
233 while (link.find("../") == 0)
234 {
235 unsigned dot(path.rfind('/') - 1), up(path.rfind('/', dot) + 1);
236
237 path.erase(up);
238 link.erase(0, 3);
239 }
240 while (link.find("./") == 0)
241 {
242 link.erase(0, 2);
243 }
244
245 hyperlink += path + link;
246 }
247
248 return hyperlink;
249 }

Properties

Name Value
svn:eol-style native
svn:keywords Id