Vitaly Buka | cad20f0 | 2015-10-16 17:27:15 -0700 | [diff] [blame] | 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. |
Vitaly Buka | 974647f | 2015-08-02 20:57:43 -0700 | [diff] [blame] | 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> |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 23 | #include <weave/provider/http_server.h> |
Vitaly Buka | 974647f | 2015-08-02 20:57:43 -0700 | [diff] [blame] | 24 | |
| 25 | namespace dbus { |
| 26 | class Bus; |
| 27 | } |
| 28 | |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 29 | namespace brillo { |
Vitaly Buka | 974647f | 2015-08-02 20:57:43 -0700 | [diff] [blame] | 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. |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 45 | class WebServClient : public weave::provider::HttpServer { |
Vitaly Buka | 974647f | 2015-08-02 20:57:43 -0700 | [diff] [blame] | 46 | public: |
| 47 | WebServClient(const scoped_refptr<dbus::Bus>& bus, |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 48 | brillo::dbus_utils::AsyncEventSequencer* sequencer, |
Alex Vakulenko | be39e93 | 2015-10-09 08:10:36 -0700 | [diff] [blame] | 49 | const base::Closure& server_available_callback); |
Vitaly Buka | 974647f | 2015-08-02 20:57:43 -0700 | [diff] [blame] | 50 | ~WebServClient() override; |
| 51 | |
| 52 | // HttpServer implementation. |
Alex Vakulenko | be39e93 | 2015-10-09 08:10:36 -0700 | [diff] [blame] | 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 | |
Vitaly Buka | 974647f | 2015-08-02 20:57:43 -0700 | [diff] [blame] | 58 | uint16_t GetHttpPort() const override; |
| 59 | uint16_t GetHttpsPort() const override; |
Alex Vakulenko | a071cd9 | 2015-11-13 16:11:17 -0800 | [diff] [blame] | 60 | base::TimeDelta GetRequestTimeout() const override; |
Alex Vakulenko | be39e93 | 2015-10-09 08:10:36 -0700 | [diff] [blame] | 61 | std::vector<uint8_t> GetHttpsCertificateFingerprint() const override; |
Vitaly Buka | 974647f | 2015-08-02 20:57:43 -0700 | [diff] [blame] | 62 | |
| 63 | private: |
Alex Vakulenko | be39e93 | 2015-10-09 08:10:36 -0700 | [diff] [blame] | 64 | void OnRequest(const RequestHandlerCallback& callback, |
Vitaly Buka | 974647f | 2015-08-02 20:57:43 -0700 | [diff] [blame] | 65 | std::unique_ptr<libwebserv::Request> request, |
| 66 | std::unique_ptr<libwebserv::Response> response); |
| 67 | |
Vitaly Buka | 974647f | 2015-08-02 20:57:43 -0700 | [diff] [blame] | 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}; |
Vitaly Buka | 097cbd7 | 2015-08-13 18:28:14 -0700 | [diff] [blame] | 76 | std::vector<uint8_t> certificate_; |
Vitaly Buka | 974647f | 2015-08-02 20:57:43 -0700 | [diff] [blame] | 77 | |
Vitaly Buka | 974647f | 2015-08-02 20:57:43 -0700 | [diff] [blame] | 78 | std::unique_ptr<libwebserv::Server> web_server_; |
Alex Vakulenko | be39e93 | 2015-10-09 08:10:36 -0700 | [diff] [blame] | 79 | base::Closure server_available_callback_; |
Vitaly Buka | 974647f | 2015-08-02 20:57:43 -0700 | [diff] [blame] | 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_ |