README.md
1# NAME
2
3Plack::Builder::Conditionals - Plack::Builder extension
4
5# SYNOPSIS
6
7 use Plack::Builder;
8 use Plack::Builder::Conditionals;
9 # exports "match_if, addr, path, method, header, browser, any, all"
10
11 builder {
12 enable match_if addr(['192.168.0.0/24','127.0.0.1']),
13 "Plack::Middleware::ReverseProxy";
14
15 enable match_if all( path(qr!^/private!), addr( '!', [qw!127.0.0.1 ::1!] ) ),
16 "Plack::Middleware::Auth::Basic", authenticator => \&authen_cb;
17
18 enable match_if sub { my $env = shift; $env->{HTTP_X_ENABLE_FRAMEWORK} },
19 "Plack::Middleware::XFramework", framework => 'Test';
20
21 $app;
22 };
23
24 use Plack::Builder::Conditionals -prefx => 'c';
25 # exports "c_match_if, c_addr, c_path, c_method, c_header, c_any, c_all"
26
27
28
29# DESCRIPTION
30
31Plack::Builder::Conditionals is..
32
33# FUNCTIONS
34
35- match\_if
36
37 enable match_if addr('127.0.0.1'), "Plack::Middleware::ReverseProxy";
38 enable match_if sub { $_[0]->{REMOTE_ADDR} eq '127.0.0.1' }, "Plack::Middleware::ReverseProxy";
39
40 As like Plack::Builder's enable\_if enable middleware if given conditions return true
41
42- addr
43
44 addr('127.0.0.1');
45 addr([qw!192.168.0.0/24 127.0.0.1 ::1!]);
46 addr('!','127.0.0.1');
47
48 return true if REMOTE\_ADDR is found in the CIDR range. If first argument is '!', return the opposite result.
49 This function supports IPv6 addresses
50
51- path
52
53 path('/')
54 path(qr!^/(\w+)/!)
55 path('!', qr!^/private!)
56
57 matching PATH\_INFO
58
59- method
60
61 method('GET')
62 method(qr/^(get|head)$/i)
63 method(qw(GET HEAD))
64 method('!','GET')
65 method('!', qr/^(post|put)$/i)
66 method('!', qw(POST PUT))
67- header
68
69 header('User-Agent',qr/iphone/)
70 header('If-Modified-Since') #exists check
71 header('!', 'User-Agent',qr/MSIE/)
72- browser
73
74 browser(qr/\bMSIE (7|8)/)
75 browser('!',qr!^Mozilla/4!);
76
77 Shortcut for header('User-Agent')
78
79- all
80
81 all( method('GET'), path(qr!^/static!) )
82
83 return true if all conditions are return true
84
85- any
86
87 any( path(qr!^/static!), path('/favicon.ico') )
88
89 return true if any condition return true
90
91# EXPORT
92
93 use Plack::Builder::Conditionals -prefx => 'c';
94 # exports "c_match_if, c_addr, c_path, c_method, c_header, c_any, c_all"
95
96
97you can add selected prefix to export functions
98
99# AUTHOR
100
101Masahiro Nagano <kazeburo {at} gmail.com>
102
103# SEE ALSO
104
105[Plack::Builder](http://search.cpan.org/perldoc?Plack::Builder)
106
107# LICENSE
108
109This library is free software; you can redistribute it and/or modify
110it under the same terms as Perl itself.
111