forked from cpv-project/cpv-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpContext.hpp
81 lines (68 loc) · 2.46 KB
/
HttpContext.hpp
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
#pragma once
#include <seastar/net/api.hh>
#include "../Container/Container.hpp"
#include "../Container/Container.hpp"
#include "../Container/ServiceStorage.hpp"
#include "../Http/HttpRequest.hpp"
#include "../Http/HttpResponse.hpp"
namespace cpv {
/** The context type for handling single http request on http server */
class HttpContext {
public:
/** Get the request received from http client */
HttpRequest& getRequest() & { return request_; }
const HttpRequest& getRequest() const& { return request_; }
/** Get the response reply to http client */
HttpResponse& getResponse() & { return response_; }
const HttpResponse& getResponse() const& { return response_; }
/** Get the socket address of http client */
const seastar::socket_address& getClientAddress() const& { return clientAddress_; }
/** Get service instance with service storage associated with this context */
template <class TService>
TService getService() const {
return container_.get<TService>(serviceStorage_);
}
/** Get service instances with service storage associated with this context */
template <class T, std::enable_if_t<ServiceTypeTrait<T>::IsCollection, int> = 0>
std::size_t getManyServices(T& collection) const {
return container_.getMany<T>(collection, serviceStorage_);
}
/** Update the request and the response in this context */
void setRequestResponse(HttpRequest&& request, HttpResponse&& response) {
request_ = std::move(request);
response_ = std::move(response);
}
/** Set the socket address of http client */
void setClientAddress(seastar::socket_address&& clientAddress) {
clientAddress_ = std::move(clientAddress);
}
/** Update the container in this context */
void setContainer(const Container& container) {
container_ = container;
}
/** Clear the service storage, usually you should call it after setRequestResponse */
void clearServiceStorage() {
serviceStorage_.clear();
}
/** Constructor */
HttpContext() :
request_(),
response_(),
clientAddress_(seastar::make_ipv4_address(0, 0)),
container_(),
serviceStorage_() { }
/** Constructor for null context, should set members later */
explicit HttpContext(nullptr_t) :
request_(nullptr),
response_(nullptr),
clientAddress_(),
container_(nullptr),
serviceStorage_() { }
private:
HttpRequest request_;
HttpResponse response_;
seastar::socket_address clientAddress_;
Container container_;
mutable ServiceStorage serviceStorage_;
};
}