blob: 1c0d44c5523b7ca42801bc519da74b37351f89fb [file] [log] [blame]
Vitaly Bukacad20f02015-10-16 17:27:15 -07001// 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.
Vitaly Buka974647f2015-08-02 20:57:43 -070014
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>
Alex Vakulenkoe32375b2015-09-28 08:55:40 -070023#include <weave/provider/http_server.h>
Vitaly Buka974647f2015-08-02 20:57:43 -070024
25namespace dbus {
26class Bus;
27}
28
Alex Vakulenko41705852015-10-13 10:12:06 -070029namespace brillo {
Vitaly Buka974647f2015-08-02 20:57:43 -070030namespace dbus_utils {
31class AsyncEventSequencer;
32}
33}
34
35namespace libwebserv {
36class ProtocolHandler;
37class Request;
38class Response;
39class Server;
40}
41
42namespace buffet {
43
44// Wrapper around libwebserv that implements HttpServer interface.
Alex Vakulenkoe32375b2015-09-28 08:55:40 -070045class WebServClient : public weave::provider::HttpServer {
Vitaly Buka974647f2015-08-02 20:57:43 -070046 public:
47 WebServClient(const scoped_refptr<dbus::Bus>& bus,
Alex Vakulenko41705852015-10-13 10:12:06 -070048 brillo::dbus_utils::AsyncEventSequencer* sequencer,
Alex Vakulenkobe39e932015-10-09 08:10:36 -070049 const base::Closure& server_available_callback);
Vitaly Buka974647f2015-08-02 20:57:43 -070050 ~WebServClient() override;
51
52 // HttpServer implementation.
Alex Vakulenkobe39e932015-10-09 08:10:36 -070053 void AddHttpRequestHandler(const std::string& path,
54 const RequestHandlerCallback& callback) override;
55 void AddHttpsRequestHandler(const std::string& path,
56 const RequestHandlerCallback& callback) override;
57
Vitaly Buka974647f2015-08-02 20:57:43 -070058 uint16_t GetHttpPort() const override;
59 uint16_t GetHttpsPort() const override;
Alex Vakulenkoa071cd92015-11-13 16:11:17 -080060 base::TimeDelta GetRequestTimeout() const override;
Alex Vakulenkobe39e932015-10-09 08:10:36 -070061 std::vector<uint8_t> GetHttpsCertificateFingerprint() const override;
Vitaly Buka974647f2015-08-02 20:57:43 -070062
63 private:
Alex Vakulenkobe39e932015-10-09 08:10:36 -070064 void OnRequest(const RequestHandlerCallback& callback,
Vitaly Buka974647f2015-08-02 20:57:43 -070065 std::unique_ptr<libwebserv::Request> request,
66 std::unique_ptr<libwebserv::Response> response);
67
Vitaly Buka974647f2015-08-02 20:57:43 -070068 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};
Vitaly Buka097cbd72015-08-13 18:28:14 -070076 std::vector<uint8_t> certificate_;
Vitaly Buka974647f2015-08-02 20:57:43 -070077
Vitaly Buka974647f2015-08-02 20:57:43 -070078 std::unique_ptr<libwebserv::Server> web_server_;
Alex Vakulenkobe39e932015-10-09 08:10:36 -070079 base::Closure server_available_callback_;
Vitaly Buka974647f2015-08-02 20:57:43 -070080
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_