aboutsummaryrefslogtreecommitdiff
path: root/web/apache/request.ixx
blob: 0d3aefc1004196741723cd8c0ddcbbab59b6b7dc (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
// file      : web/apache/request.ixx -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
// 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) == 0)
        {
          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_;
    }
  }
}