Repository navigation
Expand file tree
/
Copy pathRequestHandlerFactory.h
More file actions
76 lines (65 loc) · 2.26 KB
/
Copy pathRequestHandlerFactory.h
File metadata and controls
76 lines (65 loc) · 2.26 KB
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
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#pragma once
#include "proxygen/httpserver/RequestHandler.h"
namespace proxygen {
/**
* Factory for RequestHandlers.
*/
class RequestHandlerFactory {
public:
virtual ~RequestHandlerFactory() {}
/**
* Invoked in each thread server is going to handle requests
* before we start handling requests. Can be used to setup
* thread-local setup for each thread (stats and such).
*/
virtual void onServerStart() noexcept = 0;
/**
* Invoked in each handler thread after all the connections are
* drained from that thread. Can be used to tear down thread-local setup.
*/
virtual void onServerStop() noexcept = 0;
/**
* Invoked for each new request server handles. HTTPMessage is provided
* so that user can potentially choose among several implementation of
* handler based on URL or something. No, need to save/copy this
* HTTPMessage. RequestHandler will be given the HTTPMessage
* in a separate callback.
*
* Some request handlers don't handle the request themselves (think filters).
* They do take some actions based on request/response but otherwise they
* just hand-off request to some other RequestHandler. This upstream
* RequestHandler is given as first parameter. For the terminal RequestHandler
* this will by nullptr.
*/
virtual RequestHandler* onRequest(RequestHandler*, HTTPMessage*) noexcept = 0;
};
/**
* Helper class to help beautify the way we make chains of these filters
*/
class RequestHandlerChain {
public:
std::vector<std::unique_ptr<RequestHandlerFactory>> build() {
return std::move(chain_);
}
template <typename T, typename... Args>
RequestHandlerChain& addThen(Args&&... args) {
chain_.push_back(folly::make_unique<T>(std::forward<Args>(args)...));
return *this;
}
RequestHandlerChain& addThen(std::unique_ptr<RequestHandlerFactory> h) {
chain_.push_back(std::move(h));
return *this;
}
private:
std::vector<std::unique_ptr<RequestHandlerFactory>> chain_;
};
}