~ubuntu-branches/ubuntu/quantal/aria2/quantal

« back to all changes in this revision

Viewing changes to src/uri.cc

  • Committer: Bazaar Package Importer
  • Author(s): Kartik Mistry
  • Date: 2011-05-26 22:22:12 UTC
  • mfrom: (2.5.3 sid)
  • Revision ID: james.westby@ubuntu.com-20110526222212-8ck4ia81l227m44l
Tags: 1.11.2-1
* New upstream release
* debian/control:
  + Updated Standards-Version to 3.9.2
* debian/copyright:
  + Updated as per latest DEP-5 specification

Show diffs side-by-side

added added

removed removed

Lines of Context:
77
77
  return *this;
78
78
}
79
79
 
 
80
void UriStruct::swap(UriStruct& other)
 
81
{
 
82
  using std::swap;
 
83
  if(this != &other) {
 
84
    swap(protocol, other.protocol);
 
85
    swap(host, other.host);
 
86
    swap(port, other.port);
 
87
    swap(dir, other.dir);
 
88
    swap(file, other.file);
 
89
    swap(query, other.query);
 
90
    swap(username, other.username);
 
91
    swap(password, other.password);
 
92
    swap(hasPassword, other.hasPassword);
 
93
    swap(ipv6LiteralAddress, other.ipv6LiteralAddress);
 
94
  }
 
95
}
 
96
 
 
97
void swap(UriStruct& lhs, UriStruct& rhs)
 
98
{
 
99
  lhs.swap(rhs);
 
100
}
 
101
 
80
102
bool parse(UriStruct& result, const std::string& uri)
81
103
{
82
104
  // http://user:password@aria2.sourceforge.net:80/dir/file?query#fragment
238
260
  return true;
239
261
}
240
262
 
 
263
std::string construct(const UriStruct& us)
 
264
{
 
265
  std::string res;
 
266
  res += us.protocol;
 
267
  res += "://";
 
268
  if(!us.username.empty()) {
 
269
    res += util::percentEncode(us.username);
 
270
    if(us.hasPassword) {
 
271
      res += ":";
 
272
      res += util::percentEncode(us.password);
 
273
    }
 
274
    res += "@";
 
275
  }
 
276
  if(us.ipv6LiteralAddress) {
 
277
    res += "[";
 
278
    res += us.host;
 
279
    res += "]";
 
280
  } else {
 
281
    res += us.host;
 
282
  }
 
283
  uint16_t defPort= FeatureConfig::getInstance()->
 
284
    getDefaultPort(us.protocol);
 
285
  if(us.port != 0 && defPort != us.port) {
 
286
    res += ":";
 
287
    res += util::uitos(us.port);
 
288
  }
 
289
  res += us.dir;
 
290
  if(us.dir.empty() || us.dir[us.dir.size()-1] != '/') {
 
291
    res += "/";
 
292
  }
 
293
  res += us.file;
 
294
  res += us.query;
 
295
  return res;
 
296
}
 
297
 
 
298
std::string joinUri(const std::string& baseUri, const std::string& uri)
 
299
{
 
300
  UriStruct us;
 
301
  if(parse(us, uri)) {
 
302
    return uri;
 
303
  } else {
 
304
    UriStruct bus;
 
305
    if(!parse(bus, baseUri)) {
 
306
      return uri;
 
307
    }
 
308
    std::vector<std::string> parts;
 
309
    if(!util::startsWith(uri, "/")) {
 
310
      util::split(bus.dir, std::back_inserter(parts), "/");
 
311
    }
 
312
    std::string::const_iterator qend;
 
313
    for(qend = uri.begin(); qend != uri.end(); ++qend) {
 
314
      if(*qend == '#') {
 
315
        break;
 
316
      }
 
317
    }
 
318
    std::string::const_iterator end;
 
319
    for(end = uri.begin(); end != qend; ++end) {
 
320
      if(*end == '?') {
 
321
        break;
 
322
      }
 
323
    }
 
324
    std::string path(uri.begin(), end);
 
325
    util::split(path, std::back_inserter(parts), "/");
 
326
    bus.dir.clear();
 
327
    bus.file.clear();
 
328
    bus.query.clear();
 
329
    std::string res = construct(bus);
 
330
    res += util::joinPath(parts.begin(), parts.end());
 
331
    if((path.empty() || util::endsWith(path, "/")) &&
 
332
       !util::endsWith(res, "/")) {
 
333
      res += "/";
 
334
    }
 
335
    res += std::string(end, qend);
 
336
    return res;
 
337
  }
 
338
}
 
339
 
241
340
} // namespace uri
242
341
 
243
342
} // namespace aria2