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"
|
2009-08-30 13:18:57 +00:00
|
|
|
|
|
|
|
#include <istream>
|
|
|
|
|
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"
|
2006-02-21 12:28:42 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
|
|
|
|
2008-05-13 16:40:34 +00:00
|
|
|
const std::string HttpHeader::LOCATION("Location");
|
|
|
|
|
|
|
|
const std::string HttpHeader::TRANSFER_ENCODING("Transfer-Encoding");
|
|
|
|
|
2008-06-29 14:29:36 +00:00
|
|
|
const std::string HttpHeader::CONTENT_ENCODING("Content-Encoding");
|
|
|
|
|
2008-05-13 16:40:34 +00:00
|
|
|
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");
|
|
|
|
|
2008-05-13 16:44:21 +00:00
|
|
|
const std::string HttpHeader::CONTENT_LENGTH("Content-Length");
|
|
|
|
|
|
|
|
const std::string HttpHeader::CONTENT_RANGE("Content-Range");
|
|
|
|
|
2008-09-07 14:38:26 +00:00
|
|
|
const std::string HttpHeader::LAST_MODIFIED("Last-Modified");
|
|
|
|
|
2010-01-22 14:09:39 +00:00
|
|
|
const std::string HttpHeader::ACCEPT_ENCODING("Accept-Encoding");
|
|
|
|
|
2010-11-25 12:13:33 +00:00
|
|
|
const char HttpHeader::HTTP_1_1[] = "HTTP/1.1";
|
|
|
|
const char HttpHeader::CLOSE[] = "close";
|
|
|
|
const char HttpHeader::CHUNKED[] = "chunked";
|
|
|
|
const char HttpHeader::GZIP[] = "gzip";
|
|
|
|
const char HttpHeader::DEFLATE[] = "deflate";
|
2008-05-13 16:40:34 +00:00
|
|
|
|
2010-11-14 07:17:55 +00:00
|
|
|
HttpHeader::HttpHeader() {}
|
|
|
|
HttpHeader::~HttpHeader() {}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
void HttpHeader::put(const std::string& name, const std::string& value) {
|
2010-06-12 09:46:41 +00:00
|
|
|
std::multimap<std::string, std::string>::value_type vt
|
|
|
|
(util::toLower(name), value);
|
2010-06-21 13:51:56 +00:00
|
|
|
table_.insert(vt);
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
bool HttpHeader::defined(const std::string& name) const {
|
2010-06-21 13:51:56 +00:00
|
|
|
return table_.count(util::toLower(name)) >= 1;
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
|
2008-05-18 10:33:06 +00:00
|
|
|
const std::string& HttpHeader::getFirst(const std::string& name) const {
|
2010-02-28 16:04:52 +00:00
|
|
|
std::multimap<std::string, std::string>::const_iterator itr =
|
2010-06-21 13:51:56 +00:00
|
|
|
table_.find(util::toLower(name));
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-28 12:30:11 +00:00
|
|
|
std::vector<std::string> HttpHeader::get(const std::string& name) const
|
|
|
|
{
|
|
|
|
std::vector<std::string> v;
|
2009-10-22 15:09:00 +00:00
|
|
|
std::string n(util::toLower(name));
|
2010-02-28 12:30:11 +00:00
|
|
|
std::pair<std::multimap<std::string, std::string>::const_iterator,
|
|
|
|
std::multimap<std::string, std::string>::const_iterator> itrpair =
|
2010-06-21 13:51:56 +00:00
|
|
|
table_.equal_range(n);
|
2010-02-28 12:30:11 +00:00
|
|
|
std::multimap<std::string, std::string>::const_iterator first = itrpair.first;
|
|
|
|
while(first != itrpair.second) {
|
2008-05-18 10:40:23 +00:00
|
|
|
v.push_back((*first).second);
|
|
|
|
++first;
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
unsigned int HttpHeader::getFirstAsUInt(const std::string& name) const {
|
|
|
|
return getFirstAsULLInt(name);
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
uint64_t HttpHeader::getFirstAsULLInt(const std::string& name) const {
|
2008-05-18 10:33:06 +00:00
|
|
|
const std::string& value = getFirst(name);
|
2008-05-13 14:15:23 +00:00
|
|
|
if(value.empty()) {
|
2006-02-21 12:28:42 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
2009-10-22 15:09:00 +00:00
|
|
|
return util::parseULLInt(value);
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
}
|
2007-03-05 12:31:57 +00:00
|
|
|
|
|
|
|
RangeHandle HttpHeader::getRange() const
|
|
|
|
{
|
2008-05-18 10:33:06 +00:00
|
|
|
const std::string& rangeStr = getFirst(CONTENT_RANGE);
|
2008-05-13 14:15:23 +00:00
|
|
|
if(rangeStr.empty()) {
|
2008-05-18 10:33:06 +00:00
|
|
|
const std::string& contentLengthStr = getFirst(CONTENT_LENGTH);
|
2008-05-13 14:15:23 +00:00
|
|
|
if(contentLengthStr.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 {
|
2009-10-22 15:09:00 +00:00
|
|
|
uint64_t contentLength = util::parseULLInt(contentLengthStr);
|
2008-03-09 12:24:01 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2008-03-15 04:25:55 +00:00
|
|
|
std::string byteRangeSpec;
|
|
|
|
{
|
|
|
|
// we expect that rangeStr looks like 'bytes 100-199/100'
|
|
|
|
// but some server returns '100-199/100', omitting bytes-unit sepcifier
|
|
|
|
// 'bytes'.
|
|
|
|
std::pair<std::string, std::string> splist;
|
2010-10-10 03:39:00 +00:00
|
|
|
util::divide(splist, rangeStr, ' ');
|
2008-03-15 04:25:55 +00:00
|
|
|
if(splist.second.empty()) {
|
|
|
|
// we assume bytes-unit specifier omitted.
|
|
|
|
byteRangeSpec = splist.first;
|
|
|
|
} else {
|
|
|
|
byteRangeSpec = splist.second;
|
|
|
|
}
|
2007-03-05 12:31:57 +00:00
|
|
|
}
|
2008-03-15 04:25:55 +00:00
|
|
|
std::pair<std::string, std::string> byteRangeSpecPair;
|
2010-10-10 03:39:00 +00:00
|
|
|
util::divide(byteRangeSpecPair, byteRangeSpec, '/');
|
2008-03-15 04:25:55 +00:00
|
|
|
|
2010-10-31 07:56:01 +00:00
|
|
|
if(util::strip(byteRangeSpecPair.first) == "*" ||
|
|
|
|
util::strip(byteRangeSpecPair.second) == "*") {
|
|
|
|
// 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());
|
|
|
|
}
|
|
|
|
|
2008-03-15 04:25:55 +00:00
|
|
|
std::pair<std::string, std::string> byteRangeRespSpecPair;
|
2010-10-10 03:39:00 +00:00
|
|
|
util::divide(byteRangeRespSpecPair, byteRangeSpecPair.first, '-');
|
2007-03-05 12:31:57 +00:00
|
|
|
|
2009-10-22 15:09:00 +00:00
|
|
|
off_t startByte = util::parseLLInt(byteRangeRespSpecPair.first);
|
|
|
|
off_t endByte = util::parseLLInt(byteRangeRespSpecPair.second);
|
|
|
|
uint64_t entityLength = util::parseULLInt(byteRangeSpecPair.second);
|
2007-03-05 12:31:57 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-04-21 10:48:11 +00:00
|
|
|
void HttpHeader::fill(std::istream& in)
|
|
|
|
{
|
|
|
|
std::string line;
|
2011-01-19 15:25:01 +00:00
|
|
|
std::getline(in, line);
|
|
|
|
while(in) {
|
2010-10-09 16:49:02 +00:00
|
|
|
line = util::strip(line);
|
2008-04-21 10:48:11 +00:00
|
|
|
if(line.empty()) {
|
2011-01-19 15:25:01 +00:00
|
|
|
std::getline(in, line);
|
|
|
|
} else {
|
|
|
|
std::pair<std::string, std::string> hp;
|
|
|
|
util::divide(hp, line, ':');
|
|
|
|
while(std::getline(in, line)) {
|
|
|
|
if(!line.empty() && (line[0] == ' ' || line[0] == '\t')) {
|
|
|
|
line = util::strip(line);
|
|
|
|
hp.second += " ";
|
|
|
|
hp.second += line;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
put(hp.first, hp.second);
|
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
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|