63 |
|
#endif // _WIN32 |
64 |
|
|
65 |
|
begin = 0; |
66 |
+ |
length = 0; |
67 |
+ |
chunked = false; |
68 |
|
} |
69 |
|
|
70 |
|
HttpHandler::~HttpHandler() |
133 |
|
delete [] port; |
134 |
|
} |
135 |
|
|
136 |
+ |
// putline("Referer: " + ?referer?); |
137 |
+ |
putline("Connection: close"); |
138 |
|
putline(); |
139 |
|
|
140 |
< |
string line = getline(); |
140 |
> |
code response; |
141 |
> |
string line; |
142 |
|
|
143 |
< |
if (line.find("HTTP/") != 0) |
143 |
> |
do |
144 |
|
{ |
145 |
< |
return answer; |
141 |
< |
} |
145 |
> |
line = getline(); |
146 |
|
|
147 |
< |
unsigned dot = line.find('.'); |
148 |
< |
unsigned space = line.find(' '); |
147 |
> |
if (line.find("HTTP/") != 0) |
148 |
> |
{ |
149 |
> |
return answer; |
150 |
> |
} |
151 |
|
|
152 |
< |
unsigned major = strtoul(line.substr(5, dot - 5).c_str(), 0, 0); |
153 |
< |
unsigned minor = strtoul(line.substr(dot + 1, space - dot - 1).c_str(), 0, |
148 |
< |
0); |
152 |
> |
unsigned dot = line.find('.'); |
153 |
> |
unsigned space = line.find(' '); |
154 |
|
|
155 |
< |
if (major > 1 || minor < 1) |
156 |
< |
{ |
157 |
< |
cerr << program << ": Potentially Incompatible Server: HTTP/" << major |
153 |
< |
<< "." << minor << "\n"; |
155 |
> |
unsigned major = strtoul(line.substr(5, dot - 5).c_str(), 0, 10); |
156 |
> |
unsigned minor = strtoul(line.substr(dot + 1, space - dot - 1).c_str(), |
157 |
> |
0, 10); |
158 |
|
|
159 |
< |
return answer; |
160 |
< |
} |
159 |
> |
if (major > 1 || minor < 1) |
160 |
> |
{ |
161 |
> |
cerr << program << ": Potentially Incompatible Server: HTTP/" << |
162 |
> |
major << "." << minor << "\n"; |
163 |
|
|
164 |
< |
code response = code(strtoul(line.substr(space + 1).c_str(), 0, 0)); |
164 |
> |
return answer; |
165 |
> |
} |
166 |
> |
|
167 |
> |
response = code(strtoul(line.substr(space + 1).c_str(), 0, 10)); |
168 |
> |
|
169 |
> |
if (response < ok) do line = getline(); while (line != ""); |
170 |
> |
} |
171 |
> |
while (response < ok); |
172 |
|
|
173 |
|
do |
174 |
|
{ |
175 |
|
line = getline(); |
176 |
+ |
|
177 |
+ |
if (line != "") |
178 |
+ |
{ |
179 |
+ |
unsigned colon = line.find(':'); |
180 |
+ |
|
181 |
+ |
string field = line.substr(0, colon); |
182 |
+ |
string value = line.substr(colon + 1); |
183 |
+ |
|
184 |
+ |
while (isspace(value[0])) value.erase(0, 1); |
185 |
+ |
|
186 |
+ |
if (field == "Content-Type") |
187 |
+ |
{ |
188 |
+ |
type = value; |
189 |
+ |
} |
190 |
+ |
else if (field == "Content-Length") |
191 |
+ |
{ |
192 |
+ |
length = strtoul(value.c_str(), 0, 10); |
193 |
+ |
} |
194 |
+ |
else if (field == "Location") |
195 |
+ |
{ |
196 |
+ |
location = value; |
197 |
+ |
} |
198 |
+ |
else if (field == "Transfer-Encoding") |
199 |
+ |
{ |
200 |
+ |
chunked = value == "chunked"; |
201 |
+ |
} |
202 |
+ |
} |
203 |
|
} |
204 |
|
while (line != ""); |
205 |
|
|
206 |
|
switch (response) |
207 |
|
{ |
208 |
|
case ok: |
209 |
+ |
if (debug) cerr << "response = " << response << "\n"; |
210 |
|
answer = true; |
211 |
|
break; |
212 |
< |
case choices: |
172 |
< |
break; |
212 |
> |
case choices: |
213 |
|
case moved: |
174 |
– |
break; |
214 |
|
case found: |
215 |
+ |
if (debug) cerr << "response = " << response << "\n" |
216 |
+ |
<< "location = " << location << "\n"; |
217 |
+ |
location = getLink(location, url); |
218 |
|
break; |
219 |
|
case notfound: |
178 |
– |
break; |
220 |
|
case internal: |
221 |
+ |
if (debug) cerr << "response = " << response << "\n"; |
222 |
|
break; |
223 |
|
default: |
224 |
+ |
if (debug) cerr << "response = " << response << "\n"; |
225 |
+ |
if (response <= 299) |
226 |
+ |
{ |
227 |
+ |
answer = true; |
228 |
+ |
} |
229 |
+ |
else if (response <= 399) |
230 |
+ |
{ |
231 |
+ |
location = getLink(location, url); |
232 |
+ |
} |
233 |
|
break; |
234 |
|
} |
235 |
|
|
236 |
+ |
if (!head && answer) populate(); |
237 |
+ |
|
238 |
|
return answer; |
239 |
|
} |
240 |
|
|
280 |
|
|
281 |
|
void HttpHandler::clear() |
282 |
|
{ |
283 |
+ |
closesocket(http); |
284 |
+ |
|
285 |
|
type = ""; |
286 |
|
length = 0; |
287 |
|
location = ""; |
288 |
|
begin = 0; |
289 |
|
page = ""; |
290 |
+ |
chunked = false; |
291 |
+ |
} |
292 |
+ |
|
293 |
+ |
void HttpHandler::populate() |
294 |
+ |
{ |
295 |
+ |
if (!chunked) |
296 |
+ |
{ |
297 |
+ |
unsigned left = length; |
298 |
+ |
|
299 |
+ |
while (left > 0) |
300 |
+ |
{ |
301 |
+ |
memset(buffer, 0, BUFSIZ + 1); |
302 |
+ |
|
303 |
+ |
unsigned bytes = left > BUFSIZ ? BUFSIZ : left; |
304 |
+ |
|
305 |
+ |
if (recv(http, buffer, bytes, 0) == SOCKET_ERROR) |
306 |
+ |
{ |
307 |
+ |
error(program + ": Revc"); |
308 |
+ |
exit(1); |
309 |
+ |
} |
310 |
+ |
|
311 |
+ |
page += buffer; |
312 |
+ |
left -= bytes; |
313 |
+ |
} |
314 |
+ |
} |
315 |
+ |
else |
316 |
+ |
{ |
317 |
+ |
// |
318 |
+ |
} |
319 |
+ |
|
320 |
+ |
cerr << "\n[" << page << "]\n"; |
321 |
|
} |
322 |
|
|
323 |
|
void HttpHandler::putline(const string line) |
352 |
|
return line; |
353 |
|
} |
354 |
|
|
355 |
< |
void HttpHandler::error(const string prefix, bool host) |
355 |
> |
void HttpHandler::error(const string& prefix, bool host) |
356 |
|
{ |
357 |
|
#ifdef _WIN32 |
358 |
|
string error; |