summaryrefslogtreecommitdiff
path: root/apps/JAWS/HTTP_Handler.cpp
blob: ac671a7b73e6e3b0d35c0897b724bcfb28f32bc4 (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
// HTTP_Service.cpp -- simple implementation of the HTTP protocol

#include "HTTP_Handler.h"

HTTP_Handler::HTTP_Handler(void)
{
  sockbufp_ = sockbuf_;
  sockbufn_ = 0;
}

int
HTTP_Handler::open (void *)
{
  this->svc();
  this->close();
  return 0;
}

int
HTTP_Handler::svc (void)
{
  int result;
  int serve_result = 1;
  const int OK = HTTP_Status_Code::STATUS_OK;
  HTTP_VFS_Node *handle;

  if ((result = this->parse_request()) == OK
      && (result = HTTP_VFS::open(this->requestURI_, handle)) == OK) {
    serve_result = this->serve_file(handle);
    this->peer().close();
    HTTP_VFS::close(handle);
  }
  else if (result == -1) {
    this->peer().close();
    return -1;
  }
  else {
    serve_result = this->serve_error(result);
    this->peer().close();
  }

  return serve_result;
}

int
HTTP_Handler::parse_request (void)
{  
  // read in the HTTP request
  // --
  // What is really needed here is a real tokenizer and parser.
  // (1) It would allow someone to more easily plug in protocol revisions.
  // (2) Code would be easier to understand and maintain.

  int found = 0;
  int first = 1;

  method_[0] = '\0';
  requestURI_[0] = '\0';
  HTTPversion_[0] = '\0';


  //  ACE_HANDLE handle = this->peer().get_handle();
  //  ACE_Handle_Set handle_set;
  //  handle_set.set_bit(handle);

  //  if (ACE_OS::select(handle+1, handle_set, 0, 0, (ACE_Time_Value *)0) == -1)
  //    return HTTP_Status_Code::STATUS_BAD_REQUEST;

  if (this->sockgets(buf_, sizeof(buf_)) == 0)
    return -1;

  do {

    // Blank line means we are done
    if (ACE_OS::strcmp(buf_, "\r\n") == 0
        || ACE_OS::strcmp(buf_, "\n") == 0) {
      if (first) ACE_DEBUG((LM_DEBUG, "Whups\n"));
      break;
    }

    // if (!found && requestRE_.regex(buf_, path_)) found = 1;
    // ^^
    // Old way of doing it.  Maybe below will be a little faster.

    if (first) {
      char *lasts;
      char *tok;
      if ((tok = ACE_OS::strtok_r(buf_, " \r\n\t", &lasts)) != 0) {
        ACE_OS::strcpy(method_, tok);
        if ((tok = ACE_OS::strtok_r(0, " \r\n\t", &lasts)) != 0) {
          ACE_OS::strcpy(requestURI_, tok);
          if ((tok = ACE_OS::strtok_r(0, " \r\n\t", &lasts)) != 0) {
            ACE_OS::strcpy(HTTPversion_, tok+5);
            found = 1;
          }
          else {
            if (ACE_OS::strcmp(method_, "GET") == 0) {
              ACE_OS::strcpy(HTTPversion_, "0.9");
              found = 2;
            }
          }
        }
      }

      // Request must be the first line of HTTP request
      first = 0;
    }

  } while (this->sockgets(buf_, sizeof(buf_)) != 0);


  if (found == 0) {
    return HTTP_Status_Code::STATUS_BAD_REQUEST;
  }
  if (ACE_OS::strcmp(method_, "GET") != 0)
    return HTTP_Status_Code::STATUS_NOT_IMPLEMENTED;

  fix_path(requestURI_);

  return HTTP_Status_Code::STATUS_OK;
}

int
HTTP_Handler::serve_error (int status_code)
{
  static char const errormessage[] =
    "HTTP/1.0 %d %s\r\n"
    "Content-type: text/html\r\n"
    "\r\n"
    "<html>\n"
    "<head><title>Error message</title></head>\n"
    "<body>\n"
    "<h1>Error %d: %s</h1>"
    "Could not access file: %s.\n"
    "</body>\n"
    "</html>\n"
    ;

  int len = sprintf(this->buf_, errormessage,
                    status_code, HTTP_Status_Code::instance()[status_code],
                    status_code, HTTP_Status_Code::instance()[status_code],
                    requestURI_);

  this->peer().send_n(this->buf_, len);
  ACE_DEBUG((LM_DEBUG, "in HTTP_Handler::serve_error(%d), %s\n",
             status_code, this->path_));
  return 0;
}

int
HTTP_Handler::serve_directory (void)
{
  // We'll just forbid it for now.
  return this->serve_error(403);
}

int
HTTP_Handler::serve_file (HTTP_VFS_Node * &vf)
{
  // Just a simple response for this simple server.
  // --
  // In a real server, detect what kind of request we got,
  // and respond in kind.
  //   simple-request -> simple-response
  //   full-request -> full-response

  this->peer().send_n(vf->File(), vf->Size());
  this->peer().send_n("\r\n", 2);

  return 0;
}

void
HTTP_Handler::fix_path (char *path)
{
  // fix the path if it needs fixing/is fixable

  // replace the percentcodes with the actual character
  int i,j;
  char percentcode[3];
  
  for (i = j = 0; path_[i] != '\0'; i++,j++) {
    if (path_[i] == '%') {
      percentcode[0] = path[++i];
      percentcode[1] = path[++i];
      percentcode[2] = '\0';
      path[j] = strtol(percentcode, (char **)0, 16);
    }
    else path[j] = path[i];
  }
  path[j] = path[i];
}

// ---------------------------------------- //

int
HTTP_Handler::sockgetc(void)
{
  if (sockbufn_ == 0) {
    // buffer is empty
    sockbufn_ = this->peer().recv(sockbuf_, sizeof(sockbuf_));
    sockbufp_ = sockbuf_;
  }
  return (--sockbufn_ >= 0) ? (unsigned char) *sockbufp_++ : -1;
}

int
HTTP_Handler::sockputc(char const c)
{
  //  int n = this->peer().tv_send_n(&c, 1, ACE_Time_Value(5));
  int n = this->peer().send_n(&c, 1);
  return (n == 1) ? c : EOF;
}

int
HTTP_Handler::sockputs(char const *s)
{
  int len = ACE_OS::strlen(s);
  //  int n = this->peer().tv_send_n(s, len, ACE_Time_Value(5));
  int n = this->peer().send_n(s, len);
  return (n >= 0) ? n : EOF;
}

char *
HTTP_Handler::sockgets(char *buf, int n)
{
  int c;
  char *cbuf;

  cbuf = buf;
  while (--n > 0 && (c = this->sockgetc()) != -1) {
    if ((*cbuf++ = c) == '\n')
      break;
  }
  *cbuf = '\0';
  return (c == EOF && cbuf == buf) ? NULL : buf;
}

// ---------------------------------------- //


// This is necessary for gcc to work with templates
#if defined (ACE_TEMPLATES_REQUIRE_SPECIALIZATION)
template class ACE_Acceptor<HTTP_Handler, ACE_SOCK_ACCEPTOR>;
template class Sock_Acceptor_Adapter<HTTP_Handler>;
#endif /* ACE_TEMPLATES_REQUIRE_SPECIALIZATION */