2006-02-21 12:28:42 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
2006-09-21 15:31:24 +00:00
|
|
|
* aria2 - The high speed download utility
|
2006-02-21 12:28:42 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2006 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
|
2006-09-21 15:31:24 +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.
|
2006-02-21 12:28:42 +00:00
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "HttpHeader.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Range.h"
|
2009-10-22 15:35:33 +00:00
|
|
|
#include "util.h"
|
2008-05-13 14:15:23 +00:00
|
|
|
#include "A2STR.h"
|
2011-12-08 16:12:54 +00:00
|
|
|
#include "DownloadFailureException.h"
|
2012-09-23 11:59:34 +00:00
|
|
|
#include "array_fun.h"
|
2006-02-21 12:28:42 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
|
|
|
|
2012-10-29 14:09:16 +00:00
|
|
|
HttpHeader::HttpHeader()
|
|
|
|
: statusCode_(0)
|
|
|
|
{}
|
|
|
|
|
2010-11-14 07:17:55 +00:00
|
|
|
HttpHeader::~HttpHeader() {}
|
|
|
|
|
2012-09-23 11:59:34 +00:00
|
|
|
void HttpHeader::put(int hdKey, const std::string& value)
|
2011-11-11 13:50:18 +00:00
|
|
|
{
|
2012-09-23 11:59:34 +00:00
|
|
|
std::multimap<int, std::string>::value_type vt(hdKey, value);
|
2010-06-21 13:51:56 +00:00
|
|
|
table_.insert(vt);
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
|
2012-09-23 11:59:34 +00:00
|
|
|
bool HttpHeader::defined(int hdKey) const
|
2011-11-11 13:50:18 +00:00
|
|
|
{
|
2012-09-23 11:59:34 +00:00
|
|
|
return table_.count(hdKey);
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
|
2012-09-23 11:59:34 +00:00
|
|
|
const std::string& HttpHeader::find(int hdKey) const
|
2011-11-11 13:50:18 +00:00
|
|
|
{
|
2012-09-23 11:59:34 +00:00
|
|
|
std::multimap<int, std::string>::const_iterator itr = table_.find(hdKey);
|
2010-06-21 13:51:56 +00:00
|
|
|
if(itr == table_.end()) {
|
2008-05-13 14:15:23 +00:00
|
|
|
return A2STR::NIL;
|
2006-02-21 12:28:42 +00:00
|
|
|
} else {
|
|
|
|
return (*itr).second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-23 11:59:34 +00:00
|
|
|
std::vector<std::string> HttpHeader::findAll(int hdKey) const
|
2010-02-28 12:30:11 +00:00
|
|
|
{
|
|
|
|
std::vector<std::string> v;
|
2012-09-23 11:59:34 +00:00
|
|
|
std::pair<std::multimap<int, std::string>::const_iterator,
|
|
|
|
std::multimap<int, std::string>::const_iterator> itrpair =
|
|
|
|
table_.equal_range(hdKey);
|
2011-11-11 13:50:18 +00:00
|
|
|
while(itrpair.first != itrpair.second) {
|
|
|
|
v.push_back((*itrpair.first).second);
|
|
|
|
++itrpair.first;
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2012-09-23 11:59:34 +00:00
|
|
|
std::pair<std::multimap<int, std::string>::const_iterator,
|
|
|
|
std::multimap<int, std::string>::const_iterator>
|
|
|
|
HttpHeader::equalRange(int hdKey) const
|
2011-07-26 14:41:21 +00:00
|
|
|
{
|
2012-09-23 11:59:34 +00:00
|
|
|
return table_.equal_range(hdKey);
|
2011-07-26 14:41:21 +00:00
|
|
|
}
|
|
|
|
|
2012-09-30 08:30:35 +00:00
|
|
|
Range HttpHeader::getRange() const
|
2007-03-05 12:31:57 +00:00
|
|
|
{
|
2011-11-11 13:50:18 +00:00
|
|
|
const std::string& rangeStr = find(CONTENT_RANGE);
|
2008-05-13 14:15:23 +00:00
|
|
|
if(rangeStr.empty()) {
|
2011-11-11 13:50:18 +00:00
|
|
|
const std::string& clenStr = find(CONTENT_LENGTH);
|
2011-11-03 09:51:31 +00:00
|
|
|
if(clenStr.empty()) {
|
2012-09-30 08:30:35 +00:00
|
|
|
return Range();
|
2007-03-15 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To handle Segment as SegmentHandle:
* src/AbstractCommand.cc (execute): Rewritten.
* src/SegmentMan.h: Segment -> SegmentHandle
Introducded HttpResponse class, HttpRequest class to improve
code
extensiveness and make it clear:
* src/HttpDownloadCommand.cc: transfer encoders are now managed
by
HttpResponse class.
* src/HttpRequest.h, src/HttpRequest.cc: New class.
* src/HttpResponse.h, src/HttpResponse.cc: New class.
* src/HttpConnection.cc: Contruction of http request were moved
to
HttpRequest class.
* src/HttpResponseCommand.h, src/HttpResponseCommand.cc:
Refactored.
* src/HttpRequestCommand.cc (executeInternal): Rewritten.
* src/HttpAuthConfig.h: New class.
* src/Range.h: New class.
To make FtpTunnel{Request, Response}Command and
HttpProxy{Request, Response}Command derived from
AbstractProxy{Request, Response}Command:
* src/FtpTunnelResponseCommand.h,
src/FtpTunnelResponseCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/FtpTunnelRequestCommand.h, src/FtpTunnelRequestCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/HttpProxyRequestCommand.h, src/HttpProxyRequestCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/HttpProxyResponseCommand.h,
src/HttpProxyResponseCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/AbstractProxyRequestCommand.h,
src/AbstractProxyRequestCommand.cc
: New class.
* src/AbstractProxyResponseCommand.h,
src/AbstractProxyResponseCommand.cc: New class.
To add netrc support:
* src/Netrc.h, src/Netrc.cc: New class.
* src/Util.h, src/Util.cc (split): New function.
* src/HttpHeader.cc (getRange): Fixed so that it inspects
"Content-Range" header instead of "Range" header.
* src/HttpHeader.h
(getStatus): Removed.
(setStatus): Removed.
* src/Segment.h
(getPositionToWrite): New function.
2007-03-15 15:07:18 +00:00
|
|
|
} else {
|
2012-09-26 15:45:32 +00:00
|
|
|
int64_t contentLength;
|
|
|
|
if(!util::parseLLIntNoThrow(contentLength, clenStr) ||
|
|
|
|
contentLength < 0) {
|
|
|
|
throw DL_ABORT_EX("Content-Length must be positive integer");
|
2011-12-08 16:12:54 +00:00
|
|
|
} else if(contentLength > std::numeric_limits<off_t>::max()) {
|
|
|
|
throw DOWNLOAD_FAILURE_EXCEPTION
|
2012-06-25 13:43:33 +00:00
|
|
|
(fmt(EX_TOO_LARGE_FILE, contentLength));
|
2011-12-08 16:12:54 +00:00
|
|
|
} else if(contentLength == 0) {
|
2012-09-30 08:30:35 +00:00
|
|
|
return Range();
|
2008-03-09 12:24:01 +00:00
|
|
|
} else {
|
2012-09-30 08:30:35 +00:00
|
|
|
return Range(0, contentLength - 1, contentLength);
|
2008-03-09 12:24:01 +00:00
|
|
|
}
|
2007-03-15 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To handle Segment as SegmentHandle:
* src/AbstractCommand.cc (execute): Rewritten.
* src/SegmentMan.h: Segment -> SegmentHandle
Introducded HttpResponse class, HttpRequest class to improve
code
extensiveness and make it clear:
* src/HttpDownloadCommand.cc: transfer encoders are now managed
by
HttpResponse class.
* src/HttpRequest.h, src/HttpRequest.cc: New class.
* src/HttpResponse.h, src/HttpResponse.cc: New class.
* src/HttpConnection.cc: Contruction of http request were moved
to
HttpRequest class.
* src/HttpResponseCommand.h, src/HttpResponseCommand.cc:
Refactored.
* src/HttpRequestCommand.cc (executeInternal): Rewritten.
* src/HttpAuthConfig.h: New class.
* src/Range.h: New class.
To make FtpTunnel{Request, Response}Command and
HttpProxy{Request, Response}Command derived from
AbstractProxy{Request, Response}Command:
* src/FtpTunnelResponseCommand.h,
src/FtpTunnelResponseCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/FtpTunnelRequestCommand.h, src/FtpTunnelRequestCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/HttpProxyRequestCommand.h, src/HttpProxyRequestCommand.cc:
Derived from AbstractProxyRequestCommand class.
* src/HttpProxyResponseCommand.h,
src/HttpProxyResponseCommand.cc:
Derived from AbstractProxyResponseCommand class.
* src/AbstractProxyRequestCommand.h,
src/AbstractProxyRequestCommand.cc
: New class.
* src/AbstractProxyResponseCommand.h,
src/AbstractProxyResponseCommand.cc: New class.
To add netrc support:
* src/Netrc.h, src/Netrc.cc: New class.
* src/Util.h, src/Util.cc (split): New function.
* src/HttpHeader.cc (getRange): Fixed so that it inspects
"Content-Range" header instead of "Range" header.
* src/HttpHeader.h
(getStatus): Removed.
(setStatus): Removed.
* src/Segment.h
(getPositionToWrite): New function.
2007-03-15 15:07:18 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-02 15:31:27 +00:00
|
|
|
// we expect that rangeStr looks like 'bytes 100-199/100'
|
|
|
|
// but some server returns '100-199/100', omitting bytes-unit sepcifier
|
|
|
|
// 'bytes'.
|
|
|
|
std::string::const_iterator byteRangeSpec =
|
|
|
|
std::find(rangeStr.begin(), rangeStr.end(), ' ');
|
|
|
|
if(byteRangeSpec == rangeStr.end()) {
|
|
|
|
// we assume bytes-unit specifier omitted.
|
|
|
|
byteRangeSpec = rangeStr.begin();
|
|
|
|
} else {
|
|
|
|
while(byteRangeSpec != rangeStr.end() &&
|
|
|
|
(*byteRangeSpec == ' ' || *byteRangeSpec == '\t')) {
|
|
|
|
++byteRangeSpec;
|
2008-03-15 04:25:55 +00:00
|
|
|
}
|
2007-03-05 12:31:57 +00:00
|
|
|
}
|
2011-11-02 15:31:27 +00:00
|
|
|
std::string::const_iterator slash =
|
|
|
|
std::find(byteRangeSpec, rangeStr.end(), '/');
|
|
|
|
if(slash == rangeStr.end() || slash+1 == rangeStr.end() ||
|
|
|
|
(byteRangeSpec+1 == slash && *byteRangeSpec == '*') ||
|
|
|
|
(slash+2 == rangeStr.end() && *(slash+1) == '*')) {
|
2010-10-31 07:56:01 +00:00
|
|
|
// If byte-range-resp-spec or instance-length is "*", we returns
|
|
|
|
// empty Range. The former is usually sent with 416 (Request range
|
|
|
|
// not satisfiable) status.
|
2012-09-30 08:30:35 +00:00
|
|
|
return Range();
|
2010-10-31 07:56:01 +00:00
|
|
|
}
|
2011-11-02 15:31:27 +00:00
|
|
|
std::string::const_iterator minus = std::find(byteRangeSpec, slash, '-');
|
|
|
|
if(minus == slash) {
|
2012-09-30 08:30:35 +00:00
|
|
|
return Range();
|
2011-11-02 15:31:27 +00:00
|
|
|
}
|
2012-09-26 15:45:32 +00:00
|
|
|
int64_t startByte, endByte, entityLength;
|
|
|
|
if(!util::parseLLIntNoThrow(startByte, std::string(byteRangeSpec, minus)) ||
|
|
|
|
!util::parseLLIntNoThrow(endByte, std::string(minus+1, slash)) ||
|
|
|
|
!util::parseLLIntNoThrow(entityLength,
|
|
|
|
std::string(slash+1, rangeStr.end())) ||
|
|
|
|
startByte < 0 || endByte < 0 || entityLength < 0) {
|
2011-12-07 13:57:34 +00:00
|
|
|
throw DL_ABORT_EX("byte-range-spec must be positive");
|
|
|
|
}
|
2011-12-08 16:12:54 +00:00
|
|
|
if(startByte > std::numeric_limits<off_t>::max()) {
|
2012-06-25 13:43:33 +00:00
|
|
|
throw DOWNLOAD_FAILURE_EXCEPTION(fmt(EX_TOO_LARGE_FILE, startByte));
|
2011-12-08 16:12:54 +00:00
|
|
|
}
|
|
|
|
if(endByte > std::numeric_limits<off_t>::max()) {
|
2012-06-25 13:43:33 +00:00
|
|
|
throw DOWNLOAD_FAILURE_EXCEPTION(fmt(EX_TOO_LARGE_FILE, endByte));
|
2011-12-08 16:12:54 +00:00
|
|
|
}
|
|
|
|
if(entityLength > std::numeric_limits<off_t>::max()) {
|
2012-06-25 13:43:33 +00:00
|
|
|
throw DOWNLOAD_FAILURE_EXCEPTION(fmt(EX_TOO_LARGE_FILE, entityLength));
|
2011-12-08 16:12:54 +00:00
|
|
|
}
|
2012-09-30 08:30:35 +00:00
|
|
|
return Range(startByte, endByte, entityLength);
|
2007-03-05 12:31:57 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2008-04-21 10:48:11 +00:00
|
|
|
void HttpHeader::setVersion(const std::string& version)
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
version_ = version;
|
2008-04-21 10:48:11 +00:00
|
|
|
}
|
|
|
|
|
2009-01-25 09:58:40 +00:00
|
|
|
void HttpHeader::setMethod(const std::string& method)
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
method_ = method;
|
2009-01-25 09:58:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void HttpHeader::setRequestPath(const std::string& requestPath)
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
requestPath_ = requestPath;
|
2009-01-25 09:58:40 +00:00
|
|
|
}
|
|
|
|
|
2008-07-18 15:20:52 +00:00
|
|
|
void HttpHeader::clearField()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
table_.clear();
|
2008-07-18 15:20:52 +00:00
|
|
|
}
|
|
|
|
|
2011-11-11 13:50:18 +00:00
|
|
|
int HttpHeader::getStatusCode() const
|
|
|
|
{
|
|
|
|
return statusCode_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpHeader::setStatusCode(int code)
|
|
|
|
{
|
|
|
|
statusCode_ = code;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& HttpHeader::getVersion() const
|
|
|
|
{
|
|
|
|
return version_;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& HttpHeader::getMethod() const
|
|
|
|
{
|
|
|
|
return method_;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& HttpHeader::getRequestPath() const
|
|
|
|
{
|
|
|
|
return requestPath_;
|
|
|
|
}
|
|
|
|
|
2012-06-23 08:34:20 +00:00
|
|
|
const std::string& HttpHeader::getReasonPhrase() const
|
|
|
|
{
|
|
|
|
return reasonPhrase_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpHeader::setReasonPhrase(const std::string& reasonPhrase)
|
|
|
|
{
|
|
|
|
reasonPhrase_ = reasonPhrase;
|
|
|
|
}
|
|
|
|
|
2012-09-23 11:59:34 +00:00
|
|
|
bool HttpHeader::fieldContains(int hdKey, const char* value)
|
2012-04-08 10:00:07 +00:00
|
|
|
{
|
2012-09-23 11:59:34 +00:00
|
|
|
std::pair<std::multimap<int, std::string>::const_iterator,
|
|
|
|
std::multimap<int, std::string>::const_iterator> range =
|
|
|
|
equalRange(hdKey);
|
|
|
|
for(std::multimap<int, std::string>::const_iterator i = range.first;
|
2012-04-08 10:00:07 +00:00
|
|
|
i != range.second; ++i) {
|
|
|
|
std::vector<Scip> values;
|
|
|
|
util::splitIter((*i).second.begin(), (*i).second.end(),
|
|
|
|
std::back_inserter(values),
|
|
|
|
',',
|
|
|
|
true // doStrip
|
|
|
|
);
|
|
|
|
for(std::vector<Scip>::const_iterator j = values.begin(),
|
|
|
|
eoj = values.end(); j != eoj; ++j) {
|
2012-09-23 11:59:34 +00:00
|
|
|
if(util::strieq((*j).first, (*j).second, value)) {
|
2012-04-08 10:00:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-09-23 12:46:01 +00:00
|
|
|
bool HttpHeader::isKeepAlive() const
|
|
|
|
{
|
|
|
|
const std::string& connection = find(CONNECTION);
|
|
|
|
return !util::strieq(connection, "close") &&
|
|
|
|
(version_ == "HTTP/1.1" || util::strieq(connection, "keep-alive"));
|
|
|
|
}
|
|
|
|
|
2012-09-23 11:59:34 +00:00
|
|
|
namespace {
|
|
|
|
const char* INTERESTING_HEADER_NAMES[] = {
|
|
|
|
"accept-encoding",
|
|
|
|
"access-control-request-headers",
|
|
|
|
"access-control-request-method",
|
|
|
|
"authorization",
|
|
|
|
"connection",
|
|
|
|
"content-disposition",
|
|
|
|
"content-encoding",
|
|
|
|
"content-length",
|
|
|
|
"content-range",
|
|
|
|
"content-type",
|
|
|
|
"digest",
|
|
|
|
"infohash",
|
|
|
|
"last-modified",
|
|
|
|
"link",
|
|
|
|
"location",
|
|
|
|
"origin",
|
|
|
|
"port",
|
|
|
|
"retry-after",
|
|
|
|
"sec-websocket-key",
|
|
|
|
"sec-websocket-version",
|
|
|
|
"set-cookie",
|
|
|
|
"transfer-encoding",
|
|
|
|
"upgrade",
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
int idInterestingHeader(const char* hdName)
|
|
|
|
{
|
|
|
|
const char** i = std::lower_bound(vbegin(INTERESTING_HEADER_NAMES),
|
|
|
|
vend(INTERESTING_HEADER_NAMES),
|
|
|
|
hdName, util::strless);
|
|
|
|
if(i != vend(INTERESTING_HEADER_NAMES) && strcmp(*i, hdName) == 0 ) {
|
|
|
|
return i - vbegin(INTERESTING_HEADER_NAMES);
|
|
|
|
} else {
|
|
|
|
return HttpHeader::MAX_INTERESTING_HEADER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|