Skip to content

Commit 2023c3c

Browse files
committed
router work
1 parent 3dd34d5 commit 2023c3c

File tree

12 files changed

+669
-779
lines changed

12 files changed

+669
-779
lines changed

example/server/asio_params.hpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,10 @@
1111
#define BOOST_HTTP_IO_EXAMPLE_SERVER_ASIO_PARAMS_HPP
1212

1313
#include <boost/http_io/detail/config.hpp>
14-
#include <boost/http_io/server/handler_params.hpp>
1514

1615
namespace boost {
1716
namespace http_io {
1817

19-
struct asio_params : handler_params
20-
{
21-
};
22-
2318
} // http_io
2419
} // boost
2520

example/server/handler.cpp

Lines changed: 53 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -156,36 +156,34 @@ static
156156
void
157157
path_cat(
158158
std::string& result,
159-
core::string_view base,
160-
core::string_view path)
159+
core::string_view prefix,
160+
urls::segments_view suffix)
161161
{
162-
if(base.empty())
163-
{
164-
result = path;
165-
return;
166-
}
167-
result = base;
162+
result = prefix;
168163

169164
#ifdef BOOST_MSVC
170165
char constexpr path_separator = '\\';
171-
if(result.back() == path_separator)
172-
result.resize(result.size() - 1);
173-
result.append(path.data(), path.size());
174-
for(auto& c : result)
175-
if(c == '/')
176-
c = path_separator;
177166
#else
178167
char constexpr path_separator = '/';
179-
if(result.back() == path_separator)
180-
result.resize(result.size() - 1);
181-
result.append(path.data(), path.size());
182168
#endif
169+
if( result.back() == path_separator)
170+
result.resize(result.size() - 1); // remove trailing
171+
#ifdef BOOST_MSVC
172+
for(auto& c : result)
173+
if( c == '/')
174+
c = path_separator;
175+
#endif
176+
for(auto const& seg : suffix)
177+
{
178+
result.push_back(path_separator);
179+
result.append(seg);
180+
}
183181
}
184182

185183
//------------------------------------------------
186184

187185
static
188-
void
186+
bool
189187
make_error_response(
190188
http_proto::status code,
191189
http_proto::request_base const& req,
@@ -244,10 +242,12 @@ make_error_response(
244242

245243
sr.start(res, http_proto::string_body(
246244
std::move(body)));
245+
246+
return false;
247247
}
248248

249249
static
250-
void
250+
bool
251251
service_unavailable(
252252
http_proto::response& res,
253253
http_proto::serializer& sr,
@@ -294,18 +294,20 @@ service_unavailable(
294294
res,
295295
http_proto::string_body(
296296
std::move(s)));
297+
return false;
297298
}
298299

299300
//------------------------------------------------
300301

301-
void
302+
bool
302303
file_responder::
303304
operator()(
304-
handler_params& params) const
305+
Request& req,
306+
Response& res) const
305307
{
306-
if(params.is_shutting_down)
308+
if(req.is_shutting_down)
307309
return service_unavailable(
308-
params.res, params.serializer, params.req);
310+
res.res, res.sr, req.req);
309311

310312
#if 0
311313
// Returns a server error response
@@ -323,17 +325,15 @@ operator()(
323325
#endif
324326

325327
// Request path must be absolute and not contain "..".
326-
if( params.req.target().empty() ||
327-
params.req.target()[0] != '/' ||
328-
params.req.target().find("..") != core::string_view::npos)
328+
if( req.req.target().empty() ||
329+
req.req.target()[0] != '/' ||
330+
req.req.target().find("..") != core::string_view::npos)
329331
return make_error_response(http_proto::status::bad_request,
330-
params.req, params.res, params.serializer);
332+
req.req, res.res, res.sr);
331333

332334
// Build the path to the requested file
333335
std::string path;
334-
path_cat(path, doc_root_, params.req.target());
335-
if(params.req.target().back() == '/')
336-
path.append("index.html");
336+
path_cat(path, doc_root_, req.path);
337337

338338
// Attempt to open the file
339339
system::error_code ec;
@@ -344,52 +344,55 @@ operator()(
344344
size = f.size(ec);
345345
if(! ec.failed())
346346
{
347-
params.res.set_start_line(
347+
res.res.set_start_line(
348348
http_proto::status::ok,
349-
params.req.version());
350-
params.res.set(http_proto::field::server, "Boost");
351-
params.res.set_keep_alive(params.req.keep_alive());
352-
params.res.set_payload_size(size);
349+
req.req.version());
350+
res.res.set(http_proto::field::server, "Boost");
351+
res.res.set_keep_alive(req.req.keep_alive());
352+
res.res.set_payload_size(size);
353353

354354
auto mt = mime_type(get_extension(path));
355-
params.res.append(
355+
res.res.append(
356356
http_proto::field::content_type, mt);
357357

358-
params.serializer.start<http_proto::file_source>(
359-
params.res, std::move(f), size);
360-
return;
358+
res.sr.start<http_proto::file_source>(
359+
res.res, std::move(f), size);
360+
return false;
361361
}
362362

363363
if(ec == system::errc::no_such_file_or_directory)
364364
return make_error_response(
365365
http_proto::status::not_found,
366-
params.req, params.res, params.serializer);
366+
req.req, res.res, res.sr);
367367

368368
// ec.message()?
369369
return make_error_response(
370370
http_proto::status::internal_server_error,
371-
params.req, params.res, params.serializer);
371+
req.req, res.res, res.sr);
372372
}
373373

374374
//------------------------------------------------
375375

376-
void
376+
bool
377377
https_redirect_responder::
378-
operator()(handler_params& params) const
378+
operator()(
379+
Request& req,
380+
Response& res) const
379381
{
380-
if(params.is_shutting_down)
382+
if(req.is_shutting_down)
381383
return service_unavailable(
382-
params.res, params.serializer, params.req);
384+
res.res, res.sr, req.req);
383385

384386
std::string body;
385-
prepare_error(params.res, body,
386-
http_proto::status::moved_permanently, params.req);
387-
urls::url u1(params.req.target());
387+
prepare_error(res.res, body,
388+
http_proto::status::moved_permanently, req.req);
389+
urls::url u1(req.req.target());
388390
u1.set_scheme_id(urls::scheme::https);
389391
u1.set_host_address("localhost"); // VFALCO WTF IS THIS!
390-
params.res.append(http_proto::field::location, u1.buffer());
391-
params.serializer.start(params.res,
392+
res.res.append(http_proto::field::location, u1.buffer());
393+
res.sr.start(res.res,
392394
http_proto::string_body( std::move(body)));
395+
return false;
393396
}
394397

395398
} // http_io

example/server/handler.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#define BOOST_HTTP_IO_EXAMPLE_SERVER_HANDLER_HPP
1212

1313
#include <boost/http_io/detail/config.hpp>
14-
#include <boost/http_io/server/handler_params.hpp>
14+
#include <boost/http_io/server/route_params.hpp>
1515
#include <boost/http_io/server/router.hpp>
1616
#include <boost/http_proto/request_base.hpp>
1717
#include <boost/http_proto/response.hpp>
@@ -21,13 +21,13 @@
2121
namespace boost {
2222
namespace http_io {
2323

24-
using router_type = router<handler_params>;
24+
using router_type = router<Response>;
2525

2626
//------------------------------------------------
2727

2828
struct https_redirect_responder
2929
{
30-
void operator()(handler_params&) const;
30+
bool operator()(Request&, Response&) const;
3131
};
3232

3333
//------------------------------------------------
@@ -40,7 +40,7 @@ struct file_responder
4040
{
4141
}
4242

43-
void operator()(handler_params&) const;
43+
bool operator()(Request&, Response&) const;
4444

4545
private:
4646
std::string doc_root_;

example/server/http_responder.hpp

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,23 @@ class http_responder
8686

8787
BOOST_ASSERT(pr_.is_complete());
8888

89-
// find the route
90-
auto found = rr_.invoke(
89+
Request req{
9190
pr_.get().method(),
92-
pr_.get().target(),
93-
handler_params{
94-
*pconfig_,
95-
pr_.get(),
96-
res_,
97-
pr_,
98-
sr_,
99-
self().server().is_stopping()});
100-
BOOST_ASSERT(found);
101-
(void)found;
91+
urls::segments_encoded_view(
92+
pr_.get().target()),
93+
*this->pconfig_,
94+
pr_.get(),
95+
pr_,
96+
self().server().is_stopping()
97+
};
98+
Response res{
99+
res_,
100+
sr_
101+
};
102+
103+
// invoke handlers for the route
104+
auto cont = rr_(req, res);
105+
BOOST_ASSERT(! cont);
102106

103107
http_io::async_write(self().stream(), sr_,
104108
call_mf(&http_responder::on_write, this));
@@ -160,15 +164,13 @@ class http_responder
160164
}
161165

162166
protected:
163-
std::size_t id_ = 0;
164167
section sect_;
165-
166-
private:
167-
acceptor_config const* pconfig_ = nullptr;
168+
std::size_t id_ = 0;
168169
router_type& rr_;
169170
http_proto::request_parser pr_;
170171
http_proto::serializer sr_;
171172
http_proto::response res_;
173+
acceptor_config const* pconfig_ = nullptr;
172174
};
173175

174176
} // http_io

example/server/main.cpp

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -84,32 +84,53 @@ int server_main( int argc, char* argv[] )
8484
http_proto::install_serializer_service(srv.services(), cfg);
8585
}
8686

87-
// Create the routes
88-
// VFALCO this is a hacky WIP
89-
router_type rr0;
90-
rr0.get<https_redirect_responder>("/*splat");
91-
router_type rr;
92-
rr.get<file_responder>("/*splat", doc_root);
93-
94-
using workers_type = workers<executor_type,
95-
worker_ssl<executor_type>>;
96-
87+
router_type app;
88+
89+
// redirect HTTP to HTTPS
90+
app.use(
91+
[](Request& req, Response& res)
92+
{
93+
if(req.port.is_ssl)
94+
return true;
95+
return https_redirect_responder()(req, res);
96+
});
97+
98+
// "/" => "/index.html"
99+
app.get("/",
100+
[](Request& req, Response&)
101+
{
102+
req.path = urls::segments_encoded_view("index.html");
103+
return true;
104+
});
105+
106+
// static route for website
107+
app.get("/*",
108+
file_responder{ doc_root });
109+
110+
using workers_type =
111+
workers< executor_type, worker_ssl<executor_type> >;
112+
113+
// Create all our workers
97114
auto& vp = emplace_part<workers_type>(
98-
srv,
99-
srv.get_executor(),
100-
1,
101-
num_workers,
102-
srv.get_executor(),
103-
ssl_ctx,
104-
rr);
115+
srv, srv.get_executor(), 1, num_workers,
116+
srv.get_executor(), ssl_ctx, app);
105117

118+
// SSL port
106119
vp.emplace(
107120
acceptor_config{ true, false },
108121
workers_type::acceptor_type(
109122
srv.get_executor(),
110123
asio::ip::tcp::endpoint(addr, 443),
111124
reuse_addr));
112125

126+
// plain port
127+
vp.emplace(
128+
acceptor_config{ false, false },
129+
workers_type::acceptor_type(
130+
srv.get_executor(),
131+
asio::ip::tcp::endpoint(addr, 80),
132+
reuse_addr));
133+
113134
srv.run();
114135
}
115136
catch( std::exception const& e )

0 commit comments

Comments
 (0)