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"
|
2006-02-21 12:28:42 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
|
|
|
|
2011-11-11 13:50:18 +00:00
|
|
|
const std::string HttpHeader::LOCATION("location");
|
|
|
|
const std::string HttpHeader::TRANSFER_ENCODING("transfer-encoding");
|
|
|
|
const std::string HttpHeader::CONTENT_ENCODING("content-encoding");
|
|
|
|
const std::string HttpHeader::CONTENT_DISPOSITION("content-disposition");
|
|
|
|
const std::string HttpHeader::SET_COOKIE("set-cookie");
|
|
|
|
const std::string HttpHeader::CONTENT_TYPE("content-type");
|
|
|
|
const std::string HttpHeader::RETRY_AFTER("retry-after");
|
|
|
|
const std::string HttpHeader::CONNECTION("connection");
|
|
|
|
const std::string HttpHeader::CONTENT_LENGTH("content-length");
|
|
|
|
const std::string HttpHeader::CONTENT_RANGE("content-range");
|
|
|
|
const std::string HttpHeader::LAST_MODIFIED("last-modified");
|
|
|
|
const std::string HttpHeader::ACCEPT_ENCODING("accept-encoding");
|
|
|
|
const std::string HttpHeader::LINK("link");
|
|
|
|
const std::string HttpHeader::DIGEST("digest");
|
|
|
|
const std::string HttpHeader::PROXY_CONNECTION("proxy-connection");
|
|
|
|
const std::string HttpHeader::AUTHORIZATION("authorization");
|
2008-05-13 16:40:34 +00:00
|
|
|
|
2011-11-11 13:50:18 +00:00
|
|
|
const std::string HttpHeader::HTTP_1_1 = "HTTP/1.1";
|
|
|
|
const std::string HttpHeader::CLOSE = "close";
|
|
|
|
const std::string HttpHeader::KEEP_ALIVE = "keep-alive";
|
|
|
|
const std::string HttpHeader::CHUNKED = "chunked";
|
|
|
|
const std::string HttpHeader::GZIP = "gzip";
|
|
|
|
const std::string HttpHeader::DEFLATE = "deflate";
|
2008-05-13 16:40:34 +00:00
|
|
|
|
2010-11-14 07:17:55 +00:00
|
|
|
HttpHeader::HttpHeader() {}
|
|
|
|
HttpHeader::~HttpHeader() {}
|
|
|
|
|
2011-11-11 13:50:18 +00:00
|
|
|
void HttpHeader::put(const std::string& name, const std::string& value)
|
|
|
|
{
|
|
|
|
std::multimap<std::string, std::string>::value_type vt(name, value);
|
2010-06-21 13:51:56 +00:00
|
|
|
table_.insert(vt);
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
|
2011-11-11 13:50:18 +00:00
|
|
|
bool HttpHeader::defined(const std::string& name) const
|
|
|
|
{
|
|
|
|
return table_.count(name);
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
|
2011-11-11 13:50:18 +00:00
|
|
|
const std::string& HttpHeader::find(const std::string& name) const
|
|
|
|
{
|
2010-02-28 16:04:52 +00:00
|
|
|
std::multimap<std::string, std::string>::const_iterator itr =
|
2011-11-11 13:50:18 +00:00
|
|
|
table_.find(name);
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-11 13:50:18 +00:00
|
|
|
std::vector<std::string> HttpHeader::findAll(const std::string& name) const
|
2010-02-28 12:30:11 +00:00
|
|
|
{
|
|
|
|
std::vector<std::string> v;
|
|
|
|
std::pair<std::multimap<std::string, std::string>::const_iterator,
|
2011-11-11 13:50:18 +00:00
|
|
|
std::multimap<std::string, std::string>::const_iterator> itrpair =
|
|
|
|
table_.equal_range(name);
|
|
|
|
while(itrpair.first != itrpair.second) {
|
|
|
|
v.push_back((*itrpair.first).second);
|
|
|
|
++itrpair.first;
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2011-07-26 14:41:21 +00:00
|
|
|
std::pair<std::multimap<std::string, std::string>::const_iterator,
|
|
|
|
std::multimap<std::string, std::string>::const_iterator>
|
2011-11-11 13:50:18 +00:00
|
|
|
HttpHeader::equalRange(const std::string& name) const
|
2011-07-26 14:41:21 +00:00
|
|
|
{
|
2011-11-11 13:50:18 +00:00
|
|
|
return table_.equal_range(name);
|
2011-07-26 14:41:21 +00:00
|
|
|
}
|
|
|
|
|
2011-12-07 13:57:34 +00:00
|
|
|
int32_t HttpHeader::findAsInt(const std::string& name) const
|
|
|
|
{
|
|
|
|
const std::string& value = find(name);
|
|
|
|
if(value.empty()) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return util::parseInt(value);
|
|
|
|
}
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
|
2011-12-07 13:57:34 +00:00
|
|
|
int64_t HttpHeader::findAsLLInt(const std::string& name) const
|
|
|
|
{
|
2011-11-11 13:50:18 +00:00
|
|
|
const std::string& value = find(name);
|
2008-05-13 14:15:23 +00:00
|
|
|
if(value.empty()) {
|
2006-02-21 12:28:42 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
2011-12-07 13:57:34 +00:00
|
|
|
return util::parseLLInt(value);
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
}
|
2007-03-05 12:31:57 +00:00
|
|
|
|
|
|
|
RangeHandle HttpHeader::getRange() const
|
|
|
|
{
|
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()) {
|
2008-04-20 00:50:22 +00:00
|
|
|
return SharedHandle<Range>(new 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 {
|
2011-12-08 16:12:54 +00:00
|
|
|
int64_t contentLength = util::parseLLInt(clenStr);
|
2011-12-07 13:57:34 +00:00
|
|
|
if(contentLength < 0) {
|
|
|
|
throw DL_ABORT_EX("Content-Length must be positive");
|
2011-12-08 16:12:54 +00:00
|
|
|
} else if(contentLength > std::numeric_limits<off_t>::max()) {
|
|
|
|
throw DOWNLOAD_FAILURE_EXCEPTION
|
|
|
|
(fmt(EX_TOO_LARGE_FILE, static_cast<long long int>(contentLength)));
|
|
|
|
} else if(contentLength == 0) {
|
2010-01-05 16:01:46 +00:00
|
|
|
return SharedHandle<Range>(new Range());
|
2008-03-09 12:24:01 +00:00
|
|
|
} else {
|
2010-06-12 09:46:41 +00:00
|
|
|
return SharedHandle<Range>
|
|
|
|
(new 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.
|
|
|
|
return SharedHandle<Range>(new Range());
|
|
|
|
}
|
2011-11-02 15:31:27 +00:00
|
|
|
std::string::const_iterator minus = std::find(byteRangeSpec, slash, '-');
|
|
|
|
if(minus == slash) {
|
|
|
|
return SharedHandle<Range>(new Range());
|
|
|
|
}
|
2011-12-08 16:12:54 +00:00
|
|
|
int64_t startByte = util::parseLLInt(std::string(byteRangeSpec, minus));
|
|
|
|
int64_t endByte = util::parseLLInt(std::string(minus+1, slash));
|
|
|
|
int64_t entityLength =
|
2011-12-07 13:57:34 +00:00
|
|
|
util::parseLLInt(std::string(slash+1, rangeStr.end()));
|
|
|
|
if(startByte < 0 || endByte < 0 || entityLength < 0) {
|
|
|
|
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()) {
|
|
|
|
throw DOWNLOAD_FAILURE_EXCEPTION
|
|
|
|
(fmt(EX_TOO_LARGE_FILE, static_cast<long long int>(startByte)));
|
|
|
|
}
|
|
|
|
if(endByte > std::numeric_limits<off_t>::max()) {
|
|
|
|
throw DOWNLOAD_FAILURE_EXCEPTION
|
|
|
|
(fmt(EX_TOO_LARGE_FILE, static_cast<long long int>(endByte)));
|
|
|
|
}
|
|
|
|
if(entityLength > std::numeric_limits<off_t>::max()) {
|
|
|
|
throw DOWNLOAD_FAILURE_EXCEPTION
|
|
|
|
(fmt(EX_TOO_LARGE_FILE, static_cast<long long int>(entityLength)));
|
|
|
|
}
|
2008-04-20 00:50:22 +00:00
|
|
|
return SharedHandle<Range>(new 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
|
|
|
}
|
|
|
|
|
2011-11-02 15:31:27 +00:00
|
|
|
void HttpHeader::fill
|
|
|
|
(std::string::const_iterator first,
|
|
|
|
std::string::const_iterator last)
|
|
|
|
{
|
|
|
|
std::string name;
|
|
|
|
std::string value;
|
|
|
|
while(first != last) {
|
|
|
|
std::string::const_iterator j = first;
|
|
|
|
while(j != last && *j != '\r' && *j != '\n') {
|
|
|
|
++j;
|
|
|
|
}
|
|
|
|
if(first != j) {
|
|
|
|
std::string::const_iterator sep = std::find(first, j, ':');
|
|
|
|
if(sep == j) {
|
|
|
|
// multiline header?
|
|
|
|
if(*first == ' ' || *first == '\t') {
|
|
|
|
std::pair<std::string::const_iterator,
|
2011-11-03 07:19:21 +00:00
|
|
|
std::string::const_iterator> p = util::stripIter(first, j);
|
2011-11-02 15:31:27 +00:00
|
|
|
if(!name.empty() && p.first != p.second) {
|
|
|
|
if(!value.empty()) {
|
2011-11-12 12:27:24 +00:00
|
|
|
value += " ";
|
2011-11-02 15:31:27 +00:00
|
|
|
}
|
|
|
|
value.append(p.first, p.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(!name.empty()) {
|
|
|
|
put(name, value);
|
2011-01-19 15:25:01 +00:00
|
|
|
}
|
2011-11-02 15:31:27 +00:00
|
|
|
std::pair<std::string::const_iterator,
|
2011-11-03 07:19:21 +00:00
|
|
|
std::string::const_iterator> p = util::stripIter(first, sep);
|
2011-11-02 15:31:27 +00:00
|
|
|
name.assign(p.first, p.second);
|
2011-11-11 13:50:18 +00:00
|
|
|
util::lowercase(name);
|
2011-11-03 07:19:21 +00:00
|
|
|
p = util::stripIter(sep+1, j);
|
2011-11-02 15:31:27 +00:00
|
|
|
value.assign(p.first, p.second);
|
2011-01-19 15:25:01 +00:00
|
|
|
}
|
2008-04-21 10:48:11 +00:00
|
|
|
}
|
2011-11-02 15:31:27 +00:00
|
|
|
while(j != last && (*j == '\r' || *j == '\n')) {
|
|
|
|
++j;
|
|
|
|
}
|
|
|
|
first = j;
|
|
|
|
}
|
|
|
|
if(!name.empty()) {
|
|
|
|
put(name, value);
|
2008-04-21 10:48:11 +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_;
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|