aboutsummaryrefslogtreecommitdiff
path: root/web/apache/request.ixx
blob: f7446e5cb3fba505dff21a67e72bc9c607e167de (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
// 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 std::string& 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_;
    }
  }
}