blob: d7d9aea2aea18a4d45ed23403b0ee579d66b707f [file] [log] [blame]
// Copyright 2015 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include <vector>
#include <base/files/file_path.h>
#include <brillo/errors/error.h>
#include <brillo/secure_blob.h>
namespace webservd {
extern const char kDefaultLogDirectory[];
// This class contains global server configuration.
struct Config final {
// Configuration of one specific protocol handler.
struct ProtocolHandler final {
// Protocol Handler Name.
std::string name;
// Port to use.
uint16_t port{0};
// Specifies whether the handler is for HTTPS (true) or HTTP (false).
bool use_tls{false};
// Interface name to use if the protocol handler should work only on
// particular network interface. If empty, the TCP socket will be open
// on the specified port for all network interfaces.
std::string interface_name;
// For HTTPS handlers, these specify the certificates/private keys used
// during TLS handshake and communication session. For HTTP protocol
// handlers these fields are not used and are empty.
brillo::SecureBlob private_key;
brillo::Blob certificate;
brillo::Blob certificate_fingerprint;
// Custom socket created for protocol handlers that are bound to specific
// network interfaces only. SO_BINDTODEVICE option on a socket does exactly
// what is required but it needs root access. So we create those sockets
// before we drop privileges.
int socket_fd{-1};
// List of all registered protocol handlers for the web server.
std::vector<ProtocolHandler> protocol_handlers;
// Specifies whether additional debugging information should be included.
// When set, this turns out additional diagnostic logging in libmicrohttpd as
// well as includes additional information in error responses delivered to
// HTTP clients.
bool use_debug{false};
// Specifies whether IPv6 is enabled and should be used by the server.
bool use_ipv6{true};
// Output directory for web server's request log in Common Log Format
// (see
// The files in this directory contain only the "official" request logs, not
// general logging messages from the webserver, which still go to the standard
// system log.
std::string log_directory{kDefaultLogDirectory};
// Default request timeout (in seconds).
int default_request_timeout_seconds{60};
// Initializes the config with default preset settings (two handlers, one for
// HTTP on port 80 and one for HTTPS on port 443).
void LoadDefaultConfig(Config* config);
// Loads server configuration form specified file. The file is expected
// to exist and contain a valid configuration in JSON format.
// Returns false on error (whether opening/reading the file or parsing JSON
// content).
bool LoadConfigFromFile(const base::FilePath& json_file_path, Config* config);
// Loads the configuration from a string containing JSON data.
// In case of parsing or configuration validation errors, returns false and
// specifies the reason for the failure in |error| object.
bool LoadConfigFromString(const std::string& config_json,
Config* config,
brillo::ErrorPtr* error);
} // namespace webservd