2010-09-11 09:11:57 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
|
|
|
* aria2 - The high speed download utility
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 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
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
* 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 "uri.h"
|
|
|
|
#include "A2STR.h"
|
|
|
|
#include "FeatureConfig.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
namespace aria2 {
|
|
|
|
|
|
|
|
namespace uri {
|
|
|
|
|
2010-11-14 07:17:55 +00:00
|
|
|
UriStruct::UriStruct()
|
|
|
|
: port(0), hasPassword(false), ipv6LiteralAddress(false)
|
|
|
|
{}
|
|
|
|
|
|
|
|
UriStruct::UriStruct(const UriStruct& c)
|
|
|
|
: protocol(c.protocol),
|
|
|
|
host(c.host),
|
|
|
|
dir(c.dir),
|
|
|
|
file(c.file),
|
|
|
|
query(c.query),
|
|
|
|
username(c.username),
|
|
|
|
password(c.password),
|
2013-11-26 01:59:51 +00:00
|
|
|
port(c.port),
|
2010-11-14 07:17:55 +00:00
|
|
|
hasPassword(c.hasPassword),
|
|
|
|
ipv6LiteralAddress(c.ipv6LiteralAddress)
|
|
|
|
{}
|
|
|
|
|
|
|
|
UriStruct::~UriStruct() {}
|
|
|
|
|
|
|
|
UriStruct& UriStruct::operator=(const UriStruct& c)
|
|
|
|
{
|
|
|
|
if(this != &c) {
|
|
|
|
protocol = c.protocol;
|
|
|
|
host = c.host;
|
|
|
|
dir = c.dir;
|
|
|
|
file = c.file;
|
|
|
|
query = c.query;
|
|
|
|
username = c.username;
|
|
|
|
password = c.password;
|
2013-11-26 01:59:51 +00:00
|
|
|
port = c.port;
|
2010-11-14 07:17:55 +00:00
|
|
|
hasPassword = c.hasPassword;
|
|
|
|
ipv6LiteralAddress = c.ipv6LiteralAddress;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2011-04-29 15:29:14 +00:00
|
|
|
void UriStruct::swap(UriStruct& other)
|
|
|
|
{
|
|
|
|
using std::swap;
|
|
|
|
if(this != &other) {
|
|
|
|
swap(protocol, other.protocol);
|
|
|
|
swap(host, other.host);
|
|
|
|
swap(dir, other.dir);
|
|
|
|
swap(file, other.file);
|
|
|
|
swap(query, other.query);
|
|
|
|
swap(username, other.username);
|
|
|
|
swap(password, other.password);
|
2013-11-26 01:59:51 +00:00
|
|
|
swap(port, other.port);
|
2011-04-29 15:29:14 +00:00
|
|
|
swap(hasPassword, other.hasPassword);
|
|
|
|
swap(ipv6LiteralAddress, other.ipv6LiteralAddress);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void swap(UriStruct& lhs, UriStruct& rhs)
|
|
|
|
{
|
|
|
|
lhs.swap(rhs);
|
|
|
|
}
|
|
|
|
|
2010-09-11 09:11:57 +00:00
|
|
|
bool parse(UriStruct& result, const std::string& uri)
|
|
|
|
{
|
2012-10-13 15:14:50 +00:00
|
|
|
uri_split_result res;
|
|
|
|
int rv;
|
|
|
|
const char* p = uri.c_str();
|
|
|
|
rv = uri_split(&res, p);
|
2013-08-25 15:02:24 +00:00
|
|
|
if(rv != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
result.protocol.assign(p + res.fields[USR_SCHEME].off,
|
|
|
|
res.fields[USR_SCHEME].len);
|
|
|
|
result.host.assign(p + res.fields[USR_HOST].off, res.fields[USR_HOST].len);
|
|
|
|
if (res.port == 0) {
|
|
|
|
uint16_t defPort;
|
|
|
|
if((defPort = getDefaultPort(result.protocol)) == 0) {
|
|
|
|
return false;
|
2010-09-11 09:11:57 +00:00
|
|
|
}
|
2013-08-25 15:02:24 +00:00
|
|
|
result.port = defPort;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result.port = res.port;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(res.field_set & (1 << USR_PATH)) {
|
|
|
|
if(res.field_set & (1 << USR_BASENAME)) {
|
|
|
|
result.dir.assign(p + res.fields[USR_PATH].off,
|
|
|
|
res.fields[USR_PATH].len -
|
|
|
|
res.fields[USR_BASENAME].len);
|
|
|
|
result.file.assign(p + res.fields[USR_BASENAME].off,
|
2012-10-13 15:14:50 +00:00
|
|
|
res.fields[USR_BASENAME].len);
|
2010-09-11 09:11:57 +00:00
|
|
|
}
|
2013-08-25 15:02:24 +00:00
|
|
|
else {
|
|
|
|
result.dir.assign(p + res.fields[USR_PATH].off,
|
|
|
|
res.fields[USR_PATH].len);
|
|
|
|
result.file = A2STR::NIL;
|
2010-09-11 09:11:57 +00:00
|
|
|
}
|
|
|
|
}
|
2013-08-25 15:02:24 +00:00
|
|
|
else {
|
|
|
|
result.dir = "/";
|
|
|
|
result.file = A2STR::NIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(res.field_set & (1 << USR_QUERY)) {
|
|
|
|
result.query = "?";
|
|
|
|
result.query.append(p + res.fields[USR_QUERY].off,
|
|
|
|
res.fields[USR_QUERY].len);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result.query = A2STR::NIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(res.field_set & (1 << USR_USER)) {
|
|
|
|
result.username.assign(p + res.fields[USR_USER].off,
|
|
|
|
res.fields[USR_USER].len);
|
|
|
|
result.username = util::percentDecode(result.username.begin(),
|
|
|
|
result.username.end());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result.username = A2STR::NIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(res.field_set & (1 << USR_PASSWD)) {
|
|
|
|
result.hasPassword = true;
|
|
|
|
result.password.assign(p + res.fields[USR_PASSWD].off,
|
|
|
|
res.fields[USR_PASSWD].len);
|
|
|
|
result.password = util::percentDecode(result.password.begin(),
|
|
|
|
result.password.end());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result.hasPassword = false;
|
|
|
|
result.password = A2STR::NIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
result.ipv6LiteralAddress = res.flags & USF_IPV6ADDR;
|
|
|
|
return true;
|
2010-09-11 09:11:57 +00:00
|
|
|
}
|
|
|
|
|
2012-10-14 09:05:34 +00:00
|
|
|
std::string getFieldString(const uri_split_result& res, int field,
|
|
|
|
const char* base)
|
|
|
|
{
|
|
|
|
if(res.field_set & (1 << field)) {
|
|
|
|
return std::string(base + res.fields[field].off, res.fields[field].len);
|
|
|
|
}
|
2013-08-25 15:02:24 +00:00
|
|
|
return "";
|
2012-10-14 09:05:34 +00:00
|
|
|
}
|
|
|
|
|
2011-04-29 15:14:49 +00:00
|
|
|
std::string construct(const UriStruct& us)
|
|
|
|
{
|
|
|
|
std::string res;
|
|
|
|
res += us.protocol;
|
|
|
|
res += "://";
|
|
|
|
if(!us.username.empty()) {
|
|
|
|
res += util::percentEncode(us.username);
|
|
|
|
if(us.hasPassword) {
|
|
|
|
res += ":";
|
|
|
|
res += util::percentEncode(us.password);
|
|
|
|
}
|
|
|
|
res += "@";
|
|
|
|
}
|
|
|
|
if(us.ipv6LiteralAddress) {
|
|
|
|
res += "[";
|
|
|
|
res += us.host;
|
|
|
|
res += "]";
|
2013-08-25 15:02:24 +00:00
|
|
|
}
|
|
|
|
else {
|
2011-04-29 15:14:49 +00:00
|
|
|
res += us.host;
|
|
|
|
}
|
2013-08-25 15:02:24 +00:00
|
|
|
|
|
|
|
uint16_t defPort = getDefaultPort(us.protocol);
|
2011-04-29 15:14:49 +00:00
|
|
|
if(us.port != 0 && defPort != us.port) {
|
2011-11-12 13:36:05 +00:00
|
|
|
res += fmt(":%u", us.port);
|
2011-04-29 15:14:49 +00:00
|
|
|
}
|
2013-08-25 15:02:24 +00:00
|
|
|
|
2011-04-29 15:14:49 +00:00
|
|
|
res += us.dir;
|
|
|
|
if(us.dir.empty() || us.dir[us.dir.size()-1] != '/') {
|
|
|
|
res += "/";
|
|
|
|
}
|
2013-08-25 15:02:24 +00:00
|
|
|
|
2011-04-29 15:14:49 +00:00
|
|
|
res += us.file;
|
|
|
|
res += us.query;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-08-25 15:02:24 +00:00
|
|
|
namespace {
|
|
|
|
enum {
|
|
|
|
NPATH_START,
|
|
|
|
NPATH_SLASH,
|
|
|
|
NPATH_SDOT,
|
|
|
|
NPATH_DDOT,
|
|
|
|
NPATH_PATHCOMP
|
|
|
|
};
|
|
|
|
}
|
2013-06-18 12:27:01 +00:00
|
|
|
|
|
|
|
std::string normalizePath(std::string path)
|
|
|
|
{
|
2013-09-23 05:29:24 +00:00
|
|
|
auto begin = path.begin(), out = begin;
|
2013-06-18 12:27:01 +00:00
|
|
|
int state = NPATH_START;
|
|
|
|
bool startWithSlash = false;
|
|
|
|
std::vector<int> range;
|
|
|
|
// 32 is arbitrary
|
|
|
|
range.reserve(32);
|
2013-08-25 15:02:24 +00:00
|
|
|
|
2013-09-23 05:29:24 +00:00
|
|
|
for(auto in = begin, eoi = path.end(); in != eoi; ++in) {
|
2013-06-18 12:27:01 +00:00
|
|
|
switch(state) {
|
|
|
|
case NPATH_START:
|
|
|
|
switch(*in) {
|
|
|
|
case '.':
|
|
|
|
state = NPATH_SDOT;
|
|
|
|
range.push_back(in-begin);
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
startWithSlash = true;
|
|
|
|
state = NPATH_SLASH;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
state = NPATH_PATHCOMP;
|
|
|
|
range.push_back(in-begin);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NPATH_SLASH:
|
|
|
|
switch(*in) {
|
|
|
|
case '.':
|
|
|
|
state = NPATH_SDOT;
|
|
|
|
range.push_back(in-begin);
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
// drop duplicate '/'
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
state = NPATH_PATHCOMP;
|
|
|
|
range.push_back(in-begin);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NPATH_SDOT:
|
|
|
|
switch(*in) {
|
|
|
|
case '.':
|
|
|
|
state = NPATH_DDOT;
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
// drop path component '.'
|
|
|
|
state = NPATH_SLASH;
|
|
|
|
range.pop_back();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
state = NPATH_PATHCOMP;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NPATH_DDOT:
|
|
|
|
switch(*in) {
|
|
|
|
case '/':
|
|
|
|
// drop previous path component before '..'
|
|
|
|
for(int i = 0; i < 3 && !range.empty(); ++i) {
|
|
|
|
range.pop_back();
|
|
|
|
}
|
|
|
|
state = NPATH_SLASH;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
state = NPATH_PATHCOMP;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NPATH_PATHCOMP:
|
|
|
|
if(*in == '/') {
|
|
|
|
range.push_back(in+1-begin);
|
|
|
|
state = NPATH_SLASH;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-08-25 15:02:24 +00:00
|
|
|
|
2013-06-18 12:27:01 +00:00
|
|
|
switch(state) {
|
|
|
|
case NPATH_SDOT:
|
|
|
|
range.pop_back();
|
|
|
|
break;
|
|
|
|
case NPATH_DDOT:
|
|
|
|
for(int i = 0; i < 3 && !range.empty(); ++i) {
|
|
|
|
range.pop_back();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NPATH_PATHCOMP:
|
|
|
|
range.push_back(path.end()-begin);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-08-25 15:02:24 +00:00
|
|
|
|
2013-06-18 12:27:01 +00:00
|
|
|
if(startWithSlash) {
|
|
|
|
++out;
|
|
|
|
}
|
2013-08-25 15:02:24 +00:00
|
|
|
|
2013-06-18 12:27:01 +00:00
|
|
|
for(int i = 0; i < (int)range.size(); i += 2) {
|
2013-09-23 05:29:24 +00:00
|
|
|
auto a = begin + range[i];
|
|
|
|
auto b = begin + range[i+1];
|
2013-06-18 12:27:01 +00:00
|
|
|
if(a == out) {
|
|
|
|
out = b;
|
|
|
|
} else {
|
|
|
|
out = std::copy(a, b, out);
|
|
|
|
}
|
|
|
|
}
|
2013-08-25 15:02:24 +00:00
|
|
|
|
2013-06-18 12:27:01 +00:00
|
|
|
path.erase(out, path.end());
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
std::string joinPath(std::string basePath,
|
|
|
|
std::string::const_iterator newPathFirst,
|
|
|
|
std::string::const_iterator newPathLast)
|
|
|
|
{
|
|
|
|
if(newPathFirst == newPathLast) {
|
|
|
|
return basePath;
|
2013-08-25 15:02:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(basePath.empty() || *newPathFirst == '/') {
|
2013-06-18 12:27:01 +00:00
|
|
|
return normalizePath(std::string(newPathFirst, newPathLast));
|
2013-08-25 15:02:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(basePath[basePath.size()-1] == '/') {
|
2013-06-18 12:27:01 +00:00
|
|
|
basePath.append(newPathFirst, newPathLast);
|
|
|
|
return normalizePath(basePath);
|
|
|
|
}
|
2013-08-25 15:02:24 +00:00
|
|
|
|
|
|
|
basePath += "/";
|
|
|
|
basePath.append(newPathFirst, newPathLast);
|
|
|
|
return normalizePath(basePath);
|
2013-06-18 12:27:01 +00:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
std::string joinPath(const std::string& basePath, const std::string& newPath)
|
|
|
|
{
|
|
|
|
return joinPath(basePath, newPath.begin(), newPath.end());
|
|
|
|
}
|
|
|
|
|
2011-05-06 14:20:43 +00:00
|
|
|
std::string joinUri(const std::string& baseUri, const std::string& uri)
|
|
|
|
{
|
|
|
|
UriStruct us;
|
|
|
|
if(parse(us, uri)) {
|
|
|
|
return uri;
|
2013-08-25 15:02:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UriStruct bus;
|
|
|
|
if(!parse(bus, baseUri)) {
|
|
|
|
return uri;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string::const_iterator qend;
|
|
|
|
for(qend = uri.begin(); qend != uri.end(); ++qend) {
|
|
|
|
if(*qend == '#') {
|
|
|
|
break;
|
2011-05-06 14:20:43 +00:00
|
|
|
}
|
2013-08-25 15:02:24 +00:00
|
|
|
}
|
|
|
|
std::string::const_iterator end;
|
|
|
|
for(end = uri.begin(); end != qend; ++end) {
|
|
|
|
if(*end == '?') {
|
|
|
|
break;
|
2011-05-06 14:20:43 +00:00
|
|
|
}
|
|
|
|
}
|
2013-08-25 15:02:24 +00:00
|
|
|
std::string newpath = joinPath(bus.dir, uri.begin(), end);
|
|
|
|
bus.dir.clear();
|
|
|
|
bus.file.clear();
|
|
|
|
bus.query.clear();
|
|
|
|
std::string res = construct(bus);
|
|
|
|
if(!newpath.empty()) {
|
|
|
|
// res always ends with '/'. Since bus.dir also starts with '/',
|
|
|
|
// regardless of uri, newpath always starts with '/'.
|
|
|
|
res.append(newpath.begin()+1, newpath.end());
|
|
|
|
}
|
|
|
|
res.append(end, qend);
|
|
|
|
return res;
|
2011-05-06 14:20:43 +00:00
|
|
|
}
|
|
|
|
|
2010-09-11 09:11:57 +00:00
|
|
|
} // namespace uri
|
|
|
|
|
|
|
|
} // namespace aria2
|