blob: 8b0ab7cbeb8f9fb9cd7b5e946d1306e1bb7b811b [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 Vakulenkobe39e932015-10-09 08:10:36 -070060 std::vector<uint8_t> GetHttpsCertificateFingerprint() const override;
Vitaly Buka974647f2015-08-02 20:57:43 -070061
62 private:
Alex Vakulenkobe39e932015-10-09 08:10:36 -070063 void OnRequest(const RequestHandlerCallback& callback,
Vitaly Buka974647f2015-08-02 20:57:43 -070064 std::unique_ptr<libwebserv::Request> request,
65 std::unique_ptr<libwebserv::Response> response);
66
67 void OnResponse(std::unique_ptr<libwebserv::Response> response,
68 int status_code,
69 const std::string& data,
70 const std::string& mime_type);
71
72 void OnProtocolHandlerConnected(
73 libwebserv::ProtocolHandler* protocol_handler);
74
75 void OnProtocolHandlerDisconnected(
76 libwebserv::ProtocolHandler* protocol_handler);
77
78 uint16_t http_port_{0};
79 uint16_t https_port_{0};
Vitaly Buka097cbd72015-08-13 18:28:14 -070080 std::vector<uint8_t> certificate_;
Vitaly Buka974647f2015-08-02 20:57:43 -070081
Vitaly Buka974647f2015-08-02 20:57:43 -070082 std::unique_ptr<libwebserv::Server> web_server_;
Alex Vakulenkobe39e932015-10-09 08:10:36 -070083 base::Closure server_available_callback_;
Vitaly Buka974647f2015-08-02 20:57:43 -070084
85 base::WeakPtrFactory<WebServClient> weak_ptr_factory_{this};
86 DISALLOW_COPY_AND_ASSIGN(WebServClient);
87};
88
89} // namespace buffet
90
91#endif // BUFFET_WEBSERV_CLIENT_H_