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

#ifndef WEB_APACHE_STREAM
#define WEB_APACHE_STREAM

#include <httpd/httpd.h>
#include <httpd/http_protocol.h>

#include <ios>       // streamsize
#include <memory>    // unique_ptr
#include <cstring>   // memmove()
#include <streambuf>
#include <algorithm> // min(), max()

#include <web/module>

namespace web
{
  namespace apache
  {
    // Object of a class implementing this interface is intended for
    // keeping the state of writing response to the client.
    //
    struct write_state
    {
      // Called by ostreambuf methods when some content to be written to the
      // client.
      //
      virtual void
      set_write_state () = 0;

      // Called to check if any data have already been written to the client.
      // Such checks required for some operations which are impossible to
      // execute after response is partially written.
      //
      virtual bool
      get_write_state () const noexcept = 0;
    };

    class ostreambuf: public std::streambuf
    {
    public:
      ostreambuf (request_rec* rec, write_state& ws): rec_ (rec), ws_ (ws) {}

    private:
      virtual int_type
      overflow (int_type c)
      {
        if (c != traits_type::eof ())
        {
          ws_.set_write_state ();

          char chr = c;

          // Throwing allows to distinguish comm failure from other IO error
          // conditions.
          //
          if (ap_rwrite (&chr, sizeof (chr), rec_) == -1)
            throw invalid_request (HTTP_REQUEST_TIME_OUT);
        }

        return c;
      }

      virtual std::streamsize
      xsputn (const char* s, std::streamsize num)
      {
        ws_.set_write_state ();

        if (ap_rwrite (s, num, rec_) < 0)
        {
          throw invalid_request (HTTP_REQUEST_TIME_OUT);
        }

        return num;
      }

      virtual int
      sync ()
      {
        if (ap_rflush (rec_) < 0)
        {
          throw invalid_request (HTTP_REQUEST_TIME_OUT);
        }

        return 0;
      }

    private:

      request_rec* rec_;
      write_state& ws_;
    };

    class istreambuf: public std::streambuf
    {
    public:
      istreambuf (request_rec* rec,
                  write_state& ws,
                  size_t bufsize = 1024,
                  size_t putback = 1)
          : rec_ (rec),
            ws_ (ws),
            bufsize_ (std::max (bufsize, (size_t)1)),
            putback_ (std::min (putback, bufsize_ - 1)),
            buf_ (new char[bufsize_])
      {
        if (ws_.get_write_state ())
        {
          throw sequence_error ("::web::apache::istreambuf::istreambuf");
        }

        char* p = buf_.get () + putback_;
        setg (p, p, p);

        int status = ap_setup_client_block (rec_, REQUEST_CHUNKED_DECHUNK);

        if (status != OK)
        {
          throw invalid_request (status);
        }
      }

    private:
      virtual int_type
      underflow ()
      {
        if (ws_.get_write_state ())
        {
          throw sequence_error ("::web::apache::istreambuf::underflow");
        }

        if (gptr () < egptr ())
          return traits_type::to_int_type (*gptr ());

        size_t pb = std::min ((size_t)(gptr () - eback ()), putback_);
        std::memmove (buf_.get () + putback_ - pb, gptr () - pb, pb);

        char* p = buf_.get () + putback_;
        int rb = ap_get_client_block (rec_, p, bufsize_ - putback_);

        if (rb == 0)
        {
          return traits_type::eof ();
        }

        if (rb < 0)
        {
          throw invalid_request (HTTP_REQUEST_TIME_OUT);
        }

        setg (p - pb, p, p + rb);
        return traits_type::to_int_type (*gptr ());
      }

    private:

      request_rec* rec_;
      write_state& ws_;
      size_t bufsize_;
      size_t putback_;
      std::unique_ptr<char[]> buf_;
    };

  }
}

#endif // WEB_APACHE_STREAM