summaryrefslogtreecommitdiff
path: root/apps/JAWS2/HTTPU/parse_http_request.cpp
blob: 6d897ff2dacd21906544f24f9a4bcbc33c652780 (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
// $Id$

#include "HTTPU/parse_http_request.h"

Parse_HTTP_Request::Parse_HTTP_Request (const char *request)
  : method_ (0),
    major_version_ (-1),
    minor_version_ (-1),
    version_ (0),
    url_ (0),
    request_ (0),
    error_ (0)
{
  if (request != 0)
    this->init (request);
}

Parse_HTTP_Request::~Parse_HTTP_Request (void)
{
  if (this->request_)
    ACE_OS::free (this->request_);
  this->request_ = 0;
  this->version_ = 0;
  this->url_ = 0;
}

void
Parse_HTTP_Request::dump (void)
{
  ACE_DEBUG ((LM_DEBUG, "%s %s %s\n",
              this->method_str (), this->url (), this->version ()));
}

void
Parse_HTTP_Request::init (const char *request)
{
  char *method;

  this->request_ = ACE_OS::strdup (request);
  if (this->request_ == 0)
    {
      this->error_ = NO_MEMORY;
      return;
    }

  char buf[BUFSIZ];
  int n = ::sscanf (this->request_, "%s %*s HTTP/%d.%d",
                    buf,
                    &(this->major_version_),
                    &(this->minor_version_));

  if (n == 1 || n == 3)
    {
      char *p = this->request_;

      while (*p == ' ' || *p == '\t')
        p++;

      method = p++;

      while (*p != ' ' && *p != '\t')
        p++;

      *p++ = '\0';

      while (*p == ' ' || *p == '\t')
        p++;

      this->url_ = p;

      while (*p && !ACE_OS::strchr (" \t\r\n", *p))
        p++;

      *p++ = '\0';

      if (n == 1)
        {
          this->major_version_ = 0;
          this->minor_version_ = 9;
        }
      else
        {
          while (*p == ' ' || *p == '\t')
            p++;

          this->version_ = p;

          while (*p && !ACE_OS::strchr (" \t\r\n", *p))
            p++;

          *p++ = '\0';
        }

      if (ACE_OS::strcmp (method, "GET") == 0)
        this->method_ = &GET;
      else if (ACE_OS::strcmp (method, "HEAD") == 0)
        this->method_ = &HEAD;
      else if (ACE_OS::strcmp (method, "POST") == 0)
        this->method_ = &POST;
      else if (ACE_OS::strcmp (method, "PUT") == 0)
        this->method_ = &PUT;
      else if (ACE_OS::strcmp (method, "QUIT") == 0)
        this->method_ = &QUIT;
      else
        {
          this->method_ = &DUNNO;
          this->error_ = NOT_IMPLEMENTED;
        }
    }
  else
    this->error_ = BAD_REQUEST;
}

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