/
WebRequest.h
241 lines (194 loc) · 6.16 KB
/
WebRequest.h
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
// This may look like C code, but it's really -*- C++ -*-
/*
* Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
*
* See the LICENSE file for terms of use.
*/
#ifndef WEB_REQUEST_H_
#define WEB_REQUEST_H_
#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <Wt/WDllDefs.h>
#include <Wt/WGlobal>
#include <Wt/Http/Request>
#include <boost/cstdint.hpp>
#include <boost/function.hpp>
namespace Wt {
class EntryPoint;
class WSslInfo;
/*
* A single, raw, HTTP request/response, which conveys all of the http-related
* information to the application and gathers the response.
*/
class WT_API WebRequest
{
public:
WebRequest();
void log();
enum ResponseState {
ResponseDone,
ResponseFlush
};
enum ResponseType {
Page,
Script,
Update
};
typedef boost::function<void(WebWriteEvent)> WriteCallback;
typedef boost::function<void(WebReadEvent)> ReadCallback;
typedef boost::function<void(void)> DisconnectCallback;
/*
* Signal that the response should be flushed.
*
* ResponseDone: flush & close
*
* ResponseFlush: flush what we have so far, do not close
* - callback must be specified for ResponseFlush, and is called
* if more data can be written. Until then, you cannot do new
* writes.
*/
virtual void flush(ResponseState state = ResponseDone,
const WriteCallback& callback = WriteCallback()) = 0;
#ifdef WT_TARGET_JAVA
virtual void flushBuffer();
#endif
/*
* For a web socket request (isWebSocketRequest()), read a message
* and call the given callback function when done.
*
* The new message is available in in() and has length contentLength()
*/
virtual void readWebSocketMessage(const ReadCallback& callback);
/*
* For a web socket request (isWebSocketRequest()), returns whether
* more data is available. This is used to defer a response but wait
* for more incoming events.
*/
virtual bool webSocketMessagePending() const;
/*
* Indicate that we're deferring to write a response, but in the mean-time
* we do want to know if there's a disconnect event (by reading from
* the socket).
*/
virtual bool detectDisconnect(const DisconnectCallback& callback);
/*
* Access the stream that contains the request body (HTTP) or a
* single message (WS)
*/
virtual std::istream& in() = 0;
/*
* Access the stream to submit the response.
*
* This is either the entire response body (HTTP), or a single response
* message (WS)
*/
virtual std::ostream& out() = 0;
WT_BOSTREAM& bout() { return out(); }
/*
* (Not used)
*/
virtual std::ostream& err() = 0;
/*
* Sets the redirect (instead of anything else).
*/
virtual void setRedirect(const std::string& url) = 0;
/*
* Sets the status
*/
virtual void setStatus(int status) = 0;
/*
* Sets the content-type for a normal response.
*/
virtual void setContentType(const std::string& value) = 0;
/*
* Sets the content-length for a normal response.
*/
virtual void setContentLength(::int64_t length) = 0;
/*
* Adds a header for a normal response.
*/
virtual void addHeader(const std::string& name, const std::string& value) = 0;
/*
* Returns request information, which are not http headers.
*/
virtual const char *envValue(const char *name) const = 0;
virtual const std::string& serverName() const = 0;
virtual const std::string& serverPort() const = 0;
virtual const std::string& scriptName() const = 0;
virtual const char *requestMethod() const = 0;
virtual const std::string& queryString() const = 0;
virtual const std::string& pathInfo() const = 0;
virtual const std::string& remoteAddr() const = 0;
virtual const char *urlScheme() const = 0;
virtual bool isWebSocketMessage() const { return false; }
bool isWebSocketRequest() const { return webSocketRequest_; }
void setWebSocketRequest(bool ws) { webSocketRequest_ = ws; }
/*
* Accesses to cgi environment variables and headers -- rfc2616 name
*/
virtual const char *headerValue(const char *name) const = 0;
/*
* Accesses to specific header fields (calls headerValue()).
*/
const char *userAgent() const;
const char *referer() const;
#ifndef WT_TARGET_JAVA
virtual std::vector<Wt::Http::Message::Header> headers() const = 0;
#endif
virtual const char *contentType() const;
virtual ::int64_t contentLength() const;
#ifdef WT_TARGET_JAVA
/*
* In JavaEE, the servlet determines how session tracking is encoded in
* the URL.
*/
std::string encodeURL(const std::string& url) const;
#endif // WT_TARGET_JAVA
const std::string *getParameter(const std::string& name) const;
const Http::ParameterValues& getParameterValues(const std::string& name)
const;
const Http::ParameterMap& getParameterMap() const { return parameters_; }
const Http::UploadedFileMap& uploadedFiles() const { return files_; }
::int64_t postDataExceeded() const { return postDataExceeded_; }
WLocale parseLocale() const;
void setResponseType(ResponseType responseType);
ResponseType responseType() const { return responseType_; }
/*
* Returns \c 0 if the request does not have SSL client certificate
* information. When sslInfo() does return a pointer, the ownership of the
* pointer is transferred to the caller, which must delete it.
*/
virtual WSslInfo *sslInfo() const = 0;
protected:
const EntryPoint *entryPoint_;
virtual ~WebRequest();
void reset();
#ifndef WT_CNOR
struct AsyncEmulation;
AsyncEmulation *async_;
void emulateAsync(ResponseState state);
void setAsyncCallback(const WriteCallback& cb);
const WriteCallback& getAsyncCallback();
#endif // WT_CNOR
private:
std::string parsePreferredAcceptValue(const char *value) const;
::int64_t postDataExceeded_;
Http::ParameterMap parameters_;
Http::UploadedFileMap files_;
ResponseType responseType_;
bool webSocketRequest_;
boost::posix_time::ptime start_;
static Http::ParameterValues emptyValues_;
#ifndef WT_CNOR
WriteCallback asyncCallback_;
#endif // WT_CNOR
friend class CgiParser;
friend class Http::Request;
friend class WEnvironment;
friend class WebController;
};
class WebResponse : public WebRequest
{
};
}
#endif // WEB_REQUEST_H_