• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2017 Marshall A. Greenblatt. All rights reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 //    * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //    * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 //    * Neither the name of Google Inc. nor the name Chromium Embedded
14 // Framework nor the names of its contributors may be used to endorse
15 // or promote products derived from this software without specific prior
16 // 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 // ---------------------------------------------------------------------------
31 //
32 // The contents of this file must follow a specific format in order to
33 // support the CEF translator tool. See the translator.README.txt file in the
34 // tools directory for more information.
35 //
36 
37 #ifndef CEF_INCLUDE_CEF_SERVER_H_
38 #define CEF_INCLUDE_CEF_SERVER_H_
39 #pragma once
40 
41 #include <map>
42 #include "include/cef_base.h"
43 #include "include/cef_callback.h"
44 #include "include/cef_request.h"
45 #include "include/cef_task.h"
46 
47 class CefServerHandler;
48 
49 ///
50 // Class representing a server that supports HTTP and WebSocket requests. Server
51 // capacity is limited and is intended to handle only a small number of
52 // simultaneous connections (e.g. for communicating between applications on
53 // localhost). The methods of this class are safe to call from any thread in the
54 // brower process unless otherwise indicated.
55 ///
56 /*--cef(source=library)--*/
57 class CefServer : public CefBaseRefCounted {
58  public:
59   typedef std::multimap<CefString, CefString> HeaderMap;
60 
61   ///
62   // Create a new server that binds to |address| and |port|. |address| must be a
63   // valid IPv4 or IPv6 address (e.g. 127.0.0.1 or ::1) and |port| must be a
64   // port number outside of the reserved range (e.g. between 1025 and 65535 on
65   // most platforms). |backlog| is the maximum number of pending connections.
66   // A new thread will be created for each CreateServer call (the "dedicated
67   // server thread"). It is therefore recommended to use a different
68   // CefServerHandler instance for each CreateServer call to avoid thread safety
69   // issues in the CefServerHandler implementation. The
70   // CefServerHandler::OnServerCreated method will be called on the dedicated
71   // server thread to report success or failure. See
72   // CefServerHandler::OnServerCreated documentation for a description of server
73   // lifespan.
74   ///
75   /*--cef()--*/
76   static void CreateServer(const CefString& address,
77                            uint16 port,
78                            int backlog,
79                            CefRefPtr<CefServerHandler> handler);
80 
81   ///
82   // Returns the task runner for the dedicated server thread.
83   ///
84   /*--cef()--*/
85   virtual CefRefPtr<CefTaskRunner> GetTaskRunner() = 0;
86 
87   ///
88   // Stop the server and shut down the dedicated server thread. See
89   // CefServerHandler::OnServerCreated documentation for a description of
90   // server lifespan.
91   ///
92   /*--cef()--*/
93   virtual void Shutdown() = 0;
94 
95   ///
96   // Returns true if the server is currently running and accepting incoming
97   // connections. See CefServerHandler::OnServerCreated documentation for a
98   // description of server lifespan. This method must be called on the dedicated
99   // server thread.
100   ///
101   /*--cef()--*/
102   virtual bool IsRunning() = 0;
103 
104   ///
105   // Returns the server address including the port number.
106   ///
107   /*--cef()--*/
108   virtual CefString GetAddress() = 0;
109 
110   ///
111   // Returns true if the server currently has a connection. This method must be
112   // called on the dedicated server thread.
113   ///
114   /*--cef()--*/
115   virtual bool HasConnection() = 0;
116 
117   ///
118   // Returns true if |connection_id| represents a valid connection. This method
119   // must be called on the dedicated server thread.
120   ///
121   /*--cef()--*/
122   virtual bool IsValidConnection(int connection_id) = 0;
123 
124   ///
125   // Send an HTTP 200 "OK" response to the connection identified by
126   // |connection_id|. |content_type| is the response content type (e.g.
127   // "text/html"), |data| is the response content, and |data_size| is the size
128   // of |data| in bytes. The contents of |data| will be copied. The connection
129   // will be closed automatically after the response is sent.
130   ///
131   /*--cef()--*/
132   virtual void SendHttp200Response(int connection_id,
133                                    const CefString& content_type,
134                                    const void* data,
135                                    size_t data_size) = 0;
136 
137   ///
138   // Send an HTTP 404 "Not Found" response to the connection identified by
139   // |connection_id|. The connection will be closed automatically after the
140   // response is sent.
141   ///
142   /*--cef()--*/
143   virtual void SendHttp404Response(int connection_id) = 0;
144 
145   ///
146   // Send an HTTP 500 "Internal Server Error" response to the connection
147   // identified by |connection_id|. |error_message| is the associated error
148   // message. The connection will be closed automatically after the response is
149   // sent.
150   ///
151   /*--cef()--*/
152   virtual void SendHttp500Response(int connection_id,
153                                    const CefString& error_message) = 0;
154 
155   ///
156   // Send a custom HTTP response to the connection identified by
157   // |connection_id|. |response_code| is the HTTP response code sent in the
158   // status line (e.g. 200), |content_type| is the response content type sent
159   // as the "Content-Type" header (e.g. "text/html"), |content_length| is the
160   // expected content length, and |extra_headers| is the map of extra response
161   // headers. If |content_length| is >= 0 then the "Content-Length" header will
162   // be sent. If |content_length| is 0 then no content is expected and the
163   // connection will be closed automatically after the response is sent. If
164   // |content_length| is < 0 then no "Content-Length" header will be sent and
165   // the client will continue reading until the connection is closed. Use the
166   // SendRawData method to send the content, if applicable, and call
167   // CloseConnection after all content has been sent.
168   ///
169   /*--cef(optional_param=extra_headers)--*/
170   virtual void SendHttpResponse(int connection_id,
171                                 int response_code,
172                                 const CefString& content_type,
173                                 int64 content_length,
174                                 const HeaderMap& extra_headers) = 0;
175 
176   ///
177   // Send raw data directly to the connection identified by |connection_id|.
178   // |data| is the raw data and |data_size| is the size of |data| in bytes.
179   // The contents of |data| will be copied. No validation of |data| is
180   // performed internally so the client should be careful to send the amount
181   // indicated by the "Content-Length" header, if specified. See
182   // SendHttpResponse documentation for intended usage.
183   ///
184   /*--cef()--*/
185   virtual void SendRawData(int connection_id,
186                            const void* data,
187                            size_t data_size) = 0;
188 
189   ///
190   // Close the connection identified by |connection_id|. See SendHttpResponse
191   // documentation for intended usage.
192   ///
193   /*--cef()--*/
194   virtual void CloseConnection(int connection_id) = 0;
195 
196   ///
197   // Send a WebSocket message to the connection identified by |connection_id|.
198   // |data| is the response content and |data_size| is the size of |data| in
199   // bytes. The contents of |data| will be copied. See
200   // CefServerHandler::OnWebSocketRequest documentation for intended usage.
201   ///
202   /*--cef()--*/
203   virtual void SendWebSocketMessage(int connection_id,
204                                     const void* data,
205                                     size_t data_size) = 0;
206 };
207 
208 ///
209 // Implement this interface to handle HTTP server requests. A new thread will be
210 // created for each CefServer::CreateServer call (the "dedicated server
211 // thread"), and the methods of this class will be called on that thread. It is
212 // therefore recommended to use a different CefServerHandler instance for each
213 // CefServer::CreateServer call to avoid thread safety issues in the
214 // CefServerHandler implementation.
215 ///
216 /*--cef(source=client)--*/
217 class CefServerHandler : public virtual CefBaseRefCounted {
218  public:
219   ///
220   // Called when |server| is created. If the server was started successfully
221   // then CefServer::IsRunning will return true. The server will continue
222   // running until CefServer::Shutdown is called, after which time
223   // OnServerDestroyed will be called. If the server failed to start then
224   // OnServerDestroyed will be called immediately after this method returns.
225   ///
226   /*--cef()--*/
227   virtual void OnServerCreated(CefRefPtr<CefServer> server) = 0;
228 
229   ///
230   // Called when |server| is destroyed. The server thread will be stopped after
231   // this method returns. The client should release any references to |server|
232   // when this method is called. See OnServerCreated documentation for a
233   // description of server lifespan.
234   ///
235   /*--cef()--*/
236   virtual void OnServerDestroyed(CefRefPtr<CefServer> server) = 0;
237 
238   ///
239   // Called when a client connects to |server|. |connection_id| uniquely
240   // identifies the connection. Each call to this method will have a matching
241   // call to OnClientDisconnected.
242   ///
243   /*--cef()--*/
244   virtual void OnClientConnected(CefRefPtr<CefServer> server,
245                                  int connection_id) = 0;
246 
247   ///
248   // Called when a client disconnects from |server|. |connection_id| uniquely
249   // identifies the connection. The client should release any data associated
250   // with |connection_id| when this method is called and |connection_id| should
251   // no longer be passed to CefServer methods. Disconnects can originate from
252   // either the client or the server. For example, the server will disconnect
253   // automatically after a CefServer::SendHttpXXXResponse method is called.
254   ///
255   /*--cef()--*/
256   virtual void OnClientDisconnected(CefRefPtr<CefServer> server,
257                                     int connection_id) = 0;
258 
259   ///
260   // Called when |server| receives an HTTP request. |connection_id| uniquely
261   // identifies the connection, |client_address| is the requesting IPv4 or IPv6
262   // client address including port number, and |request| contains the request
263   // contents (URL, method, headers and optional POST data). Call CefServer
264   // methods either synchronously or asynchronusly to send a response.
265   ///
266   /*--cef()--*/
267   virtual void OnHttpRequest(CefRefPtr<CefServer> server,
268                              int connection_id,
269                              const CefString& client_address,
270                              CefRefPtr<CefRequest> request) = 0;
271 
272   ///
273   // Called when |server| receives a WebSocket request. |connection_id| uniquely
274   // identifies the connection, |client_address| is the requesting IPv4 or
275   // IPv6 client address including port number, and |request| contains the
276   // request contents (URL, method, headers and optional POST data). Execute
277   // |callback| either synchronously or asynchronously to accept or decline the
278   // WebSocket connection. If the request is accepted then OnWebSocketConnected
279   // will be called after the WebSocket has connected and incoming messages will
280   // be delivered to the OnWebSocketMessage callback. If the request is declined
281   // then the client will be disconnected and OnClientDisconnected will be
282   // called. Call the CefServer::SendWebSocketMessage method after receiving the
283   // OnWebSocketConnected callback to respond with WebSocket messages.
284   ///
285   /*--cef()--*/
286   virtual void OnWebSocketRequest(CefRefPtr<CefServer> server,
287                                   int connection_id,
288                                   const CefString& client_address,
289                                   CefRefPtr<CefRequest> request,
290                                   CefRefPtr<CefCallback> callback) = 0;
291 
292   ///
293   // Called after the client has accepted the WebSocket connection for |server|
294   // and |connection_id| via the OnWebSocketRequest callback. See
295   // OnWebSocketRequest documentation for intended usage.
296   ///
297   /*--cef()--*/
298   virtual void OnWebSocketConnected(CefRefPtr<CefServer> server,
299                                     int connection_id) = 0;
300 
301   ///
302   // Called when |server| receives an WebSocket message. |connection_id|
303   // uniquely identifies the connection, |data| is the message content and
304   // |data_size| is the size of |data| in bytes. Do not keep a reference to
305   // |data| outside of this method. See OnWebSocketRequest documentation for
306   // intended usage.
307   ///
308   /*--cef()--*/
309   virtual void OnWebSocketMessage(CefRefPtr<CefServer> server,
310                                   int connection_id,
311                                   const void* data,
312                                   size_t data_size) = 0;
313 };
314 
315 #endif  // CEF_INCLUDE_CEF_SERVER_H_
316