• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <base/bind.h>
16 #include <base/macros.h>
17 #include <brillo/mime_utils.h>
18 #include <brillo/syslog_logging.h>
19 #include <libwebserv/protocol_handler.h>
20 #include <libwebserv/request_handler_interface.h>
21 #include <libwebserv/server.h>
22 #include <sysexits.h>
23 
24 #if defined(WEBSERV_USE_DBUS)
25 
26 #include <brillo/daemons/dbus_daemon.h>
27 #include <brillo/dbus/async_event_sequencer.h>
28 
29 // If we're using DBus, pick a base class that does DBus related init.
30 using WebservTestClientBaseClass = brillo::DBusDaemon;
31 using brillo::dbus_utils::AsyncEventSequencer;
32 
33 #elif defined(WEBSERV_USE_BINDER)
34 
35 #include <brillo/daemons/daemon.h>
36 using WebservTestClientBaseClass = brillo::Daemon;
37 
38 #else
39 #error "You must select one of Binder or DBus as an RPC mechanism."
40 #endif  // defined(WEBSERV_USE_DBUS)
41 
42 using libwebserv::Server;
43 using libwebserv::ProtocolHandler;
44 using libwebserv::RequestHandlerInterface;
45 using libwebserv::Request;
46 using libwebserv::Response;
47 
48 namespace {
49 
LogServerOnlineStatus(bool online)50 void LogServerOnlineStatus(bool online) {
51   LOG(INFO) << "Webserver is "
52             << ((online) ? "online" : "offline");
53 }
54 
55 class PingRequestHandler : public RequestHandlerInterface {
56  public:
57   static const char kMethods[];
58   static const char kResponse[];
59   static const char kUrl[];
60 
61   ~PingRequestHandler() override = default;
HandleRequest(std::unique_ptr<Request>,std::unique_ptr<Response> response)62   void HandleRequest(std::unique_ptr<Request> /* request */,
63                      std::unique_ptr<Response> response) override {
64     response->ReplyWithText(200, kResponse, brillo::mime::text::kPlain);
65   }
66 };  // class PingRequestHandler
67 
68 const char PingRequestHandler::kMethods[] = "";  // all methods
69 const char PingRequestHandler::kResponse[] = "Still alive, still alive!\n";
70 const char PingRequestHandler::kUrl[] = "/webservd-test-client/ping";
71 
72 class WebservTestClient : public WebservTestClientBaseClass {
73  public:
74   WebservTestClient() = default;
75   ~WebservTestClient() override = default;
76 
77  protected:
OnInit()78   int OnInit() override {
79     int exit_code = WebservTestClientBaseClass::OnInit();
80     if (exit_code != EX_OK)
81       return exit_code;
82 
83 #ifdef WEBSERV_USE_DBUS
84     webserver_ = Server::ConnectToServerViaDBus(
85         bus_, bus_->GetConnectionName(),
86         AsyncEventSequencer::GetDefaultCompletionAction(),
87         base::Bind(&LogServerOnlineStatus, true /* online */),
88         base::Bind(&LogServerOnlineStatus, false /* offline */));
89 #elif WEBSERV_USE_BINDER
90     webserver_ = Server::ConnectToServerViaBinder(
91         message_loop(),
92         base::Bind(&LogServerOnlineStatus, true /* online */),
93         base::Bind(&LogServerOnlineStatus, false /* offline */));
94 #endif  // WEBSERV_USE_DBUS || WEBSERV_USE_BINDER
95 
96     // Note that adding this handler is only local, and we won't receive
97     // requests until the library does some async book keeping.
98     ProtocolHandler* http_handler = webserver_->GetDefaultHttpHandler();
99     http_handler->AddHandler(
100         PingRequestHandler::kUrl,
101         PingRequestHandler::kMethods,
102         std::unique_ptr<RequestHandlerInterface>(new PingRequestHandler()));
103 
104     return exit_code;
105   }
106 
107  private:
108   std::unique_ptr<Server> webserver_;
109 
110   DISALLOW_COPY_AND_ASSIGN(WebservTestClient);
111 };  // class WebservTestClient
112 
113 }  // namespace
114 
main(int,char * [])115 int main(int /* argc */, char* /* argv */[]) {
116   brillo::InitLog(brillo::kLogToSyslog | brillo::kLogHeader);
117   WebservTestClient client;
118   return client.Run();
119 }
120