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

File Contents

# User Rev Content
1 douglas 1 /* ============================================================================
2     * Douglas Thrift's Search Engine License
3     *
4 douglas 312 * Copyright (C) 2002-2004, Douglas Thrift. All Rights Reserved.
5 douglas 1 * 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 Douglas Thrift 331 // $Id$
50 douglas 1
51 Douglas Thrift 334 #include "URL.hpp"
52 douglas 1
53     string URL::getURL()
54     {
55 douglas 195 ostringstream url;
56 Douglas Thrift 348
57 douglas 195 #ifndef _OpenSSL_
58     url << "http://" << address;
59 douglas 1
60     if (port != 80)
61 douglas 195 #else
62     url << (tls ? "https://" : "http://") << address;
63    
64     if (port != 80 && !tls || port != 443 && tls)
65     #endif
66 douglas 1 {
67 douglas 195 url << ":" << port;
68 douglas 1 }
69    
70 douglas 195 url << path;
71 douglas 1
72 douglas 195 return url.str();
73 douglas 1 }
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 douglas 195 #ifdef _OpenSSL_
81     this->tls = url.tls;
82     #endif
83 douglas 1 }
84    
85     void URL::setURL(const string& url)
86     {
87 douglas 195 #ifndef _OpenSSL_
88     if (url.find("http://") != 0 || url.length() <= 7)
89 douglas 1 {
90     cerr << program << ": Malformed URL: " << url << "\n";
91     exit(1);
92     }
93    
94 Douglas Thrift 348 unsigned begin(7);
95    
96 douglas 195 #else
97     tls = false;
98 douglas 1
99 douglas 195 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 Douglas Thrift 348
107 douglas 195 exit(1);
108     }
109    
110 Douglas Thrift 348 unsigned begin(tls ? 8 : 7);
111 douglas 195 #endif
112    
113 Douglas Thrift 348 unsigned colon(url.find(':', begin)), end(url.find('/', begin));
114    
115 douglas 1 if (colon != string::npos && colon < end)
116     {
117     address = url.substr(begin, colon - begin);
118 douglas 212
119     istringstream number((url.substr(colon + 1, end - colon - 1)));
120    
121     number >> port;
122 douglas 1 }
123     else
124     {
125     address = url.substr(begin, end - begin);
126 douglas 195 #ifndef _OpenSSL_
127 douglas 1 port = 80;
128 douglas 195 #else
129     port = tls ? 443 : 80;
130     #endif
131 douglas 1 }
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 douglas 17
162     string getLink(string link, URL& url)
163     {
164 Douglas Thrift 348 string hyperlink;
165 douglas 17
166     if (link.find('#') != string::npos)
167     {
168 Douglas Thrift 348 unsigned pound(link.find('#'));
169    
170 douglas 17 link.erase(pound);
171     }
172    
173     if (link.find("://") != string::npos)
174     {
175 douglas 195 #ifndef _OpenSSL_
176 douglas 22 if (link.find("http://") == 0 && link.length() > 7) hyperlink = link;
177 douglas 195 #else
178     if (link.find("http://") == 0 && link.length() > 7 ||
179     link.find("https://") == 0 && link.length() > 8) hyperlink = link;
180     #endif
181 douglas 17 }
182     else if (link.find("mailto:") == 0)
183     {
184     // do nothing we are not evil spammers!
185     }
186 douglas 18 else if (link.find("news:") == 0)
187     {
188     // do nothing this isn't Google Groups
189     }
190 Douglas Thrift 348 else if (link.find("aim:") == 0)
191     {
192     // do nothing we don't do AIM
193     }
194 douglas 17 else if (link.find("//") == 0)
195     {
196 douglas 195 #ifndef _OpenSSL_
197 douglas 17 hyperlink = "http:" + link;
198 douglas 195 #else
199     hyperlink = (url.getTls() ? "https:" : "http:") + link;
200     #endif
201 douglas 17 }
202     else if (link.find('/') == 0)
203     {
204     hyperlink = url.getURL();
205    
206 douglas 195 #ifndef _OpenSSL_
207 Douglas Thrift 348 unsigned path(hyperlink.find('/', 7));
208 douglas 195 #else
209 Douglas Thrift 348 unsigned path(hyperlink.find('/', url.getTls() ? 8 : 7));
210 douglas 195 #endif
211 Douglas Thrift 348
212 douglas 17 hyperlink.erase(path);
213    
214     hyperlink += link;
215     }
216 Douglas Thrift 355 else if (link.empty())
217 douglas 17 {
218     // a blank link is useless
219     }
220     else
221     {
222     hyperlink = url.getURL();
223    
224 Douglas Thrift 348 string path(url.getPath());
225     unsigned cutoff(hyperlink.rfind(path));
226    
227 douglas 17 hyperlink.erase(cutoff);
228    
229 Douglas Thrift 348 unsigned dir(path.rfind('/') + 1);
230    
231 douglas 17 path.erase(dir);
232    
233     while (link.find("../") == 0)
234     {
235 Douglas Thrift 348 unsigned dot(path.rfind('/') - 1), up(path.rfind('/', dot) + 1);
236 douglas 17
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