| 1 |
// Copyright (c) 2009, Whispersoft s.r.l. |
|---|
| 2 |
// All rights reserved. |
|---|
| 3 |
// |
|---|
| 4 |
// Redistribution and use in source and binary forms, with or without |
|---|
| 5 |
// modification, are permitted provided that the following conditions are |
|---|
| 6 |
// met: |
|---|
| 7 |
// |
|---|
| 8 |
// * Redistributions of source code must retain the above copyright |
|---|
| 9 |
// notice, this list of conditions and the following disclaimer. |
|---|
| 10 |
// * Redistributions in binary form must reproduce the above |
|---|
| 11 |
// copyright notice, this list of conditions and the following disclaimer |
|---|
| 12 |
// in the documentation and/or other materials provided with the |
|---|
| 13 |
// distribution. |
|---|
| 14 |
// * Neither the name of Whispersoft s.r.l. nor the names of its |
|---|
| 15 |
// contributors may be used to endorse or promote products derived from |
|---|
| 16 |
// this software without specific prior written permission. |
|---|
| 17 |
// |
|---|
| 18 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 19 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 20 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 21 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 22 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 23 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 24 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 25 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 26 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 27 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 28 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 29 |
// |
|---|
| 30 |
// Author: Catalin Popescu |
|---|
| 31 |
// |
|---|
| 32 |
// Here are classes that you need for having an http server. The main idea |
|---|
| 33 |
// is to decouple the protocol detail (server-client conversation) from |
|---|
| 34 |
// the connection details, and the processing of request from anything else |
|---|
| 35 |
// possible. |
|---|
| 36 |
// |
|---|
| 37 |
// The main classes you need to care here are: |
|---|
| 38 |
// - http::ServerParams - a bunch of prameters that define how the server |
|---|
| 39 |
// behaves (self describing) |
|---|
| 40 |
// - http::Server - this manages the server state. It has a name, a set |
|---|
| 41 |
// of ServerParams and dispatches the request processing. |
|---|
| 42 |
// You need to register a processing function for your desired |
|---|
| 43 |
// path and your function will be called when a request comes on |
|---|
| 44 |
// that path. |
|---|
| 45 |
// - http::ServerProtocol - this manages a conversation with a client: |
|---|
| 46 |
// it reads the requests and writes out the response out (is |
|---|
| 47 |
// one per connection) - the idea is to sepparate the |
|---|
| 48 |
// protocol details from the communication part. |
|---|
| 49 |
// - http::BaseServerConnection - a connection for talking http over. It |
|---|
| 50 |
// provides some interface functions and is required to call |
|---|
| 51 |
// functions into the protool. (We have the simple implementation: |
|---|
| 52 |
// http::SimpleServerConnection for a normal http connection) |
|---|
| 53 |
// - http::ServerRequest - this is what needs to be processed by the |
|---|
| 54 |
// processing function. Upon return it should contain what the |
|---|
| 55 |
// client should receive. There are two modes of operation from |
|---|
| 56 |
// server's point of view: streaming data and normal data. For |
|---|
| 57 |
// the streaming data, the response data is produced in multiple |
|---|
| 58 |
// chuncks that are sent over a longer period of time. For normal |
|---|
| 59 |
// data, the entire response is produced at once. |
|---|
| 60 |
// |
|---|
| 61 |
// Please check http_server_test.cc for an example of how to use it. Also |
|---|
| 62 |
// check the other comments in this .h file. |
|---|
| 63 |
// |
|---|
| 64 |
|
|---|
| 65 |
#ifndef __NET_HTTP_HTTP_SERVER_PROTOCOL_H__ |
|---|
| 66 |
#define __NET_HTTP_HTTP_SERVER_PROTOCOL_H__ |
|---|
| 67 |
|
|---|
| 68 |
#include <map> |
|---|
| 69 |
#include <set> |
|---|
| 70 |
#include <string> |
|---|
| 71 |
|
|---|
| 72 |
#include <whisperlib/common/base/types.h> |
|---|
| 73 |
|
|---|
| 74 |
#include WHISPER_HASH_SET_HEADER |
|---|
| 75 |
#include WHISPER_HASH_MAP_HEADER |
|---|
| 76 |
|
|---|
| 77 |
#include <whisperlib/common/sync/thread_pool.h> |
|---|
| 78 |
#include <whisperlib/common/sync/mutex.h> |
|---|
| 79 |
#include <whisperlib/net/http/http_request.h> |
|---|
| 80 |
#include <whisperlib/net/base/selector.h> |
|---|
| 81 |
#include <whisperlib/net/base/timeouter.h> |
|---|
| 82 |
#include <whisperlib/net/base/connection.h> |
|---|
| 83 |
#include <whisperlib/net/base/address.h> |
|---|
| 84 |
#include <whisperlib/net/url/url.h> |
|---|
| 85 |
#include <whisperlib/net/base/user_authenticator.h> |
|---|
| 86 |
|
|---|
| 87 |
namespace http { |
|---|
| 88 |
|
|---|
| 89 |
////////////////////////////////////////////////////////////////////// |
|---|
| 90 |
// |
|---|
| 91 |
// ServerParams - Parameters for the behaviour of the http server |
|---|
| 92 |
// |
|---|
| 93 |
struct ServerParams { |
|---|
| 94 |
// What to do when we reach the limit of the outgoing buffer ? |
|---|
| 95 |
enum FlowControlPolicy { |
|---|
| 96 |
POLICY_CLOSE = 1, // close the connection |
|---|
| 97 |
POLICY_DROP_OLD_DATA = 2, // drop the old data in the buffer and stuff the |
|---|
| 98 |
// new one |
|---|
| 99 |
POLICY_DROP_NEW_DATA = 3, // keep the old data, drop the new one |
|---|
| 100 |
}; |
|---|
| 101 |
|
|---|
| 102 |
ServerParams(); |
|---|
| 103 |
ServerParams(const char* root_url, |
|---|
| 104 |
bool dlog_level_, |
|---|
| 105 |
bool strict_request_headers, |
|---|
| 106 |
bool allow_all_methods, |
|---|
| 107 |
int32 max_header_size, |
|---|
| 108 |
int32 max_body_size, |
|---|
| 109 |
int32 max_chunk_size, |
|---|
| 110 |
int64 max_num_chunks, |
|---|
| 111 |
http::Header::ParseError worst_accepted_header_error, |
|---|
| 112 |
int32 max_concurrent_connections, |
|---|
| 113 |
int32 max_concurrent_requests, |
|---|
| 114 |
int32 max_concurrent_requests_per_connection, |
|---|
| 115 |
int32 request_read_timeout_ms, |
|---|
| 116 |
int32 keep_alive_timeout_sec, |
|---|
| 117 |
int32 reply_write_timeout_ms, |
|---|
| 118 |
int32 max_reply_buffer_size, |
|---|
| 119 |
FlowControlPolicy reply_full_buffer_policy, |
|---|
| 120 |
const char* default_content_type, |
|---|
| 121 |
bool ignore_different_http_hosts, |
|---|
| 122 |
int32 ready_signal_limit); |
|---|
| 123 |
|
|---|
| 124 |
// Root for our serving path |
|---|
| 125 |
URL root_url_; |
|---|
| 126 |
|
|---|
| 127 |
// Log in detail ? |
|---|
| 128 |
bool dlog_level_; |
|---|
| 129 |
|
|---|
| 130 |
// Parse strictly all HTTP headers |
|---|
| 131 |
bool strict_request_headers_; |
|---|
| 132 |
|
|---|
| 133 |
// Allowed methods - by default we allow non destructive methods only: |
|---|
| 134 |
// CONNECT, HEAD, GET and POST. If this is turned on, it is the |
|---|
| 135 |
// responsability of the processors to decide. |
|---|
| 136 |
bool allow_all_methods_; |
|---|
| 137 |
|
|---|
| 138 |
// How long the acceptable HTTP header can be ? |
|---|
| 139 |
int32 max_header_size_; |
|---|
| 140 |
// For non chunked body, how long this can be ? |
|---|
| 141 |
int32 max_body_size_; |
|---|
| 142 |
// For chunked body, how big one chunk can be ? |
|---|
| 143 |
int32 max_chunk_size_; |
|---|
| 144 |
// For chunked body, how many chuncks can we accept in a request / reply ? |
|---|
| 145 |
// (-1 => no limit) |
|---|
| 146 |
int64 max_num_chunks_; |
|---|
| 147 |
|
|---|
| 148 |
// When parsing headers, what is the worst acceptable error ? |
|---|
| 149 |
// (Recommended: Header::READ_NO_STATUS_REASON) |
|---|
| 150 |
http::Header::ParseError worst_accepted_header_error_; |
|---|
| 151 |
|
|---|
| 152 |
// We default to these: |
|---|
| 153 |
// bool accept_wrong_method = false, |
|---|
| 154 |
// bool accept_wrong_version = false, |
|---|
| 155 |
|
|---|
| 156 |
// How many concurrent connections can we have ? |
|---|
| 157 |
int32 max_concurrent_connections_; |
|---|
| 158 |
// How many concurrent requests can we accept ? |
|---|
| 159 |
int32 max_concurrent_requests_; |
|---|
| 160 |
// How many concurrent requests per each connection do we accept ? |
|---|
| 161 |
int32 max_concurrent_requests_per_connection_; |
|---|
| 162 |
|
|---|
| 163 |
// How long to wait to receive a request ? |
|---|
| 164 |
int32 request_read_timeout_ms_; |
|---|
| 165 |
// Timeout on keep-alive connections ? (On 0 - we close the connection) |
|---|
| 166 |
int32 keep_alive_timeout_sec_; |
|---|
| 167 |
// How long to wait for reads from the outbud to complete ? |
|---|
| 168 |
int32 reply_write_timeout_ms_; |
|---|
| 169 |
// How big can grow the reply buffer ? |
|---|
| 170 |
int32 max_reply_buffer_size_; |
|---|
| 171 |
FlowControlPolicy reply_full_buffer_policy_; |
|---|
| 172 |
|
|---|
| 173 |
// Default content type for our answers |
|---|
| 174 |
string default_content_type_; |
|---|
| 175 |
// If true we ignore different http hosts received in the sourece urls.. |
|---|
| 176 |
bool ignore_different_http_hosts_; |
|---|
| 177 |
|
|---|
| 178 |
// For flow control and efficiency we signal ready buffers |
|---|
| 179 |
// only when they have at least this much free |
|---|
| 180 |
int32 ready_signal_limit_; |
|---|
| 181 |
}; |
|---|
| 182 |
|
|---|
| 183 |
class ServerRequest; |
|---|
| 184 |
class BaseServerConnection; |
|---|
| 185 |
class ServerProtocol; |
|---|
| 186 |
class Server; |
|---|
| 187 |
|
|---|
| 188 |
////////////////////////////////////////////////////////////////////// |
|---|
| 189 |
// |
|---|
| 190 |
// Server accepting connection - we need to know a factory method |
|---|
| 191 |
// for the serving connection. |
|---|
| 192 |
// |
|---|
| 193 |
class ServerAcceptor { |
|---|
| 194 |
public: |
|---|
| 195 |
typedef ResultCallback3<http::BaseServerConnection*, |
|---|
| 196 |
net::Selector*, |
|---|
| 197 |
net::NetConnection*, |
|---|
| 198 |
http::ServerProtocol*> FactoryCallback; |
|---|
| 199 |
|
|---|
| 200 |
ServerAcceptor(net::Selector* selector, |
|---|
| 201 |
net::NetFactory* net_factory, |
|---|
| 202 |
net::PROTOCOL net_protocol, |
|---|
| 203 |
const net::HostPort& local_addr, |
|---|
| 204 |
http::Server* server, |
|---|
| 205 |
FactoryCallback* factory_callback); |
|---|
| 206 |
virtual ~ServerAcceptor(); |
|---|
| 207 |
|
|---|
| 208 |
const net::HostPort& local_addr() const { |
|---|
| 209 |
return local_addr_; |
|---|
| 210 |
} |
|---|
| 211 |
|
|---|
| 212 |
bool Listen(); |
|---|
| 213 |
void Close(); |
|---|
| 214 |
|
|---|
| 215 |
private: |
|---|
| 216 |
// Called every time a client wants to connect. |
|---|
| 217 |
// We return false to shutdown this guy, |
|---|
| 218 |
// or true to accept it. |
|---|
| 219 |
bool AcceptorFilterHandler(const net::HostPort& peer_address); |
|---|
| 220 |
// After AcceptorFilterHandler returned true, |
|---|
| 221 |
// the client connection is delivered here. |
|---|
| 222 |
void AcceptorAcceptHandler(net::NetConnection* peer_connection); |
|---|
| 223 |
|
|---|
| 224 |
private: |
|---|
| 225 |
net::Selector* selector_; |
|---|
| 226 |
net::NetAcceptor* acceptor_; |
|---|
| 227 |
|
|---|
| 228 |
const net::HostPort local_addr_; |
|---|
| 229 |
|
|---|
| 230 |
// This guy does the work (registering the serving connections, |
|---|
| 231 |
// processing requests etc.) |
|---|
| 232 |
http::Server* const server_; |
|---|
| 233 |
|
|---|
| 234 |
// This creates a new serving connection for us - must be permanent |
|---|
| 235 |
FactoryCallback* const factory_callback_; |
|---|
| 236 |
|
|---|
| 237 |
private: |
|---|
| 238 |
DISALLOW_EVIL_CONSTRUCTORS(ServerAcceptor); |
|---|
| 239 |
}; |
|---|
| 240 |
|
|---|
| 241 |
////////////////////////////////////////////////////////////////////// |
|---|
| 242 |
|
|---|
| 243 |
class Server { |
|---|
| 244 |
public: |
|---|
| 245 |
// Creates a server with a given name, main network thread and |
|---|
| 246 |
// protocol behavior. |
|---|
| 247 |
// The factory callback is used to create the serving connection |
|---|
| 248 |
// of a desired type, and requests may be processed by threads in a |
|---|
| 249 |
// thread pool *iff* threadpool_size > 0 |
|---|
| 250 |
Server(const char* name, |
|---|
| 251 |
net::Selector* selector, |
|---|
| 252 |
net::NetFactory* net_factory, |
|---|
| 253 |
const ServerParams* protocol_params, |
|---|
| 254 |
ServerAcceptor::FactoryCallback* conn_factory_callback, |
|---|
| 255 |
int thread_pool_size); |
|---|
| 256 |
virtual ~Server(); |
|---|
| 257 |
|
|---|
| 258 |
// Add an Acceptor for listening on a local port. |
|---|
| 259 |
// The local_address should contain at least the port. |
|---|
| 260 |
void AddAcceptor(net::PROTOCOL net_protocol, |
|---|
| 261 |
const net::HostPort& local_address); |
|---|
| 262 |
|
|---|
| 263 |
// Binds every Acceptor to it's port and start serving requests. |
|---|
| 264 |
// If a bind fails, we abort (crash by SIGABRT). |
|---|
| 265 |
void StartServing(); |
|---|
| 266 |
|
|---|
| 267 |
// Stop every Acceptor |
|---|
| 268 |
void StopServing(); |
|---|
| 269 |
|
|---|
| 270 |
typedef Callback1<ServerRequest*> ServerCallback; |
|---|
| 271 |
|
|---|
| 272 |
// Registers processing functions for a given path. The new callback |
|---|
| 273 |
// will replace the old one (returns true). |
|---|
| 274 |
// If the processor is delete or replaced, the corresponding allowed |
|---|
| 275 |
// ips are deleted !! (so watch out). |
|---|
| 276 |
// The most specific path is chosen: |
|---|
| 277 |
// E.g. if you register for "/test1" -> processor1 and |
|---|
| 278 |
// "/test1/test2" -> processor2, for a path like: |
|---|
| 279 |
// "/test1/test2/test3" -> we invoke processor2 |
|---|
| 280 |
// "/test1" -> we invoke processor1 |
|---|
| 281 |
// "/test1/test3" -> we invoke processor1 |
|---|
| 282 |
// |
|---|
| 283 |
// IMPORTANT: The given callback is in our realm if this function |
|---|
| 284 |
// returns true. |
|---|
| 285 |
// We delete it upon unregistration. |
|---|
| 286 |
bool RegisterProcessor(const string& path, ServerCallback* callback, |
|---|
| 287 |
bool is_public); |
|---|
| 288 |
|
|---|
| 289 |
// The reverse of RegisterProcessor. |
|---|
| 290 |
// NOTE: we delete the previously registered callback (if any) |
|---|
| 291 |
bool UnregisterProcessor(const string& path) { |
|---|
| 292 |
return RegisterProcessor(path, NULL, false); |
|---|
| 293 |
} |
|---|
| 294 |
|
|---|
| 295 |
// Sets IP sets for given given path (if not public). We do not take |
|---|
| 296 |
// control of the pointer (which should be around for the whole |
|---|
| 297 |
// duration of Server's life. |
|---|
| 298 |
// If the given filter is NULL -> means all allowed :) |
|---|
| 299 |
// The path resolving is same as for processors |
|---|
| 300 |
void RegisterAllowedIpAddresses(const string& path, |
|---|
| 301 |
const net::IpV4Filter* ips); |
|---|
| 302 |
|
|---|
| 303 |
// Registers that on the given path the server receives streams |
|---|
| 304 |
// of data from the client and we need to do multiple processing calls. |
|---|
| 305 |
void RegisterClientStreaming(const string& path, |
|---|
| 306 |
bool is_client_streaming); |
|---|
| 307 |
|
|---|
| 308 |
// Processes a given request - fully read from the client. |
|---|
| 309 |
// In this function we call the right handler, and in |
|---|
| 310 |
// case of errors (like handlers called from bad ips, 404s or requests |
|---|
| 311 |
// that are rejected from other reasons). |
|---|
| 312 |
// Used internally. |
|---|
| 313 |
void ProcessRequest(http::ServerRequest* req); |
|---|
| 314 |
|
|---|
| 315 |
// Determines if specific protocol params must be followed by the |
|---|
| 316 |
// given request. (e.g. is a client streaming - etc) |
|---|
| 317 |
// Used internally. |
|---|
| 318 |
void SetSpecificProtocolParams(http::ServerRequest* req); |
|---|
| 319 |
|
|---|
| 320 |
|
|---|
| 321 |
// Set your own default request processor |
|---|
| 322 |
void set_default_processor(ServerCallback* default_processor) { |
|---|
| 323 |
delete default_processor_; |
|---|
| 324 |
default_processor_ = default_processor; |
|---|
| 325 |
} |
|---|
| 326 |
|
|---|
| 327 |
// This is called in case an error occurred on the request |
|---|
| 328 |
void set_error_processor(ServerCallback* error_processor) { |
|---|
| 329 |
delete error_processor_; |
|---|
| 330 |
error_processor_ = error_processor; |
|---|
| 331 |
} |
|---|
| 332 |
|
|---|
| 333 |
const ServerParams* protocol_params() const { |
|---|
| 334 |
return protocol_params_; |
|---|
| 335 |
} |
|---|
| 336 |
int num_connections() const { |
|---|
| 337 |
return protocols_.size(); |
|---|
| 338 |
} |
|---|
| 339 |
|
|---|
| 340 |
// Notification Received from the server acceptor when a new protocol |
|---|
| 341 |
// (w/ connection is created). |
|---|
| 342 |
void AddClient(ServerProtocol* proto); |
|---|
| 343 |
void DeleteClient(ServerProtocol* proto); |
|---|
| 344 |
|
|---|
| 345 |
net::Selector* selector() { return selector_; } |
|---|
| 346 |
const string& name() const { return name_; } |
|---|
| 347 |
private: |
|---|
| 348 |
// Runs the processor callback or schedules the req to be completed |
|---|
| 349 |
// in the thread pool. |
|---|
| 350 |
void PerformProcessing(http::ServerRequest* req); |
|---|
| 351 |
|
|---|
| 352 |
void DefaultRequestProcessor(ServerRequest* req); |
|---|
| 353 |
void ErrorRequestProcessor(ServerRequest* req); |
|---|
| 354 |
|
|---|
| 355 |
// Name of the server - we return this in the "Server" field |
|---|
| 356 |
const string name_; |
|---|
| 357 |
// Main network thread selector |
|---|
| 358 |
net::Selector* const selector_; |
|---|
| 359 |
// Main network factory (for creating tcp/ssl acceptors) |
|---|
| 360 |
net::NetFactory* const net_factory_; |
|---|
| 361 |
|
|---|
| 362 |
// Parameters regarding the behavior of the protocol |
|---|
| 363 |
// (timeouts, buffer sizes etc) - Should be around all the time.. |
|---|
| 364 |
const ServerParams* const protocol_params_; |
|---|
| 365 |
|
|---|
| 366 |
// Creates serving connections for the acceptor |
|---|
| 367 |
ServerAcceptor::FactoryCallback* const conn_factory_callback_; |
|---|
| 368 |
|
|---|
| 369 |
const int thread_pool_size_; |
|---|
| 370 |
// This may process the requests .. |
|---|
| 371 |
thread::ThreadPool* thread_pool_; |
|---|
| 372 |
|
|---|
| 373 |
// The port we are serving on |
|---|
| 374 |
//int port_; |
|---|
| 375 |
|
|---|
| 376 |
// The guys who accept connections for us. |
|---|
| 377 |
// We use multiple acceptors, to listen on multiple ports. |
|---|
| 378 |
vector<ServerAcceptor*> acceptors_; |
|---|
| 379 |
|
|---|
| 380 |
// Callbacks for processing regular |
|---|
| 381 |
typedef map<string, ServerCallback*> ProcessorMap; |
|---|
| 382 |
ProcessorMap processors_; |
|---|
| 383 |
|
|---|
| 384 |
// Signals streaming clients |
|---|
| 385 |
typedef map<string, bool> IsStreamingClientMap; |
|---|
| 386 |
IsStreamingClientMap is_streaming_client_map_; |
|---|
| 387 |
|
|---|
| 388 |
// Allowed ips / per path - NULL -> All allowed |
|---|
| 389 |
typedef map<string, const net::IpV4Filter*> AllowedIpsMap; |
|---|
| 390 |
AllowedIpsMap allowed_ips_; |
|---|
| 391 |
|
|---|
| 392 |
// This is called when we cannot find a processor for a given path |
|---|
| 393 |
// (by default we return a 404) |
|---|
| 394 |
ServerCallback* default_processor_; |
|---|
| 395 |
|
|---|
| 396 |
// This is called in case an error occured on the request |
|---|
| 397 |
// (by default we return a 500); |
|---|
| 398 |
ServerCallback* error_processor_; |
|---|
| 399 |
|
|---|
| 400 |
// All the current opened communications |
|---|
| 401 |
typedef hash_set<http::ServerProtocol*> ProtocolSet; |
|---|
| 402 |
ProtocolSet protocols_; |
|---|
| 403 |
|
|---|
| 404 |
// Number of equests that are in processing state - we used to track |
|---|
| 405 |
// w/ these the over to top clients and orphaned requests.. |
|---|
| 406 |
typedef hash_map<http::ServerProtocol*, int> ProtoReqMap; |
|---|
| 407 |
ProtoReqMap protocol_outstanding_requests_; |
|---|
| 408 |
private: |
|---|
| 409 |
DISALLOW_EVIL_CONSTRUCTORS(Server); |
|---|
| 410 |
}; |
|---|
| 411 |
|
|---|
| 412 |
////////////////////////////////////////////////////////////////////// |
|---|
| 413 |
// |
|---|
| 414 |
// Derive your server connection from here. You need to override the |
|---|
| 415 |
// virtual functions declared in here.. Make sure that your protocol |
|---|
| 416 |
// sees the payload data you receive !! (called on ProcessMoreData()); |
|---|
| 417 |
// The ServerProtocol must exist for all the duration of a connection. |
|---|
| 418 |
// The connection must inform (on deletion) that is going away.. |
|---|
| 419 |
// |
|---|
| 420 |
class BaseServerConnection { |
|---|
| 421 |
public: |
|---|
| 422 |
// we take ownership of "net_connection" |
|---|
| 423 |
explicit BaseServerConnection(net::Selector* selector, |
|---|
| 424 |
net::NetConnection * net_connection, |
|---|
| 425 |
http::ServerProtocol* protocol); |
|---|
| 426 |
virtual ~BaseServerConnection(); |
|---|
| 427 |
|
|---|
| 428 |
// Called when protocol puts some data on the wire |
|---|
| 429 |
void NotifyWrite(); |
|---|
| 430 |
|
|---|
| 431 |
// TODO(cosmin): remove useless function. |
|---|
| 432 |
// You never separate fd from connection. |
|---|
| 433 |
// Detaches the connection from the fd - you an use it at that point |
|---|
| 434 |
// as you wish |
|---|
| 435 |
net::NetConnection* DetachFromFd() { |
|---|
| 436 |
net::NetConnection* conn = net_connection_; |
|---|
| 437 |
net_connection_ = NULL; |
|---|
| 438 |
conn->DetachAllHandlers(); |
|---|
| 439 |
return conn; |
|---|
| 440 |
} |
|---|
| 441 |
void RequestReadEvents(bool enable) { |
|---|
| 442 |
if ( net_connection_ ) { |
|---|
| 443 |
net_connection_->RequestReadEvents(enable); |
|---|
| 444 |
} |
|---|
| 445 |
} |
|---|
| 446 |
void RequestWriteEvents(bool enable) { |
|---|
| 447 |
if ( net_connection_ ) { |
|---|
| 448 |
net_connection_->RequestWriteEvents(enable); |
|---|
| 449 |
} |
|---|
| 450 |
} |
|---|
| 451 |
|
|---|
| 452 |
const http::ServerProtocol* protocol() const; |
|---|
| 453 |
|
|---|
| 454 |
const net::HostPort& remote_address() const { |
|---|
| 455 |
return net_connection_->remote_address(); |
|---|
| 456 |
} |
|---|
| 457 |
const net::HostPort& local_address() const { |
|---|
| 458 |
return net_connection_->local_address(); |
|---|
| 459 |
} |
|---|
| 460 |
io::MemoryStream* inbuf() { |
|---|
| 461 |
return net_connection_->inbuf(); |
|---|
| 462 |
} |
|---|
| 463 |
io::MemoryStream* outbuf() { |
|---|
| 464 |
return net_connection_->outbuf(); |
|---|
| 465 |
} |
|---|
| 466 |
int64 count_bytes_written() const { |
|---|
| 467 |
return net_connection_->count_bytes_written(); |
|---|
| 468 |
} |
|---|
| 469 |
int64 count_bytes_read() const { |
|---|
| 470 |
return net_connection_->count_bytes_read(); |
|---|
| 471 |
} |
|---|
| 472 |
void FlushAndClose() { |
|---|
| 473 |
net_connection_->FlushAndClose(); |
|---|
| 474 |
} |
|---|
| 475 |
void ForceClose() { |
|---|
| 476 |
net_connection_->ForceClose(); |
|---|
| 477 |
} |
|---|
| 478 |
|
|---|
| 479 |
private: |
|---|
| 480 |
bool ConnectionReadHandler(); |
|---|
| 481 |
bool ConnectionWriteHandler(); |
|---|
| 482 |
void ConnectionCloseHandler(int err, net::NetConnection::CloseWhat what); |
|---|
| 483 |
|
|---|
| 484 |
private: |
|---|
| 485 |
net::Selector * selector_; |
|---|
| 486 |
net::NetConnection* net_connection_; |
|---|
| 487 |
|
|---|
| 488 |
// This guy needs to be informed about all data received and contains |
|---|
| 489 |
// the communication state (when needed) |
|---|
| 490 |
http::ServerProtocol* protocol_; |
|---|
| 491 |
private: |
|---|
| 492 |
DISALLOW_EVIL_CONSTRUCTORS(BaseServerConnection); |
|---|
| 493 |
}; |
|---|
| 494 |
|
|---|
| 495 |
////////////////////////////////////////////////////////////////////// |
|---|
| 496 |
// |
|---|
| 497 |
// ServerProtocol - manages a client conversation - parses the requests |
|---|
| 498 |
// and effectively writes the response to the client. Does some |
|---|
| 499 |
// request error checking (but all responses, even the error |
|---|
| 500 |
// ones go through the server first, even if in the end come to us). |
|---|
| 501 |
// We have this to separate the protocol decisions themselves |
|---|
| 502 |
// from the implementation details of the communication channel. |
|---|
| 503 |
// |
|---|
| 504 |
class ServerProtocol { |
|---|
| 505 |
public: |
|---|
| 506 |
ServerProtocol(net::Selector* selector, Server* server); |
|---|
| 507 |
~ServerProtocol(); |
|---|
| 508 |
|
|---|
| 509 |
// Parameters for how we speak the protocol |
|---|
| 510 |
const ServerParams* protocol_params() const { |
|---|
| 511 |
return protocol_params_; |
|---|
| 512 |
} |
|---|
| 513 |
const net::HostPort& remote_address() const { |
|---|
| 514 |
return remote_address_; |
|---|
| 515 |
} |
|---|
| 516 |
const net::HostPort& local_address() const { |
|---|
| 517 |
return local_address_; |
|---|
| 518 |
} |
|---|
| 519 |
http::Server* server() const { |
|---|
| 520 |
return server_; |
|---|
| 521 |
} |
|---|
| 522 |
net::Selector* selector() const { |
|---|
| 523 |
return selector_; |
|---|
| 524 |
} |
|---|
| 525 |
// Can be used to do flow control (beside the default parametes from |
|---|
| 526 |
// protocol_params_) |
|---|
| 527 |
const int32 outbuf_size() { |
|---|
| 528 |
DCHECK(connection_ != NULL); |
|---|
| 529 |
return connection_->outbuf()->Size(); |
|---|
| 530 |
} |
|---|
| 531 |
|
|---|
| 532 |
// Sets the underground TCP connection - call it once |
|---|
| 533 |
// (We also set some parameters) |
|---|
| 534 |
void set_connection(BaseServerConnection* conn); |
|---|
| 535 |
|
|---|
| 536 |
// Detaches the given request from the associated fd. On return the |
|---|
| 537 |
// req. will have the protocol NULL-ified, and you should not use |
|---|
| 538 |
// that member again. |
|---|
| 539 |
// It returns the fd of the serving connection - is your duty to read/write |
|---|
| 540 |
// from it and to close it. |
|---|
| 541 |
// If this returns INVALID_FD_VALUE means a orphaned connection (closed |
|---|
| 542 |
// connection by client in the meantime) and you should disregard any |
|---|
| 543 |
// continuation or use of req. |
|---|
| 544 |
net::NetConnection* DetachFromFd(http::ServerRequest* req); |
|---|
| 545 |
|
|---|
| 546 |
// called when the connection is deleted |
|---|
| 547 |
void NotifyConnectionDeletion(); |
|---|
| 548 |
// called when the connection handled a write |
|---|
| 549 |
void NotifyConnectionWrite(); |
|---|
| 550 |
|
|---|
| 551 |
// Flow control functions: |
|---|
| 552 |
void PauseReading() { |
|---|
| 553 |
if ( connection_ != NULL ) { |
|---|
| 554 |
connection_->RequestReadEvents(false); |
|---|
| 555 |
} |
|---|
| 556 |
} |
|---|
| 557 |
void UnpauseReading() { |
|---|
| 558 |
if ( connection_ != NULL ) { |
|---|
| 559 |
connection_->RequestReadEvents(true); |
|---|
| 560 |
} |
|---|
| 561 |
} |
|---|
| 562 |
|
|---|
| 563 |
// This closes all active requests and clears all stuff |
|---|
| 564 |
// PRECONDITION: condition_ == NULL |
|---|
| 565 |
void CloseAllActiveRequests(); |
|---|
| 566 |
|
|---|
| 567 |
// This callback processes more data from connection_->inbuf() |
|---|
| 568 |
bool ProcessMoreData(); |
|---|
| 569 |
|
|---|
| 570 |
// Response finalization - replies to the given request. If |
|---|
| 571 |
// is_server_streaming is on we expect a chuncked multi chunk |
|---|
| 572 |
// response (you need to continue calling StreamData until you |
|---|
| 573 |
// call w/ a io_eos on true. |
|---|
| 574 |
// Is your duty not to leek requests or to double-call this function |
|---|
| 575 |
// w. the same req. |
|---|
| 576 |
// |
|---|
| 577 |
// PRECONDITION : a lock is held on req->request_lock() for multithreading |
|---|
| 578 |
void ReplyForRequestLocked(ServerRequest* req, HttpReturnCode status); |
|---|
| 579 |
// Streams a new chunk of data for given request. If is_eos is true, |
|---|
| 580 |
// it means that it was the last chunk.. |
|---|
| 581 |
// |
|---|
| 582 |
// PRECONDITION : a lock is held on req->request_lock() for multithreading |
|---|
| 583 |
void StreamDataLocked(ServerRequest* req, bool is_eos); |
|---|
| 584 |
|
|---|
| 585 |
// Does the actual header setup and puts data out (if not orphaned). |
|---|
| 586 |
// Returns true if we should close the conn. |
|---|
| 587 |
bool PrepareResponseLocked(ServerRequest* req, HttpReturnCode status); |
|---|
| 588 |
|
|---|
| 589 |
const string& name() const { return name_; } |
|---|
| 590 |
const bool dlog_level() const { return dlog_level_; } |
|---|
| 591 |
|
|---|
| 592 |
private: |
|---|
| 593 |
// Prepares what status to return on a parsing error |
|---|
| 594 |
void PrepareErrorRequest(ServerRequest* server_request); |
|---|
| 595 |
// This disposes the request and maybe closes the connection.. |
|---|
| 596 |
void EndRequestProcessing(ServerRequest* req, bool is_eos); |
|---|
| 597 |
// Timeout handling - basically we close the connection |
|---|
| 598 |
void HandleTimeout(int64 timeout_id); |
|---|
| 599 |
|
|---|
| 600 |
// Timeout ids: |
|---|
| 601 |
static const int32 kWriteTimeout = 1; |
|---|
| 602 |
static const int32 kRequestTimeout = 2; |
|---|
| 603 |
|
|---|
| 604 |
string name_; |
|---|
| 605 |
const bool dlog_level_; |
|---|
| 606 |
net::Selector* const selector_; // Our main net thread selector |
|---|
| 607 |
http::Server* const server_; // The server - as above |
|---|
| 608 |
const ServerParams* const protocol_params_; // dictate how we behave |
|---|
| 609 |
net::Timeouter timeouter_; // We set timeouts using this guy |
|---|
| 610 |
http::RequestParser parser_; // Parses requests for us |
|---|
| 611 |
|
|---|
| 612 |
BaseServerConnection* connection_; // The connection that we process |
|---|
| 613 |
|
|---|
| 614 |
http::ServerRequest* crt_recv_; // The request in process of receiving |
|---|
| 615 |
http::ServerRequest* crt_send_; // The request in process of sending |
|---|
| 616 |
|
|---|
| 617 |
typedef set<http::ServerRequest*> RequestSet; |
|---|
| 618 |
RequestSet active_requests_; |
|---|
| 619 |
// The address & port where we received the request. |
|---|
| 620 |
net::HostPort local_address_; |
|---|
| 621 |
// Who is (was) on the other side |
|---|
| 622 |
net::HostPort remote_address_; |
|---|
| 623 |
|
|---|
| 624 |
friend class Server; |
|---|
| 625 |
|
|---|
| 626 |
DISALLOW_EVIL_CONSTRUCTORS(ServerProtocol); |
|---|
| 627 |
}; |
|---|
| 628 |
|
|---|
| 629 |
////////////////////////////////////////////////////////////////////// |
|---|
| 630 |
// |
|---|
| 631 |
// ServerRequest just holds data necessary for processing a request |
|---|
| 632 |
// in the http::Server framework. |
|---|
| 633 |
// It contains the main data structures: |
|---|
| 634 |
// server_ -> the processing http::Server |
|---|
| 635 |
// protocol_ -> manages the client connection |
|---|
| 636 |
// request_ -> the actual request |
|---|
| 637 |
// |
|---|
| 638 |
// Synchronization members: |
|---|
| 639 |
// request_mutex_ -> if you process the request in a different thread |
|---|
| 640 |
// *and* you are using some server side streaming of the reply |
|---|
| 641 |
// you need to lock on this when modifying request_. |
|---|
| 642 |
// !!! Hold this as little as possible and, **very important** |
|---|
| 643 |
// release it before calling the output functions !!! |
|---|
| 644 |
// ready_callback_ -> after streaming some data you can provide an callback |
|---|
| 645 |
// that will be called when some space is available in the |
|---|
| 646 |
// output buffer for client |
|---|
| 647 |
// |
|---|
| 648 |
// State variables: |
|---|
| 649 |
// is_server_streaming_ - this means that the server is sending |
|---|
| 650 |
// the answer in multiple data chunks. |
|---|
| 651 |
// is_server_streaming_chunks_ - this means that the server is |
|---|
| 652 |
// actually encoding the reply in chunks, otherwise the reply |
|---|
| 653 |
// is just a regular reply with no content-length, |
|---|
| 654 |
// terminated by the server closing the connection |
|---|
| 655 |
// is streaming but not using chunks - used only when |
|---|
| 656 |
// is_server_streaming_ is true |
|---|
| 657 |
// is_client_streaming_ - the client sends data in multiple chunks. |
|---|
| 658 |
// when this is on, expect to have your handler called |
|---|
| 659 |
// multiple times with the same ServerRequest |
|---|
| 660 |
// is_keep_alive_ - used internally, this signals a "Keep-Alive" |
|---|
| 661 |
// underline http connection |
|---|
| 662 |
// is_orphaned_ - is turned on by the server for connections that |
|---|
| 663 |
// got closed by the client / timed out or other errors. |
|---|
| 664 |
// When this is on you should stop processing that request and |
|---|
| 665 |
// discard it .. |
|---|
| 666 |
// free_output_bytes_ - you should not output more then these many |
|---|
| 667 |
// bytes in the request_->server_data() as this will be more |
|---|
| 668 |
// then the connection output buffer and discarded / determine |
|---|
| 669 |
// connection closing etc |
|---|
| 670 |
// |
|---|
| 671 |
// The response for a request can come in two modes. The common part is |
|---|
| 672 |
// that the relevant data must be set in request()->server_header_ (control |
|---|
| 673 |
// data, like content type) and request()->server_data_(the actual reply). |
|---|
| 674 |
// |
|---|
| 675 |
class ServerRequest { |
|---|
| 676 |
public: |
|---|
| 677 |
// We are just a placeholder for these things - we do not own them, but |
|---|
| 678 |
// they should be around for the whole life of the request. |
|---|
| 679 |
// There is one exception of this rule: |
|---|
| 680 |
// -- if the connection closes underneath us, we become |
|---|
| 681 |
explicit ServerRequest(http::ServerProtocol* protocol) |
|---|
| 682 |
: protocol_(protocol), |
|---|
| 683 |
request_mutex_(true), |
|---|
| 684 |
request_(protocol->protocol_params()->strict_request_headers_), |
|---|
| 685 |
is_server_streaming_(false), |
|---|
| 686 |
is_server_streaming_chunks_(true), |
|---|
| 687 |
is_client_streaming_(false), |
|---|
| 688 |
is_keep_alive_(false), |
|---|
| 689 |
is_orphaned_(false), |
|---|
| 690 |
is_parsing_finished_(false), |
|---|
| 691 |
free_output_bytes_(protocol->protocol_params()->max_reply_buffer_size_ - |
|---|
| 692 |
protocol->protocol_params()->max_header_size_), |
|---|
| 693 |
ready_callback_(NULL), |
|---|
| 694 |
closed_callback_(NULL), |
|---|
| 695 |
server_callback_(NULL), |
|---|
| 696 |
is_initialized_(false) { |
|---|
| 697 |
} |
|---|
| 698 |
~ServerRequest() { |
|---|
| 699 |
if ( ready_callback_ && !ready_callback_->is_permanent() ) { |
|---|
| 700 |
delete ready_callback_; |
|---|
| 701 |
} |
|---|
| 702 |
if ( closed_callback_ && !closed_callback_->is_permanent() ) { |
|---|
| 703 |
delete closed_callback_; |
|---|
| 704 |
} |
|---|
| 705 |
} |
|---|
| 706 |
////////////////////////////////////////////////////////////////////// |
|---|
| 707 |
// |
|---|
| 708 |
// Accessors |
|---|
| 709 |
// |
|---|
| 710 |
|
|---|
| 711 |
// Use this to access the interesting data in this request (what client |
|---|
| 712 |
// sent) and to write data for the client. |
|---|
| 713 |
// W/ thread pooled requests thread you need to hold request_mutex to |
|---|
| 714 |
// modify this |
|---|
| 715 |
http::Request* request() { return &request_; } |
|---|
| 716 |
|
|---|
| 717 |
// Use this to lock before modifyig the request members in another |
|---|
| 718 |
// thread then the main server network thread |
|---|
| 719 |
synch::Mutex* request_mutex() { return &request_mutex_; } |
|---|
| 720 |
|
|---|
| 721 |
// Use these to inspect the state of the request [these are protected |
|---|
| 722 |
// by request_mutex] |
|---|
| 723 |
|
|---|
| 724 |
// is_server_streaming_ - this means that the server is sending |
|---|
| 725 |
// the answer in multiple data chunks. |
|---|
| 726 |
bool is_server_streaming() const { return is_server_streaming_; } |
|---|
| 727 |
|
|---|
| 728 |
// is_server_streaming_chunks_ - this means that the server is |
|---|
| 729 |
// actually encoding the reply in chunks. |
|---|
| 730 |
bool is_server_streaming_chunks() const { |
|---|
| 731 |
return is_server_streaming_chunks_; |
|---|
| 732 |
} |
|---|
| 733 |
|
|---|
| 734 |
// is_client_streaming_ - the client sends data in multiple chunks. |
|---|
| 735 |
// when this is on, expect to have your handler called |
|---|
| 736 |
// multiple times with the same ServerRequest |
|---|
| 737 |
bool is_client_streaming() const { return is_client_streaming_; } |
|---|
| 738 |
|
|---|
| 739 |
// is_orphaned_ - is turned on by the server for connections that |
|---|
| 740 |
// got closed by the client / timed out or other errors. |
|---|
| 741 |
// When this is on you should stop processing that request and |
|---|
| 742 |
// delete it (for a server streaming request) |
|---|
| 743 |
bool is_orphaned() const { return is_orphaned_; } |
|---|
| 744 |
|
|---|
| 745 |
// free_output_bytes_ - you should not output more then these many |
|---|
| 746 |
// bytes in the request_->server_data() as this will be more |
|---|
| 747 |
// then the connection output buffer and discarded / determine |
|---|
| 748 |
// connection closing etc |
|---|
| 749 |
int32 free_output_bytes() const { return free_output_bytes_; } |
|---|
| 750 |
|
|---|
| 751 |
// Who is on the other side of the wire ? [NOT thread safe w/ request_mutex] |
|---|
| 752 |
const net::HostPort& remote_address() const { |
|---|
| 753 |
return protocol_->remote_address(); |
|---|
| 754 |
} |
|---|
| 755 |
// Local address where we received the request. |
|---|
| 756 |
const net::HostPort& local_address() const { |
|---|
| 757 |
return protocol_->local_address(); |
|---|
| 758 |
} |
|---|
| 759 |
|
|---|
| 760 |
// The closed_callback is called when the peer closes connection |
|---|
| 761 |
// while uploading. Useful in client streaming. |
|---|
| 762 |
// NOTE: If the client request completes, and the server starts sending |
|---|
| 763 |
// back the response, this callback is reset to a server function. |
|---|
| 764 |
void set_closed_callback(Closure* closed_callback) { |
|---|
| 765 |
CHECK_NOT_NULL(closed_callback); |
|---|
| 766 |
CHECK(closed_callback->is_permanent()); |
|---|
| 767 |
CHECK_NULL(closed_callback_); |
|---|
| 768 |
closed_callback_ = closed_callback; |
|---|
| 769 |
} |
|---|
| 770 |
|
|---|
| 771 |
////////////////////////////////////////////////////////////////////// |
|---|
| 772 |
// |
|---|
| 773 |
// Resolving 'non-streaming' (i.e. one answer shot) requests |
|---|
| 774 |
// |
|---|
| 775 |
|
|---|
| 776 |
// Request resolving methods that involve one answer per request |
|---|
| 777 |
// (i.e. no streaming). |
|---|
| 778 |
// Once you call this you should not touch the ServerRequest again |
|---|
| 779 |
// -> is gone |
|---|
| 780 |
void ReplyWithStatus(HttpReturnCode status); |
|---|
| 781 |
void Reply() { |
|---|
| 782 |
ReplyWithStatus(http::OK); |
|---|
| 783 |
} |
|---|
| 784 |
|
|---|
| 785 |
////////////////////////////////////////////////////////////////////// |
|---|
| 786 |
// |
|---|
| 787 |
// Resolving 'steaming' (i.e. data comes to client in multiple shots) |
|---|
| 788 |
// requests |
|---|
| 789 |
// |
|---|
| 790 |
|
|---|
| 791 |
// Request resolving that imply streaming of data on multiple chunks |
|---|
| 792 |
// status should be a 1xx or 2xx code ! |
|---|
| 793 |
// On return you should check the orphaned status of the request |
|---|
| 794 |
// (if is true you better not continue w/ this one). |
|---|
| 795 |
// Anyway, after this call, the request is still in your yard and |
|---|
| 796 |
// you should take care of it. |
|---|
| 797 |
// |
|---|
| 798 |
// [MIHAI] We also allow the code using the server protocol |
|---|
| 799 |
// to stream without encoding the reply in chunks, as this |
|---|
| 800 |
// is valid per the HTTP RFC and there are situations when |
|---|
| 801 |
// this is highly desirable (low latency, small chunks). |
|---|
| 802 |
// |
|---|
| 803 |
// See http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.4 |
|---|
| 804 |
void BeginStreamingData(HttpReturnCode status, |
|---|
| 805 |
Closure* ready_callback, |
|---|
| 806 |
Closure* closed_callback, |
|---|
| 807 |
bool chunked = true); |
|---|
| 808 |
|
|---|
| 809 |
// Contiues outputting data. |
|---|
| 810 |
// On return you should check the orphaned status of the request |
|---|
| 811 |
// (if is true you better not continue w/ this one). |
|---|
| 812 |
// Anyway, after this call, the request is still in your yard and |
|---|
| 813 |
// you should take care of it. |
|---|
| 814 |
void ContinueStreamingData(Closure* ready_callback); |
|---|
| 815 |
|
|---|
| 816 |
// Finishes a streaming request (outputs the last chunk of data). |
|---|
| 817 |
// Upon return the request is gone. You should not touch that again.. |
|---|
| 818 |
void EndStreamingData(); |
|---|
| 819 |
|
|---|
| 820 |
// Detaches this request from fd (check Server to see) |
|---|
| 821 |
net::NetConnection* DetachFromFd() { |
|---|
| 822 |
return protocol_->DetachFromFd(this); |
|---|
| 823 |
} |
|---|
| 824 |
|
|---|
| 825 |
// Flow control functions: |
|---|
| 826 |
void PauseReading() { |
|---|
| 827 |
protocol_->PauseReading(); |
|---|
| 828 |
} |
|---|
| 829 |
void UnpauseReading() { |
|---|
| 830 |
protocol_->UnpauseReading(); |
|---|
| 831 |
} |
|---|
| 832 |
|
|---|
| 833 |
// Helper - clears the close callback (if we desire to close from |
|---|
| 834 |
// another thread |
|---|
| 835 |
void ClearClosedCallback() { |
|---|
| 836 |
synch::MutexLocker l(&request_mutex_); |
|---|
| 837 |
if ( closed_callback_ ) { |
|---|
| 838 |
delete closed_callback_; |
|---|
| 839 |
closed_callback_ = NULL; |
|---|
| 840 |
} |
|---|
| 841 |
} |
|---|
| 842 |
// Authenticates this request against a given authenticator (looking at |
|---|
| 843 |
// the authorization fields / http Basic Authorization fields.. |
|---|
| 844 |
// Returns the authentication status: |
|---|
| 845 |
// -- true - the user is authenticated and is OK. |
|---|
| 846 |
// -- false - the user is noth authenticated - and the proper reply was sent |
|---|
| 847 |
// (401 reply) - you just discard it.. |
|---|
| 848 |
bool AuthenticateRequest(net::UserAuthenticator* authenticator); |
|---|
| 849 |
void AuthenticateRequest( |
|---|
| 850 |
net::UserAuthenticator* authenticator, |
|---|
| 851 |
net::UserAuthenticator::AnswerCallback* answer_callback); |
|---|
| 852 |
void AnswerUnauthorizedRequest(net::UserAuthenticator* authenticator); |
|---|
| 853 |
|
|---|
| 854 |
|
|---|
| 855 |
// Returns a string representation for this request: |
|---|
| 856 |
string ToString() const { |
|---|
| 857 |
return strutil::StringPrintf("HTTP[%s -> %s]", |
|---|
| 858 |
remote_address().ToString().c_str(), |
|---|
| 859 |
request_.url() != NULL |
|---|
| 860 |
? request_.url()->spec().c_str() |
|---|
| 861 |
: "??"); |
|---|
| 862 |
} |
|---|
| 863 |
|
|---|
| 864 |
// If this is on, you will not see any more data on this guy, the parser |
|---|
| 865 |
// is in final state .. |
|---|
| 866 |
bool is_parsing_finished() const { |
|---|
| 867 |
return is_parsing_finished_; |
|---|
| 868 |
} |
|---|
| 869 |
|
|---|
| 870 |
private: |
|---|
| 871 |
////////////////////////////////////////////////////////////////////// |
|---|
| 872 |
// |
|---|
| 873 |
// Internal stuff.. |
|---|
| 874 |
// |
|---|
| 875 |
|
|---|
| 876 |
http::ServerProtocol* protocol() { return protocol_; } |
|---|
| 877 |
bool is_keep_alive() const { return is_keep_alive_; } |
|---|
| 878 |
|
|---|
| 879 |
// Signals that request is ready for more server data by |
|---|
| 880 |
// calling the ready_callback |
|---|
| 881 |
void SignalReady() { |
|---|
| 882 |
Closure* c = NULL; |
|---|
| 883 |
{ |
|---|
| 884 |
synch::MutexLocker l(&request_mutex_); |
|---|
| 885 |
if ( ready_callback_ ) { |
|---|
| 886 |
c = ready_callback_; |
|---|
| 887 |
ready_callback_ = NULL; |
|---|
| 888 |
} |
|---|
| 889 |
} |
|---|
| 890 |
if ( c ) c->Run(); |
|---|
| 891 |
} |
|---|
| 892 |
// Signals that request is done |
|---|
| 893 |
void SignalClosed() { |
|---|
| 894 |
Closure* c = NULL; |
|---|
| 895 |
{ |
|---|
| 896 |
synch::MutexLocker l(&request_mutex_); |
|---|
| 897 |
if ( closed_callback_ ) { |
|---|
| 898 |
c = closed_callback_; |
|---|
| 899 |
closed_callback_ = NULL; |
|---|
| 900 |
} |
|---|
| 901 |
} |
|---|
| 902 |
if ( c ) c->Run(); |
|---|
| 903 |
} |
|---|
| 904 |
// Used *only* for detaching operations ! |
|---|
| 905 |
void ResetProtocol() { |
|---|
| 906 |
protocol_ = NULL; |
|---|
| 907 |
} |
|---|
| 908 |
// Helper used to give ourselves to a processor |
|---|
| 909 |
void ProcessRequest() { |
|---|
| 910 |
server_callback_->Run(this); |
|---|
| 911 |
} |
|---|
| 912 |
|
|---|
| 913 |
// Processors: |
|---|
| 914 |
http::ServerProtocol* protocol_; |
|---|
| 915 |
|
|---|
| 916 |
// Request data |
|---|
| 917 |
synch::Mutex request_mutex_; |
|---|
| 918 |
http::Request request_; |
|---|
| 919 |
|
|---|
| 920 |
// Request state |
|---|
| 921 |
bool is_server_streaming_; |
|---|
| 922 |
bool is_server_streaming_chunks_; |
|---|
| 923 |
bool is_client_streaming_; |
|---|
| 924 |
bool is_keep_alive_; |
|---|
| 925 |
bool is_orphaned_; |
|---|
| 926 |
bool is_parsing_finished_; |
|---|
| 927 |
int32 free_output_bytes_; |
|---|
| 928 |
|
|---|
| 929 |
// Notification of more output buffer |
|---|
| 930 |
Closure* ready_callback_; |
|---|
| 931 |
// Notification of an orhphaned / closed request |
|---|
| 932 |
Closure* closed_callback_; |
|---|
| 933 |
// The callback that processes this event - set and used internally by |
|---|
| 934 |
// the server |
|---|
| 935 |
Server::ServerCallback* server_callback_; |
|---|
| 936 |
// Used internally to signal the header is passed and request has some |
|---|
| 937 |
// specific members set. |
|---|
| 938 |
bool is_initialized_; |
|---|
| 939 |
|
|---|
| 940 |
friend class Server; |
|---|
| 941 |
friend class ServerProtocol; |
|---|
| 942 |
|
|---|
| 943 |
DISALLOW_EVIL_CONSTRUCTORS(ServerRequest); |
|---|
| 944 |
}; |
|---|
| 945 |
|
|---|
| 946 |
////////////////////////////////////////////////////////////////////// |
|---|
| 947 |
// |
|---|
| 948 |
// A simple implementation for a BaseServerConnection - should fit most |
|---|
| 949 |
// desires. |
|---|
| 950 |
// |
|---|
| 951 |
http::BaseServerConnection* |
|---|
| 952 |
SimpleServerConnectionFactory(net::Selector* selector, |
|---|
| 953 |
net::NetConnection* net_connection, |
|---|
| 954 |
http::ServerProtocol* proto); |
|---|
| 955 |
|
|---|
| 956 |
class SimpleServerConnection : public BaseServerConnection { |
|---|
| 957 |
public: |
|---|
| 958 |
explicit SimpleServerConnection(net::Selector* selector, |
|---|
| 959 |
net::NetConnection * net_connection, |
|---|
| 960 |
http::ServerProtocol* protocol); |
|---|
| 961 |
virtual ~SimpleServerConnection(); |
|---|
| 962 |
|
|---|
| 963 |
private: |
|---|
| 964 |
DISALLOW_EVIL_CONSTRUCTORS(SimpleServerConnection); |
|---|
| 965 |
}; |
|---|
| 966 |
} |
|---|
| 967 |
|
|---|
| 968 |
#endif // __NET_HTTP_HTTP_SERVER_PROTOCOL_H__ |
|---|