Skip to content

Commit b07484d

Browse files
Allow a built-in default page to be set and used when one is not
configured. Do not require secure settings, unless configured.
1 parent 79c33b6 commit b07484d

File tree

5 files changed

+83
-49
lines changed

5 files changed

+83
-49
lines changed

include/bitcoin/protocol/web/manager.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ class BCP_API manager
7272
const origin_list origins);
7373
~manager();
7474

75+
// If the endpoint is reached and no default page can be found,
76+
// send a response that includes this data.
77+
void set_default_page_data(const std::string& data);
78+
7579
bool initialize();
7680
bool bind(const system::config::endpoint& address,
7781
const bind_options& options);
@@ -141,6 +145,7 @@ class BCP_API manager
141145
system::shared_mutex task_mutex_;
142146

143147
const origin_list origins_;
148+
std::string page_data_;
144149
};
145150

146151
} // namespace http

include/bitcoin/protocol/web/socket.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ class BCP_API socket
129129
// Send a message to every connected websocket client.
130130
void broadcast(const std::string& json);
131131

132+
// Optionally set a fixed reply instead of returning a 404 not found if
133+
// the web root cannot be found.
134+
void set_default_page_data(const std::string& data);
135+
132136
// The zmq socket operates on only this one thread.
133137
bc::protocol::zmq::context& context_;
134138
const bool secure_;

src/settings.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@ settings::settings()
3737
send_milliseconds(0),
3838
web_priority(false),
3939
web_origins({}),
40-
web_root("web"),
40+
web_root(""),
4141
web_ca_certificate(""),
42-
web_server_private_key("key.pem"),
43-
web_server_certificate("server.pem"),
44-
web_client_certificates("clients")
42+
web_server_private_key(""),
43+
web_server_certificate("")
4544
{
4645
}
4746

@@ -56,11 +55,10 @@ settings::settings(uint32_t send_high_water, uint32_t receive_high_water)
5655
send_milliseconds(0),
5756
web_priority(false),
5857
web_origins({}),
59-
web_root("web"),
58+
web_root(""),
6059
web_ca_certificate(""),
61-
web_server_private_key("key.pem"),
62-
web_server_certificate("server.pem"),
63-
web_client_certificates("clients")
60+
web_server_private_key(""),
61+
web_server_certificate("")
6462
{
6563
}
6664

src/web/manager.cpp

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ manager::manager(bool ssl, event_handler handler, path document_root,
6161
ca_certificate_{},
6262
handler_(handler),
6363
document_root_(document_root),
64-
origins_(origins)
64+
origins_(origins),
65+
page_data_{}
6566
{
6667
#ifndef WITH_MBEDTLS
6768
BITCOIN_ASSERT_MSG(!ssl, "Secure HTTP requires MBEDTLS library.");
@@ -76,6 +77,11 @@ manager::~manager()
7677
#endif
7778
}
7879

80+
void manager::set_default_page_data(const std::string& data)
81+
{
82+
page_data_ = data;
83+
}
84+
7985
// Initialize is not thread safe.
8086
bool manager::initialize()
8187
{
@@ -1027,32 +1033,35 @@ bool manager::send_response(connection_ptr connection,
10271033
{
10281034
auto path = document_root_;
10291035

1030-
if (request.uri == "/")
1036+
if (!document_root_.empty())
10311037
{
1032-
static const std::vector<boost::filesystem::path> index_files
1038+
if (request.uri == "/")
10331039
{
1034-
{ "index.html" },
1035-
{ "index.htm" },
1036-
{ "index.shtml" }
1037-
};
1040+
static const std::vector<boost::filesystem::path> index_files
1041+
{
1042+
{ "index.html" },
1043+
{ "index.htm" },
1044+
{ "index.shtml" }
1045+
};
10381046

1039-
for (const auto& index: index_files)
1040-
{
1041-
const auto test_path = path / index;
1042-
if (boost::filesystem::exists(test_path))
1047+
for (const auto& index: index_files)
10431048
{
1044-
path = test_path;
1045-
break;
1049+
const auto test_path = path / index;
1050+
if (boost::filesystem::exists(test_path))
1051+
{
1052+
path = test_path;
1053+
break;
1054+
}
10461055
}
1047-
}
10481056

1049-
if (path == document_root_)
1050-
return false;
1051-
}
1052-
else
1053-
{
1054-
// BUGBUG: sanitize path to guard against information leak.
1055-
path /= request.uri;
1057+
if (path == document_root_)
1058+
return false;
1059+
}
1060+
else
1061+
{
1062+
// BUGBUG: sanitize path to guard against information leak.
1063+
path /= request.uri;
1064+
}
10561065
}
10571066

10581067
if (!boost::filesystem::exists(path))
@@ -1069,15 +1078,29 @@ bool manager::send_response(connection_ptr connection,
10691078
std::strftime(time_buffer.data(), time_buffer.size(),
10701079
"%a, %d %b %Y %H:%M:%S GMT", std::gmtime(&current_time));
10711080

1072-
const auto response = std::string(
1073-
"HTTP/1.1 404 Not Found\r\n"
1074-
"Date: ") + time_buffer.data() + std::string("\r\n"
1075-
"Content-Type: text/html\r\n"
1076-
"Content-Length: 90\r\n\r\n"
1077-
"<html><head><title>Page not found</title></head>"
1078-
"<body>The page was not found.</body></html>\r\n\r\n");
1081+
std::stringstream response;
1082+
1083+
if (page_data_.empty())
1084+
{
1085+
response
1086+
<< "HTTP/1.1 404 Not Found\r\n"
1087+
<< "Date: " << time_buffer.data() << "\r\n"
1088+
<< "Content-Type: text/html\r\n"
1089+
<< "Content-Length: 90\r\n\r\n"
1090+
<< "<html><head><title>Page not found</title></head>"
1091+
<< "<body>The page was not found.</body></html>\r\n\r\n";
1092+
}
1093+
else
1094+
{
1095+
response
1096+
<< "HTTP/1.0 200 OK\r\n"
1097+
<< "Date: " << time_buffer.data() << "\r\n"
1098+
<< "Content-Type: text/html\r\n"
1099+
<< "Content-Length: " << page_data_.size() << "\r\n\r\n"
1100+
<< page_data_ << "\r\n\r\n";
1101+
}
10791102

1080-
connection->write(response);
1103+
connection->write(response.str());
10811104
return true;
10821105
}
10831106

src/web/socket.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ socket::socket(zmq::context& context, const protocol::settings& settings,
359359

360360
bool socket::start()
361361
{
362-
if (!exists(settings_.web_root))
362+
if (!settings_.web_root.empty() && !exists(settings_.web_root))
363363
{
364364
LOG_ERROR(LOG_PROTOCOL)
365365
<< "Configured HTTP root path '" << settings_.web_root
@@ -370,31 +370,30 @@ bool socket::start()
370370
if (secure_)
371371
{
372372
#ifdef WITH_MBEDTLS
373-
if (!settings_.web_ca_certificate.generic_string().empty() &&
373+
if (!settings_.web_ca_certificate.empty() &&
374374
!exists(settings_.web_server_certificate))
375375
{
376376
LOG_ERROR(LOG_PROTOCOL)
377-
<< "Requested CA certificate '"
378-
<< settings_.web_ca_certificate
377+
<< "Configured CA certificate '" << settings_.web_ca_certificate
379378
<< "' does not exist.";
380379
return false;
381380
}
382381

383-
if (!exists(settings_.web_server_certificate))
382+
if (!settings_.web_server_certificate.empty() &&
383+
!exists(settings_.web_server_certificate))
384384
{
385385
LOG_ERROR(LOG_PROTOCOL)
386-
<< "Required server certificate '"
387-
<< settings_.web_server_certificate
388-
<< "' does not exist.";
386+
<< "Configured server certificate '"
387+
<< settings_.web_server_certificate << "' does not exist.";
389388
return false;
390389
}
391390

392-
if (!exists(settings_.web_server_private_key))
391+
if (!settings_.web_server_private_key.empty() &&
392+
!exists(settings_.web_server_private_key))
393393
{
394394
LOG_ERROR(LOG_PROTOCOL)
395-
<< "Required server private key '"
396-
<< settings_.web_server_private_key
397-
<< "' does not exist.";
395+
<< "Configured server private key '"
396+
<< settings_.web_server_private_key << "' does not exist.";
398397
return false;
399398
}
400399
#else
@@ -686,6 +685,11 @@ void socket::broadcast(const std::string& json)
686685
std::for_each(work_.begin(), work_.end(), sender);
687686
}
688687

688+
void socket::set_default_page_data(const std::string& data)
689+
{
690+
manager_->set_default_page_data(data);
691+
}
692+
689693
} // namespace http
690694
} // namespace protocol
691695
} // namespace libbitcoin

0 commit comments

Comments
 (0)