uri.cc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2010 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "uri.h"
  36. #include "A2STR.h"
  37. #include "FeatureConfig.h"
  38. #include "util.h"
  39. namespace aria2 {
  40. namespace uri {
  41. UriStruct::UriStruct()
  42. : port(0), hasPassword(false), ipv6LiteralAddress(false)
  43. {}
  44. UriStruct::UriStruct(const UriStruct& c)
  45. : protocol(c.protocol),
  46. host(c.host),
  47. port(c.port),
  48. dir(c.dir),
  49. file(c.file),
  50. query(c.query),
  51. username(c.username),
  52. password(c.password),
  53. hasPassword(c.hasPassword),
  54. ipv6LiteralAddress(c.ipv6LiteralAddress)
  55. {}
  56. UriStruct::~UriStruct() {}
  57. UriStruct& UriStruct::operator=(const UriStruct& c)
  58. {
  59. if(this != &c) {
  60. protocol = c.protocol;
  61. host = c.host;
  62. port = c.port;
  63. dir = c.dir;
  64. file = c.file;
  65. query = c.query;
  66. username = c.username;
  67. password = c.password;
  68. hasPassword = c.hasPassword;
  69. ipv6LiteralAddress = c.ipv6LiteralAddress;
  70. }
  71. return *this;
  72. }
  73. void UriStruct::swap(UriStruct& other)
  74. {
  75. using std::swap;
  76. if(this != &other) {
  77. swap(protocol, other.protocol);
  78. swap(host, other.host);
  79. swap(port, other.port);
  80. swap(dir, other.dir);
  81. swap(file, other.file);
  82. swap(query, other.query);
  83. swap(username, other.username);
  84. swap(password, other.password);
  85. swap(hasPassword, other.hasPassword);
  86. swap(ipv6LiteralAddress, other.ipv6LiteralAddress);
  87. }
  88. }
  89. void swap(UriStruct& lhs, UriStruct& rhs)
  90. {
  91. lhs.swap(rhs);
  92. }
  93. bool parse(UriStruct& result, const std::string& uri)
  94. {
  95. // http://user:password@aria2.sourceforge.net:80/dir/file?query#fragment
  96. // | || || | | | |
  97. // | || hostLast| | | | |
  98. // | || portFirst| | | |
  99. // authorityFirst || authorityLast | | |
  100. // || | | | |
  101. // userInfoLast | | | |
  102. // | | | | |
  103. // hostPortFirst | | | |
  104. // | | | |
  105. // dirFirst dirLast| |
  106. // | |
  107. // queryFirst fragmentFirst
  108. // find fragment part
  109. std::string::const_iterator fragmentFirst = uri.begin();
  110. for(; fragmentFirst != uri.end(); ++fragmentFirst) {
  111. if(*fragmentFirst == '#') break;
  112. }
  113. // find query part
  114. std::string::const_iterator queryFirst = uri.begin();
  115. for(; queryFirst != fragmentFirst; ++queryFirst) {
  116. if(*queryFirst == '?') break;
  117. }
  118. result.query.assign(queryFirst, fragmentFirst);
  119. // find protocol
  120. std::string::size_type protocolOffset = uri.find("://");
  121. if(protocolOffset == std::string::npos) return false;
  122. result.protocol.assign(uri.begin(), uri.begin()+protocolOffset);
  123. uint16_t defPort;
  124. if((defPort = FeatureConfig::getInstance()->
  125. getDefaultPort(result.protocol)) == 0) {
  126. return false;
  127. }
  128. // find authority
  129. std::string::const_iterator authorityFirst = uri.begin()+protocolOffset+3;
  130. std::string::const_iterator authorityLast = authorityFirst;
  131. for(; authorityLast != queryFirst; ++authorityLast) {
  132. if(*authorityLast == '/') break;
  133. }
  134. if(authorityFirst == authorityLast) {
  135. // No authority found
  136. return false;
  137. }
  138. // find userinfo(username and password) in authority if they exist
  139. result.username = A2STR::NIL;
  140. result.password = A2STR::NIL;
  141. result.hasPassword = false;
  142. std::string::const_iterator userInfoLast = authorityLast;
  143. std::string::const_iterator hostPortFirst = authorityFirst;
  144. for(; userInfoLast != authorityFirst-1; --userInfoLast) {
  145. if(*userInfoLast == '@') {
  146. hostPortFirst = userInfoLast;
  147. ++hostPortFirst;
  148. std::string::const_iterator userLast = authorityFirst;
  149. for(; userLast != userInfoLast; ++userLast) {
  150. if(*userLast == ':') {
  151. result.password =
  152. util::percentDecode(userLast+1,userInfoLast);
  153. result.hasPassword = true;
  154. break;
  155. }
  156. }
  157. result.username =
  158. util::percentDecode(authorityFirst, userLast);
  159. break;
  160. }
  161. }
  162. std::string::const_iterator hostLast = hostPortFirst;
  163. std::string::const_iterator portFirst = authorityLast;
  164. result.ipv6LiteralAddress = false;
  165. if(*hostPortFirst == '[') {
  166. // Detected IPv6 literal address in square brackets
  167. for(; hostLast != authorityLast; ++hostLast) {
  168. if(*hostLast == ']') {
  169. ++hostLast;
  170. if(hostLast == authorityLast) {
  171. result.ipv6LiteralAddress = true;
  172. } else {
  173. if(*hostLast == ':') {
  174. portFirst = hostLast;
  175. ++portFirst;
  176. result.ipv6LiteralAddress = true;
  177. }
  178. }
  179. break;
  180. }
  181. }
  182. if(!result.ipv6LiteralAddress) {
  183. return false;
  184. }
  185. } else {
  186. for(; hostLast != authorityLast; ++hostLast) {
  187. if(*hostLast == ':') {
  188. portFirst = hostLast;
  189. ++portFirst;
  190. break;
  191. }
  192. }
  193. }
  194. if(hostPortFirst == hostLast) {
  195. // No host
  196. return false;
  197. }
  198. if(portFirst == authorityLast) {
  199. // If port is not specified, then we set it to default port of
  200. // its protocol..
  201. result.port = defPort;
  202. } else {
  203. uint32_t tempPort;
  204. if(util::parseUIntNoThrow(tempPort, portFirst, authorityLast)) {
  205. if(65535 < tempPort) {
  206. return false;
  207. }
  208. result.port = tempPort;
  209. } else {
  210. return false;
  211. }
  212. }
  213. if(result.ipv6LiteralAddress) {
  214. result.host.assign(hostPortFirst+1, hostLast-1);
  215. } else {
  216. result.host.assign(hostPortFirst, hostLast);
  217. }
  218. // find directory and file part
  219. std::string::const_iterator dirLast = authorityLast;
  220. for(std::string::const_iterator i = authorityLast;
  221. i != queryFirst; ++i) {
  222. if(*i == '/') {
  223. dirLast = i;
  224. }
  225. }
  226. if(dirLast == queryFirst) {
  227. result.file = A2STR::NIL;
  228. } else {
  229. result.file.assign(dirLast+1, queryFirst);
  230. }
  231. // Erase duplicated slashes.
  232. std::string::const_iterator dirFirst = authorityLast;
  233. for(; dirFirst != dirLast; ++dirFirst) {
  234. if(*dirFirst != '/') {
  235. --dirFirst;
  236. break;
  237. }
  238. }
  239. for(; dirLast != dirFirst; --dirLast) {
  240. if(*dirLast != '/') {
  241. ++dirLast;
  242. break;
  243. }
  244. }
  245. if(dirFirst == dirLast) {
  246. result.dir = A2STR::SLASH_C;
  247. } else {
  248. result.dir.assign(dirFirst, dirLast);
  249. }
  250. return true;
  251. }
  252. std::string construct(const UriStruct& us)
  253. {
  254. std::string res;
  255. res += us.protocol;
  256. res += "://";
  257. if(!us.username.empty()) {
  258. res += util::percentEncode(us.username);
  259. if(us.hasPassword) {
  260. res += ":";
  261. res += util::percentEncode(us.password);
  262. }
  263. res += "@";
  264. }
  265. if(us.ipv6LiteralAddress) {
  266. res += "[";
  267. res += us.host;
  268. res += "]";
  269. } else {
  270. res += us.host;
  271. }
  272. uint16_t defPort= FeatureConfig::getInstance()->
  273. getDefaultPort(us.protocol);
  274. if(us.port != 0 && defPort != us.port) {
  275. res += ":";
  276. res += util::uitos(us.port);
  277. }
  278. res += us.dir;
  279. if(us.dir.empty() || us.dir[us.dir.size()-1] != '/') {
  280. res += "/";
  281. }
  282. res += us.file;
  283. res += us.query;
  284. return res;
  285. }
  286. std::string joinUri(const std::string& baseUri, const std::string& uri)
  287. {
  288. UriStruct us;
  289. if(parse(us, uri)) {
  290. return uri;
  291. } else {
  292. UriStruct bus;
  293. if(!parse(bus, baseUri)) {
  294. return uri;
  295. }
  296. std::vector<std::string> parts;
  297. if(uri.empty() || uri[0] != '/') {
  298. util::split(bus.dir.begin(), bus.dir.end(), std::back_inserter(parts),
  299. '/');
  300. }
  301. std::string::const_iterator qend;
  302. for(qend = uri.begin(); qend != uri.end(); ++qend) {
  303. if(*qend == '#') {
  304. break;
  305. }
  306. }
  307. std::string::const_iterator end;
  308. for(end = uri.begin(); end != qend; ++end) {
  309. if(*end == '?') {
  310. break;
  311. }
  312. }
  313. util::split(uri.begin(), end, std::back_inserter(parts), '/');
  314. bus.dir.clear();
  315. bus.file.clear();
  316. bus.query.clear();
  317. std::string res = construct(bus);
  318. res += util::joinPath(parts.begin(), parts.end());
  319. if((uri.begin() == end || *(end-1) == '/') && *(res.end()-1) != '/') {
  320. res += "/";
  321. }
  322. res.append(end, qend);
  323. return res;
  324. }
  325. }
  326. } // namespace uri
  327. } // namespace aria2