Skip to content

Commit d0d5ed6

Browse files
committed
Rationalize default (html) web sites with explore site.
1 parent 13603b0 commit d0d5ed6

File tree

4 files changed

+73
-104
lines changed

4 files changed

+73
-104
lines changed

include/bitcoin/node/protocols/protocol_explore.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ class BCN_API protocol_explore
5353
const network::http::method::get& request) NOEXCEPT override;
5454

5555
/// Dispatch.
56-
virtual bool dispatch_object(
57-
const network::http::request& request) NOEXCEPT;
56+
bool try_dispatch_object(
57+
const network::http::request& request) NOEXCEPT override;
5858
};
5959

6060
} // namespace node

include/bitcoin/node/protocols/protocol_html.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ class BCN_API protocol_html
5252
const network::http::method::get& request) NOEXCEPT override;
5353

5454
/// Dispatch.
55-
virtual bool dispatch_embedded(
55+
virtual bool try_dispatch_object(
56+
const network::http::request& request) NOEXCEPT;
57+
virtual void dispatch_file(
58+
const network::http::request& request) NOEXCEPT;
59+
virtual void dispatch_embedded(
5660
const network::http::request& request) NOEXCEPT;
5761

5862
/// Senders.

src/protocols/protocol_explore.cpp

Lines changed: 2 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -32,87 +32,13 @@ using namespace system;
3232

3333
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
3434

35-
// Handle get method.
36-
// ----------------------------------------------------------------------------
37-
38-
void protocol_explore::handle_receive_get(const code& ec,
39-
const method::get& request) NOEXCEPT
40-
{
41-
BC_ASSERT(stranded());
42-
43-
if (stopped(ec))
44-
return;
45-
46-
// Enforce http origin policy (requires configured hosts).
47-
if (!is_allowed_origin(*request, request->version()))
48-
{
49-
send_forbidden(*request);
50-
return;
51-
}
52-
53-
// Enforce http host header (if any hosts are configured).
54-
if (!is_allowed_host(*request, request->version()))
55-
{
56-
send_bad_host(*request);
57-
return;
58-
}
59-
60-
// Dispatch object with specified encoding.
61-
if (dispatch_object(*request))
62-
return;
63-
64-
// Embedded page site.
65-
if (dispatch_embedded(*request))
66-
return;
67-
68-
// Empty path implies malformed target (terminal).
69-
auto path = to_local_path(request->target());
70-
if (path.empty())
71-
{
72-
send_bad_target(*request);
73-
return;
74-
}
75-
76-
// If no file extension it's REST on the single/default html page.
77-
if (!path.has_extension())
78-
{
79-
path = to_local_path();
80-
81-
// Default html page (e.g. index.html) is not configured.
82-
if (path.empty())
83-
{
84-
send_not_implemented(*request);
85-
return;
86-
}
87-
}
88-
89-
// Get the single/default or explicitly requested page.
90-
auto file = get_file_body(path);
91-
if (!file.is_open())
92-
{
93-
send_not_found(*request);
94-
return;
95-
}
96-
97-
send_file(*request, std::move(file),
98-
file_mime_type(path, mime_type::application_octet_stream));
99-
}
100-
10135
// Dispatch.
10236
// ----------------------------------------------------------------------------
10337

104-
bool protocol_explore::dispatch_object(
105-
const network::http::request& request) NOEXCEPT
38+
bool protocol_explore::try_dispatch_object(const request& request) NOEXCEPT
10639
{
107-
const auto target = request.target();
108-
if (!is_origin_form(target))
109-
{
110-
send_bad_target(request);
111-
return true;
112-
}
113-
11440
wallet::uri uri{};
115-
if (!uri.decode(target))
41+
if (!uri.decode(request.target()))
11642
{
11743
send_bad_target(request);
11844
return true;

src/protocols/protocol_html.cpp

Lines changed: 64 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,14 @@ void protocol_html::handle_receive_get(const code& ec,
4141
if (stopped(ec))
4242
return;
4343

44-
// Enforce http origin policy (requires configured hosts).
44+
// Enforce http origin form for get.
45+
if (!is_origin_form(request->target()))
46+
{
47+
send_bad_target(*request);
48+
return;
49+
}
50+
51+
// Enforce http origin policy (if any origins are configured).
4552
if (!is_allowed_origin(*request, request->version()))
4653
{
4754
send_forbidden(*request);
@@ -55,67 +62,99 @@ void protocol_html::handle_receive_get(const code& ec,
5562
return;
5663
}
5764

58-
// Embedded page site.
59-
if (dispatch_embedded(*request))
65+
// Always try object dispatch, false if unhandled.
66+
if (try_dispatch_object(*request))
6067
return;
6168

62-
// Empty path implies malformed target (terminal).
63-
const auto path = to_local_path(request->target());
64-
if (path.empty())
69+
// Otherwise require file dispatch if path is configured (always handles).
70+
if (!options_.path.empty())
6571
{
66-
// TODO: split out sanitize from canonicalize so that this can return
67-
// send_not_found() when the request is sanitary but not found.
68-
send_bad_target(*request);
72+
dispatch_file(*request);
6973
return;
7074
}
7175

72-
// Not open implies file not found (non-terminal).
73-
auto file = get_file_body(path);
74-
if (!file.is_open())
76+
// Dispatch from embedded site if site is configured. (always handles).
77+
if (options_.pages.enabled())
7578
{
76-
send_not_found(*request);
79+
dispatch_embedded(*request);
7780
return;
7881
}
7982

80-
send_file(*request, std::move(file),
81-
file_mime_type(path, mime_type::application_octet_stream));
83+
// Neither site is enabled and object dispatch doesn't support accept type.
84+
send_not_implemented(*request);
8285
}
8386

8487
// Dispatch.
8588
// ----------------------------------------------------------------------------
8689

87-
bool protocol_html::dispatch_embedded(const request& request) NOEXCEPT
90+
bool protocol_html::try_dispatch_object(const request& request) NOEXCEPT
8891
{
89-
// False only if not enabled, otherwise handled below.
90-
if (!options_.pages.enabled())
91-
return false;
92+
return false;
93+
}
9294

95+
void protocol_html::dispatch_embedded(const request& request) NOEXCEPT
96+
{
9397
const auto& pages = config().server.explore.pages;
9498
switch (const auto mime = file_mime_type(to_path(request.target())))
9599
{
96100
case mime_type::text_css:
97101
send_span(request, pages.css(), mime);
98-
return true;
102+
break;
99103
case mime_type::text_html:
100104
send_span(request, pages.html(), mime);
101-
return true;
105+
break;
102106
case mime_type::application_javascript:
103107
send_span(request, pages.ecma(), mime);
104-
return true;
108+
break;
105109
case mime_type::font_woff:
106110
case mime_type::font_woff2:
107111
send_span(request, pages.font(), mime);
108-
return true;
112+
break;
109113
case mime_type::image_png:
110114
case mime_type::image_gif:
111115
case mime_type::image_jpeg:
112116
send_span(request, pages.icon(), mime);
113-
return true;
117+
break;
114118
default:
115-
return false;
119+
send_not_found(request);
116120
}
117121
}
118122

123+
void protocol_html::dispatch_file(const request& request) NOEXCEPT
124+
{
125+
// Empty path implies malformed target (terminal).
126+
auto path = to_local_path(request.target());
127+
if (path.empty())
128+
{
129+
send_bad_target(request);
130+
return;
131+
}
132+
133+
// If no file extension it's REST on the single/default html page.
134+
if (!path.has_extension())
135+
{
136+
path = to_local_path();
137+
138+
// Default html page (e.g. index.html) is not configured.
139+
if (path.empty())
140+
{
141+
send_not_implemented(request);
142+
return;
143+
}
144+
}
145+
146+
// Get the single/default or explicitly requested page.
147+
auto file = get_file_body(path);
148+
if (!file.is_open())
149+
{
150+
send_not_found(request);
151+
return;
152+
}
153+
154+
const auto octet_stream = mime_type::application_octet_stream;
155+
send_file(request, std::move(file), file_mime_type(path, octet_stream));
156+
}
157+
119158
// Senders.
120159
// ----------------------------------------------------------------------------
121160

0 commit comments

Comments
 (0)