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