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
|
// $Id: HTTP_Handler.cpp 91670 2010-09-08 18:02:26Z johnnyw $
// HTTP_Service.cpp -- simple implementation of the HTTP protocol
#include "ace/Message_Block.h"
#include "ace/Filecache.h"
#include "HTTP_Handler.h"
#include "HTTP_Helpers.h"
#include "JAWS_IO.h"
#include "ace/OS_NS_sys_socket.h"
#include "ace/OS_NS_stdio.h"
HTTP_Handler::HTTP_Handler (JAWS_IO &io,
HTTP_Handler_Factory &factory)
: factory_ (factory),
request_data_ (0),
handle_ (ACE_INVALID_HANDLE),
response_ (io, request_),
io_ (io)
{
this->io_.handler (this);
}
HTTP_Handler::~HTTP_Handler (void)
{
this->request_data_->release ();
this->request_data_ = 0;
}
void
HTTP_Handler::open (ACE_HANDLE handle,
ACE_Message_Block &initial_data)
{
ACE_DEBUG ((LM_DEBUG, "(%t) New connection\n"));
int sockbufsize = HTTP_Handler::MAX_SOCKBUFSIZE;
int result = ACE_OS::setsockopt (handle,
SOL_SOCKET,
SO_RCVBUF,
(char *) &sockbufsize,
sizeof sockbufsize);
if (result == -1)
ACE_ERROR ((LM_ERROR, "%p\n", "SO_RCVBUF"));
sockbufsize = HTTP_Handler::MAX_SOCKBUFSIZE;
result = ACE_OS::setsockopt (handle,
SOL_SOCKET,
SO_SNDBUF,
(char *) &sockbufsize,
sizeof sockbufsize);
if (result == -1)
ACE_ERROR ((LM_ERROR, "%p\n", "SO_SNDBUF"));
this->handle_ = handle;
this->io_.handle (this->handle_);
this->request_data_ = initial_data.duplicate ();
this->read_complete (initial_data);
}
void
HTTP_Handler::read_complete (ACE_Message_Block &message_block)
{
// This is actually a callback entry point. The JAWS_IO framework
// calls into this method after some data has been read in.
switch (this->request_.parse_request (message_block))
{
case 0:
do
{
int next_read_size
= HTTP_Handler::MAX_REQUEST_SIZE - message_block.length ();
if (next_read_size == 0)
{
this->request_too_long ();
return;
}
this->io_.read (message_block, next_read_size);
}
while (0);
break;
default:
// this->request_.respond ();
this->response_.process_request ();
}
}
void
HTTP_Handler::receive_file_complete (void)
{
ACE_DEBUG ((LM_DEBUG, " (%t) %s received successfully\n",
request_.uri ()));
char buffer[BUFSIZ];
int buflen =
ACE_OS::sprintf (buffer,
"%s %d %s\r\n",
this->request_.version (),
HTTP_Status_Code::STATUS_OK,
"Successful");
this->io_.send_confirmation_message (buffer, buflen);
}
void
HTTP_Handler::receive_file_error (int result)
{
ACE_DEBUG ((LM_DEBUG, " (%t) %s error in receiving file\n",
request_.uri ()));
char buffer[BUFSIZ];
int status_code;
switch (result)
{
case ACE_Filecache_Handle::ACE_ACCESS_FAILED:
case ACE_Filecache_Handle::ACE_WRITE_FAILED:
case ACE_Filecache_Handle::ACE_OPEN_FAILED:
status_code = HTTP_Status_Code::STATUS_NOT_FOUND;
break;
case ACE_Filecache_Handle::ACE_COPY_FAILED:
case ACE_Filecache_Handle::ACE_STAT_FAILED:
case ACE_Filecache_Handle::ACE_MEMMAP_FAILED:
status_code = HTTP_Status_Code::STATUS_FORBIDDEN;
break;
default:
status_code = HTTP_Status_Code::STATUS_INTERNAL_SERVER_ERROR;
break;
}
int buflen =
ACE_OS::sprintf (buffer,
"%s %d %s",
this->request_.version (),
status_code,
"Failed");
this->io_.send_confirmation_message (buffer, buflen);
}
void
HTTP_Handler::confirmation_message_complete (void)
{
this->done ();
}
void
HTTP_Handler::error_message_complete (void)
{
this->done ();
}
void
HTTP_Handler::transmit_file_complete (void)
{
ACE_DEBUG ((LM_DEBUG, " (%t) %s transmitted successfully\n",
request_.uri ()));
this->done ();
}
void
HTTP_Handler::transmit_file_error (int result)
{
ACE_DEBUG ((LM_DEBUG,
" (%t) %s error in transmitting file\n",
request_.uri ()));
int status_code;
switch (result)
{
case ACE_Filecache_Handle::ACE_ACCESS_FAILED:
case ACE_Filecache_Handle::ACE_WRITE_FAILED:
case ACE_Filecache_Handle::ACE_OPEN_FAILED:
status_code = HTTP_Status_Code::STATUS_NOT_FOUND;
break;
case ACE_Filecache_Handle::ACE_COPY_FAILED:
case ACE_Filecache_Handle::ACE_STAT_FAILED:
case ACE_Filecache_Handle::ACE_MEMMAP_FAILED:
status_code = HTTP_Status_Code::STATUS_FORBIDDEN;
break;
default:
status_code = HTTP_Status_Code::STATUS_INTERNAL_SERVER_ERROR;
break;
}
this->response_.error_response (status_code, "error in transmitting file");
}
void
HTTP_Handler::read_error (void)
{
ACE_DEBUG ((LM_DEBUG, " (%t) error in reading request\n"));
this->done ();
}
void
HTTP_Handler::write_error (void)
{
ACE_DEBUG ((LM_DEBUG, " (%t) %s error in writing response\n",
request_.uri ()));
this->done ();
}
void
HTTP_Handler::timeout (void)
{
ACE_DEBUG ((LM_DEBUG, " (%t) %s error in reading request\n",
request_.uri ()));
this->response_.
error_response (HTTP_Status_Code::STATUS_INTERNAL_SERVER_ERROR,
"error in reading request");
}
void
HTTP_Handler::request_too_long (void)
{
ACE_DEBUG ((LM_DEBUG, " (%t) request too long\n"));
this->response_.
error_response (HTTP_Status_Code::STATUS_BAD_REQUEST,
"request too long");
}
void
HTTP_Handler::done (void)
{
this->factory_.destroy_http_handler (*this, this->io_);
}
HTTP_Handler_Factory::~HTTP_Handler_Factory (void)
{
}
HTTP_Handler *
Synch_HTTP_Handler_Factory::create_http_handler (void)
{
JAWS_Synch_IO *io;
ACE_NEW_RETURN (io, JAWS_Synch_IO, 0);
HTTP_Handler *handler;
ACE_NEW_RETURN (handler, HTTP_Handler (*io, *this), 0);
return handler;
}
void
Synch_HTTP_Handler_Factory::destroy_http_handler (HTTP_Handler &handler,
JAWS_IO &io)
{
delete &io;
delete &handler;
}
//-------------SYNCH IO no Cache
HTTP_Handler *
No_Cache_Synch_HTTP_Handler_Factory::create_http_handler (void)
{
JAWS_Synch_IO_No_Cache *io = 0;
ACE_NEW_RETURN (io, JAWS_Synch_IO_No_Cache, 0);
HTTP_Handler *handler = 0;
ACE_NEW_RETURN (handler, HTTP_Handler (*io, *this), 0);
return handler;
}
void
No_Cache_Synch_HTTP_Handler_Factory::destroy_http_handler (HTTP_Handler &handler,
JAWS_IO &io)
{
delete &io;
delete &handler;
}
//----------------
// This only works on Win32
#if defined (ACE_HAS_WIN32_OVERLAPPED_IO)
void
Asynch_HTTP_Handler_Factory::open (ACE_HANDLE handle,
ACE_Message_Block &mb)
{
JAWS_Asynch_IO *io;
ACE_NEW (io, JAWS_Asynch_IO);
HTTP_Handler *handler;
ACE_NEW (handler, HTTP_Handler (*io, *this));
handler->open (handle, mb);
}
void
Asynch_HTTP_Handler_Factory::destroy_http_handler (HTTP_Handler &handler,
JAWS_IO &io)
{
delete &handler;
delete &io;
delete this;
}
HTTP_Handler *
Asynch_HTTP_Handler_Factory::create_http_handler (void)
{
return 0;
}
#endif /* ACE_HAS_WIN32_OVERLAPPED_IO */
|