Welcome to mirror list, hosted at ThFree Co, Russian Federation.

uri.cpp « coding - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1c1dafbf9bb7027b04aae89f6c5fbe5947e40feb (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
#include "coding/uri.hpp"
#include "coding/url_encode.hpp"

#include "base/assert.hpp"

namespace url_scheme
{

void Uri::Init()
{
  if (!Parse())
  {
    ASSERT(m_scheme.empty() && m_path.empty() && !IsValid(), ());
    m_queryStart = m_url.size();
  }
}

bool Uri::Parse()
{
  // get url scheme
  size_t pathStart = m_url.find(':');
  if (pathStart == string::npos || pathStart == 0)
    return false;
  m_scheme.assign(m_url, 0, pathStart);

  // skip slashes
  while (++pathStart < m_url.size() && m_url[pathStart] == '/') {}

  // Find query starting point for (key, value) parsing.
  m_queryStart = m_url.find('?', pathStart);
  size_t pathLength;
  if (m_queryStart == string::npos)
  {
    m_queryStart = m_url.size();
    pathLength = m_queryStart - pathStart;
  }
  else
  {
    pathLength = m_queryStart - pathStart;
    ++m_queryStart;
  }

  // Get path (url without query).
  m_path.assign(m_url, pathStart, pathLength);

  return true;
}

bool Uri::ForEachKeyValue(TCallback const & callback) const
{
  // parse query for keys and values
  size_t const count = m_url.size();
  size_t const queryStart = m_queryStart;

  // Just a URL without parameters.
  if (queryStart == count)
    return false;

  for (size_t start = queryStart; start < count; )
  {
    size_t end = m_url.find('&', start);
    if (end == string::npos)
      end = count;

    // Skip empty keys.
    if (end != start)
    {
      size_t const eq = m_url.find('=', start);

      string key, value;
      if (eq != string::npos && eq < end)
      {
        key = UrlDecode(m_url.substr(start, eq - start));
        value = UrlDecode(m_url.substr(eq + 1, end - eq - 1));
      }
      else
        key = UrlDecode(m_url.substr(start, end - start));

      if (!callback(key, value))
        return false;
    }

    start = end + 1;
  }
  return true;
}

}