ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/proj/trunk/Search/HttpHandler.cpp
Revision: 13
Committed: 2002-12-06T19:56:56-08:00 (22 years, 6 months ago) by douglas
File size: 8539 byte(s)
Log Message:
Added stuff for BSD and Windows Sockets to HttpHandler.

File Contents

# Content
1 /* ============================================================================
2 * Douglas Thrift's Search Engine License
3 *
4 * Copyright (C) 2002, 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 HTTP Handler
46 //
47 // Douglas Thrift
48 //
49 // HttpHandler.cpp
50
51 #include "HttpHandler.h"
52
53 HttpHandler::HttpHandler()
54 {
55 #ifdef _WIN32
56 WSADATA data;
57 if (WSAStartup(MAKEWORD(2, 0) != 0, &data))
58 {
59 error(program + ": WSAStartup()");
60 exit(1);
61 }
62 #endif // _WIN32
63
64 begin = 0;
65 }
66
67 HttpHandler::~HttpHandler()
68 {
69 #ifdef _WIN32
70 WSACleanup();
71 #endif // _WIN32
72 }
73
74 bool HttpHandler::connect(URL &url, bool head)
75 {
76 bool answer = false;
77
78 // if (status != JNI_ERR)
79 // {
80 // if (cls != 0)
81 // {
82 // if (mid != 0)
83 // {
84 // jstring addressJ = env->NewStringUTF(url.getAddress().c_str());
85 // jint portJ = url.getPort();
86 // jstring pathJ = env->NewStringUTF(url.getPath().c_str());
87 // jstring programNameJ = env->NewStringUTF(programName.c_str());
88 // jstring programVersionJ =
89 // env->NewStringUTF(programVersion.c_str());
90
91 // jstring pageJ = (jstring)env->CallStaticObjectMethod(cls, mid,
92 // addressJ, portJ, pathJ, programNameJ, programVersionJ);
93
94 // const char* pageC = env->GetStringUTFChars(pageJ, 0);
95 // page = pageC;
96 // env->ReleaseStringUTFChars(pageJ, pageC);
97
98 // if (page != "unknown host\n" && page != "io exception\n" &&
99 // page != "bad headers\n") answer = true;
100 // }
101 // }
102 // }
103
104 return answer;
105 }
106
107 HttpHandler& HttpHandler::getline(string& line, char endline)
108 {
109 int end = page.find(endline, begin);
110 int newline = page.find('\n', begin);
111
112 if (newline < end || end == string::npos)
113 {
114 end = newline;
115 }
116
117 line = page.substr(begin, end - begin);
118
119 if (end == string::npos)
120 {
121 begin = end;
122 }
123 else
124 {
125 begin = end + 1;
126 }
127
128 return *this;
129 }
130
131 bool HttpHandler::good()
132 {
133 bool answer = true;
134
135 if (begin >= page.length())
136 {
137 answer = false;
138 }
139 else if (begin == string::npos)
140 {
141 answer = false;
142 }
143
144 return answer;
145 }
146
147 void HttpHandler::clear()
148 {
149 begin = 0;
150 page = "";
151 }
152
153 void HttpHandler::error(const string prefix, bool host)
154 {
155 #ifdef _WIN32
156 string error;
157
158 switch (WSAGetLastError())
159 {
160 case WSAEACCES:
161 error = "Permission denied.";
162 break;
163 case WSAEADDRINUSE:
164 error = "Address already in use.";
165 break;
166 case WSAEADDRNOTAVAIL:
167 error = "Cannot assign requested address.";
168 break;
169 case WSAEAFNOSUPPORT:
170 error = "Address family not supported by protocol family.";
171 break;
172 case WSAEALREADY:
173 error = "Operation already in progress.";
174 break;
175 case WSAECONNABORTED:
176 error = "Software caused connection abort.";
177 break;
178 case WSAECONNREFUSED:
179 error = "Connection refused.";
180 break;
181 case WSAECONNRESET:
182 error = "Connection reset by peer.";
183 break;
184 case WSAEDESTADDRREQ:
185 error = "Destination address required.";
186 break;
187 case WSAEFAULT:
188 error = "Bad address.";
189 break;
190 case WSAEHOSTDOWN:
191 error = "Host is down.";
192 break;
193 case WSAEHOSTUNREACH:
194 error = "No route to host.";
195 break;
196 case WSAEINPROGRESS:
197 error = "Operation now in progress.";
198 break;
199 case WSAEINTR:
200 error = "Interrupted function call.";
201 break;
202 case WSAEINVAL:
203 error = "Invalid argument.";
204 break;
205 case WSAEISCONN:
206 error = "Socket is already connected.";
207 break;
208 case WSAEMFILE:
209 error = "Too many open files.";
210 break;
211 case WSAEMSGSIZE:
212 error = "Message too long.";
213 break;
214 case WSAENETDOWN:
215 error = "Network is down.";
216 break;
217 case WSAENETRESET:
218 error = "Network dropped connection on reset.";
219 break;
220 case WSAENETUNREACH:
221 error = "Network is unreachable.";
222 break;
223 case WSAENOBUFS:
224 error = "No buffer space available.";
225 break;
226 case WSAENOPROTOOPT:
227 error = "Bad protocol option.";
228 break;
229 case WSAENOTCONN:
230 error = "Socket is not connected.";
231 break;
232 case WSAENOTSOCK:
233 error = "Socket operation on non-socket.";
234 break;
235 case WSAEOPNOTSUPP:
236 error = "Operation not supported.";
237 break;
238 case WSAEPFNOSUPPORT:
239 error = "Protocol family not supported.";
240 break;
241 case WSAEPROCLIM:
242 error = "Too many processes.";
243 break;
244 case WSAEPROTONOSUPPORT:
245 error = "Protocol not supported.";
246 break;
247 case WSAEPROTOTYPE:
248 error = "Protocol wrong type for socket.";
249 break;
250 case WSAESHUTDOWN:
251 error = "Cannot send after socket shutdown.";
252 break;
253 case WSAESOCKTNOSUPPORT:
254 error = "Socket type not supported.";
255 break;
256 case WSAETIMEDOUT:
257 error = "Connection timed out.";
258 break;
259 case WSATYPE_NOT_FOUND:
260 error = "Class type not found.";
261 break;
262 case WSAEWOULDBLOCK:
263 error = "Resource temporarily unavailable.";
264 break;
265 case WSAHOST_NOT_FOUND:
266 error = "Host not found.";
267 break;
268 case WSA_INVALID_HANDLE:
269 error = "Specified event object handle is invalid.";
270 break;
271 case WSA_INVALID_PARAMETER:
272 error = "One or more parameters are invalid.";
273 break;
274 // case WSAINVALIDPROCTABLE:
275 // error = "Invalid procedure table from service provider.";
276 // break;
277 // case WSAINVALIDPROVIDER:
278 // error = "Invalid service provider version number.";
279 // break;
280 case WSA_IO_INCOMPLETE:
281 error = "Overlapped I/O event object not in signaled state.";
282 break;
283 case WSA_IO_PENDING:
284 error = "Overlapped operations will complete later.";
285 break;
286 case WSA_NOT_ENOUGH_MEMORY:
287 error = "Insufficient memory available.";
288 break;
289 case WSANOTINITIALISED:
290 error = "Successful WSAStartup not yet performed.";
291 break;
292 case WSANO_DATA:
293 error = "Valid name, no data record of requested type.";
294 break;
295 case WSANO_RECOVERY:
296 error = "This is a non-recoverable error.";
297 break;
298 // case WSAPROVIDERFAILEDINIT:
299 // error = "Unable to initialize a service provider.";
300 // break;
301 case WSASYSCALLFAILURE:
302 error = "System call failure.";
303 break;
304 case WSASYSNOTREADY:
305 error = "Network subsystem is unavailable.";
306 break;
307 case WSATRY_AGAIN:
308 error = "Non-authoritative host not found.";
309 break;
310 case WSAVERNOTSUPPORTED:
311 error = "WINSOCK.DLL version out of range.";
312 break;
313 case WSAEDISCON:
314 error = "Graceful shutdown in progress.";
315 break;
316 case WSA_OPERATION_ABORTED:
317 error = "Overlapped operation aborted.";
318 break;
319 default:
320 error = "Unknown error.";
321 break;
322 }
323
324 cerr << prefix << ": " << error << "\n";
325 #else
326 if (host)
327 {
328 herror(prefix.c_str());
329 }
330 else
331 {
332 perror(prefix.c_str());
333 }
334 #endif // _WIN32
335 }