/
Request.pm
1224 lines (914 loc) · 33.6 KB
/
Request.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# See bottom of file for license and copyright information
=begin TML
---+!! package Foswiki::Request
Class to encapsulate request data.
Fields:
* =action= action requested (view, edit, save, ...)
* =cookies= hashref whose keys are cookie names and values
are CGI::Cookie objects
* =headers= hashref whose keys are header name
* =method= request method (GET, HEAD, POST)
* =param= hashref of parameters, both query and body ones
* =param_list= arrayref with parameter names in received order
* =path_info= path_info of request (eg. /WebName/TopciName)
* =remote_address= Client's IP address
* =remote_user= Remote HTTP authenticated user
* =secure= Boolean value about use of encryption
* =server_port= Port that the webserver listens on
* =uploads= hashref whose keys are parameter name of uploaded
files
* =uri= the request uri
The following fields are parsed from the path_info
* =web= the requested web. Access using web method
* =topic= the requested topic. Access using topic
=cut
package Foswiki::Request;
use strict;
use warnings;
use CGI ();
our @ISA = ('CGI');
use Assert;
use Error ();
use IO::File ();
use CGI::Util qw(rearrange);
use Time::HiRes ();
use Foswiki::Sandbox ();
use constant TRACE => 0;
BEGIN {
if ( $Foswiki::cfg{UseLocale} ) {
require locale;
import locale();
}
}
sub getTime {
my $this = shift;
my $endTime = [Time::HiRes::gettimeofday];
my $timeDiff = Time::HiRes::tv_interval( $this->{start_time}, $endTime );
return $timeDiff;
}
=begin TML
---++ ClassMethod new([$initializer])
Constructs a Foswiki::Request object.
* =$initializer= - may be a filehandle or hashref.
* If it's a filehandle, it'll be used to reload the Foswiki::Request
object. See =save= method. Note: Restore only parameters
* It can be a hashref whose keys are parameter names. Values may be
arrayref's to multivalued parameters. Same note as above.
=cut
sub new {
my ( $proto, $initializer ) = @_;
my $this;
my $class = ref($proto) || $proto;
$this = {
action => '',
cookies => {},
headers => {},
method => undef,
param => {},
param_list => [],
path_info => '',
remote_address => '',
remote_user => undef,
secure => 0,
server_port => undef,
start_time => [Time::HiRes::gettimeofday],
uploads => {},
uri => '',
_pathParsed => undef,
web => undef,
invalidWeb => undef,
topic => undef,
invalidTopic => undef,
};
bless $this, $class;
if ( ref($initializer) eq 'HASH' ) {
while ( my ( $key, $value ) = each %$initializer ) {
$this->multi_param(
-name => $key,
-value => ref($value) eq 'ARRAY' ? [@$value] : [$value]
);
}
}
elsif ( ref($initializer) && UNIVERSAL::isa( $initializer, 'GLOB' ) ) {
$this->load($initializer);
}
return $this;
}
=begin TML
---++ ObjectMethod action([$action]) -> $action
Gets/Sets action requested (view, edit, save, ...)
=cut
sub action {
my ( $this, $action ) = @_;
if ( defined $action ) {
# Record the very first action set in this request. It will be required
# later if a redirect cache overlays this request.
$this->{base_action} = $action unless defined $this->{base_action};
$ENV{FOSWIKI_ACTION} = $this->{action} = $action;
return $action;
}
else {
return $this->{action};
}
}
=begin TML
---++ ObjectMethod base_action() -> $action
Get the first action ever set in this request object. This remains
unchanged even if a request cache is unwrapped on to of this request.
The idea is that callers can always find out the action that initiated
the HTTP request. This is required for (for example) checking access
controls.
=cut
sub base_action {
my $this = shift;
return defined $this->{base_action}
? $this->{base_action}
: $this->action();
}
=begin TML
---++ ObjectMethod method( [ $method ] ) -> $method
Sets/Gets request method (GET, HEAD, POST).
=cut
sub method {
return @_ == 1 ? $_[0]->{method} : ( $_[0]->{method} = $_[1] );
}
=begin TML
---++ ObjectMethod pathInfo( [ $path ] ) -> $path
Sets/Gets request path info.
Called without parameters returns current pathInfo.
There is a =path_info()= alias for compatibility with CGI.
Note that the string returned is a *URL encoded byte string*
i.e. it will only contain characters -A-Za-z0-9_.~!*\'();:@&=+$,/?%#[]
If you intend to analyse it, you will probably have to
Foswiki::urlDecode it first.
=cut
*path_info = \&pathInfo;
sub pathInfo {
return @_ == 1 ? $_[0]->{path_info} : ( $_[0]->{path_info} = $_[1] );
}
=begin TML
---++ ObjectMethod protocol() -> $protocol
Returns 'https' if secure connection. 'http' otherwise.
=cut
# SMELL : review this
sub protocol {
return $_[0]->secure ? 'https' : 'http';
}
=begin TML
---++ ObjectMethod uri( [$uri] ) -> $uri
Gets/Sets request uri.
=cut
sub uri {
return @_ == 1 ? $_[0]->{uri} : ( $_[0]->{uri} = $_[1] );
}
=begin TML
---++ ObjectMethod queryString() -> $query_string
Returns query_string part of request uri, if any.
=query_string()= alias provided for compatibility with CGI.
Note that the string returned is a *URL encoded byte string*
i.e. it will only contain characters -A-Za-z0-9_.~!*\'();:@&=+$,/?%#[]
If you intend to analyse it, you will probably have to
Foswiki::urlDecode it first.
=cut
*query_string = \&queryString;
sub queryString {
my $this = shift;
my @params;
foreach my $name ( $this->param ) {
next if $name eq 'POSTDATA';
my $key = Foswiki::urlEncode($name);
push @params,
map { $key . "=" . Foswiki::urlEncode( defined $_ ? $_ : '' ) }
$this->param($name);
}
return join( ';', @params );
}
=begin TML
---++ ObjectMethod url( [-full => 1,
-base => 1,
-absolute => 1,
-relative => 1,
-path => 1,
-query => 1] ) -> $url
Returns many url info.
* If called without parameters or with -full => 1 returns full url, e.g.
http://mysite.net/view
* If called with -base => 1 returns base url, e.g. http://foswiki.org
* -absolute => 1 returns absolute action path, e.g. /cgi-bin/view
* -relative => 1 returns relative action path, e.g. view
* -path => 1, -query => 1 also includes path info and query string
respectively
Note that the path and query components are returned as a *URL encoded byte string*
You will most likely need to Foswiki::urlDecode it for use.
Reasonably compatible with CGI corresponding method. Doesn't support
-rewrite. See Item5914.
=cut
sub url {
my ( $this, @p ) = @_;
my ( $relative, $absolute, $full, $base, $path_info, $query ) = rearrange(
[
qw(RELATIVE ABSOLUTE FULL BASE), [qw(PATH PATH_INFO)],
[qw(QUERY_STRING QUERY)],
],
@p
);
my $url;
$full++ if $base || !( $relative || $absolute );
my $path = $this->pathInfo;
my $name;
## See Foswiki.spec for the difference between ScriptUrlPath and ScriptUrlPaths
if ( defined $Foswiki::cfg{ScriptUrlPaths}{ $this->{action} } ) {
# When this is set, it is the complete script path including prefix/suffix.
$name = $Foswiki::cfg{ScriptUrlPaths}{ $this->{action} };
}
else {
$name = $Foswiki::cfg{ScriptUrlPath} . '/' . $this->{action};
# Don't add suffix if no script is used.
$name .= $Foswiki::cfg{ScriptSuffix} if $name;
}
$name =~ s(//+)(/)g;
if ($full) {
if ( $Foswiki::cfg{ForceDefaultUrlHost} ) {
$url = $Foswiki::cfg{DefaultUrlHost};
}
else {
my ( $client, $protocol, $host, $port ) =
Foswiki::Engine::_getConnectionData();
$port = ( $port && $port != 80 && $port != 443 ) ? ":$port" : '';
$url =
$host
? $protocol . '://' . $host . $port
: $Foswiki::cfg{DefaultUrlHost};
}
return $url if $base;
$url .= $name;
}
elsif ($relative) {
($url) = $name =~ m{([^/]+)$};
}
elsif ($absolute) {
$url = $name;
}
$url .= $path if $path_info && defined $path;
my $queryString = $this->queryString();
$url .= '?' . $queryString if $query && $queryString;
$url = '' unless defined $url;
return $url;
}
=begin TML
---++ ObjectMethod secure( [$secure] ) -> $secure
Gets/Sets connection's secure flag.
=cut
sub secure {
return @_ == 1 ? $_[0]->{secure} : ( $_[0]->{secure} = $_[1] );
}
=begin TML
---++ ObjectMethod remoteAddress( [$ip] ) -> $ip
Gets/Sets client IP address.
=remote_addr()= alias for compatibility with CGI.
=cut
*remote_addr = \&remoteAddress;
sub remoteAddress {
return @_ == 1
? $_[0]->{remote_address}
: ( $_[0]->{remote_address} = $_[1] );
}
=begin TML
---++ ObjectMethod remoteUser( [$userName] ) -> $userName
Gets/Sets remote user's name.
=remote_user()= alias for compatibility with CGI.
=cut
*remote_user = \&remoteUser;
sub remoteUser {
return @_ == 1 ? $_[0]->{remote_user} : ( $_[0]->{remote_user} = $_[1] );
}
=begin TML
---++ ObjectMethod serverPort( [$userName] ) -> $userName
Gets/Sets server user's name.
=server_port()= alias for compatibility with CGI.
=cut
*server_port = \&serverPort;
sub serverPort {
return @_ == 1 ? $_[0]->{server_port} : ( $_[0]->{server_port} = $_[1] );
}
=begin TML
---++ ObjectMethod queryParam( [-name => $name, -value => $value |
-name => $name, -values => [ $v1, $v2, ... ] |
$name, $v1, $v2, ... |
name, [ $v1, $v2, ... ]
] ) -> @paramNames | @values | $firstValue
This method is used by engines, during its prepare phase. Should not be used
anywhere else. Since bodyParam must exist and it has different semantics from
param method, this one exists for symmetry, and could be modified in the
future, so it could be possible to get query and body parameters independently.
=cut
sub queryParam {
my $this = shift;
return if $this->method && $this->method eq 'POST';
return $this->param(@_);
}
=begin TML
---++ ObjectMethod bodyParam( [-name => $name, -value => $value |
-name => $name, -values => [ $v1, $v2, ... ] |
$name, $v1, $v2, ... |
name, [ $v1, $v2, ... ]
] ) -> @paramNames | @values | $firstValue
Adds parameters passed within request body to the object. Should be called
only by engines. Otherwise use param() method.
=cut
sub bodyParam {
my $this = shift;
return $this->param(@_);
}
=begin TML
---++ ObjectMethod param( [-name => $name, -value => $value |
-name => $name, -values => [ $v1, $v2, ... ] |
$name, $v1, $v2, ... |
name, [ $v1, $v2, ... ]
] ) -> @paramNames | @values | $firstValue
* Called without parameters returns all parameter names
* Called only with parameter name or with -name => 'name'
* In list context returns all associated values (maybe empty list)
* In scalar context returns first value (maybe undef)
* Called with name and list of values or with
-name => 'name', -value => 'value' or -name => 'name', -values => [ ... ]
sets parameter value
* Returns parameter values as UTF-8 encoded binary strings
Resonably compatible with CGI.
*NOTE* this method will assert if it is called in a list context. A list
context might be:
* in a list of parameters e.g. =my_function( $query->param( ...=
* assigning to a list e.g. =my @l = $query->param(...=
* in a loop condition e.g. =foreach ($query->param(...=
The following are *scalar* contexts:
* =defined($query->param( ...= is OK
* =lc($query->param( ...= is OK
* =... if ( $query->param( ...= is OK
In a list context, you should call =multi_param= (fully compatible) to
retrieve list parameters.
=cut
sub multi_param {
my @list_of_params = param(@_);
return @list_of_params;
}
sub param {
my ( $this, @p ) = @_;
my ( $key, @value ) = rearrange( [ 'NAME', [qw(VALUE VALUES)] ], @p );
# param() - just return the list of param names
return @{ $this->{param_list} } unless defined $key;
# list context can be dangerous so warn:
# http://blog.gerv.net/2014.10/new-class-of-vulnerability-in-perl-web-applications
if ( DEBUG && wantarray ) {
my ( $package, $filename, $line ) = caller;
if ( substr( $package, 0, 16 ) ne 'Foswiki::Request' ) {
ASSERT( 0,
"Foswiki::Request::param called in list context from package $package, $filename line $line, declare as scalar, or call multi_param. "
);
}
}
if ( defined $value[0] ) {
push @{ $this->{param_list} }, $key
unless exists $this->{param}{$key};
$this->{param}{$key} = ref $value[0] eq 'ARRAY' ? $value[0] : [@value];
}
if ( defined $this->{param}{$key} ) {
return wantarray
? @{ $this->{param}{$key} }
: $this->{param}{$key}->[0];
}
else {
return wantarray ? () : undef;
}
}
=begin TML
---++ ObjectMethod cookie($name [, $value, $path, $secure, $expires]) -> $value
* If called without parameters returns a list of cookie names.
* If called only with =$name= parameter returns value of cookie
with that name or undef if it doesn't exist.
* If called with defined $value and other parameters returns a
CGI::Cookie object created with those parameters. Doesn't
store this new created cookie within request object. This way
for compatibility with CGI.
=cut
sub cookie {
eval { require CGI::Cookie; 1 } or throw Error::Simple($@);
my ( $this, @p ) = @_;
my ( $name, $value, $path, $secure, $expires ) =
rearrange( [ 'NAME', [qw(VALUE VALUES)], 'PATH', 'SECURE', 'EXPIRES' ],
@p );
unless ( defined $value ) {
return keys %{ $this->{cookies} } unless $name;
return () unless $this->{cookies}{$name};
return $this->{cookies}{$name}->value if defined $name && $name ne '';
}
return unless defined $name && $name ne '';
return CGI::Cookie->new(
-name => $name,
-value => $value,
-path => $path || '/',
-secure => $secure || $this->secure,
-expires => $expires || abs( $Foswiki::cfg{Sessions}{ExpireAfter} ),
-domain => $Foswiki::cfg{Sessions}{CookieRealm} || '',
-samesite => $Foswiki::cfg{Sessions}{SameSite} || 'Lax',
);
}
=begin TML
ObjectMethod cookies( \%cookies ) -> $hashref
Gets/Sets cookies hashref. Keys are cookie names
and values CGI::Cookie objects.
=cut
sub cookies {
return @_ == 1 ? $_[0]->{cookies} : ( $_[0]->{cookies} = $_[1] );
}
=begin TML
---++ ObjectMethod delete( @paramNames )
Deletes parameters from request.
=Delete()= alias provided for compatibility with CGI
=cut
*Delete = \&delete;
sub delete {
my $this = shift;
foreach my $p (@_) {
next unless exists $this->{param}{$p};
if ( my $upload = $this->{uploads}{ $this->param($p) } ) {
$upload->finish;
CORE::delete $this->{uploads}{ $this->param($p) };
}
CORE::delete $this->{param}{$p};
@{ $this->{param_list} } = grep { $_ ne $p } @{ $this->{param_list} };
}
}
=begin TML
---++ ObjectMethod deleteAll()
Deletes all parameter name and value(s).
=delete_all()= alias provided for compatibility with CGI.
=cut
*delete_all = \&deleteAll;
sub deleteAll {
my $this = shift;
$this->delete( $this->param() );
}
=begin TML
---++ ObjectMethod header([-name => $name, -value => $value |
-name => $name, -values => [ $v1, $v2, ... ] |
$name, $v1, $v2, ... |
name, [ $v1, $v2, ... ]
] ) -> @paramNames | @values | $firstValue
Gets/Sets a header field:
* Called without parameters returns all header field names
* Called only with header field name or with -name => 'name'
* In list context returns all associated values (maybe empty list)
* In scalar context returns the first value (maybe undef)
* Called with name and list of values or with
-name => 'name', -value => 'value' or -name => 'name', -values => [ ... ]
sets header field value
*Not compatible with CGI*, since CGI correspondent is a
response write method. CGI scripts obtain headers from %ENV
or =http= method. %ENV is not available and must be replaced
by calls to this and other methods of this class. =http= is
provided for compatibility, but is deprecated. Use this one
instead.
Calls to CGI =header= method must be replaced by calls to
Foswiki::Response =header= method.
=cut
sub header {
my ( $this, @p ) = @_;
my ( $key, @value ) = rearrange( [ 'NAME', [qw(VALUE VALUES)] ], @p );
return keys %{ $this->{headers} } unless $key;
$key =~ tr/_/-/;
$key = lc($key);
if ( defined $value[0] ) {
$this->{headers}{$key} =
ref $value[0] eq 'ARRAY' ? $value[0] : [@value];
}
if ( defined $this->{headers}{$key} ) {
return wantarray
? @{ $this->{headers}{$key} }
: $this->{headers}{$key}->[0];
}
else {
return wantarray ? () : undef;
}
}
=begin TML
---++ ObjectMethod save( $fh )
Saves object state to filehandle. Object may be loaded later
passing $fh to new constructor or by calling load().
=cut
sub save {
my ( $this, $fh ) = @_;
local ( $\, $, ) = ( '', '' );
foreach my $name ( $this->param ) {
foreach my $value ( $this->param($name) ) {
$value = '' unless defined $value;
next if $name eq '' && $value eq ''; # Item12371
print $fh Foswiki::urlEncode($name), '=',
Foswiki::urlEncode($value), "\n";
}
}
print $fh "=\n";
}
=begin TML
---++ ObjectMethod load( $fh )
Loads object state from filehandle, probably created with
a previous save().
=cut
sub load {
my ( $this, $file ) = @_;
my %param = ();
my @plist = ();
local $/ = "\n";
while (<$file>) {
chomp;
last if /^=$/;
my ( $key, $value ) =
map { defined $_ ? Foswiki::urlDecode($_) : $_ } split /=/;
# Item12956: Split returns only a single entry in array for null values.
# save sets null values to '', so load needs to restore '', not undef
$value = '' unless defined $value;
if ( exists $param{$key} ) {
push @{ $param{$key} }, $value;
}
else {
push @plist, $key;
$param{$key} = [$value];
}
}
foreach my $key (@plist) {
$this->multi_param( -name => $key, -value => $param{$key} );
}
}
=begin TML
---++ ObjectMethod upload( $name ) -> $handle
Called with file name parameter returns an open filehandle
to uploaded file.
=cut
sub upload {
my ( $this, $name ) = @_;
my $upload = $this->{uploads}{$name};
return defined $upload ? $upload->handle : undef;
}
=begin TML
---++ ObjectMethod uploadInfo( $fname ) -> $headers
Returns a hashref to information about uploaded
files as sent by browser.
=cut
sub uploadInfo {
return $_[0]->{uploads}{ $_[1] }->uploadInfo;
}
=begin TML
---++ ObjectMethod tmpFileName( $fname ) -> $tmpFileName
Returns the name of temporarly created file to store uploaded $fname.
$fname may be obtained by calling =param()= with form field name.
=cut
sub tmpFileName {
my ( $this, $fname ) = @_;
return $this->{uploads}{$fname}
? $this->{uploads}{$fname}->tmpFileName
: undef;
}
=begin TML
---++ ObjectMethod uploads( [ \%uploads ] ) -> $hashref
Gets/Sets request uploads field. Keys are uploaded file names,
as sent by browser, and values are Foswiki::Request::Upload objects.
=cut
sub uploads {
return @_ == 1 ? $_[0]->{uploads} : ( $_[0]->{uploads} = $_[1] );
}
# ======== possible accessors =======
# auth_type
# content_length
# content_type
=begin TML
---++ ObjectMethod http( [$header] ) -> $value DEPRECATED
Called without parameters returns a list of all available header filed names.
Given a field name returns value associated.
http('HTTP_USER_AGENT'); http('User-Agent') and http('User_Agent')
are equivalent.
Please, use =header()= instead. Present only for compatibility with CGI.
=cut
sub http {
my ( $this, $p ) = @_;
if ( defined $p ) {
$p =~ s/^https?[_-]//i;
return $this->header($p);
}
return $this->header();
}
=begin TML
---++ ObjectMethod https( [$name] ) -> $value || $secure DEPRECATED
Similar to =http()= method above. Called with no parameters returns
secure flag.
Please, use =header()= and =secure()= instead.
Present only for compatibility with CGI.
=cut
sub https {
my ( $this, $p ) = @_;
return !defined $p || $p =~ m/^https$/i ? $this->secure : $this->http($p);
}
=begin TML
---++ ObjectMethod userAgent() -> $userAgent;
Convenience method to get User-Agent string.
=user_agent()= alias provided for compatibility with CGI.
=cut
*user_agent = \&userAgent;
sub userAgent { shift->header('User-Agent') }
=begin TML
---++ ObjectMethod referer()
Convenience method to get Referer uri.
=cut
sub referer { shift->header('Referer') }
=begin TML
---++ ObjectMethod web() -> $baseweb
Gets the complete Web path parsed from the query path, or the topic=
query param. This either returns a valid parsed web path, or undef.
* It does not filter out any illegal characters.
* It does not set a default web.
This is read only.
=cut
sub web {
my $this = shift;
unless ( $this->{_pathParsed} ) {
$this->_establishAddress();
}
print STDERR "Request->web() returns " . ( $this->{web} || 'undef' ) . "\n"
if TRACE;
return $this->{web};
}
=begin TML
---++ ObjectMethod topic() -> $basetopic
Gets the complete topic name parsed from the query path, or the topic=
queryparam. This either returns a valid parsed topic name, or undef.
* It does not filter out any illegal characters.
* It does not set a default topic.
This is read only.
=cut
sub topic {
my $this = shift;
unless ( $this->{_pathParsed} ) {
$this->_establishAddress();
}
print STDERR "Request->topic() returns "
. ( $this->{topic} || 'undef' ) . "\n"
if TRACE;
return $this->{topic};
}
=begin TML
---++ ObjectMethod invalidWeb() -> "Invalid path component
Returns the bad part of the path, or the entire bad path, depending upon
the parsing process. Returns undef when the requested web is valid.
* It does not filter out or encode any illegal characters. Use caution when returning this string to the UI.
This is read only.
=cut
sub invalidWeb {
my $this = shift;
return $this->{invalidWeb};
}
=begin TML
---++ ObjectMethod invalidTopic() -> "Invalid requested topic"
Returns the invalid topic name, when the parser is able to identify it as a topic.
Returns undef when the requested topic is valid.
* It does not filter out or encode any illegal characters. Use caution when returning this string to the UI.
This is read only.
=cut
sub invalidTopic {
my $this = shift;
return $this->{invalidTopic};
}
=begin TML
---++ private objectMethod _establishAddress() -> n/a
Used internally by the web() and topic() methods to trigger parsing of the url and/or topic= parameter
and set object variables with the results.
=cut
sub _establishAddress {
my $this = shift;
# Allow topic= query param to override the path
my $topicParam = $this->param('topic');
my $parse =
Foswiki::Request::parse( $topicParam
|| Foswiki::urlDecode( $this->path_info() ) );
# Item3270 - here's the appropriate place to enforce spec
# http://develop.twiki.org/~twiki4/cgi-bin/view/Bugs/Item3270
$this->{topic} = ucfirst( $parse->{topic} )
if ( defined $parse->{topic} );
if ( $topicParam && !$parse->{web} ) {
$parse =
Foswiki::Request::parse( Foswiki::urlDecode( $this->path_info() ) )
; # Didn't get a web, so try the path
}
# Note that Web can still be undefined. Caller then determines if the
# defaultweb query param, or the HomeWeb config parameter should be used.
$this->{web} = $parse->{web};
$this->{invalidWeb} = $parse->{invalidWeb};
$this->{invalidTopic} = $parse->{invalidTopic};
$this->{_pathParsed} = 1;
}
=begin TML
---++ staticMethod parse($query_path, $topic_flag) ->
{ web => $web,
topic => $topic,
invalidWeb => optional,
invalidTopic => optional }
Parses the rquests query_path and returns a hash of web and topic names.
If passed a query string, it will parse it and return the extracted
web / topic.
*This method cannot set the web and topic parsed from the query path.*
Slash (/) can separate webs, subwebs and topics.
Dot (.) can *only* separate a web path from a topic.
Trailing slash disambiguates a topic from a subweb when both exist with same name.
With Strict URL parsing enabled, Leading slash disambiguates a web from a topic with a single part request.
/blah is assumed to be a web
blah is assumed to be a topic
If Strict URL parsing is disabled (the default), then a single component is allowed to be a topic:
/blah is a web *if it exists* Otherwise it is assumed to be a topic.
If any illegal characters are present, then web and/or topic are undefined. The original bad
components are returned in the invalidWeb or invalidTopic entries.