Home | History | Annotate | Download | only in buffet
      1 // Copyright 2015 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 #ifndef BUFFET_WEBSERV_CLIENT_H_
     16 #define BUFFET_WEBSERV_CLIENT_H_
     17 
     18 #include <memory>
     19 #include <string>
     20 #include <vector>
     21 
     22 #include <base/memory/weak_ptr.h>
     23 #include <weave/provider/http_server.h>
     24 
     25 namespace dbus {
     26 class Bus;
     27 }
     28 
     29 namespace brillo {
     30 namespace dbus_utils {
     31 class AsyncEventSequencer;
     32 }
     33 }
     34 
     35 namespace libwebserv {
     36 class ProtocolHandler;
     37 class Request;
     38 class Response;
     39 class Server;
     40 }
     41 
     42 namespace buffet {
     43 
     44 // Wrapper around libwebserv that implements HttpServer interface.
     45 class WebServClient : public weave::provider::HttpServer {
     46  public:
     47   WebServClient(const scoped_refptr<dbus::Bus>& bus,
     48                 brillo::dbus_utils::AsyncEventSequencer* sequencer,
     49                 const base::Closure& server_available_callback);
     50   ~WebServClient() override;
     51 
     52   // HttpServer implementation.
     53   void AddHttpRequestHandler(const std::string& path,
     54                              const RequestHandlerCallback& callback) override;
     55   void AddHttpsRequestHandler(const std::string& path,
     56                               const RequestHandlerCallback& callback) override;
     57 
     58   uint16_t GetHttpPort() const override;
     59   uint16_t GetHttpsPort() const override;
     60   base::TimeDelta GetRequestTimeout() const override;
     61   std::vector<uint8_t> GetHttpsCertificateFingerprint() const override;
     62 
     63  private:
     64   void OnRequest(const RequestHandlerCallback& callback,
     65                  std::unique_ptr<libwebserv::Request> request,
     66                  std::unique_ptr<libwebserv::Response> response);
     67 
     68   void OnProtocolHandlerConnected(
     69       libwebserv::ProtocolHandler* protocol_handler);
     70 
     71   void OnProtocolHandlerDisconnected(
     72       libwebserv::ProtocolHandler* protocol_handler);
     73 
     74   uint16_t http_port_{0};
     75   uint16_t https_port_{0};
     76   std::vector<uint8_t> certificate_;
     77 
     78   std::unique_ptr<libwebserv::Server> web_server_;
     79   base::Closure server_available_callback_;
     80 
     81   base::WeakPtrFactory<WebServClient> weak_ptr_factory_{this};
     82   DISALLOW_COPY_AND_ASSIGN(WebServClient);
     83 };
     84 
     85 }  // namespace buffet
     86 
     87 #endif  // BUFFET_WEBSERV_CLIENT_H_
     88