summaryrefslogtreecommitdiff
path: root/apps/JAWS2/HTTPU/http_base.cpp
blob: 3a653e5feef13bb7e08aa3a9bca58b034ec7db8f (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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// $Id$

#include "JAWS/Parse_Headers.h"
#include "HTTPU/http_base.h"
#include "HTTPU/http_headers.h"

int
HTTP_Base::receive (ACE_Message_Block &mb)
{
  if (this->line () == 0)
    {
      if (this->extract_line (mb) == 0)
        return 0;
      if (this->status () != STATUS_OK)
        return 1;

      // Call into the receive hook.
      this->parse_line ();
      if (this->status_ == STATUS_INTERNAL_SERVER_ERROR || this->no_headers_)
        return 1;
    }

  // Parse headers
  JAWS_Parse_Headers *parser = JAWS_Parse_Headers_Singleton::instance ();
  int ret = parser->parse_headers (&(this->info_), mb);

  switch (this->info_.status ())
    {
    case JAWS_Header_Info::OK:
      break;

    case JAWS_Header_Info::NO_MEMORY:
    case JAWS_Header_Info::TOO_LONG:
    default:
      this->status_ = STATUS_INTERNAL_SERVER_ERROR;
      break;
    }

  return ret;
}

int
HTTP_Base::deliver (ACE_Message_Block &mb)
{
  JAWS_Header_Data *data = 0;

  // Deliver this outgoing request.
  // We do this by building the request up and writing it into the
  // message block.
  if (this->mb_ == 0)
    {
      // Make our Message Block big enough to hold a header name and
      // header value
      this->mb_ = new ACE_Message_Block (16384); // MAGIC!  2 x 8192
      if (this->mb_ == 0)
        {
          this->status_ = STATUS_INTERNAL_SERVER_ERROR;
          return -1;
        }

      // Call into the deliver hook
      int r = this->espouse_line ();
      if (r == -1)
        return -1;

      if (r == 1)
        this->deliver_state_ = 2;

      this->iter_.first ();
    }

  while (this->deliver_state_ < 3)
    {
      // Deliver whatever is currently held in this->mb_.
      size_t sz = (mb.space () < this->mb_->length ()
                   ? mb.space ()
                   : this->mb_->length ());

      if (sz > 0)
        {
          mb.copy (this->mb_->rd_ptr (), sz);
          this->mb_->rd_ptr (sz);
        }

      if (mb.space () == 0)
        return 0;

      // Arriving here means this->mb_ has been emptied.
      this->mb_->crunch ();

      switch (this->deliver_state_)
        {
        case 0: // Obtain the next header data // Deliver a header name
          this->deliver_state_ = this->deliver_header_name (data);
          break;

        case 1: // Deliver a header value
          this->deliver_state_ = this->deliver_header_value (data);
          break;

        case 2: // Finished!
          delete this->mb_;
          this->mb_ = 0;
          this->deliver_state_ = 3;
        }
    }

  return 1;
}

int
HTTP_Base::receive_payload (ACE_Message_Block &mb)
{
  int result = 0;

  if (this->payload_.space () < mb.length ())
    result = this->payload_.size (this->payload_.size () +
                                  mb.length () - this->payload_.space ());

  if (result == 0)
    {
      this->payload_.copy (mb.rd_ptr (), mb.length ());
      mb.rd_ptr (mb.wr_ptr ());
      mb.crunch ();
    }
  else
    this->status_ = STATUS_INTERNAL_SERVER_ERROR;

  return result;
}

int
HTTP_Base::receive_payload (ACE_Message_Block &mb, long length)
{
  int result = 0;

  if (length == -1)
    return this->receive_payload (mb);

  if (this->payload_.size () < (unsigned long) length)
    result = this->payload_.size (length);

  if (result == -1)
    {
      this->status_ = STATUS_INTERNAL_SERVER_ERROR;
      return -1;
    }

  if (this->payload_.space () >= mb.length ())
    {
      this->payload_.copy (mb.rd_ptr (), mb.length ());
      mb.rd_ptr (mb.wr_ptr ());
      mb.crunch ();
    }
  else
    {
      size_t space = this->payload_.space ();
      this->payload_.copy (mb.rd_ptr (), space);
      mb.rd_ptr (space);
    }

  return this->payload_.length () == (unsigned long) length;
}

const char *
HTTP_Base::payload (void)
{
  return this->payload_.rd_ptr ();
}

unsigned long
HTTP_Base::payload_size (void)
{
  return this->payload_.length ();
}

int
HTTP_Base::build_headers (JAWS_Headers *new_headers)
{
  JAWS_Header_Data *data = 0;
  JAWS_Header_Data *data2 = 0;
  JAWS_Header_Table_Iterator iter (*new_headers);

  iter.first ();
  while (! iter.done ())
    {
      data = iter.next ();
      if (data == 0)
        {
          iter.advance ();
          continue;
        }

      if (data->header_type () == HTTP_HCodes::REPLACE_HEADER)
        this->headers ()->remove_all (data->header_name ());
      else if (data->header_type () == HTTP_HCodes::INSERT_HEADER
               || data->header_type () == HTTP_HCodes::APPENDTO_HEADER)
        {
          data2 = this->headers ()->find (data->header_name ());
          if (data2 != 0)
            {
              if (data->header_type () == HTTP_HCodes::APPENDTO_HEADER)
                {
                  // Append to existing header
                  size_t len
                    = ACE_OS::strlen (data->header_value ())
                    + ACE_OS::strlen (data2->header_value ())
                    + 3; /* for comma, space, and nul */
                  char *buf = new char [len];
                  if (buf == 0)
                    {
                      this->status_ = STATUS_INTERNAL_SERVER_ERROR;
                      return -1;
                    }
                  ACE_OS::sprintf (buf, "%s, %s",
                                   data2->header_value (),
                                   data->header_value ());
                  data2->header_value (buf);
                  delete [] buf;
                }

              // Only insert if it isn't already present
              iter.advance ();
              continue;
            }
        }

      data2 = new JAWS_Header_Data (data->header_name (),
                                    data->header_value ());
      if (data2 == 0 || data2->header_name () == 0
          || data2->header_value () == 0)
        {
          this->status_ = STATUS_INTERNAL_SERVER_ERROR;
          return -1;
        }
      this->headers ()->insert (data2);

      iter.advance ();
    }

  return 0;
}

int
HTTP_Base::deliver_header_name (JAWS_Header_Data *&data)
{
  data = 0;

  for (;;)
    {
      if ((data = this->iter_.next ()) == 0)
        {
          // No more headers, deliver final "\r\n"
          this->mb_->copy ("\r\n", 2);
          return 2;
        }

      if (data->header_name ())
        break;

      this->iter_.advance ();
    }

  // Assume the following lines will always succeed.
  this->mb_->copy (data->header_name ());
  this->mb_->wr_ptr (this->mb_->wr_ptr () - 1);
  this->mb_->copy (": ", 2);

  return 1;
}

int
HTTP_Base::deliver_header_value (JAWS_Header_Data *&data)
{
  // Assume the following line will always succeed.
  if (data->header_value ())
    {
      this->mb_->copy (data->header_value ());
      this->mb_->wr_ptr (this->mb_->wr_ptr () - 1);
    }
  this->mb_->copy ("\r\n", 2);

  this->iter_.advance ();
  return 0;
}


int
HTTP_Base::extract_line (ACE_Message_Block &mb)
{
  JAWS_Parse_Headers *parser = JAWS_Parse_Headers_Singleton::instance ();
  char *p = parser->skipset ("\n", mb.rd_ptr (), mb.wr_ptr ());
  if (p == mb.wr_ptr ())
    return 0;

  this->status_ = STATUS_OK;

  *p = '\0';
  if (p[-1] == '\r')
    p[-1] = '\0';

  this->line_ = ACE_OS::strdup (mb.rd_ptr ());
  if (this->line_ == 0)
    this->status_ = STATUS_INTERNAL_SERVER_ERROR;

  mb.rd_ptr (p+1);
  this->info_.end_of_line (1);
  return 1;
}

void
HTTP_Base::dump (void)
{
  ACE_DEBUG ((LM_DEBUG, "%s\n", this->line ()));
  this->info_.dump ();
  ACE_DEBUG ((LM_DEBUG, "STATUS IS %d %s\n",
              this->status (),
              (*HTTP_SCode::instance ())[this->status ()]));
}

#if !defined (ACE_HAS_INLINED_OSCALLS)
#   include "HTTPU/http_base.i"
# endif /* ACE_HAS_INLINED_OSCALLS */