Create dedicate crate for php_escaping.rs
[hiphop-php.git] / hphp / runtime / server / xbox-server.h
blob5816af26fd65310c9e0c8f523b27e9b1ed430342
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_XBOX_SERVER_H_
18 #define incl_HPHP_XBOX_SERVER_H_
20 #include "hphp/runtime/base/runtime-option.h"
21 #include "hphp/runtime/base/type-string.h"
22 #include "hphp/runtime/server/satellite-server.h"
23 #include "hphp/runtime/server/server-task-event.h"
24 #include "hphp/runtime/server/transport.h"
25 #include "hphp/util/synchronizable.h"
27 namespace HPHP {
28 ///////////////////////////////////////////////////////////////////////////////
30 struct Array;
31 struct XboxServerInfo;
32 struct RPCRequestHandler;
33 struct XboxTransport;
35 struct XboxServer {
36 /**
37 * Start or restart xbox server.
39 static void Restart();
40 static void Stop();
42 public:
43 /**
44 * Send/PostMessage paradigm for local and remote RPC.
46 static bool SendMessage(const String& message,
47 Array& ret,
48 int timeout_ms,
49 const String& host = "localhost");
50 static bool PostMessage(const String& message, const String& host = "localhost");
52 /**
53 * Local tasklet for parallel processing.
55 static Resource TaskStart(const String& msg, const String& reqInitDoc = "",
56 ServerTaskEvent<XboxServer, XboxTransport> *event = nullptr);
57 static void TaskStartFromNonRequest(
58 const folly::StringPiece msg,
59 const folly::StringPiece reqInitDoc = "");
60 static bool TaskStatus(const Resource& task);
61 static int TaskResult(const Resource& task, int timeout_ms, Variant *ret);
62 static int TaskResult(XboxTransport* const job, int timeout_ms, Variant *ret);
64 /**
65 * Gets the ServerInfo and RequestHandler for the current xbox worker thread.
66 * Returns NULL for non-xbox threads.
68 static std::shared_ptr<XboxServerInfo> GetServerInfo();
69 static RPCRequestHandler *GetRequestHandler();
72 ///////////////////////////////////////////////////////////////////////////////
74 struct XboxServerInfo : SatelliteServerInfo {
75 XboxServerInfo() : SatelliteServerInfo(IniSetting::Map::object, Hdf()) {
76 m_type = SatelliteServer::Type::KindOfXboxServer;
77 m_name = "xbox";
78 reload();
81 void reload() {
82 m_threadCount = RuntimeOption::XboxServerThreadCount;
83 m_port = RuntimeOption::XboxServerPort;
84 m_maxRequest = RuntimeOption::XboxServerInfoMaxRequest;
85 m_maxDuration = RuntimeOption::XboxServerInfoDuration;
86 m_reqInitFunc = RuntimeOption::XboxServerInfoReqInitFunc;
87 m_reqInitDoc = RuntimeOption::XboxServerInfoReqInitDoc;
88 m_alwaysReset = RuntimeOption::XboxServerInfoAlwaysReset;
91 void setMaxDuration(int duration) { m_maxDuration = duration; }
94 ///////////////////////////////////////////////////////////////////////////////
96 const StaticString s_xbox("xbox");
98 struct XboxTransport final : Transport, Synchronizable {
99 explicit XboxTransport(
100 const folly::StringPiece message,
101 const folly::StringPiece reqInitDoc = "");
103 timespec getStartTimer() const { return m_queueTime; }
106 * Request URI.
108 const char *getUrl() override;
109 const char *getRemoteHost() override { return "127.0.0.1"; }
110 uint16_t getRemotePort() override { return 0; }
111 const std::string& getServerAddr() override {
112 auto const& ipv4 = RuntimeOption::GetServerPrimaryIPv4();
113 return ipv4.empty() ? RuntimeOption::GetServerPrimaryIPv6() : ipv4;
117 * Request data.
119 Method getMethod() override { return Transport::Method::POST; }
120 const void *getPostData(size_t &size) override {
121 size = m_message.size();
122 return m_message.data();
126 * Manage headers.
128 std::string getHeader(const char *name) override;
129 const HeaderMap& getHeaders() override {
130 static const HeaderMap emptyMap{};
131 return emptyMap;
133 void addHeaderImpl(const char* /*name*/, const char* /*value*/) override {}
134 void removeHeaderImpl(const char* /*name*/) override {}
136 void sendImpl(const void *data, int size, int code, bool chunked, bool eom)
137 override;
138 void onSendEndImpl() override;
141 * Get a description of the type of transport.
143 String describe() const override {
144 return s_xbox;
148 * Task interface.
150 bool isDone() { return m_done; }
151 String getResults(int &code, int timeout_ms = 0);
153 void setHost(const std::string &host) { m_host = host;}
154 void setAsioEvent(ServerTaskEvent<XboxServer, XboxTransport> *event) {
155 m_event = event;
158 // Refcounting.
159 void incRefCount() {
160 ++m_refCount;
162 void decRefCount() {
163 assertx(m_refCount.load());
164 if (--m_refCount == 0) {
165 delete this;
169 private:
170 std::atomic<int> m_refCount;
172 std::string m_message;
174 bool m_done;
175 std::string m_response;
176 int m_code;
177 std::string m_host;
178 std::string m_reqInitDoc;
180 // points to an event with an attached waithandle from a different request
181 ServerTaskEvent<XboxServer, XboxTransport> *m_event;
184 ///////////////////////////////////////////////////////////////////////////////
187 #endif // incl_HPHP_XBOX_SERVER_H_