aboutsummaryrefslogtreecommitdiff
path: root/web/mime-url-encoding.cxx
blob: 1d68bf35cc5d48a566b77e724c2103fb7bad9d69 (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
// file      : web/mime-url-encoding.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <web/mime-url-encoding>

#include <ios>       // hex, uppercase, right
#include <string>
#include <iomanip>   // setw(), setfill ()
#include <ostream>
#include <sstream>
#include <cstring>   // size_t, strspn()
#include <stdexcept> // invalid_argument

using namespace std;

namespace web
{
  // Encode characters different from unreserved ones specified in
  // "2.3.  Unreserved Characters" of http://tools.ietf.org/html/rfc3986.
  //
  void
  mime_url_encode (const char* v, ostream& o)
  {
    char f (o.fill ());
    ostream::fmtflags g (o.flags ());
    o << hex << uppercase << right << setfill ('0');

    char c;
    while ((c = *v++) != '\0')
    {
      if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
          (c >= '0' && c <= '9'))
      {
        o << c;
      }
      else
      {
        switch (c)
        {
        case ' ': o << '+'; break;
        case '.':
        case '_':
        case '-':
        case '~': o << c; break;
        default:
          {
            o << "%" << setw (2) << static_cast<unsigned short> (c);
            break;
          }
        }
      }
    }

    o.flags (g);
    o.fill (f);
  }

  string
  mime_url_encode (const char* v)
  {
    stringstream o;
    mime_url_encode (v, o);
    return o.str ();
  }

  string
  mime_url_encode (const string& v)
  {
    return mime_url_encode (v.c_str ());
  }

  string
  mime_url_decode (const char* b, const char* e, bool trim)
  {
    if (trim)
    {
      b += strspn (b, " ");

      if (b >= e)
        return string ();

      while (*--e == ' ');
      ++e;
    }

    string value;
    value.reserve (e - b);

    char bf[3];
    bf[2] = '\0';

    while (b != e)
    {
      char c (*b++);
      switch (c)
      {
      case '+':
        {
          value.append (" ");
          break;
        }
      case '%':
        {
          if (*b == '\0' || b[1] == '\0')
          {
            throw invalid_argument ("::web::mime_url_decode short");
          }

          *bf = *b;
          bf[1] = b[1];

          char* ebf (nullptr);
          size_t vl (strtoul (bf, &ebf, 16));

          if (*ebf != '\0')
          {
            throw invalid_argument ("::web::mime_url_decode wrong");
          }

          value.append (1, static_cast<char> (vl));
          b += 2;
          break;
        }
      default:
        {
          value.append (1, c);
          break;
        }
      }
    }

    return value;
  }
}