summaryrefslogtreecommitdiff
path: root/apps/JAWS/server/HTTP_Handler.h
blob: 65685909428ab4ab945ba457cee40aed5df1b34a (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
/* -*- c++ -*- */

//=============================================================================
/**
 *  @file    HTTP_Handler.h
 *
 *  $Id$
 *
 *  @author James Hu and Irfan Pyarali
 */
//=============================================================================


#ifndef HTTP_HANDLER_H
#define HTTP_HANDLER_H

// = Forward declarations
class Message_Block;
class HTTP_Handler_Factory;

#include "ace/Asynch_IO.h"

#if !defined (ACE_LACKS_PRAGMA_ONCE)
# pragma once
#endif /* ACE_LACKS_PRAGMA_ONCE */

#include "HTTP_Request.h"
#include "HTTP_Response.h"
#include "JAWS_IO.h"

/**
 * @class HTTP_Handler
 *
 * @brief This class is used to implement the HTTP protocol
 *
 * The HTTP_Handler class is a state based implementation of the
 * HTTP protocol. Therefore, it can be used synchronously and
 * asynchronously. It uses an abstract IO class to move between
 * different HTTP protocol states. It is up to the IO class to
 * decide on synchronous or asynchronous I/O.
 */
class HTTP_Handler : protected JAWS_IO_Handler
{
  // Friend I/O classes. Can call protected methods.
  friend class JAWS_Synch_IO;
  friend class JAWS_Synch_IO_No_Cache;
  friend class JAWS_Asynch_IO;

  // Factories
  friend class Asynch_HTTP_Handler_Factory;
  friend class Synch_HTTP_Handler_Factory;
  friend class No_Cache_Synch_HTTP_Handler_Factory;

public:
  /**
   * The handler is initialized with a connection <handle> of a new
   * client and any <initial_data> that came across. The
   * <initial_data> block will be of MAX_REQUEST_SIZE and the number
   * of bytes in <initial_data> can be found from
   * <initial_data>.length ()
   */
  virtual void open (ACE_HANDLE handle,
                     ACE_Message_Block &initial_data);

protected:
  /// The constructor is passed the factory that created <this> and the
  /// IO mechanism that the handler should use.
  HTTP_Handler (JAWS_IO &io,
                HTTP_Handler_Factory &factory);

  /// Destructor
  virtual ~HTTP_Handler (void);

  /// This method is called by the framework when there is a timeout.
  virtual void timeout (void);

  /**
   * This is the termination state of the handler. After successful or
   * unsuccessful completions, the handler will end up in this state
   * (method).
   */
  virtual void done (void);

  /// Request too long.
  virtual void request_too_long (void);

  /// Reference to the creating factory.
  HTTP_Handler_Factory &factory_;

protected:
  // = Completion methods inherited from <JAWS_IO_Handler>.

  virtual void read_complete (ACE_Message_Block &data);
  virtual void read_error (void);
  virtual void transmit_file_complete (void);
  virtual void transmit_file_error (int result);
  virtual void receive_file_complete (void);
  virtual void receive_file_error (int result);
  virtual void write_error (void);
  virtual void confirmation_message_complete (void);
  virtual void error_message_complete (void);

public:
  enum
  {
    MAX_SOCKBUFSIZE = 64 * 1024,
    MAX_REQUEST_SIZE = 8192,
    METHODSIZ = 10,
    VERSIONSIZ = 10
  };

private:
  /// This points to the request sent by the client
  ACE_Message_Block *request_data_;

  /// I/O handle to the client
  ACE_HANDLE handle_;

  HTTP_Request request_;
  HTTP_Response response_;

  /// IO class used by the handler
  JAWS_IO &io_;
};

/**
 * @class HTTP_Handler_Factory
 *
 * @brief This class is used to create new HTTP handlers
 *
 * This is an abstract factory for creating new HTTP handlers.
 */
class HTTP_Handler_Factory
{
public:
  /// Destructor
  virtual ~HTTP_Handler_Factory (void);

  /// This creates a new HTTP_Handler
  virtual HTTP_Handler *create_http_handler (void) = 0;

  /**
   * The HTTP handler will call this method from HTTP_Handler::done to
   * tell the factory to reap up the handler as it is now done with
   * the protocol
   */
  virtual void destroy_http_handler (HTTP_Handler &handler,
                                     JAWS_IO &io) = 0;
};

/**
 * @class Synch_HTTP_Handler_Factory
 *
 * @brief This class is used to create new HTTP handlers that will use
 * Synch IO
 *
 */
class Synch_HTTP_Handler_Factory : public HTTP_Handler_Factory
{
public:
  /// This creates a new HTTP_Handler
  HTTP_Handler *create_http_handler (void);

  /**
   * The HTTP handler will call this method from HTTP_Handler::done to
   * tell the factory to reap up the handler as it is now done with
   * the protocol
   */
  void destroy_http_handler (HTTP_Handler &handler,
                             JAWS_IO &io);
};

//--------------Added a factory for SYNCH IO without caching

/**
 * @class No_Cache_Synch_HTTP_Handler_Factory
 *
 * @brief This class is used to create new HTTP handlers that will use
 * Synch IO without caching
 *
 */
class No_Cache_Synch_HTTP_Handler_Factory : public HTTP_Handler_Factory
{
public:
  /// This creates a new HTTP_Handler
  HTTP_Handler *create_http_handler (void);

  /**
   * The HTTP handler will call this method from HTTP_Handler::done to
   * tell the factory to reap up the handler as it is now done with
   * the protocol
   */
  void destroy_http_handler (HTTP_Handler &handler,
                             JAWS_IO &io);
};

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

#if defined (ACE_HAS_WIN32_OVERLAPPED_IO)
/**
 * @class Asynch_HTTP_Handler_Factory
 *
 * @brief This class is used to create new HTTP handlers that will use
 * Asynchronous IO.  This only works on Win32.
 *
 */
class Asynch_HTTP_Handler_Factory : public HTTP_Handler_Factory, public ACE_Service_Handler
{
public:
  /**
   * The HTTP handler will call this method from HTTP_Handler::done to
   * tell the factory to reap up the handler as it is now done with
   * the protocol
   */
  void destroy_http_handler (HTTP_Handler &handler,
                             JAWS_IO &io);

  /**
   * <open> is called by <ACE_Asynch_Acceptor> to initialize a new
   * instance of ACE_Service_Handler that has been created after the a
   * new connection is accepted.
   *
   * This will act as a creation point for new handlers.
   */
  virtual void open (ACE_HANDLE handle,
                     ACE_Message_Block &message_block);

private:
  /**
   * This method is private as users are not allowed to create new
   * handlers. New handlers can only be created by the framework when
   * new client connections arrive.
   */
  HTTP_Handler *create_http_handler (void);
};
#endif /* ACE_HAS_WIN32_OVERLAPPED_IO */
#endif /* HTTP_HANDLER_H */