46 |
|
// |
47 |
|
// Douglas Thrift |
48 |
|
// |
49 |
< |
// $Id: URL.cpp,v 1.6 2003/07/11 07:54:47 douglas Exp $ |
49 |
> |
// $Id: URL.cpp,v 1.7 2003/07/12 05:50:04 douglas Exp $ |
50 |
|
|
51 |
|
#include "URL.h" |
52 |
|
|
60 |
|
this->address = address; |
61 |
|
this->port = port; |
62 |
|
this->path = path; |
63 |
+ |
#ifdef _OpenSSL_ |
64 |
+ |
tls = false; |
65 |
+ |
#endif |
66 |
|
} |
67 |
|
|
68 |
+ |
#ifdef _OpenSSL_ |
69 |
+ |
URL::URL(const string& address, unsigned port, const string& path, bool tls) |
70 |
+ |
{ |
71 |
+ |
this->address = address; |
72 |
+ |
this->port = port; |
73 |
+ |
this->path = path; |
74 |
+ |
this->tls = tls; |
75 |
+ |
} |
76 |
+ |
#endif |
77 |
+ |
|
78 |
|
string URL::getURL() |
79 |
|
{ |
80 |
< |
string url = "http://" + address; |
80 |
> |
ostringstream url; |
81 |
> |
#ifndef _OpenSSL_ |
82 |
> |
url << "http://" << address; |
83 |
|
|
84 |
|
if (port != 80) |
85 |
< |
{ |
86 |
< |
char* cport = new char[1024]; |
85 |
> |
#else |
86 |
> |
url << (tls ? "https://" : "http://") << address; |
87 |
|
|
88 |
< |
sprintf(cport, "%u", port); |
89 |
< |
|
90 |
< |
url += string(":") + cport; |
91 |
< |
|
77 |
< |
delete [] cport; |
88 |
> |
if (port != 80 && !tls || port != 443 && tls) |
89 |
> |
#endif |
90 |
> |
{ |
91 |
> |
url << ":" << port; |
92 |
|
} |
93 |
|
|
94 |
< |
url += path; |
94 |
> |
url << path; |
95 |
|
|
96 |
< |
return url; |
96 |
> |
return url.str(); |
97 |
|
} |
98 |
|
|
99 |
|
void URL::setURL(const URL& url) |
101 |
|
this->address = url.address; |
102 |
|
this->port = url.port; |
103 |
|
this->path = url.path; |
104 |
+ |
#ifdef _OpenSSL_ |
105 |
+ |
this->tls = url.tls; |
106 |
+ |
#endif |
107 |
|
} |
108 |
|
|
109 |
|
void URL::setURL(const string& url) |
110 |
|
{ |
111 |
< |
if (url.find("http://") || url.length() <= 7) |
111 |
> |
#ifndef _OpenSSL_ |
112 |
> |
if (url.find("http://") != 0 || url.length() <= 7) |
113 |
> |
{ |
114 |
> |
cerr << program << ": Malformed URL: " << url << "\n"; |
115 |
> |
exit(1); |
116 |
> |
} |
117 |
> |
|
118 |
> |
unsigned begin = 7; |
119 |
> |
#else |
120 |
> |
tls = false; |
121 |
> |
|
122 |
> |
if (url.find("https://") == 0 && url.length() > 8) |
123 |
> |
{ |
124 |
> |
tls = true; |
125 |
> |
} |
126 |
> |
else if (url.find("http://") != 0 || url.length() <= 7) |
127 |
|
{ |
128 |
|
cerr << program << ": Malformed URL: " << url << "\n"; |
129 |
|
exit(1); |
130 |
|
} |
131 |
|
|
132 |
< |
int begin = 7; |
133 |
< |
int colon = url.find(':', begin); |
134 |
< |
int end = url.find('/', begin); |
132 |
> |
unsigned begin = tls ? 8 : 7; |
133 |
> |
#endif |
134 |
> |
unsigned colon = url.find(':', begin); |
135 |
> |
unsigned end = url.find('/', begin); |
136 |
|
|
137 |
|
if (colon != string::npos && colon < end) |
138 |
|
{ |
142 |
|
else |
143 |
|
{ |
144 |
|
address = url.substr(begin, end - begin); |
145 |
+ |
#ifndef _OpenSSL_ |
146 |
|
port = 80; |
147 |
+ |
#else |
148 |
+ |
port = tls ? 443 : 80; |
149 |
+ |
#endif |
150 |
|
} |
151 |
|
|
152 |
|
if (end == string::npos) |
159 |
|
} |
160 |
|
} |
161 |
|
|
125 |
– |
void URL::setAddress(const string& address) |
126 |
– |
{ |
127 |
– |
this->address = address; |
128 |
– |
} |
129 |
– |
|
130 |
– |
void URL::setPort(unsigned port) |
131 |
– |
{ |
132 |
– |
this->port = port; |
133 |
– |
} |
134 |
– |
|
162 |
|
void URL::setPath(const string& path) |
163 |
|
{ |
164 |
|
if (path.find('/') != 0) |
190 |
|
|
191 |
|
if (link.find("://") != string::npos) |
192 |
|
{ |
193 |
+ |
#ifndef _OpenSSL_ |
194 |
|
if (link.find("http://") == 0 && link.length() > 7) hyperlink = link; |
195 |
+ |
#else |
196 |
+ |
if (link.find("http://") == 0 && link.length() > 7 || |
197 |
+ |
link.find("https://") == 0 && link.length() > 8) hyperlink = link; |
198 |
+ |
#endif |
199 |
|
} |
200 |
|
else if (link.find("mailto:") == 0) |
201 |
|
{ |
207 |
|
} |
208 |
|
else if (link.find("//") == 0) |
209 |
|
{ |
210 |
+ |
#ifndef _OpenSSL_ |
211 |
|
hyperlink = "http:" + link; |
212 |
+ |
#else |
213 |
+ |
hyperlink = (url.getTls() ? "https:" : "http:") + link; |
214 |
+ |
#endif |
215 |
|
} |
216 |
|
else if (link.find('/') == 0) |
217 |
|
{ |
218 |
|
hyperlink = url.getURL(); |
219 |
|
|
220 |
+ |
#ifndef _OpenSSL_ |
221 |
|
unsigned path = hyperlink.find('/', 7); |
222 |
+ |
#else |
223 |
+ |
unsigned path = hyperlink.find('/', url.getTls() ? 8 : 7); |
224 |
+ |
#endif |
225 |
|
hyperlink.erase(path); |
226 |
|
|
227 |
|
hyperlink += link; |