• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 WEBSERVER_WEBSERVD_CONFIG_H_
16 #define WEBSERVER_WEBSERVD_CONFIG_H_
17 
18 #include <string>
19 #include <vector>
20 
21 #include <base/files/file_path.h>
22 #include <brillo/errors/error.h>
23 #include <brillo/secure_blob.h>
24 
25 namespace webservd {
26 
27 extern const char kDefaultLogDirectory[];
28 
29 // This class contains global server configuration.
30 struct Config final {
31  public:
32   // Configuration of one specific protocol handler.
33   struct ProtocolHandler final {
34     ~ProtocolHandler();
35     // Protocol Handler Name.
36     std::string name;
37     // Port to use.
38     uint16_t port{0};
39     // Specifies whether the handler is for HTTPS (true) or HTTP (false).
40     bool use_tls{false};
41     // Interface name to use if the protocol handler should work only on
42     // particular network interface. If empty, the TCP socket will be open
43     // on the specified port for all network interfaces.
44     std::string interface_name;
45     // For HTTPS handlers, these specify the certificates/private keys used
46     // during TLS handshake and communication session. For HTTP protocol
47     // handlers these fields are not used and are empty.
48     brillo::SecureBlob private_key;
49     brillo::Blob certificate;
50     brillo::Blob certificate_fingerprint;
51 
52     // Custom socket created for protocol handlers that are bound to specific
53     // network interfaces only. SO_BINDTODEVICE option on a socket does exactly
54     // what is required but it needs root access. So we create those sockets
55     // before we drop privileges.
56     int socket_fd{-1};
57   };
58 
59   // List of all registered protocol handlers for the web server.
60   std::vector<ProtocolHandler> protocol_handlers;
61 
62   // Specifies whether additional debugging information should be included.
63   // When set, this turns out additional diagnostic logging in libmicrohttpd as
64   // well as includes additional information in error responses delivered to
65   // HTTP clients.
66   bool use_debug{false};
67 
68   // Specifies whether IPv6 is enabled and should be used by the server.
69   bool use_ipv6{true};
70 
71   // Output directory for web server's request log in Common Log Format
72   // (see http://www.w3.org/Daemon/User/Config/Logging.html).
73   // The files in this directory contain only the "official" request logs, not
74   // general logging messages from the webserver, which still go to the standard
75   // system log.
76   std::string log_directory{kDefaultLogDirectory};
77 
78   // Default request timeout (in seconds).
79   int default_request_timeout_seconds{60};
80 };
81 
82 // Initializes the config with default preset settings (two handlers, one for
83 // HTTP on port 80 and one for HTTPS on port 443).
84 void LoadDefaultConfig(Config* config);
85 
86 // Loads server configuration form specified file. The file is expected
87 // to exist and contain a valid configuration in JSON format.
88 // Returns false on error (whether opening/reading the file or parsing JSON
89 // content).
90 bool LoadConfigFromFile(const base::FilePath& json_file_path, Config* config);
91 
92 // Loads the configuration from a string containing JSON data.
93 // In case of parsing or configuration validation errors, returns false and
94 // specifies the reason for the failure in |error| object.
95 bool LoadConfigFromString(const std::string& config_json,
96                           Config* config,
97                           brillo::ErrorPtr* error);
98 
99 }  // namespace webservd
100 
101 #endif  // WEBSERVER_WEBSERVD_CONFIG_H_
102