Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / ACE / apps / JAWS / server / HTTP_Handler.cpp
blobcc4ccfad349874cd986e791ef46bc7cc842653e4
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"
8 #include "JAWS_IO.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)
14 : factory_ (factory),
15 request_data_ (0),
16 handle_ (ACE_INVALID_HANDLE),
17 response_ (io, request_),
18 io_ (io)
20 this->io_.handler (this);
23 HTTP_Handler::~HTTP_Handler ()
25 this->request_data_->release ();
26 this->request_data_ = 0;
29 void
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,
37 SOL_SOCKET,
38 SO_RCVBUF,
39 (char *) &sockbufsize,
40 sizeof sockbufsize);
42 if (result == -1)
43 ACE_ERROR ((LM_ERROR, "%p\n", "SO_RCVBUF"));
45 sockbufsize = HTTP_Handler::MAX_SOCKBUFSIZE;
47 result = ACE_OS::setsockopt (handle,
48 SOL_SOCKET,
49 SO_SNDBUF,
50 (char *) &sockbufsize,
51 sizeof sockbufsize);
52 if (result == -1)
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);
62 void
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))
70 case 0:
73 int next_read_size
74 = HTTP_Handler::MAX_REQUEST_SIZE - message_block.length ();
76 if (next_read_size == 0)
78 this->request_too_long ();
79 return;
82 this->io_.read (message_block, next_read_size);
84 while (0);
85 break;
87 default:
88 // this->request_.respond ();
89 this->response_.process_request ();
93 void
94 HTTP_Handler::receive_file_complete ()
96 ACE_DEBUG ((LM_DEBUG, " (%t) %s received successfully\n",
97 request_.uri ()));
99 char buffer[BUFSIZ];
100 int buflen =
101 ACE_OS::sprintf (buffer,
102 "%s %d %s\r\n",
103 this->request_.version (),
104 HTTP_Status_Code::STATUS_OK,
105 "Successful");
107 this->io_.send_confirmation_message (buffer, buflen);
110 void
111 HTTP_Handler::receive_file_error (int result)
113 ACE_DEBUG ((LM_DEBUG, " (%t) %s error in receiving file\n",
114 request_.uri ()));
116 char buffer[BUFSIZ];
118 int status_code;
119 switch (result)
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;
125 break;
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;
130 break;
131 default:
132 status_code = HTTP_Status_Code::STATUS_INTERNAL_SERVER_ERROR;
133 break;
135 int buflen =
136 ACE_OS::sprintf (buffer,
137 "%s %d %s",
138 this->request_.version (),
139 status_code,
140 "Failed");
142 this->io_.send_confirmation_message (buffer, buflen);
145 void
146 HTTP_Handler::confirmation_message_complete ()
148 this->done ();
151 void
152 HTTP_Handler::error_message_complete ()
154 this->done ();
157 void
158 HTTP_Handler::transmit_file_complete ()
160 ACE_DEBUG ((LM_DEBUG, " (%t) %s transmitted successfully\n",
161 request_.uri ()));
163 this->done ();
166 void
167 HTTP_Handler::transmit_file_error (int result)
169 ACE_DEBUG ((LM_DEBUG,
170 " (%t) %s error in transmitting file\n",
171 request_.uri ()));
173 int status_code;
175 switch (result)
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;
181 break;
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;
186 break;
187 default:
188 status_code = HTTP_Status_Code::STATUS_INTERNAL_SERVER_ERROR;
189 break;
192 this->response_.error_response (status_code, "error in transmitting file");
195 void
196 HTTP_Handler::read_error ()
198 ACE_DEBUG ((LM_DEBUG, " (%t) error in reading request\n"));
199 this->done ();
202 void
203 HTTP_Handler::write_error ()
205 ACE_DEBUG ((LM_DEBUG, " (%t) %s error in writing response\n",
206 request_.uri ()));
208 this->done ();
211 void
212 HTTP_Handler::timeout ()
214 ACE_DEBUG ((LM_DEBUG, " (%t) %s error in reading request\n",
215 request_.uri ()));
217 this->response_.
218 error_response (HTTP_Status_Code::STATUS_INTERNAL_SERVER_ERROR,
219 "error in reading request");
222 void
223 HTTP_Handler::request_too_long ()
225 ACE_DEBUG ((LM_DEBUG, " (%t) request too long\n"));
226 this->response_.
227 error_response (HTTP_Status_Code::STATUS_BAD_REQUEST,
228 "request too long");
231 void
232 HTTP_Handler::done ()
234 this->factory_.destroy_http_handler (*this, this->io_);
237 HTTP_Handler_Factory::~HTTP_Handler_Factory ()
241 HTTP_Handler *
242 Synch_HTTP_Handler_Factory::create_http_handler ()
244 JAWS_Synch_IO *io;
245 ACE_NEW_RETURN (io, JAWS_Synch_IO, 0);
246 HTTP_Handler *handler;
247 ACE_NEW_RETURN (handler, HTTP_Handler (*io, *this), 0);
249 return handler;
252 void
253 Synch_HTTP_Handler_Factory::destroy_http_handler (HTTP_Handler &handler,
254 JAWS_IO &io)
256 delete &io;
257 delete &handler;
260 //-------------SYNCH IO no Cache
262 HTTP_Handler *
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);
270 return handler;
273 void
274 No_Cache_Synch_HTTP_Handler_Factory::destroy_http_handler (HTTP_Handler &handler,
275 JAWS_IO &io)
277 delete &io;
278 delete &handler;
281 //----------------
283 // This only works on Win32
284 #if defined (ACE_HAS_WIN32_OVERLAPPED_IO)
285 void
286 Asynch_HTTP_Handler_Factory::open (ACE_HANDLE handle,
287 ACE_Message_Block &mb)
289 JAWS_Asynch_IO *io;
290 ACE_NEW (io, JAWS_Asynch_IO);
291 HTTP_Handler *handler;
292 ACE_NEW (handler, HTTP_Handler (*io, *this));
293 handler->open (handle, mb);
296 void
297 Asynch_HTTP_Handler_Factory::destroy_http_handler (HTTP_Handler &handler,
298 JAWS_IO &io)
300 delete &handler;
301 delete &io;
302 delete this;
305 HTTP_Handler *
306 Asynch_HTTP_Handler_Factory::create_http_handler ()
308 return 0;
310 #endif /* ACE_HAS_WIN32_OVERLAPPED_IO */