1 // HTTP_Service.cpp -- simple implementation of the HTTP protocol
3 #include "ace/Message_Block.h"
4 #include "ace/Filecache.h"
6 #include "HTTP_Handler.h"
7 #include "HTTP_Helpers.h"
9 #include "ace/OS_NS_sys_socket.h"
10 #include "ace/OS_NS_stdio.h"
12 HTTP_Handler::HTTP_Handler (JAWS_IO
&io
,
13 HTTP_Handler_Factory
&factory
)
16 handle_ (ACE_INVALID_HANDLE
),
17 response_ (io
, request_
),
20 this->io_
.handler (this);
23 HTTP_Handler::~HTTP_Handler ()
25 this->request_data_
->release ();
26 this->request_data_
= 0;
30 HTTP_Handler::open (ACE_HANDLE handle
,
31 ACE_Message_Block
&initial_data
)
33 ACE_DEBUG ((LM_DEBUG
, "(%t) New connection\n"));
35 int sockbufsize
= HTTP_Handler::MAX_SOCKBUFSIZE
;
36 int result
= ACE_OS::setsockopt (handle
,
39 (char *) &sockbufsize
,
43 ACE_ERROR ((LM_ERROR
, "%p\n", "SO_RCVBUF"));
45 sockbufsize
= HTTP_Handler::MAX_SOCKBUFSIZE
;
47 result
= ACE_OS::setsockopt (handle
,
50 (char *) &sockbufsize
,
53 ACE_ERROR ((LM_ERROR
, "%p\n", "SO_SNDBUF"));
55 this->handle_
= handle
;
56 this->io_
.handle (this->handle_
);
58 this->request_data_
= initial_data
.duplicate ();
59 this->read_complete (initial_data
);
63 HTTP_Handler::read_complete (ACE_Message_Block
&message_block
)
65 // This is actually a callback entry point. The JAWS_IO framework
66 // calls into this method after some data has been read in.
68 switch (this->request_
.parse_request (message_block
))
74 = HTTP_Handler::MAX_REQUEST_SIZE
- message_block
.length ();
76 if (next_read_size
== 0)
78 this->request_too_long ();
82 this->io_
.read (message_block
, next_read_size
);
88 // this->request_.respond ();
89 this->response_
.process_request ();
94 HTTP_Handler::receive_file_complete ()
96 ACE_DEBUG ((LM_DEBUG
, " (%t) %s received successfully\n",
101 ACE_OS::sprintf (buffer
,
103 this->request_
.version (),
104 HTTP_Status_Code::STATUS_OK
,
107 this->io_
.send_confirmation_message (buffer
, buflen
);
111 HTTP_Handler::receive_file_error (int result
)
113 ACE_DEBUG ((LM_DEBUG
, " (%t) %s error in receiving file\n",
121 case ACE_Filecache_Handle::ACE_ACCESS_FAILED
:
122 case ACE_Filecache_Handle::ACE_WRITE_FAILED
:
123 case ACE_Filecache_Handle::ACE_OPEN_FAILED
:
124 status_code
= HTTP_Status_Code::STATUS_NOT_FOUND
;
126 case ACE_Filecache_Handle::ACE_COPY_FAILED
:
127 case ACE_Filecache_Handle::ACE_STAT_FAILED
:
128 case ACE_Filecache_Handle::ACE_MEMMAP_FAILED
:
129 status_code
= HTTP_Status_Code::STATUS_FORBIDDEN
;
132 status_code
= HTTP_Status_Code::STATUS_INTERNAL_SERVER_ERROR
;
136 ACE_OS::sprintf (buffer
,
138 this->request_
.version (),
142 this->io_
.send_confirmation_message (buffer
, buflen
);
146 HTTP_Handler::confirmation_message_complete ()
152 HTTP_Handler::error_message_complete ()
158 HTTP_Handler::transmit_file_complete ()
160 ACE_DEBUG ((LM_DEBUG
, " (%t) %s transmitted successfully\n",
167 HTTP_Handler::transmit_file_error (int result
)
169 ACE_DEBUG ((LM_DEBUG
,
170 " (%t) %s error in transmitting file\n",
177 case ACE_Filecache_Handle::ACE_ACCESS_FAILED
:
178 case ACE_Filecache_Handle::ACE_WRITE_FAILED
:
179 case ACE_Filecache_Handle::ACE_OPEN_FAILED
:
180 status_code
= HTTP_Status_Code::STATUS_NOT_FOUND
;
182 case ACE_Filecache_Handle::ACE_COPY_FAILED
:
183 case ACE_Filecache_Handle::ACE_STAT_FAILED
:
184 case ACE_Filecache_Handle::ACE_MEMMAP_FAILED
:
185 status_code
= HTTP_Status_Code::STATUS_FORBIDDEN
;
188 status_code
= HTTP_Status_Code::STATUS_INTERNAL_SERVER_ERROR
;
192 this->response_
.error_response (status_code
, "error in transmitting file");
196 HTTP_Handler::read_error ()
198 ACE_DEBUG ((LM_DEBUG
, " (%t) error in reading request\n"));
203 HTTP_Handler::write_error ()
205 ACE_DEBUG ((LM_DEBUG
, " (%t) %s error in writing response\n",
212 HTTP_Handler::timeout ()
214 ACE_DEBUG ((LM_DEBUG
, " (%t) %s error in reading request\n",
218 error_response (HTTP_Status_Code::STATUS_INTERNAL_SERVER_ERROR
,
219 "error in reading request");
223 HTTP_Handler::request_too_long ()
225 ACE_DEBUG ((LM_DEBUG
, " (%t) request too long\n"));
227 error_response (HTTP_Status_Code::STATUS_BAD_REQUEST
,
232 HTTP_Handler::done ()
234 this->factory_
.destroy_http_handler (*this, this->io_
);
237 HTTP_Handler_Factory::~HTTP_Handler_Factory ()
242 Synch_HTTP_Handler_Factory::create_http_handler ()
245 ACE_NEW_RETURN (io
, JAWS_Synch_IO
, 0);
246 HTTP_Handler
*handler
;
247 ACE_NEW_RETURN (handler
, HTTP_Handler (*io
, *this), 0);
253 Synch_HTTP_Handler_Factory::destroy_http_handler (HTTP_Handler
&handler
,
260 //-------------SYNCH IO no Cache
263 No_Cache_Synch_HTTP_Handler_Factory::create_http_handler ()
265 JAWS_Synch_IO_No_Cache
*io
= 0;
266 ACE_NEW_RETURN (io
, JAWS_Synch_IO_No_Cache
, 0);
267 HTTP_Handler
*handler
= 0;
268 ACE_NEW_RETURN (handler
, HTTP_Handler (*io
, *this), 0);
274 No_Cache_Synch_HTTP_Handler_Factory::destroy_http_handler (HTTP_Handler
&handler
,
283 // This only works on Win32
284 #if defined (ACE_HAS_WIN32_OVERLAPPED_IO)
286 Asynch_HTTP_Handler_Factory::open (ACE_HANDLE handle
,
287 ACE_Message_Block
&mb
)
290 ACE_NEW (io
, JAWS_Asynch_IO
);
291 HTTP_Handler
*handler
;
292 ACE_NEW (handler
, HTTP_Handler (*io
, *this));
293 handler
->open (handle
, mb
);
297 Asynch_HTTP_Handler_Factory::destroy_http_handler (HTTP_Handler
&handler
,
306 Asynch_HTTP_Handler_Factory::create_http_handler ()
310 #endif /* ACE_HAS_WIN32_OVERLAPPED_IO */