aboutsummaryrefslogtreecommitdiff
path: root/web/apache/request.ixx
blob: b41de8dc35a35f71ecfd0caa9f609a736542bce3 (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
// file      : web/apache/request.ixx -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#include <strings.h> // strcasecmp()

#include <iomanip>
#include <sstream>
#include <cstring>
#include <cstdlib>

namespace web
{
  namespace apache
  {
    inline int request::
    flush ()
    {
      if (buffer_ && out_buf_)
      {
        auto b = dynamic_cast<std::stringbuf*> (out_buf_.get ());
        assert (b);

        std::string s (b->str ());

        if (!s.empty ())
        {
          // Before writing response read and discard request body if any.
          //
          int r = ap_discard_request_body (rec_);

          if (r == OK)
          {
            set_write_state ();

            if (ap_rwrite (s.c_str (), s.length (), rec_) < 0)
              rec_->status = HTTP_REQUEST_TIME_OUT;
          }

          else
            rec_->status = r;
        }

        out_.reset ();
        out_buf_.reset ();
      }

      return rec_->status == HTTP_OK || get_write_state () ? OK : rec_->status;
    }

    inline const request::string_ptr& request::
    form_data ()
    {
      if (!form_data_)
      {
        form_data_.reset (new std::string ());
        const char *ct = apr_table_get (rec_->headers_in, "Content-Type");

        if (ct && !strncasecmp ("application/x-www-form-urlencoded", ct, 33))
        {
          std::istream& istr (content ());
          std::getline (istr, *form_data_);

          // Make request data still be available.
          //

          std::unique_ptr<std::streambuf> in_buf (
            new std::stringbuf (*form_data_));

          in_.reset (new std::istream (in_buf.get ()));
          in_buf_ = std::move (in_buf);
          in_->exceptions (std::ios::failbit | std::ios::badbit);
        }
      }

      return form_data_;
    }

    inline void request::
    parse_parameters (const char* args)
    {
      for (auto n (args); n != 0; )
      {
        const char* v = std::strchr (n, '=');
        const char* e = ::strchr (n, '&');

        if (e && e < v)
          v = 0;

        std::string name (v
                          ? mime_url_decode (n, v) :
                          (e
                           ? mime_url_decode (n, e)
                           : mime_url_decode (n, n + std::strlen (n))));

        std::string value;

        if (v++)
        {
          value = e
            ? mime_url_decode (v, e)
            : mime_url_decode (v, v + std::strlen (v));
        }

        if (!name.empty () || !value.empty ())
          parameters_->emplace_back (std::move (name), std::move (value));

        n = e ? e + 1 : 0;
      }
    }

    inline void request::
    mime_url_encode (const char* v, std::ostream& o)
    {
      char f = o.fill ();
      std::ios_base::fmtflags g = o.flags ();
      o << std::hex << std::uppercase << std::right << std::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 << "%" << std::setw (2) << static_cast<unsigned short> (c);
              break;
            }
          }
      }

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

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

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

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

      std::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 std::invalid_argument (
                "::web::apache::request::mime_url_decode short");
            }

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

            char* ebf = 0;
            size_t vl = std::strtoul (bf, &ebf, 16);

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

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

      return value;
    }

  }
}