Skip to content

Commit 509280d

Browse files
committed
chore: route_params
1 parent 409b76f commit 509280d

3 files changed

Lines changed: 82 additions & 70 deletions

File tree

include/boost/http_proto/server/basic_router.hpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ class basic_router : public /*detail::*/any_router
795795
}
796796

797797
private:
798-
// used to avoid a race when modifying res.resume_
798+
// used to avoid a race when modifying req.resume_
799799
struct set_resume
800800
{
801801
std::size_t& resume;
@@ -806,10 +806,10 @@ class basic_router : public /*detail::*/any_router
806806
resume = 0;
807807
}
808808
set_resume(
809-
basic_response& res) noexcept
810-
: resume(res.resume_)
809+
basic_request& req) noexcept
810+
: resume(req.resume_)
811811
{
812-
resume = res.pos_;
812+
resume = req.pos_;
813813
}
814814
void apply() noexcept
815815
{
@@ -877,11 +877,11 @@ class basic_router : public /*detail::*/any_router
877877
route_result invoke(Req& req, Res& res,
878878
std::integral_constant<int, 1>) const
879879
{
880-
basic_response& res_(res);
881-
if( res_.ec_.failed() ||
882-
res_.ep_)
880+
basic_request& req_(req);
881+
if( req_.ec_.failed() ||
882+
req_.ep_)
883883
return http_proto::route::next;
884-
set_resume u(res_);
884+
set_resume u(req_);
885885
auto rv = h(req, res);
886886
if(rv == http_proto::route::detach)
887887
{
@@ -896,11 +896,11 @@ class basic_router : public /*detail::*/any_router
896896
invoke(Req& req, Res& res,
897897
std::integral_constant<int, 2>) const
898898
{
899-
basic_response& res_(res);
900-
if(! res_.ec_.failed())
899+
basic_request& req_(req);
900+
if(! req_.ec_.failed())
901901
return http_proto::route::next;
902-
set_resume u(res_);
903-
auto rv = h(req, res, res_.ec_);
902+
set_resume u(req_);
903+
auto rv = h(req, res, req_.ec_);
904904
if(rv == http_proto::route::detach)
905905
{
906906
u.apply();
@@ -913,10 +913,10 @@ class basic_router : public /*detail::*/any_router
913913
route_result invoke(Req& req, Res& res,
914914
std::integral_constant<int, 4>) const
915915
{
916-
basic_response& res_(res);
917-
if( res_.resume_ > 0 ||
918-
( ! res_.ec_.failed() &&
919-
! res_.ep_))
916+
basic_request& req_(req);
917+
if( req_.resume_ > 0 ||
918+
( ! req_.ec_.failed() &&
919+
! req_.ep_))
920920
return h.dispatch_impl(req, res);
921921
return http_proto::route::next;
922922
}
@@ -944,16 +944,16 @@ class basic_router : public /*detail::*/any_router
944944
invoke(Req& req, Res& res) const override
945945
{
946946
#ifndef BOOST_NO_EXCEPTIONS
947-
basic_response& res_(res);
948-
if(! res_.ep_)
947+
basic_request& req_(req);
948+
if(! req_.ep_)
949949
return http_proto::route::next;
950950
try
951951
{
952-
std::rethrow_exception(res_.ep_);
952+
std::rethrow_exception(req_.ep_);
953953
}
954954
catch(E const& ex)
955955
{
956-
set_resume u(res_);
956+
set_resume u(req_);
957957
// VFALCO What if h throws?
958958
auto rv = h(req, res, ex);
959959
if(rv == http_proto::route::detach)
@@ -965,7 +965,7 @@ class basic_router : public /*detail::*/any_router
965965
}
966966
catch(...)
967967
{
968-
BOOST_ASSERT(res_.ep_);
968+
BOOST_ASSERT(req_.ep_);
969969
return http_proto::route::next;
970970
}
971971
#else

include/boost/http_proto/server/router_types.hpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,20 @@ class basic_request
314314

315315
private:
316316
friend class /*detail::*/any_router;
317+
template<class, class>
318+
friend class basic_router;
317319
struct match_result;
318-
http_proto::method verb_ =
319-
http_proto::method::unknown;
320+
basic_request& operator=(
321+
basic_request const&) = delete;
322+
320323
std::string verb_str_;
321324
std::string decoded_path_;
325+
system::error_code ec_;
326+
std::exception_ptr ep_;
327+
std::size_t pos_ = 0;
328+
std::size_t resume_ = 0;
329+
http_proto::method verb_ =
330+
http_proto::method::unknown;
322331
bool addedSlash_ = false;
323332
bool case_sensitive = false;
324333
bool strict = false;
@@ -337,11 +346,6 @@ class basic_response
337346
friend class /*detail::*/any_router;
338347
template<class, class>
339348
friend class basic_router;
340-
341-
std::size_t pos_ = 0;
342-
std::size_t resume_ = 0;
343-
system::error_code ec_;
344-
std::exception_ptr ep_;
345349
};
346350

347351
} // http_proto

src/server/basic_router.cpp

Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ resume_impl(
592592
route_result ec) const ->
593593
route_result
594594
{
595-
BOOST_ASSERT(res.resume_ > 0);
595+
BOOST_ASSERT(req.resume_ > 0);
596596
if( ec == route::send ||
597597
ec == route::close ||
598598
ec == route::complete)
@@ -611,9 +611,9 @@ resume_impl(
611611
req.path.remove_suffix(1);
612612

613613
// resume_ was set in the handler's wrapper
614-
BOOST_ASSERT(res.resume_ == res.pos_);
615-
res.pos_ = 0;
616-
res.ec_ = ec;
614+
BOOST_ASSERT(req.resume_ == req.pos_);
615+
req.pos_ = 0;
616+
req.ec_ = ec;
617617
return do_dispatch(req, res);
618618
}
619619

@@ -627,9 +627,17 @@ dispatch_impl(
627627
basic_request& req,
628628
basic_response& res) const
629629
{
630-
// VFALCO we could reuse the string storage by not clearing them
631-
// set req.case_sensitive, req.strict to default of false
632-
req = {};
630+
req.verb_str_.clear();
631+
req.decoded_path_.clear();
632+
req.ec_.clear();
633+
req.ep_ = nullptr;
634+
req.pos_ = 0;
635+
req.resume_ = 0;
636+
req.verb_ = method::unknown;
637+
req.addedSlash_ = false;
638+
req.case_sensitive = false;
639+
req.strict = false;
640+
633641
if(verb == http_proto::method::unknown)
634642
{
635643
BOOST_ASSERT(! verb_str.empty());
@@ -664,13 +672,13 @@ dispatch_impl(
664672
auto rv = do_dispatch(req, res);
665673
if(rv == route::detach)
666674
return rv;
667-
if(res.ep_)
675+
if(req.ep_)
668676
{
669-
res.ep_ = nullptr;
677+
req.ep_ = nullptr;
670678
return error::unhandled_exception;
671679
}
672-
if( res.ec_.failed())
673-
res.ec_ = {};
680+
if( req.ec_.failed())
681+
req.ec_ = {};
674682
return rv;
675683
}
676684

@@ -728,12 +736,12 @@ dispatch_impl(
728736
match_result mr;
729737
for(auto const& i : impl_->layers)
730738
{
731-
if(res.resume_ > 0)
739+
if(req.resume_ > 0)
732740
{
733741
auto const n = i.count(); // handlers in layer
734-
if(res.pos_ + n < res.resume_)
742+
if(req.pos_ + n < req.resume_)
735743
{
736-
res.pos_ += n; // skip layer
744+
req.pos_ += n; // skip layer
737745
continue;
738746
}
739747
// repeat match to recreate the stack
@@ -743,29 +751,29 @@ dispatch_impl(
743751
}
744752
else
745753
{
746-
if(i.match.end && res.ec_.failed())
754+
if(i.match.end && req.ec_.failed())
747755
{
748756
// routes can't have error handlers
749-
res.pos_ += i.count(); // skip layer
757+
req.pos_ += i.count(); // skip layer
750758
continue;
751759
}
752760
if(! i.match(req, mr))
753761
{
754762
// not a match
755-
res.pos_ += i.count(); // skip layer
763+
req.pos_ += i.count(); // skip layer
756764
continue;
757765
}
758766
}
759767
for(auto it = i.entries.begin();
760768
it != i.entries.end(); ++it)
761769
{
762770
auto const& e(*it);
763-
if(res.resume_)
771+
if(req.resume_)
764772
{
765773
auto const n = e.handler->count();
766-
if(res.pos_ + n < res.resume_)
774+
if(req.pos_ + n < req.resume_)
767775
{
768-
res.pos_ += n; // skip entry
776+
req.pos_ += n; // skip entry
769777
continue;
770778
}
771779
BOOST_ASSERT(e.match_method(req));
@@ -775,15 +783,15 @@ dispatch_impl(
775783
// check verb for match
776784
if(! e.match_method(req))
777785
{
778-
res.pos_ += e.handler->count(); // skip entry
786+
req.pos_ += e.handler->count(); // skip entry
779787
continue;
780788
}
781789
}
782790

783791
route_result rv;
784792
// increment before invoke
785-
++res.pos_;
786-
if(res.pos_ != res.resume_)
793+
++req.pos_;
794+
if(req.pos_ != req.resume_)
787795
{
788796
// call the handler
789797
#ifdef BOOST_NO_EXCEPTIONS
@@ -792,19 +800,19 @@ dispatch_impl(
792800
try
793801
{
794802
rv = e.handler->invoke(req, res);
795-
if(res.ec_.failed())
796-
res.ep_ = {}; // transition to error mode
803+
if(req.ec_.failed())
804+
req.ep_ = {}; // transition to error mode
797805
}
798806
catch(...)
799807
{
800-
if(res.ec_.failed())
801-
res.ec_ = {}; // transition to except mode
802-
res.ep_ = std::current_exception();
808+
if(req.ec_.failed())
809+
req.ec_ = {}; // transition to except mode
810+
req.ep_ = std::current_exception();
803811
rv = route::next;
804812
}
805813
#endif
806814

807-
// res.pos_ can be incremented further
815+
// req.pos_ can be incremented further
808816
// inside the above call to invoke.
809817
if(rv == route::detach)
810818
{
@@ -821,21 +829,21 @@ dispatch_impl(
821829
// a subrouter never detaches on its own
822830
BOOST_ASSERT(e.handler->count() == 1);
823831
// can't detach on resume
824-
if(res.ec_ == route::detach)
832+
if(req.ec_ == route::detach)
825833
detail::throw_invalid_argument();
826834
// do resume
827-
res.resume_ = 0;
828-
rv = res.ec_;
829-
res.ec_ = {};
835+
req.resume_ = 0;
836+
rv = req.ec_;
837+
req.ec_ = {};
830838
}
831839
if( rv == route::send ||
832840
rv == route::complete ||
833841
rv == route::close)
834842
{
835-
if( res.ec_.failed())
836-
res.ec_ = {};
837-
if( res.ep_)
838-
res.ep_ = nullptr;
843+
if( req.ec_.failed())
844+
req.ec_ = {};
845+
if( req.ep_)
846+
req.ep_ = nullptr;
839847
return rv;
840848
}
841849
if(rv == route::next)
@@ -846,7 +854,7 @@ dispatch_impl(
846854
if(! i.match.end)
847855
detail::throw_invalid_argument();
848856
while(++it != i.entries.end())
849-
res.pos_ += it->handler->count();
857+
req.pos_ += it->handler->count();
850858
break; // skip remaining entries
851859
}
852860
// we must handle all route enums
@@ -857,12 +865,12 @@ dispatch_impl(
857865
detail::throw_invalid_argument();
858866
}
859867
// error handling mode
860-
res.ec_ = rv;
868+
req.ec_ = rv;
861869
if(! i.match.end)
862870
continue; // next entry
863871
// routes don't have error handlers
864872
while(++it != i.entries.end())
865-
res.pos_ += it->handler->count();
873+
req.pos_ += it->handler->count();
866874
break; // skip remaining entries
867875
}
868876

@@ -887,13 +895,13 @@ do_dispatch(
887895
// without attempting to perform any additional operations.
888896
return rv;
889897
}
890-
if(! res.ec_.failed())
898+
if(! req.ec_.failed())
891899
{
892900
// unhandled route
893901
return route::next;
894902
}
895903
// error condition
896-
return res.ec_;
904+
return req.ec_;
897905
}
898906

899907
//} // detail

0 commit comments

Comments
 (0)