1 |
douglas |
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 |
|
|
#ifdef _WIN32 |
54 |
|
|
#define PATH_SEPARATOR "\\" |
55 |
|
|
#define ENV_SEPARATOR ';' |
56 |
|
|
#else |
57 |
|
|
#define PATH_SEPARATOR "/" |
58 |
|
|
#define ENV_SEPARATOR ':' |
59 |
|
|
#endif |
60 |
|
|
|
61 |
|
|
HttpHandler::HttpHandler() |
62 |
|
|
{ |
63 |
|
|
string arguments = "(Ljava/lang/String;ILjava/lang/String;Ljava/lang/Strin" |
64 |
|
|
+ string("g;Ljava/lang/String;)Ljava/lang/String;"); |
65 |
|
|
|
66 |
|
|
begin = 0; |
67 |
|
|
|
68 |
douglas |
7 |
// setJarPath(); |
69 |
douglas |
1 |
|
70 |
|
|
// options[0].optionString = jarPath; |
71 |
|
|
// memset(&vm_args, 0, sizeof (vm_args)); |
72 |
|
|
// vm_args.version = JNI_VERSION_1_4; |
73 |
|
|
// vm_args.nOptions = 1; |
74 |
|
|
// vm_args.options = options; |
75 |
|
|
|
76 |
|
|
// status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args); |
77 |
|
|
|
78 |
|
|
// if (status != JNI_ERR) |
79 |
|
|
// { |
80 |
|
|
// cls = env->FindClass("HttpConnector"); |
81 |
|
|
|
82 |
|
|
// if (cls != 0) |
83 |
|
|
// { |
84 |
|
|
// mid = env->GetStaticMethodID(cls, "connect", arguments.c_str()); |
85 |
|
|
// } |
86 |
|
|
// } |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
HttpHandler::~HttpHandler() |
90 |
|
|
{ |
91 |
|
|
// if (status != JNI_ERR) |
92 |
|
|
// { |
93 |
|
|
// jvm->DestroyJavaVM(); |
94 |
|
|
// } |
95 |
|
|
|
96 |
|
|
// delete [] jarPath; |
97 |
|
|
} |
98 |
|
|
|
99 |
|
|
bool HttpHandler::connect(URL& url) |
100 |
|
|
{ |
101 |
|
|
bool answer = false; |
102 |
|
|
|
103 |
|
|
// if (status != JNI_ERR) |
104 |
|
|
// { |
105 |
|
|
// if (cls != 0) |
106 |
|
|
// { |
107 |
|
|
// if (mid != 0) |
108 |
|
|
// { |
109 |
|
|
// jstring addressJ = env->NewStringUTF(url.getAddress().c_str()); |
110 |
|
|
// jint portJ = url.getPort(); |
111 |
|
|
// jstring pathJ = env->NewStringUTF(url.getPath().c_str()); |
112 |
|
|
// jstring programNameJ = env->NewStringUTF(programName.c_str()); |
113 |
|
|
// jstring programVersionJ = |
114 |
|
|
// env->NewStringUTF(programVersion.c_str()); |
115 |
|
|
|
116 |
|
|
// jstring pageJ = (jstring)env->CallStaticObjectMethod(cls, mid, |
117 |
|
|
// addressJ, portJ, pathJ, programNameJ, programVersionJ); |
118 |
|
|
|
119 |
|
|
// const char* pageC = env->GetStringUTFChars(pageJ, 0); |
120 |
|
|
// page = pageC; |
121 |
|
|
// env->ReleaseStringUTFChars(pageJ, pageC); |
122 |
|
|
|
123 |
|
|
// if (page != "unknown host\n" && page != "io exception\n" && |
124 |
|
|
// page != "bad headers\n") answer = true; |
125 |
|
|
// } |
126 |
|
|
// } |
127 |
|
|
// } |
128 |
|
|
|
129 |
|
|
return answer; |
130 |
|
|
} |
131 |
|
|
|
132 |
|
|
HttpHandler& HttpHandler::getline(string& line, char endline) |
133 |
|
|
{ |
134 |
|
|
int end = page.find(endline, begin); |
135 |
|
|
int newline = page.find('\n', begin); |
136 |
|
|
|
137 |
|
|
if (newline < end || end == string::npos) |
138 |
|
|
{ |
139 |
|
|
end = newline; |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
line = page.substr(begin, end - begin); |
143 |
|
|
|
144 |
|
|
if (end == string::npos) |
145 |
|
|
{ |
146 |
|
|
begin = end; |
147 |
|
|
} |
148 |
|
|
else |
149 |
|
|
{ |
150 |
|
|
begin = end + 1; |
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
return *this; |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
bool HttpHandler::good() |
157 |
|
|
{ |
158 |
|
|
bool answer = true; |
159 |
|
|
|
160 |
|
|
if (begin >= page.length()) |
161 |
|
|
{ |
162 |
|
|
answer = false; |
163 |
|
|
} |
164 |
|
|
else if (begin == string::npos) |
165 |
|
|
{ |
166 |
|
|
answer = false; |
167 |
|
|
} |
168 |
|
|
|
169 |
|
|
return answer; |
170 |
|
|
} |
171 |
|
|
|
172 |
|
|
void HttpHandler::clear() |
173 |
|
|
{ |
174 |
|
|
begin = 0; |
175 |
|
|
page = ""; |
176 |
|
|
} |
177 |
|
|
|
178 |
douglas |
7 |
/*void HttpHandler::setJarPath() |
179 |
douglas |
1 |
{ |
180 |
|
|
const string jarFile = "HttpConnector.jar"; |
181 |
|
|
string jarFilePath = jarFile; |
182 |
|
|
|
183 |
|
|
ifstream fin(jarFilePath.c_str()); |
184 |
|
|
if (!fin.is_open()) |
185 |
|
|
{ |
186 |
|
|
unsigned end = program.rfind(PATH_SEPARATOR); |
187 |
|
|
if (end != string::npos) |
188 |
|
|
{ |
189 |
|
|
string path = program.substr(0, end); |
190 |
|
|
|
191 |
|
|
jarFilePath = path + PATH_SEPARATOR + jarFile; |
192 |
|
|
|
193 |
|
|
fin.open(jarFilePath.c_str()); |
194 |
|
|
if (!fin.is_open()) |
195 |
|
|
{ |
196 |
|
|
cerr << program << ": Could not find required file: " |
197 |
|
|
<< jarFile << "\n"; |
198 |
|
|
exit(1); |
199 |
|
|
} |
200 |
|
|
else |
201 |
|
|
{ |
202 |
|
|
fin.close(); |
203 |
|
|
} |
204 |
|
|
} |
205 |
|
|
else |
206 |
|
|
{ |
207 |
|
|
string envPath = getenv("PATH"); |
208 |
|
|
|
209 |
|
|
if (debug) cerr << "envPath = " << envPath << "\n"; |
210 |
|
|
|
211 |
|
|
unsigned begin = 0; |
212 |
|
|
do |
213 |
|
|
{ |
214 |
|
|
unsigned end = envPath.find(ENV_SEPARATOR, begin); |
215 |
|
|
string path = envPath.substr(begin, end); |
216 |
|
|
|
217 |
|
|
if (debug) cerr << "path = " << path << "\n"; |
218 |
|
|
|
219 |
|
|
jarFilePath = path + PATH_SEPARATOR + jarFile; |
220 |
|
|
|
221 |
|
|
fin.open(jarFilePath.c_str()); |
222 |
|
|
if (fin.is_open()) |
223 |
|
|
{ |
224 |
|
|
fin.close(); |
225 |
|
|
break; |
226 |
|
|
} |
227 |
|
|
|
228 |
|
|
begin = end != string::npos ? end + 1 : string::npos; |
229 |
|
|
} |
230 |
|
|
while (begin < envPath.length()); |
231 |
|
|
|
232 |
|
|
if (begin == string::npos) |
233 |
|
|
{ |
234 |
|
|
cerr << program << ": Could not find required file: " |
235 |
|
|
<< jarFile << "\n"; |
236 |
|
|
exit(1); |
237 |
|
|
} |
238 |
|
|
} |
239 |
|
|
} |
240 |
|
|
else |
241 |
|
|
{ |
242 |
|
|
fin.close(); |
243 |
|
|
} |
244 |
|
|
|
245 |
|
|
string jarPath = "-Djava.class.path=" + jarFilePath; |
246 |
|
|
|
247 |
|
|
// this->jarPath = new char[jarPath.length()]; |
248 |
|
|
// strcpy(this->jarPath, jarPath.c_str()); |
249 |
douglas |
7 |
}*/ |