File: uri.cc

package info (click to toggle)
monotone 0.48-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 20,096 kB
  • ctags: 8,077
  • sloc: cpp: 81,000; sh: 6,402; perl: 1,241; lisp: 1,045; makefile: 655; python: 566; sql: 112; ansic: 52
file content (217 lines) | stat: -rw-r--r-- 6,435 bytes parent folder | download
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
// Copyright (C) 2006 Graydon Hoare <graydon@pobox.com>
//
// This program is made available under the GNU GPL version 2.0 or
// greater. See the accompanying file COPYING for details.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE.

#include "base.hh"
#include "sanity.hh"
#include "uri.hh"

using std::string;
typedef string::size_type stringpos;

static void
parse_authority(string const & in, uri_t & uri, origin::type made_from)
{
  L(FL("matched URI authority: '%s'") % in);

  stringpos p = 0;

  // First, there might be a user: one or more non-@ characters followed
  // by an @.
  stringpos user_end = in.find('@', p);
  if (user_end != 0 && user_end < in.size())
    {
      uri.user.assign(in, 0, user_end);
      p = user_end + 1;
      L(FL("matched URI user: '%s'") % uri.user);
    }

  // The next thing must either be an ipv6 address, which has the form
  // \[ [0-9A-Za-z:]+ \] and we discard the square brackets, or some other
  // sort of hostname, [^:]+.  (A host-part can be terminated by /, ?, or #
  // as well as :, but our caller has taken care of that.)
  if (p < in.size() && in.at(p) == '[')
    {
      p++;
      stringpos ipv6_end = in.find(']', p);
      E(ipv6_end != string::npos, made_from,
        F("IPv6 address in URI has no closing ']'"));

      uri.host.assign(in, p, ipv6_end - p);
      p = ipv6_end + 1;
      L(FL("matched URI host (IPv6 address): '%s'") % uri.host);
    }
  else
    {
      stringpos host_end = in.find(':', p);
      uri.host.assign(in, p, host_end - p);
      p = host_end;
      L(FL("matched URI host: '%s'") % uri.host);
    }

  // Finally, if the host-part was ended by a colon, there is a port number
  // following, which must consist entirely of digits.
  if (p < in.size() && in.at(p) == ':')
    {
      p++;
      E(p < in.size(), made_from,
        F("explicit port-number specification in URI has no digits"));

      E(in.find_first_not_of("0123456789", p) == string::npos, made_from,
        F("explicit port-number specification in URI contains nondigits"));

      uri.port.assign(in, p, string::npos);
      L(FL("matched URI port: '%s'") % uri.port);
    }
}

void
parse_uri(string const & in, uri_t & uri, origin::type made_from)
{
  uri.clear();

  stringpos p = 0;

  // This is a simplified URI grammar. It does the basics.

  // First there may be a scheme: one or more characters which are not
  // ":/?#", followed by a colon.
  stringpos scheme_end = in.find_first_of(":/?#", p);

  if (scheme_end != 0 && scheme_end < in.size() && in.at(scheme_end) == ':')
    {
      uri.scheme.assign(in, p, scheme_end - p);
      p = scheme_end + 1;
      L(FL("matched URI scheme: '%s'") % uri.scheme);
    }

  // Next, there may be an authority: "//" followed by zero or more
  // characters which are not "/?#".

  if (p + 1 < in.size() && in.at(p) == '/' && in.at(p+1) == '/')
    {
      p += 2;
      stringpos authority_end = in.find_first_of("/?#", p);
      if (authority_end != p)
        {
          parse_authority(string(in, p, authority_end - p), uri, made_from);
          p = authority_end;
        }
      if (p >= in.size())
        return;
    }

  // Next, a path: zero or more characters which are not "?#".
  {
    stringpos path_end = in.find_first_of("?#", p);
    uri.path.assign(in, p, path_end - p);
    p = path_end;
    L(FL("matched URI path: '%s'") % uri.path);
    if (p >= in.size())
      return;
  }

  // Next, perhaps a query: "?" followed by zero or more characters
  // which are not "#".
  if (in.at(p) == '?')
    {
      p++;
      stringpos query_end = in.find('#', p);
      uri.query.assign(in, p, query_end - p);
      p = query_end;
      L(FL("matched URI query: '%s'") % uri.query);
      if (p >= in.size())
        return;
    }

  // Finally, if there is a '#', then whatever comes after it in the string
  // is a fragment identifier.
  if (in.at(p) == '#')
    {
      uri.fragment.assign(in, p + 1, string::npos);
      L(FL("matched URI fragment: '%s'") % uri.fragment);
    }
}

string
urldecode(string const & in, origin::type made_from)
{
  string out;

  for (string::const_iterator i = in.begin(); i != in.end(); ++i)
    {
      if (*i != '%')
        out += *i;
      else
        {
          char d1, d2;
          ++i;
          E(i != in.end(), made_from, F("Bad URLencoded string '%s'") % in);
          d1 = *i;
          ++i;
          E(i != in.end(), made_from, F("Bad URLencoded string '%s'") % in);
          d2 = *i;

          char c = 0;
          switch(d1)
            {
            case '0': c += 0; break;
            case '1': c += 1; break;
            case '2': c += 2; break;
            case '3': c += 3; break;
            case '4': c += 4; break;
            case '5': c += 5; break;
            case '6': c += 6; break;
            case '7': c += 7; break;
            case '8': c += 8; break;
            case '9': c += 9; break;
            case 'a': case 'A': c += 10; break;
            case 'b': case 'B': c += 11; break;
            case 'c': case 'C': c += 12; break;
            case 'd': case 'D': c += 13; break;
            case 'e': case 'E': c += 14; break;
            case 'f': case 'F': c += 15; break;
            default: E(false, made_from, F("Bad URLencoded string '%s'") % in);
            }
          c *= 16;
          switch(d2)
            {
            case '0': c += 0; break;
            case '1': c += 1; break;
            case '2': c += 2; break;
            case '3': c += 3; break;
            case '4': c += 4; break;
            case '5': c += 5; break;
            case '6': c += 6; break;
            case '7': c += 7; break;
            case '8': c += 8; break;
            case '9': c += 9; break;
            case 'a': case 'A': c += 10; break;
            case 'b': case 'B': c += 11; break;
            case 'c': case 'C': c += 12; break;
            case 'd': case 'D': c += 13; break;
            case 'e': case 'E': c += 14; break;
            case 'f': case 'F': c += 15; break;
            default: E(false, made_from, F("Bad URLencoded string '%s'") % in);
            }
          out += c;
        }
    }

  return out;
}



// Local Variables:
// mode: C++
// fill-column: 76
// c-file-style: "gnu"
// indent-tabs-mode: nil
// End:
// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s: