8 #ifndef CPPCMS_URL_DISPATCHER_H 9 #define CPPCMS_URL_DISPATCHER_H 11 #include <booster/noncopyable.h> 12 #include <cppcms/defs.h> 13 #include <booster/function.h> 14 #include <booster/hold_ptr.h> 15 #include <booster/traits/enable_if.h> 16 #include <booster/traits/is_base_of.h> 17 #include <booster/traits/type_traits.h> 18 #include <booster/regex.h> 19 #include <cppcms/application.h> 20 #include <cppcms/steal_buf.h> 32 output.assign(parameter.
begin(),parameter.
end());
39 template<
typename ParamType>
43 if(!parameter || !parameter.eof())
129 void map_generic(std::string
const &method,
booster::regex const &re,generic_handler
const &h);
137 void map_generic(
booster::regex const &re,generic_handler
const &h);
140 #ifdef CPPCMS_DOXYGEN_DOCS 179 template<
typename Application,
typename... ApplicationMemberArgs>
180 void map(std::string
const &method,std::string
const &re,
void (Application::*member)(ApplicationMemberArgs...),Application *app, int ... groups );
218 template<
typename Application,
typename... ApplicationMemberArgs>
219 void map(std::string
const &re,
void (Application::*member)(ApplicationMemberArgs...),Application *app, int ... groups );
260 template<
typename Application,
typename... ApplicationMemberArgs>
261 void map(std::string
const &method,
booster::regex const &re,
void (Application::*member)(ApplicationMemberArgs...),Application *app, int ... groups );
300 template<
typename Application,
typename... ApplicationMemberArgs>
301 void map(
booster::regex const &re,
void (Application::*member)(ApplicationMemberArgs...),Application *app, int ... groups );
311 void assign_generic(std::string
const ®ex,rhandler handler);
316 void assign(std::string
const ®ex,handler handler);
327 void assign(std::string
const ®ex,handler1 handler,
int exp1);
333 void assign(std::string
const ®ex,handler2 handler,
int exp1,
int exp2);
339 void assign(std::string
const ®ex,handler3 handler,
int exp1,
int exp2,
int exp3);
345 void assign(std::string
const ®ex,handler4 handler,
int exp1,
int exp2,
int exp3,
int exp4);
352 void assign(std::string
const ®ex,handler5 handler,
int exp1,
int exp2,
int exp3,
int exp4,
int exp5);
359 void assign(std::string
const ®ex,handler6 handler,
int exp1,
int exp2,
int exp3,
int exp4,
int exp5,
int exp6);
368 bool dispatch(std::string url);
385 void assign(std::string
const ®ex,
void (C::*member)(),C *
object)
387 assign(regex,binder0<C>(member,
object));
400 assign_generic(regex,rbinder<C>(member,
object));
410 void assign(std::string
const ®ex,
void (C::*member)(std::string),C *
object,
int e1)
412 assign(regex,binder1<C>(member,
object),e1);
422 void assign(std::string
const ®ex,
void (C::*member)(std::string,std::string),C *
object,
int e1,
int e2)
424 assign(regex,binder2<C>(member,
object),e1,e2);
434 void assign(std::string
const ®ex,
void (C::*member)(std::string,std::string,std::string),C *
object,
int e1,
int e2,
int e3)
436 assign(regex,binder3<C>(member,
object),e1,e2,e3);
446 void assign(std::string
const ®ex,
void (C::*member)(std::string,std::string,std::string,std::string),C *
object,
int e1,
int e2,
int e3,
int e4)
448 assign(regex,binder4<C>(member,
object),e1,e2,e3,e4);
459 void assign(std::string
const ®ex,
void (C::*member)(std::string,std::string,std::string,std::string,std::string),C *
object,
int e1,
int e2,
int e3,
int e4,
int e5)
461 assign(regex,binder5<C>(member,
object),e1,e2,e3,e4,e5);
472 void assign(std::string
const ®ex,
void (C::*member)(std::string,std::string,std::string,std::string,std::string,std::string),C *
object,
int e1,
int e2,
int e3,
int e4,
int e5,
int e6)
474 assign(regex,binder6<C>(member,
object),e1,e2,e3,e4,e5,e6);
489 void mount(std::string
const &match,
application &app,
int part);
493 template<
typename C,
typename Enable =
void>
497 page_guard(C * ,std::istream &) {}
502 class page_guard<C,typename booster::enable_if<booster::is_base_of< cppcms::application,C> >::type > {
520 typedef void (C::*member_type)();
524 binder0(member_type m,C *o) :
529 void operator()()
const 531 page_guard<C> guard(
object);
542 rbinder(member_type m,C *o) :
549 page_guard<C> guard(
object);
550 (
object->*member)(p1);
556 typedef void (C::*member_type)(std::string);
560 binder1(member_type m,C *o) :
565 void operator()(std::string p1)
const 567 page_guard<C> guard(
object);
568 (
object->*member)(p1);
574 typedef void (C::*member_type)(std::string,std::string);
578 binder2(member_type m,C *o) :
583 void operator()(std::string p1,std::string p2)
const 585 page_guard<C> guard(
object);
586 (
object->*member)(p1,p2);
591 typedef void (C::*member_type)(std::string,std::string,std::string);
595 binder3(member_type m,C *o) :
600 void operator()(std::string p1,std::string p2,std::string p3)
const 602 page_guard<C> guard(
object);
603 (
object->*member)(p1,p2,p3);
608 typedef void (C::*member_type)(std::string,std::string,std::string,std::string);
612 binder4(member_type m,C *o) :
617 void operator()(std::string p1,std::string p2,std::string p3,std::string p4)
const 619 page_guard<C> guard(
object);
620 (
object->*member)(p1,p2,p3,p4);
626 typedef void (C::*member_type)(std::string,std::string,std::string,std::string,std::string);
630 binder5(member_type m,C *o) :
635 void operator()(std::string p1,std::string p2,std::string p3,std::string p4,std::string p5)
const 637 page_guard<C> guard(
object);
638 (
object->*member)(p1,p2,p3,p4,p5);
642 static bool validate_encoding(
application &app,
char const *begin,
char const *end);
643 static void setup_stream(
application &app,std::istream &s);
647 typedef void (C::*member_type)(std::string,std::string,std::string,std::string,std::string,std::string);
651 binder6(member_type m,C *o) :
656 void operator()(std::string p1,std::string p2,std::string p3,std::string p4,std::string p5,std::string p6)
const 658 page_guard<C> guard(
object);
659 (
object->*member)(p1,p2,p3,p4,p5,p6);
668 if(!validate_encoding(app,m[group].first,m[group].second))
670 p.
range(m[group].first,m[group].second);
679 #define CPPCMS_DEFANDPARSE(N) typename booster::remove_const_reference<P##N>::type p##N; \ 680 if(!parse(app,s,m,g##N,p##N)) return false; 682 #define CPPCMS_DEFANDPARSE2(N1,N2) CPPCMS_DEFANDPARSE(N1) CPPCMS_DEFANDPARSE(N2) 683 #define CPPCMS_DEFANDPARSE3(N1,N2,N3) CPPCMS_DEFANDPARSE(N1) CPPCMS_DEFANDPARSE2(N2,N3) 684 #define CPPCMS_DEFANDPARSE4(N1,N2,N3,N4) CPPCMS_DEFANDPARSE2(N1,N2) CPPCMS_DEFANDPARSE2(N3,N4) 685 #define CPPCMS_DEFANDPARSE5(N1,N2,N3,N4,N5) CPPCMS_DEFANDPARSE2(N1,N2) CPPCMS_DEFANDPARSE3(N3,N4,N5) 689 #define CPPCMS_URLBINDER \ 691 template<typename C CPPCMS_URLBINDER_PRD CPPCMS_URLBINDER_TPAR> \ 692 struct url_binder<void (C::*)(CPPCMS_URLBINDER_MPAR)> { \ 693 typedef void (C::*member_type)(CPPCMS_URLBINDER_MPAR); \ 694 member_type member; C *self; \ 695 CPPCMS_URLBINDER_GPAR \ 697 member_type m,C *s CPPCMS_URLBINDER_PRD \ 698 CPPCMS_URLBINDER_IPAR \ 700 self(s) CPPCMS_URLBINDER_PRD \ 701 CPPCMS_URLBINDER_CPAR \ 703 bool operator()(application &CPPCMS_URLBINDER_P1,booster::cmatch const &CPPCMS_URLBINDER_P2) { \ 704 CPPCMS_URLBINDER_INIT CPPCMS_URLBINDER_PARSE \ 705 page_guard<C> guard(self); \ 706 (self->*member)(CPPCMS_URLBINDER_PPAR); \ 711 template<typename C CPPCMS_URLBINDER_PRD CPPCMS_URLBINDER_TPAR> \ 712 void map(std::string const &me,std::string const &re, \ 713 void (C::*mb)(CPPCMS_URLBINDER_MPAR),C *app CPPCMS_URLBINDER_PRD \ 714 CPPCMS_URLBINDER_IPAR) \ 716 typedef url_binder<void(C::*)(CPPCMS_URLBINDER_MPAR)> btype; \ 717 map_generic(me,re,btype(mb,app CPPCMS_URLBINDER_PRD CPPCMS_URLBINDER_PPAR)); \ 719 template<typename C CPPCMS_URLBINDER_PRD CPPCMS_URLBINDER_TPAR> \ 720 void map(std::string const &re, \ 721 void (C::*mb)(CPPCMS_URLBINDER_MPAR),C *app CPPCMS_URLBINDER_PRD \ 722 CPPCMS_URLBINDER_IPAR) \ 724 typedef url_binder<void (C::*)(CPPCMS_URLBINDER_MPAR)> btype; \ 725 map_generic(re,btype(mb,app CPPCMS_URLBINDER_PRD CPPCMS_URLBINDER_PPAR)); \ 727 template<typename C CPPCMS_URLBINDER_PRD CPPCMS_URLBINDER_TPAR> \ 728 void map(std::string const &me,booster::regex const &re, \ 729 void (C::*mb)(CPPCMS_URLBINDER_MPAR),C *app CPPCMS_URLBINDER_PRD \ 730 CPPCMS_URLBINDER_IPAR) \ 732 typedef url_binder<void (C::*)(CPPCMS_URLBINDER_MPAR)> btype; \ 733 map_generic(me,re,btype(mb,app CPPCMS_URLBINDER_PRD CPPCMS_URLBINDER_PPAR)); \ 735 template<typename C CPPCMS_URLBINDER_PRD CPPCMS_URLBINDER_TPAR> \ 736 void map(booster::regex const &re, \ 737 void (C::*mb)(CPPCMS_URLBINDER_MPAR),C *app CPPCMS_URLBINDER_PRD \ 738 CPPCMS_URLBINDER_IPAR) \ 740 typedef url_binder<void (C::*)(CPPCMS_URLBINDER_MPAR)> btype; \ 741 map_generic(re,btype(mb,app CPPCMS_URLBINDER_PRD CPPCMS_URLBINDER_PPAR)); \ 746 #define CPPCMS_URLBINDER_PRD 747 #define CPPCMS_URLBINDER_INIT 748 #define CPPCMS_URLBINDER_TPAR 749 #define CPPCMS_URLBINDER_MPAR 750 #define CPPCMS_URLBINDER_PPAR 751 #define CPPCMS_URLBINDER_GPAR 752 #define CPPCMS_URLBINDER_IPAR 753 #define CPPCMS_URLBINDER_CPAR 754 #define CPPCMS_URLBINDER_PARSE 755 #define CPPCMS_URLBINDER_P1 756 #define CPPCMS_URLBINDER_P2 760 #undef CPPCMS_URLBINDER_TPAR 761 #undef CPPCMS_URLBINDER_MPAR 762 #undef CPPCMS_URLBINDER_PPAR 763 #undef CPPCMS_URLBINDER_GPAR 764 #undef CPPCMS_URLBINDER_IPAR 765 #undef CPPCMS_URLBINDER_CPAR 766 #undef CPPCMS_URLBINDER_PARSE 768 #undef CPPCMS_URLBINDER_INIT 769 #undef CPPCMS_URLBINDER_PRD 770 #undef CPPCMS_URLBINDER_P1 771 #undef CPPCMS_URLBINDER_P2 774 #define CPPCMS_URLBINDER_PRD , 775 #define CPPCMS_URLBINDER_INIT util::const_char_istream s; setup_stream(app,s); 776 #define CPPCMS_URLBINDER_P1 app 777 #define CPPCMS_URLBINDER_P2 m 779 #define CPPCMS_URLBINDER_TPAR typename P1 780 #define CPPCMS_URLBINDER_MPAR P1 781 #define CPPCMS_URLBINDER_PPAR p1 782 #define CPPCMS_URLBINDER_GPAR int g1; 783 #define CPPCMS_URLBINDER_IPAR int p1 784 #define CPPCMS_URLBINDER_CPAR g1(p1) 785 #define CPPCMS_URLBINDER_PARSE CPPCMS_DEFANDPARSE(1) 789 #undef CPPCMS_URLBINDER_TPAR 790 #undef CPPCMS_URLBINDER_MPAR 791 #undef CPPCMS_URLBINDER_PPAR 792 #undef CPPCMS_URLBINDER_GPAR 793 #undef CPPCMS_URLBINDER_IPAR 794 #undef CPPCMS_URLBINDER_CPAR 795 #undef CPPCMS_URLBINDER_PARSE 798 #define CPPCMS_URLBINDER_TPAR typename P1,typename P2 799 #define CPPCMS_URLBINDER_MPAR P1,P2 800 #define CPPCMS_URLBINDER_PPAR p1,p2 801 #define CPPCMS_URLBINDER_GPAR int g1,g2; 802 #define CPPCMS_URLBINDER_IPAR int p1,int p2 803 #define CPPCMS_URLBINDER_CPAR g1(p1),g2(p2) 804 #define CPPCMS_URLBINDER_PARSE CPPCMS_DEFANDPARSE2(1,2) 808 #undef CPPCMS_URLBINDER_TPAR 809 #undef CPPCMS_URLBINDER_MPAR 810 #undef CPPCMS_URLBINDER_PPAR 811 #undef CPPCMS_URLBINDER_GPAR 812 #undef CPPCMS_URLBINDER_IPAR 813 #undef CPPCMS_URLBINDER_CPAR 814 #undef CPPCMS_URLBINDER_PARSE 817 #define CPPCMS_URLBINDER_TPAR typename P1,typename P2,typename P3 818 #define CPPCMS_URLBINDER_MPAR P1,P2,P3 819 #define CPPCMS_URLBINDER_PPAR p1,p2,p3 820 #define CPPCMS_URLBINDER_GPAR int g1,g2,g3; 821 #define CPPCMS_URLBINDER_IPAR int p1,int p2,int p3 822 #define CPPCMS_URLBINDER_CPAR g1(p1),g2(p2),g3(p3) 823 #define CPPCMS_URLBINDER_PARSE CPPCMS_DEFANDPARSE3(1,2,3) 827 #undef CPPCMS_URLBINDER_TPAR 828 #undef CPPCMS_URLBINDER_MPAR 829 #undef CPPCMS_URLBINDER_PPAR 830 #undef CPPCMS_URLBINDER_GPAR 831 #undef CPPCMS_URLBINDER_IPAR 832 #undef CPPCMS_URLBINDER_CPAR 833 #undef CPPCMS_URLBINDER_PARSE 836 #define CPPCMS_URLBINDER_TPAR typename P1,typename P2,typename P3,typename P4 837 #define CPPCMS_URLBINDER_MPAR P1,P2,P3,P4 838 #define CPPCMS_URLBINDER_PPAR p1,p2,p3,p4 839 #define CPPCMS_URLBINDER_GPAR int g1,g2,g3,g4; 840 #define CPPCMS_URLBINDER_IPAR int p1,int p2,int p3,int p4 841 #define CPPCMS_URLBINDER_CPAR g1(p1),g2(p2),g3(p3),g4(p4) 842 #define CPPCMS_URLBINDER_PARSE CPPCMS_DEFANDPARSE4(1,2,3,4) 846 #undef CPPCMS_URLBINDER_TPAR 847 #undef CPPCMS_URLBINDER_MPAR 848 #undef CPPCMS_URLBINDER_PPAR 849 #undef CPPCMS_URLBINDER_GPAR 850 #undef CPPCMS_URLBINDER_IPAR 851 #undef CPPCMS_URLBINDER_CPAR 852 #undef CPPCMS_URLBINDER_PARSE 855 #define CPPCMS_URLBINDER_TPAR typename P1,typename P2,typename P3,typename P4,typename P5 856 #define CPPCMS_URLBINDER_MPAR P1,P2,P3,P4,P5 857 #define CPPCMS_URLBINDER_PPAR p1,p2,p3,p4,p5 858 #define CPPCMS_URLBINDER_GPAR int g1,g2,g3,g4,g5; 859 #define CPPCMS_URLBINDER_IPAR int p1,int p2,int p3,int p4,int p5 860 #define CPPCMS_URLBINDER_CPAR g1(p1),g2(p2),g3(p3),g4(p4),g5(p5) 861 #define CPPCMS_URLBINDER_PARSE CPPCMS_DEFANDPARSE5(1,2,3,4,5) 865 #undef CPPCMS_URLBINDER_TPAR 866 #undef CPPCMS_URLBINDER_MPAR 867 #undef CPPCMS_URLBINDER_PPAR 868 #undef CPPCMS_URLBINDER_GPAR 869 #undef CPPCMS_URLBINDER_IPAR 870 #undef CPPCMS_URLBINDER_CPAR 871 #undef CPPCMS_URLBINDER_PARSE 874 #define CPPCMS_URLBINDER_TPAR typename P1,typename P2,typename P3,typename P4,typename P5,typename P6 875 #define CPPCMS_URLBINDER_MPAR P1,P2,P3,P4,P5,P6 876 #define CPPCMS_URLBINDER_PPAR p1,p2,p3,p4,p5,p6 877 #define CPPCMS_URLBINDER_GPAR int g1,g2,g3,g4,g5,g6; 878 #define CPPCMS_URLBINDER_IPAR int p1,int p2,int p3,int p4,int p5,int p6 879 #define CPPCMS_URLBINDER_CPAR g1(p1),g2(p2),g3(p3),g4(p4),g5(p5),g6(p6) 880 #define CPPCMS_URLBINDER_PARSE CPPCMS_DEFANDPARSE5(1,2,3,4,5) CPPCMS_DEFANDPARSE(6) 884 #undef CPPCMS_URLBINDER_TPAR 885 #undef CPPCMS_URLBINDER_MPAR 886 #undef CPPCMS_URLBINDER_PPAR 887 #undef CPPCMS_URLBINDER_GPAR 888 #undef CPPCMS_URLBINDER_IPAR 889 #undef CPPCMS_URLBINDER_CPAR 890 #undef CPPCMS_URLBINDER_PARSE 892 #define CPPCMS_URLBINDER_TPAR typename P1,typename P2,typename P3,typename P4,typename P5,typename P6,typename P7 893 #define CPPCMS_URLBINDER_MPAR P1,P2,P3,P4,P5,P6,P7 894 #define CPPCMS_URLBINDER_PPAR p1,p2,p3,p4,p5,p6,p7 895 #define CPPCMS_URLBINDER_GPAR int g1,g2,g3,g4,g5,g6,g7; 896 #define CPPCMS_URLBINDER_IPAR int p1,int p2,int p3,int p4,int p5,int p6,int p7 897 #define CPPCMS_URLBINDER_CPAR g1(p1),g2(p2),g3(p3),g4(p4),g5(p5),g6(p6),g7(p7) 898 #define CPPCMS_URLBINDER_PARSE CPPCMS_DEFANDPARSE5(1,2,3,4,5) CPPCMS_DEFANDPARSE2(6,7) 902 #undef CPPCMS_URLBINDER_TPAR 903 #undef CPPCMS_URLBINDER_MPAR 904 #undef CPPCMS_URLBINDER_PPAR 905 #undef CPPCMS_URLBINDER_GPAR 906 #undef CPPCMS_URLBINDER_IPAR 907 #undef CPPCMS_URLBINDER_CPAR 908 #undef CPPCMS_URLBINDER_PARSE 910 #define CPPCMS_URLBINDER_TPAR typename P1,typename P2,typename P3,typename P4,typename P5,typename P6,typename P7,typename P8 911 #define CPPCMS_URLBINDER_MPAR P1,P2,P3,P4,P5,P6,P7,P8 912 #define CPPCMS_URLBINDER_PPAR p1,p2,p3,p4,p5,p6,p7,p8 913 #define CPPCMS_URLBINDER_GPAR int g1,g2,g3,g4,g5,g6,g7,g8; 914 #define CPPCMS_URLBINDER_IPAR int p1,int p2,int p3,int p4,int p5,int p6,int p7,int p8 915 #define CPPCMS_URLBINDER_CPAR g1(p1),g2(p2),g3(p3),g4(p4),g5(p5),g6(p6),g7(p7),g8(p8) 916 #define CPPCMS_URLBINDER_PARSE CPPCMS_DEFANDPARSE5(1,2,3,4,5) CPPCMS_DEFANDPARSE3(6,7,8) 920 #undef CPPCMS_URLBINDER_TPAR 921 #undef CPPCMS_URLBINDER_MPAR 922 #undef CPPCMS_URLBINDER_PPAR 923 #undef CPPCMS_URLBINDER_GPAR 924 #undef CPPCMS_URLBINDER_IPAR 925 #undef CPPCMS_URLBINDER_CPAR 926 #undef CPPCMS_URLBINDER_PARSE 928 #undef CPPCMS_URLBINDER 929 #undef CPPCMS_URLBINDER_INIT 930 #undef CPPCMS_URLBINDER_PRD 931 #undef CPPCMS_URLBINDER_P1 932 #undef CPPCMS_URLBINDER_P2 This is a simple wrapper of PCRE library.
Definition: perl_regex.h:35
void assign(std::string const ®ex, void(C::*member)(std::string, std::string, std::string), C *object, int e1, int e2, int e3)
Definition: url_dispatcher.h:434
void range(char const *begin, char const *end)
Definition: steal_buf.h:106
char const * end() const
Definition: steal_buf.h:99
void assign(std::string const ®ex, void(C::*member)(std::string, std::string, std::string, std::string), C *object, int e1, int e2, int e3, int e4)
Definition: url_dispatcher.h:446
Definition: steal_buf.h:72
booster::function< bool(cppcms::application &, booster::cmatch const &)> generic_handler
RESTful API Handler that validates parameters and executes a method.
Definition: url_dispatcher.h:109
void assign(std::string const ®ex, void(C::*member)(std::string, std::string), C *object, int e1, int e2)
Definition: url_dispatcher.h:422
The object that hold the result of matching a regular expression against the text using regex_match a...
Definition: regex_match.h:110
void assign(std::string const ®ex, void(C::*member)(std::string), C *object, int e1)
Definition: url_dispatcher.h:410
void assign_generic(std::string const ®ex, void(C::*member)(booster::cmatch const &), C *object)
Definition: url_dispatcher.h:398
void assign(std::string const ®ex, void(C::*member)(std::string, std::string, std::string, std::string, std::string, std::string), C *object, int e1, int e2, int e3, int e4, int e5, int e6)
Definition: url_dispatcher.h:472
This is the namespace where all CppCMS functionality is placed.
Definition: application.h:19
void assign(std::string const ®ex, void(C::*member)(), C *object)
Definition: url_dispatcher.h:385
void assign(std::string const ®ex, void(C::*member)(std::string, std::string, std::string, std::string, std::string), C *object, int e1, int e2, int e3, int e4, int e5)
Definition: url_dispatcher.h:459
Definition: function.h:16
application class is the base class for all user created applications.
Definition: application.h:82
This class is used to glue between member function of application class and urls. ...
Definition: url_dispatcher.h:101
char const * begin() const
Definition: steal_buf.h:92
This class makes impossible to copy any class derived from this one.
Definition: noncopyable.h:15
bool parse_url_parameter(util::const_char_istream ¶meter, std::string &output)
Definition: url_dispatcher.h:30