Branch data Line data Source code
1 : : /*
2 : : * Copyright 2006-2009 Savarese Software Research Corporation
3 : : *
4 : : * Licensed under the Apache License, Version 2.0 (the "License");
5 : : * you may not use this file except in compliance with the License.
6 : : * You may obtain a copy of the License at
7 : : *
8 : : * https://www.savarese.com/software/ApacheLicense-2.0
9 : : *
10 : : * Unless required by applicable law or agreed to in writing, software
11 : : * distributed under the License is distributed on an "AS IS" BASIS,
12 : : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 : : * See the License for the specific language governing permissions and
14 : : * limitations under the License.
15 : : */
16 : :
17 : : /**
18 : : * @file
19 : : * This header defines the HTTPResponse class.
20 : : */
21 : :
22 : : #ifndef __SSRC_WSPR_FCGI_HTTP_RESPONSE_H
23 : : #define __SSRC_WSPR_FCGI_HTTP_RESPONSE_H
24 : :
25 : : #include <string>
26 : : #include <boost/filesystem/path.hpp>
27 : :
28 : : #include <ssrc/wispers-packages.h>
29 : :
30 : : __BEGIN_NS_SSRC_WSPR_FCGI
31 : :
32 : : using std::string;
33 : :
34 : : /**
35 : : * Defines HTTP status codes.
36 : : */
37 : : enum HTTPStatusCode {
38 : : StatusOK = 200,
39 : : StatusFound = 302,
40 : : StatusBadRequest = 400,
41 : : StatusUnauthorized = 401,
42 : : StatusPaymentRequired = 402,
43 : : StatusForbidden = 403,
44 : : StatusNotFound = 404,
45 : : StatusMethodNotAllowed = 405,
46 : : StatusNotAcceptable = 406,
47 : : StatusProxyAuthenticationRequired = 407,
48 : : StatusRequestTimeout = 408,
49 : : StatusConflict = 409,
50 : : StatusGone = 410,
51 : : StatusLengthRequired = 411,
52 : : StatusPreconditionFailed = 412,
53 : : StatusRequestEntityTooLarge = 413,
54 : : StatusRequestURITooLong = 414,
55 : : StatusInternalServerError = 500,
56 : : StatusMethodNotImplemented = 501,
57 : : StatusBadGateway = 502,
58 : : StatusServiceUnavailable = 503,
59 : : StatusGatewayTimeout = 504
60 : : };
61 : :
62 : : const bool CacheDisable = true;
63 : : const bool CacheEnable = false;
64 : :
65 : : /**
66 : : * Abstract class defining the interface for a response to an HTTP request.
67 : : */
68 : : struct HTTPResponse {
69 : :
70 [ # # ]: 0 : virtual ~HTTPResponse() { }
71 : :
72 : : /**
73 : : * Completes the response, rendering the response to the client.
74 : : *
75 : : * @param content A pointer to final data to send to client.
76 : : * @param content_length Length of final content to send to client.
77 : : * @param cache_disable Specifies whether or not the client should
78 : : * cache output. A value of CacheDisable disables caching and
79 : : * CacheEnable enables caching.
80 : : */
81 : : virtual void complete(const char *content = 0,
82 : : const unsigned int content_length = 0,
83 : : bool cache_disable = CacheEnable) = 0;
84 : :
85 : : /**
86 : : * Returns true if the response has completed, false if not.
87 : : *
88 : : * @return True if the response has completed, false if not.
89 : : */
90 : : virtual bool completed() const = 0;
91 : :
92 : : /**
93 : : * Suspends the response, indicating it will be completed later via
94 : : * a continuation.
95 : : */
96 : : virtual void suspend() = 0;
97 : :
98 : : /**
99 : : * Resumes the response, indicating it is being continued via
100 : : * a continuation.
101 : : */
102 : : virtual void resume() = 0;
103 : :
104 : : /**
105 : : * Returns true if the response has been suspended for later
106 : : * completion, false if not.
107 : : *
108 : : * @return True if the response has been suspended for later
109 : : * completion, false if not.
110 : : */
111 : : virtual bool suspended() const = 0;
112 : :
113 : : /**
114 : : * Sends the specified status code and renders a basic error page.
115 : : *
116 : : * @param status The status code to send.
117 : : */
118 : : virtual void send_error(const HTTPStatusCode status) = 0;
119 : :
120 : : /**
121 : : * Redirects the client to a new URL. If the redirect target is not
122 : : * an absolute URL, it is converted to an absolute URL based on the
123 : : * original request URI. A relative path with no leading / will
124 : : * interpret the original request URI as a parent path. Therefore,
125 : : * if you're redirecting a request URI that does not map to a
126 : : * directory, you should calculate the parent path and create a path
127 : : * with a leading / from it.
128 : : *
129 : : * @param redirect The relative, or absolute URL of the
130 : : * redirect target.
131 : : */
132 : : virtual void send_redirect(const string & redirect) = 0;
133 : :
134 : : /**
135 : : * Sets the content type of the response. In general, this method
136 : : * should be used only for responses that aren't text/html.
137 : : *
138 : : * @param type The content type to set.
139 : : */
140 : : virtual void set_content_type(const string & type) = 0;
141 : :
142 : : /**
143 : : * Returns the content type previously set by
144 : : * set_contetn_type(const string & type). The initial value is text/html.
145 : : *
146 : : * @return The content type previously set by
147 : : * set_contetn_type(const string & type).
148 : : */
149 : : virtual const string & content_type() const = 0;
150 : :
151 : : /**
152 : : * Sets the status code for the response. In general, this method
153 : : * should be used only for responses that don't constitute errors.
154 : : * To render an error page along with an error status, you should
155 : : * use send_error(const HTTPStatusCode).
156 : : *
157 : : * @param status The status code to set.
158 : : */
159 : : virtual void set_status(const HTTPStatusCode status) = 0;
160 : :
161 : : /**
162 : : * Returns the status code previously set by
163 : : * set_status(const HTTPStatus code). The initial value is StatusOK.
164 : : *
165 : : * @return The status code previously set by
166 : : * set_status(const HTTPStatus code).
167 : : */
168 : : virtual HTTPStatusCode status() const = 0;
169 : :
170 : : /*
171 : : typedef HTTPRequest::parameter_map header_map;
172 : : virtual void set_header(const header_map::value_type & header_value) = 0;
173 : : virtual void add_header(const header_map::value_type & header_value) = 0;
174 : : */
175 : :
176 : : // TODO: Add domain support.
177 : : /**
178 : : * Sets the session id to store on the client.
179 : : *
180 : : * @param session_id The new session id to set.
181 : : * @param max_age The lifetime of the cookie in seconds. A value of 1
182 : : * is interpreted specially to mean the cookie expires when
183 : : * the user agent terminates.
184 : : */
185 : : virtual void set_session_id(const string & session_id,
186 : : const unsigned int max_age = 1,
187 : : const bool secure = false) = 0;
188 : :
189 : : /**
190 : : * Instructs the client to expire the current session id.
191 : : */
192 : : virtual void clear_session_id() = 0;
193 : :
194 : : /**
195 : : * Returns either the session id provided with the original request
196 : : * or the session id that will be sent back to the client. If no
197 : : * session id was provided by the client and no session id has been
198 : : * set via set_sesion_id(const string &), it returns an empty
199 : : * string. Otherwise it returns the session id provided by the
200 : : * client if set_session_id has not been called, else the id set via
201 : : * set_session_id.
202 : : *
203 : : * @return The sesion id provided with the original request or set
204 : : * via set_session_id.
205 : : */
206 : : virtual const string & session_id() const = 0;
207 : : };
208 : :
209 : :
210 : : __END_NS_SSRC_WSPR_FCGI
211 : :
212 : : #endif
|