2009-01-25 09:58:40 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
|
|
|
* aria2 - The high speed download utility
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* 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
|
2010-01-05 16:01:46 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2009-01-25 09:58:40 +00:00
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give
|
|
|
|
* permission to link the code of portions of this program with the
|
|
|
|
* OpenSSL library under certain conditions as described in each
|
|
|
|
* individual source file, and distribute linked combinations
|
|
|
|
* including the two.
|
|
|
|
* You must obey the GNU General Public License in all respects
|
|
|
|
* for all of the code used other than OpenSSL. If you modify
|
|
|
|
* file(s) with this exception, you may extend this exception to your
|
|
|
|
* version of the file(s), but you are not obligated to do so. If you
|
|
|
|
* do not wish to do so, delete this exception statement from your
|
|
|
|
* version. If you delete this exception statement from all source
|
|
|
|
* files in the program, then also delete it here.
|
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "HttpServer.h"
|
2009-05-08 07:58:50 +00:00
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
2009-01-25 09:58:40 +00:00
|
|
|
#include "HttpHeader.h"
|
|
|
|
#include "SocketCore.h"
|
|
|
|
#include "HttpHeaderProcessor.h"
|
|
|
|
#include "DlAbortEx.h"
|
|
|
|
#include "message.h"
|
2009-10-22 15:35:33 +00:00
|
|
|
#include "util.h"
|
2009-01-25 10:55:27 +00:00
|
|
|
#include "LogFactory.h"
|
|
|
|
#include "Logger.h"
|
2011-11-05 12:13:49 +00:00
|
|
|
#include "base64.h"
|
2009-06-06 12:33:07 +00:00
|
|
|
#include "a2functional.h"
|
2010-11-20 08:21:36 +00:00
|
|
|
#include "fmt.h"
|
2011-01-16 08:27:01 +00:00
|
|
|
#include "SocketRecvBuffer.h"
|
2011-02-09 14:01:01 +00:00
|
|
|
#include "TimeA2.h"
|
2011-11-04 13:27:58 +00:00
|
|
|
#include "array_fun.h"
|
2009-01-25 09:58:40 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
|
|
|
|
2010-11-20 08:21:36 +00:00
|
|
|
HttpServer::HttpServer
|
|
|
|
(const SharedHandle<SocketCore>& socket,
|
|
|
|
DownloadEngine* e)
|
|
|
|
: socket_(socket),
|
2011-01-16 08:27:01 +00:00
|
|
|
socketRecvBuffer_(new SocketRecvBuffer(socket_)),
|
2010-11-20 08:21:36 +00:00
|
|
|
socketBuffer_(socket),
|
|
|
|
e_(e),
|
|
|
|
headerProcessor_(new HttpHeaderProcessor()),
|
|
|
|
keepAlive_(true),
|
|
|
|
gzip_(false),
|
|
|
|
acceptsPersistentConnection_(true),
|
|
|
|
acceptsGZip_(false)
|
2009-01-25 09:58:40 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
HttpServer::~HttpServer() {}
|
|
|
|
|
|
|
|
SharedHandle<HttpHeader> HttpServer::receiveRequest()
|
|
|
|
{
|
2011-01-16 08:27:01 +00:00
|
|
|
if(socketRecvBuffer_->bufferEmpty()) {
|
|
|
|
if(socketRecvBuffer_->recv() == 0 &&
|
|
|
|
!socket_->wantRead() && !socket_->wantWrite()) {
|
|
|
|
throw DL_ABORT_EX(EX_EOF_FROM_PEER);
|
|
|
|
}
|
2009-01-25 09:58:40 +00:00
|
|
|
}
|
2011-01-16 08:27:01 +00:00
|
|
|
headerProcessor_->update(socketRecvBuffer_->getBuffer(),
|
|
|
|
socketRecvBuffer_->getBufferLength());
|
|
|
|
if(headerProcessor_->eoh()) {
|
|
|
|
SharedHandle<HttpHeader> header = headerProcessor_->getHttpRequestHeader();
|
|
|
|
size_t putbackDataLength = headerProcessor_->getPutBackDataLength();
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_INFO(fmt("HTTP Server received request\n%s",
|
|
|
|
headerProcessor_->getHeaderString().c_str()));
|
2011-01-16 08:27:01 +00:00
|
|
|
socketRecvBuffer_->shiftBuffer
|
|
|
|
(socketRecvBuffer_->getBufferLength()-putbackDataLength);
|
2010-06-21 13:51:56 +00:00
|
|
|
lastRequestHeader_ = header;
|
|
|
|
lastBody_.clear();
|
|
|
|
lastBody_.str("");
|
|
|
|
lastContentLength_ =
|
2011-11-11 13:50:18 +00:00
|
|
|
lastRequestHeader_->findAsUInt(HttpHeader::CONTENT_LENGTH);
|
2010-06-21 13:51:56 +00:00
|
|
|
headerProcessor_->clear();
|
2009-01-25 10:55:27 +00:00
|
|
|
|
2011-11-11 16:13:55 +00:00
|
|
|
const std::string& connection =
|
|
|
|
lastRequestHeader_->find(HttpHeader::CONNECTION);
|
2010-06-21 13:51:56 +00:00
|
|
|
acceptsPersistentConnection_ =
|
2011-11-11 16:13:55 +00:00
|
|
|
util::strifind(connection.begin(),
|
|
|
|
connection.end(),
|
|
|
|
HttpHeader::CLOSE.begin(),
|
|
|
|
HttpHeader::CLOSE.end()) == connection.end() &&
|
2010-06-21 13:51:56 +00:00
|
|
|
(lastRequestHeader_->getVersion() == HttpHeader::HTTP_1_1 ||
|
2011-11-11 16:13:55 +00:00
|
|
|
util::strifind(connection.begin(),
|
|
|
|
connection.end(),
|
|
|
|
HttpHeader::KEEP_ALIVE.begin(),
|
|
|
|
HttpHeader::KEEP_ALIVE.end()) != connection.end());
|
2010-01-22 14:09:39 +00:00
|
|
|
|
2011-11-04 13:27:58 +00:00
|
|
|
std::vector<Scip> acceptEncodings;
|
|
|
|
const std::string& acceptEnc =
|
2011-11-11 13:50:18 +00:00
|
|
|
lastRequestHeader_->find(HttpHeader::ACCEPT_ENCODING);
|
2011-11-04 13:27:58 +00:00
|
|
|
util::splitIter(acceptEnc.begin(), acceptEnc.end(),
|
|
|
|
std::back_inserter(acceptEncodings), ',', true);
|
|
|
|
acceptsGZip_ = false;
|
|
|
|
for(std::vector<Scip>::const_iterator i = acceptEncodings.begin(),
|
|
|
|
eoi = acceptEncodings.end(); i != eoi; ++i) {
|
2011-11-11 13:50:18 +00:00
|
|
|
if(util::strieq((*i).first, (*i).second,
|
|
|
|
HttpHeader::GZIP.begin(), HttpHeader::GZIP.end())) {
|
2011-11-04 13:27:58 +00:00
|
|
|
acceptsGZip_ = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-01-16 08:27:01 +00:00
|
|
|
return header;
|
|
|
|
} else {
|
|
|
|
socketRecvBuffer_->clearBuffer();
|
|
|
|
return SharedHandle<HttpHeader>();
|
2010-01-22 14:09:39 +00:00
|
|
|
}
|
2009-01-25 10:55:27 +00:00
|
|
|
}
|
|
|
|
|
2009-05-08 07:58:50 +00:00
|
|
|
bool HttpServer::receiveBody()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
if(lastContentLength_ == 0) {
|
2009-05-08 07:58:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
2011-01-16 08:27:01 +00:00
|
|
|
if(socketRecvBuffer_->bufferEmpty()) {
|
|
|
|
if(socketRecvBuffer_->recv() == 0 &&
|
|
|
|
!socket_->wantRead() && !socket_->wantWrite()) {
|
|
|
|
throw DL_ABORT_EX(EX_EOF_FROM_PEER);
|
|
|
|
}
|
2009-05-08 07:58:50 +00:00
|
|
|
}
|
2011-01-16 08:27:01 +00:00
|
|
|
size_t length =
|
|
|
|
std::min(socketRecvBuffer_->getBufferLength(),
|
|
|
|
static_cast<size_t>(lastContentLength_-lastBody_.tellg()));
|
|
|
|
lastBody_.write(reinterpret_cast<const char*>(socketRecvBuffer_->getBuffer()),
|
|
|
|
length);
|
|
|
|
socketRecvBuffer_->shiftBuffer(length);
|
2010-06-21 13:51:56 +00:00
|
|
|
return lastContentLength_ == static_cast<uint64_t>(lastBody_.tellp());
|
2009-05-08 07:58:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string HttpServer::getBody() const
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return lastBody_.str();
|
2009-05-08 07:58:50 +00:00
|
|
|
}
|
|
|
|
|
2011-03-13 15:57:05 +00:00
|
|
|
const std::string& HttpServer::getMethod() const
|
|
|
|
{
|
|
|
|
return lastRequestHeader_->getMethod();
|
|
|
|
}
|
|
|
|
|
2009-05-08 07:58:50 +00:00
|
|
|
const std::string& HttpServer::getRequestPath() const
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return lastRequestHeader_->getRequestPath();
|
2009-05-08 07:58:50 +00:00
|
|
|
}
|
|
|
|
|
2011-11-05 08:28:48 +00:00
|
|
|
void HttpServer::feedResponse(std::string& text, const std::string& contentType)
|
2009-01-25 09:58:40 +00:00
|
|
|
{
|
2009-05-09 15:38:23 +00:00
|
|
|
feedResponse("200 OK", "", text, contentType);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpServer::feedResponse(const std::string& status,
|
2010-01-05 16:01:46 +00:00
|
|
|
const std::string& headers,
|
2011-11-05 08:28:48 +00:00
|
|
|
std::string& text,
|
2010-01-05 16:01:46 +00:00
|
|
|
const std::string& contentType)
|
2009-05-09 15:38:23 +00:00
|
|
|
{
|
2011-02-09 14:01:01 +00:00
|
|
|
std::string httpDate = Time().toHTTPDate();
|
2009-06-06 12:33:07 +00:00
|
|
|
std::string header = "HTTP/1.1 ";
|
|
|
|
strappend(header, status, "\r\n",
|
2011-02-09 14:01:01 +00:00
|
|
|
"Date: ", httpDate, "\r\n",
|
|
|
|
"Content-Type: ", contentType, "\r\n");
|
|
|
|
strappend(header, "Content-Length: ", util::uitos(text.size()), "\r\n",
|
|
|
|
"Expires: ", httpDate, "\r\n",
|
|
|
|
"Cache-Control: no-cache\r\n");
|
2011-08-18 12:22:48 +00:00
|
|
|
if(!allowOrigin_.empty()) {
|
|
|
|
strappend(header, "Access-Control-Allow-Origin: ", allowOrigin_, "\r\n");
|
|
|
|
}
|
2010-01-22 14:09:39 +00:00
|
|
|
if(supportsGZip()) {
|
|
|
|
header += "Content-Encoding: gzip\r\n";
|
|
|
|
}
|
2009-01-25 10:55:27 +00:00
|
|
|
if(!supportsPersistentConnection()) {
|
|
|
|
header += "Connection: close\r\n";
|
|
|
|
}
|
2009-05-09 15:38:23 +00:00
|
|
|
if(!headers.empty()) {
|
|
|
|
header += headers;
|
2011-11-04 15:25:24 +00:00
|
|
|
if(headers.size() < 2 ||
|
|
|
|
(headers[headers.size()-2] != '\r' &&
|
|
|
|
headers[headers.size()-1] != '\n')) {
|
2009-05-09 15:38:23 +00:00
|
|
|
header += "\r\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-25 10:55:27 +00:00
|
|
|
header += "\r\n";
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_DEBUG(fmt("HTTP Server sends response:\n%s", header.c_str()));
|
2011-11-11 16:19:01 +00:00
|
|
|
socketBuffer_.pushStr(header);
|
|
|
|
socketBuffer_.pushStr(text);
|
2009-01-25 09:58:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t HttpServer::sendResponse()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return socketBuffer_.send();
|
2009-01-25 09:58:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool HttpServer::sendBufferIsEmpty() const
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return socketBuffer_.sendBufferIsEmpty();
|
2009-01-25 09:58:40 +00:00
|
|
|
}
|
|
|
|
|
2009-05-09 15:38:23 +00:00
|
|
|
bool HttpServer::authenticate()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
if(username_.empty()) {
|
2009-05-09 15:38:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-11-11 13:50:18 +00:00
|
|
|
const std::string& authHeader =
|
|
|
|
lastRequestHeader_->find(HttpHeader::AUTHORIZATION);
|
2009-05-09 15:38:23 +00:00
|
|
|
if(authHeader.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-11-03 14:09:03 +00:00
|
|
|
std::pair<Scip, Scip> p;
|
|
|
|
util::divide(p, authHeader.begin(), authHeader.end(), ' ');
|
2011-11-11 14:00:41 +00:00
|
|
|
static const char A2_AUTHMETHOD[] = "Basic";
|
2011-11-04 13:27:58 +00:00
|
|
|
if(!util::streq(p.first.first, p.first.second,
|
|
|
|
A2_AUTHMETHOD, vend(A2_AUTHMETHOD)-1)) {
|
2009-05-09 15:38:23 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-11-05 14:30:46 +00:00
|
|
|
std::string userpass = base64::decode(p.second.first, p.second.second);
|
2011-11-05 03:01:57 +00:00
|
|
|
std::pair<Sip, Sip> up;
|
|
|
|
util::divide(up, userpass.begin(), userpass.end(), ':');
|
|
|
|
return util::streq(up.first.first, up.first.second,
|
2011-11-04 13:27:58 +00:00
|
|
|
username_.begin(), username_.end()) &&
|
2011-11-05 03:01:57 +00:00
|
|
|
util::streq(up.second.first, up.second.second,
|
2011-11-04 13:27:58 +00:00
|
|
|
password_.begin(), password_.end());
|
2009-05-09 15:38:23 +00:00
|
|
|
}
|
|
|
|
|
2010-11-14 07:17:55 +00:00
|
|
|
void HttpServer::setUsernamePassword
|
|
|
|
(const std::string& username, const std::string& password)
|
|
|
|
{
|
|
|
|
username_ = username;
|
|
|
|
password_ = password;
|
|
|
|
}
|
|
|
|
|
2009-01-25 09:58:40 +00:00
|
|
|
} // namespace aria2
|