1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
/*
* Copyright 2005-2013 Fabrice Colin
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdarg.h>
#include <pthread.h>
#include <iostream>
#include <neon/ne_socket.h>
#include <neon/ne_session.h>
#include <neon/ne_request.h>
#include "Url.h"
#include "HtmlFilter.h"
#include "FilterUtils.h"
#include "NeonDownloader.h"
using namespace std;
unsigned int NeonDownloader::m_initialized = 0;
NeonDownloader::NeonDownloader() :
DownloaderInterface()
{
if (m_initialized == 0)
{
// Initialize
ne_sock_init();
++m_initialized;
}
}
NeonDownloader::~NeonDownloader()
{
--m_initialized;
if (m_initialized == 0)
{
// Shutdown
ne_sock_exit();
}
}
string NeonDownloader::handleRedirection(const char *pBody, unsigned int length)
{
if ((pBody == NULL) ||
(length == 0))
{
return "";
}
Document doc;
Dijon::HtmlFilter htmlFilter("text/html");
set<Dijon::Link> linksSet;
doc.setData(pBody, length);
// Extract the link from the 3xx message
if ((FilterUtils::feedFilter(doc, &htmlFilter) == true) &&
(htmlFilter.get_links(linksSet) == true))
{
// There should be one and only one
if (linksSet.size() > 1)
{
#ifdef DEBUG
clog << "NeonDownloader::handleRedirection: " << linksSet.size() << " links found in " << length << " bytes" << endl;
clog << "NeonDownloader::handleRedirection: redirection message was " << pBody << endl;
#endif
return "";
}
set<Dijon::Link>::const_iterator iter = linksSet.begin();
if (iter != linksSet.end())
{
// Update the URL
return iter->m_url;
}
}
return "";
}
//
// Implementation of DownloaderInterface
//
/// Retrieves the specified document; NULL if error.
Document *NeonDownloader::retrieveUrl(const DocumentInfo &docInfo)
{
map<string, string> headers;
return retrieveUrl(docInfo, headers);
}
/// Retrieves the specified document; NULL if error.
Document *NeonDownloader::retrieveUrl(const DocumentInfo &docInfo,
const map<string, string> &headers)
{
Document *pDocument = NULL;
string url = Url::escapeUrl(docInfo.getLocation());
char *pContent = NULL;
size_t contentLen = 0;
int statusCode = 200;
unsigned int redirectionsCount = 0;
if (url.empty() == true)
{
#ifdef DEBUG
clog << "NeonDownloader::retrieveUrl: no URL specified !" << endl;
#endif
return NULL;
}
Url urlObj(url);
string protocol(urlObj.getProtocol());
string hostName(urlObj.getHost());
string location(urlObj.getLocation());
string file(urlObj.getFile());
string parameters(urlObj.getParameters());
string lastModifiedHeaderValue, locationHeaderValue, contentTypeHeaderValue;
// Create a session
ne_session *pSession = ne_session_create(protocol.c_str(), hostName.c_str(), 80); // urlObj.getPort());
if (pSession == NULL)
{
#ifdef DEBUG
clog << "NeonDownloader::retrieveUrl: couldn't create session !" << endl;
#endif
return NULL;
}
// Set the user agent
ne_set_useragent(pSession, m_userAgent.c_str());
// ...and the timeout
ne_set_read_timeout(pSession, (int)m_timeout);
// Is a proxy defined ?
if ((m_proxyAddress.empty() == false) &&
(m_proxyPort > 0))
{
// Type is HTTP
ne_session_proxy(pSession, m_proxyAddress.c_str(), m_proxyPort);
}
string fullLocation = "/";
if (location.empty() == false)
{
fullLocation += location;
}
if (file.empty() == false)
{
if (location.empty() == false)
{
fullLocation += "/";
}
fullLocation += file;
}
if (parameters.empty() == false)
{
fullLocation += "?";
fullLocation += parameters;
}
// Create a request for this URL
ne_request *pRequest = NULL;
if (m_method == "POST")
{
pRequest = ne_request_create(pSession, "POST", fullLocation.c_str());
if ((pRequest != NULL) &&
(m_postFields.empty() == false))
{
ne_set_request_body_buffer(pRequest, m_postFields.c_str(), m_postFields.length());
}
}
else
{
pRequest = ne_request_create(pSession, "GET", fullLocation.c_str());
}
if (pRequest == NULL)
{
#ifdef DEBUG
clog << "NeonDownloader::retrieveUrl: couldn't create request !" << endl;
#endif
ne_session_destroy(pSession);
return NULL;
}
#ifdef DEBUG
clog << "NeonDownloader::retrieveUrl: request for " << fullLocation << " on " << hostName << endl;
#endif
int requestStatus = NE_RETRY;
while (requestStatus == NE_RETRY)
{
lastModifiedHeaderValue.clear();
locationHeaderValue.clear();
contentTypeHeaderValue.clear();
// Begin the request
requestStatus = ne_begin_request(pRequest);
#ifdef DEBUG
clog << "NeonDownloader::retrieveUrl: request begun with status " << requestStatus << endl;
#endif
if (requestStatus == NE_OK)
{
ssize_t bytesRead = 0;
char buffer[1024];
// Get the status
const ne_status *pStatus = ne_get_status(pRequest);
if (pStatus != NULL)
{
statusCode = pStatus->code;
#ifdef DEBUG
clog << "NeonDownloader::retrieveUrl: status is " << statusCode << endl;
#endif
}
else
{
// Assume all is well
statusCode = 200;
}
// Read the content
while ((bytesRead = ne_read_response_block(pRequest, buffer, 1024)) > 0)
{
pContent = (char*)realloc(pContent, contentLen + bytesRead);
memcpy((void*)(pContent + contentLen), (const void*)buffer, bytesRead);
contentLen += bytesRead;
}
// Get headers
const char *pValue = ne_get_response_header(pRequest, "Last-Modified");
if (pValue != NULL)
{
lastModifiedHeaderValue = pValue;
}
pValue = ne_get_response_header(pRequest, "Location");
if (pValue != NULL)
{
locationHeaderValue = pValue;
}
pValue = ne_get_response_header(pRequest, "Content-Type");
if (pValue != NULL)
{
contentTypeHeaderValue = pValue;
}
// Redirection ?
if ((statusCode >= 300) &&
(statusCode < 400) &&
(redirectionsCount < 10))
{
ne_end_request(pRequest);
ne_request_destroy(pRequest);
pRequest = NULL;
string documentUrl = handleRedirection(pContent, contentLen);
if (documentUrl.empty() == true)
{
// Did we find a Location header ?
if (locationHeaderValue.empty() == true)
{
// Fail
free(pContent);
pContent = NULL;
contentLen = 0;
break;
}
documentUrl = locationHeaderValue;
}
#ifdef DEBUG
clog << "NeonDownloader::retrieveUrl: redirected to " << documentUrl << endl;
#endif
urlObj = Url(documentUrl);
location = urlObj.getLocation();
file = urlObj.getFile();
// Is this on the same host ?
if (hostName != urlObj.getHost())
{
// No, it isn't
hostName = urlObj.getHost();
// Create a new session
ne_session_destroy(pSession);
pSession = ne_session_create(protocol.c_str(), hostName.c_str(), 80); // urlObj.getPort());
if (pSession == NULL)
{
#ifdef DEBUG
clog << "NeonDownloader::retrieveUrl: couldn't create session !" << endl;
#endif
return NULL;
}
ne_set_useragent(pSession, m_userAgent.c_str());
ne_set_read_timeout(pSession, (int)m_timeout);
}
// Try again
fullLocation = "/";
if (location.empty() == false)
{
fullLocation += location;
fullLocation += "/";
}
if (file.empty() == false)
{
fullLocation += file;
}
#ifdef DEBUG
clog << "NeonDownloader::retrieveUrl: redirected to " << fullLocation << " on " << hostName << endl;
#endif
// Create a new request for this URL
pRequest = ne_request_create(pSession, "GET", fullLocation.c_str());
if (pRequest == NULL)
{
#ifdef DEBUG
clog << "NeonDownloader::retrieveUrl: couldn't create request !" << endl;
#endif
ne_session_destroy(pSession);
return NULL;
}
redirectionsCount++;
requestStatus = NE_RETRY;
// Discard whatever content we have already got
free(pContent);
pContent = NULL;
contentLen = 0;
continue;
}
}
// End the request
requestStatus = ne_end_request(pRequest);
}
if ((pContent != NULL) &&
(contentLen > 0))
{
if (statusCode < 400)
{
// Copy the document content
pDocument = new Document(docInfo);
pDocument->setData(pContent, contentLen);
pDocument->setLocation(url);
pDocument->setType(contentTypeHeaderValue);
if (lastModifiedHeaderValue.empty() == false)
{
pDocument->setTimestamp(lastModifiedHeaderValue);
}
#ifdef DEBUG
clog << "NeonDownloader::retrieveUrl: document size is " << contentLen << endl;
#endif
}
free(pContent);
}
// Cleanup
ne_request_destroy(pRequest);
ne_session_destroy(pSession);
return pDocument;
}
Document *NeonDownloader::putUrl(const DocumentInfo &docInfo,
const map<string, string> &headers,
const string &url)
{
// FIXME: implement this
return NULL;
}
|