@@ -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 API 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+ // Require file system 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+ // Require embedded dispatch 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.
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&) 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