summaryrefslogtreecommitdiff
path: root/apps/JAWS/PROTOTYPE/HTTP_10_Parse.cpp
blob: dcd8e229bb608ee340187c976e7715f0b5ad75af (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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// $Id$

#include "HTTP_10.h"

#include "JAWS/JAWS.h"
#include "JAWS/IO.h"
#include "JAWS/IO_Handler.h"
#include "JAWS/Policy.h"

ACE_RCSID(PROTOTYPE, HTTP_10_Parse, "$Id$")

// --------------- PARSE TASK ----------------------

JAWS_HTTP_10_Parse_Task::JAWS_HTTP_10_Parse_Task (void)
{
}

JAWS_HTTP_10_Parse_Task::~JAWS_HTTP_10_Parse_Task (void)
{
}

int
JAWS_HTTP_10_Parse_Task::handle_put (JAWS_Data_Block *data, ACE_Time_Value *)
{
  JAWS_TRACE ("JAWS_HTTP_10_Parse_Task::handle_put");

  JAWS_IO_Handler *handler = data->io_handler ();
  JAWS_Dispatch_Policy *policy = this->policy ();
  if (policy == 0) policy = data->policy ();
  JAWS_IO *io = policy->io ();

  JAWS_HTTP_10_Request *info;

  if (data->payload ())
    info = ACE_reinterpret_cast (JAWS_HTTP_10_Request *, data->payload ());
  else
    {
      info = new JAWS_HTTP_10_Request;
      if (info == 0)
        {
          ACE_ERROR ((LM_ERROR, "%p\n", "JAWS_HTTP_10_Parse_Task::handle_put"));
          return -1;
        }
      data->payload (ACE_static_cast (void *, info));
    }

  while (this->parse_request (info, data) == 0)
    {
      int next_read_size
        = JAWS_Data_Block::JAWS_DATA_BLOCK_SIZE - data->length ();

      if (next_read_size == 0)
        {
          // Set payload to reflect "request too long"
          break;
        }

      io->read (handler, data, next_read_size);
      switch (handler->status ())
        {
        case JAWS_IO_Handler::READ_OK:
          // Behaved synchronously, reiterate
          continue;
        case JAWS_IO_Handler::READ_ERROR:
          return -1;
        default:
          // This needs to be a value that tells the framework that
          // the call is asynchronous, but that we should remain in
          // the current task state.
          return 2;
        }
    }

  // request completely parsed

  return 0;
}

int
JAWS_HTTP_10_Parse_Task::parse_request (JAWS_HTTP_10_Request *info,
                                        JAWS_Data_Block *data)
{
  JAWS_TRACE ("JAWS_HTTP_10_Parse_Task::parse_request");

  // Algorithm --
  //
  // -- if we have not parsed a request line
  //      scan for a newline
  //        if no newline present, return
  //      parse the request line
  //      set "parsed request line" flag
  //      set "end of line" flag
  //      adjust the read pointer
  //
  // -- parse the header line input
  //    loop:
  //      if no more input break
  //      if "end of line" flag is set and
  //        line does not begin with leading white space
  //          if this char is a newline
  //            set "end of headers" flag
  //            return
  //          get the header type (i.e, up to colon)
  //            if there is no colon
  //              ignore input up to next newline or no more input
  //              adjust the read pointer
  //              goto loop:
  //          set header value to something empty
  //          set last header to point to current header
  //          unset "end of line" flag
  //      if "end of line" flag is not set or
  //        if the line begins with leading white space,
  //          while not a newline character present
  //            if "end of input" end while
  //            otherwise append char to last header value
  //            adjust the read pointer
  //          if a newline, set "end of line" flag
  //          goto loop:
  //
  // -- adjust read and write pointer
  //      if "end of input" occurs but there is still

  ACE_Message_Block &mb = *data;

  if (!info->got_request_line ())
    {
      char *p;
      p = this->skipset ("\n", mb.rd_ptr (), mb.wr_ptr ());
      if (p == mb.wr_ptr ())
        // can't do anything yet
        return 0;

      info->parse_request_line (mb.rd_ptr ());

      mb.rd_ptr (p+1);
      info->end_of_line (1);
    }

  return this->parse_headers (info, mb);
}

int
JAWS_HTTP_10_Parse_Task::parse_headers (JAWS_HTTP_10_Request *info,
                                        ACE_Message_Block &mb)
{
  JAWS_TRACE ("JAWS_HTTP_10_Parse_Task::parse_headers");

  for (;;)
    {
      if (mb.rd_ptr () == mb.wr_ptr ())
          break;

      char *p = mb.rd_ptr ();

      if (info->end_of_line ()
          && (*p != ' ' && *p != '\t'))
        {
          int r = this->parse_header_type (info, mb);
          if (r == 1)
            return info->end_of_headers ();
          continue;
        }
      else
        {
          int r = this->parse_header_value (info, mb);
          if (r == 1)
            break;
          continue;
        }
    }

  if (mb.rd_ptr () != mb.base ())
    {
      size_t len = mb.length ();
      if (len > 0)
        ACE_OS::memmove (mb.base (), mb.rd_ptr (), len);
      mb.rd_ptr (mb.base ());
      mb.wr_ptr (mb.base () + len);
      return 0;
    }
  else if (mb.length () == mb.size ())
    {
      // This is one of those cases that should rarely ever happen.
      // If we get here, it means the initial HTTP request line was
      // over 8K long or that the header type name is over 8K long.
      // In either case, we flag this as a bad request.

      // In HTTP/1.1, I have to remember that a bad request means the
      // connection needs to be closed and the client has to
      // reinitiate the connection.

      info->status (JAWS_HTTP_10_Request::STATUS_BAD_REQUEST);
      return 1;
    }
}

char *
JAWS_HTTP_10_Parse_Task::skipset (const char *set, char *start, char *end)
{
  char *p = start;
  while (p < end)
    {
      if (ACE_OS::strchr (set, *p) != NULL)
        break;
      p++;
    }
  return p;
}

int
JAWS_HTTP_10_Parse_Task::parse_header_type (JAWS_HTTP_10_Request *info,
                                            ACE_Message_Block &mb)
{
  JAWS_TRACE ("JAWS_HTTP_10_Parse_Task::parse_header_type");

  char *p = mb.rd_ptr ();
  char *q;

  q = this->skipset (":\n", p, mb.wr_ptr ());
  if (q == mb.wr_ptr ())
    // no more progress can be made until we find a ':'
    return 1;
  if (q == p)
    {
      // Ignore empty header type names
      info->create_next_header_value (0, 0);
      info->end_of_line (0);
      mb.rd_ptr (q+1);
      return 0;
    }
  if (*q == '\n')
    {
      // ignore this line
      mb.rd_ptr (q+1);
      if (q == p || ((q-1) == p && q[-1] == '\r'))
        {
          // blank line means end of headers
          info->create_next_header_value (0, 0);
          info->end_of_headers (1);
          if (mb.rd_ptr () == mb.wr_ptr ())
            {
              mb.rd_ptr (mb.base ());
              mb.wr_ptr (mb.base ());
            }
          return 1;
        }

      // not a blank line, but no ':', so ignore it
      info->create_next_header_value (0, 0);
      return 0;
    }

  // otherwise, we have a header type name!
  *q = '\0';
  char *header_type = ACE_OS::strdup (p);
  char *header_value = info->header_buf ();

  if (header_type == 0)
    {
      ACE_ERROR ((LM_ERROR,
                  "%p\n",
                  "JAWS_HTTP_10_Parse_Task::parse_request"));
      ACE_OS::free (header_type);
      header_type = 0;
      header_value = 0;
      info->create_next_header_value (header_type, header_value);
    }
  else
    {
      JAWS_TRACE ("JAWS_HTTP_10_Parse_Task::parse_header_type got one!");

      info->create_next_header_value (header_type, header_value);
      header_value[0] = '\0';
      info->table ()->insert (header_type, header_value);
    }

  info->end_of_line (0);

  mb.rd_ptr (q+1);
  return 0;
}

int
JAWS_HTTP_10_Parse_Task::parse_header_value (JAWS_HTTP_10_Request *info,
                                             ACE_Message_Block &mb)
{
  JAWS_TRACE ("JAWS_HTTP_10_Parse_Task::parse_header_value");

  // break --> return 1;
  // continue --> return 0;

  char *q = mb.rd_ptr ();

  if (info->last_header_value () == 0)
    {
      JAWS_TRACE ("JAWS_HTTP_10_Parse_Task::parse_header_value ignore");

      q = this->skipset ("\n", mb.rd_ptr (), mb.wr_ptr ());
      if (q == mb.wr_ptr ())
        {
          info->end_of_line (0);
          mb.rd_ptr (q);
          if (q[-1] == '\r')
            mb.rd_ptr (q-1);
          return 1;
        }
      if (*q == '\n')
        {
          info->end_of_line (1);
          q++;
        }
      mb.rd_ptr (q);
      return 0;
    }
  else
    {
      JAWS_TRACE ("JAWS_HTTP_10_Parse_Task::parse_header_value skip");

      // Skip over leading linear white space
      q = this->skipset (" \t", mb.rd_ptr (), mb.wr_ptr ());
      if (q == mb.wr_ptr ())
        {
          // need more input
          info->end_of_line (0);
          mb.rd_ptr (q);
          return 1;
        }

      // If not the first header value character,
      // insert a single space
      if (info->last_header_length () != 0)
        info->append_last_header_value (' ');

      while (q < mb.wr_ptr ())
        {
          if (*q == '\n')
            break;
          info->append_last_header_value (*q);
          q++;
        }

      if (q == mb.wr_ptr ())
        {
          info->end_of_line (0);
          mb.rd_ptr (q);
          return 1;
        }

      if (*q == '\n')
        {
          if (q[-1] == '\r')
            info->reduce_last_header_value ();
          info->end_of_line (1);
          mb.rd_ptr (q+1);
          return 0;
        }
    }

  // NOT REACHED
  return 1;
}