summaryrefslogtreecommitdiff
path: root/src/uri.cc
blob: 1241ea2c967051968111609e0334116090666451 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/* <!-- 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 {

UriStruct::UriStruct() : port(0), hasPassword(false), ipv6LiteralAddress(false)
{
}

UriStruct::UriStruct(const UriStruct& c) = default;

UriStruct::~UriStruct() = default;

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;
    port = c.port;
    hasPassword = c.hasPassword;
    ipv6LiteralAddress = c.ipv6LiteralAddress;
  }
  return *this;
}

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);
    swap(port, other.port);
    swap(hasPassword, other.hasPassword);
    swap(ipv6LiteralAddress, other.ipv6LiteralAddress);
  }
}

void swap(UriStruct& lhs, UriStruct& rhs) { lhs.swap(rhs); }

bool parse(UriStruct& result, const std::string& uri)
{
  uri_split_result res;
  int rv;
  const char* p = uri.c_str();
  rv = uri_split(&res, p);
  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;
    }
    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,
                         res.fields[USR_BASENAME].len);
    }
    else {
      result.dir.assign(p + res.fields[USR_PATH].off, res.fields[USR_PATH].len);
      result.file = A2STR::NIL;
    }
  }
  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;
}

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);
  }
  return "";
}

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 += "]";
  }
  else {
    res += us.host;
  }

  uint16_t defPort = getDefaultPort(us.protocol);
  if (us.port != 0 && defPort != us.port) {
    res += fmt(":%u", us.port);
  }

  res += us.dir;
  if (us.dir.empty() || us.dir[us.dir.size() - 1] != '/') {
    res += "/";
  }

  res += us.file;
  res += us.query;
  return res;
}

namespace {
enum { NPATH_START, NPATH_SLASH, NPATH_SDOT, NPATH_DDOT, NPATH_PATHCOMP };
}

std::string normalizePath(std::string path)
{
  auto begin = path.begin(), out = begin;
  int state = NPATH_START;
  bool startWithSlash = false;
  std::vector<int> range;
  // 32 is arbitrary
  range.reserve(32);

  for (auto in = begin, eoi = path.end(); in != eoi; ++in) {
    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;
    }
  }

  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;
  }

  if (startWithSlash) {
    ++out;
  }

  for (int i = 0; i < (int)range.size(); i += 2) {
    auto a = begin + range[i];
    auto b = begin + range[i + 1];
    if (a == out) {
      out = b;
    }
    else {
      out = std::copy(a, b, out);
    }
  }

  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;
  }

  if (basePath.empty() || *newPathFirst == '/') {
    return normalizePath(std::string(newPathFirst, newPathLast));
  }

  if (basePath[basePath.size() - 1] == '/') {
    basePath.append(newPathFirst, newPathLast);
    return normalizePath(basePath);
  }

  basePath += "/";
  basePath.append(newPathFirst, newPathLast);
  return normalizePath(basePath);
}
} // namespace

std::string joinPath(const std::string& basePath, const std::string& newPath)
{
  return joinPath(basePath, newPath.begin(), newPath.end());
}

std::string joinUri(const std::string& baseUri, const std::string& uri)
{
  UriStruct us;
  if (parse(us, uri)) {
    return uri;
  }

  UriStruct bus;
  if (!parse(bus, baseUri)) {
    return uri;
  }

  std::string::const_iterator qend;
  for (qend = uri.begin(); qend != uri.end(); ++qend) {
    if (*qend == '#') {
      break;
    }
  }
  std::string::const_iterator end;
  for (end = uri.begin(); end != qend; ++end) {
    if (*end == '?') {
      break;
    }
  }
  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;
}

} // namespace uri

} // namespace aria2