/
Rest.pm
187 lines (134 loc) · 5.35 KB
/
Rest.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
# See bottom of file for license and copyright information
=begin TML
---+!! package Foswiki::Request::Rest
Class to encapsulate request data for REST requests.
The following fields are parsed from the path_info and/or query params
* =
* =web= the requested web. Access using web method
* =topic= the requested topic. Access using topic
* =filename= the requested attachment filename
=cut
package Foswiki::Request::Rest;
use v5.14;
use Assert;
use IO::File ();
use Foswiki::Sandbox ();
use Try::Tiny;
use Foswiki::Exception;
use Foswiki::Class;
extends qw(Foswiki::Request);
has subject => (
is => 'rw',
lazy => 1,
default => sub { $_[0]->_pathParsed->{subject} },
);
has verb => (
is => 'rw',
lazy => 1,
default => sub { $_[0]->_pathParsed->{verb} },
);
has invalidSubject => (
is => 'rw',
lazy => 1,
default => sub { $_[0]->_pathParsed->{invalidSubject} },
);
has invalidVerb => (
is => 'rw',
lazy => 1,
default => sub { $_[0]->_pathParsed->{invalidVerb} },
);
=begin TML
---++ private objectMethod _establishAttributes() -> n/a
Used internally by the web(), topic() and attachment() methods to trigger parsing of the url and/or topic= parameter
and set object variables with the results. Attachment requests have to also accommodate redirect requests
where a pub/Web/Topic/Attachment path is redirected to a bin/viewfile request.
=cut
around _establishAttributes => sub {
my $orig = shift;
my $this = shift;
# For REST requests, the topic urlparam is the only way to get
# the web/topic.
my $parse;
if ( $this->param('topic') ) {
# SMELL XXX We must use the SUPER method to parse topic as it may fetch
# web from PATH_INFO which is incorrect for REST. Parsing has to be done
# right here.
$parse = $orig->( $this, $this->param('topic') );
print STDERR Data::Dumper::Dumper( \$parse )
if $Foswiki::Request::TRACE;
}
my $pathInfo = Foswiki::urlDecode( $this->pathInfo );
# Foswiki rest invocations are defined as having a subject (pluginName)
# and verb (restHandler in that plugin). Make sure the path_info is
# well-structured. Subject/verb or Subject.verb. URL Encode anything that
# doesn't pass validation.
unless ( $pathInfo =~ m#/(.*?)[./]([^/]*)# ) {
$parse->{invalidSubject} = Foswiki::urlEncode($pathInfo);
return $parse;
}
my $subject = $1;
my $verb = $2;
$parse->{subject} = Foswiki::Sandbox::untaint( $subject,
\&Foswiki::Sandbox::validateTopicName );
unless ( $parse->{subject} ) {
$parse->{invalidSubject} = Foswiki::urlEncode($subject);
}
$parse->{verb} =
Foswiki::Sandbox::untaint( $verb, \&Foswiki::Sandbox::validateTopicName );
unless ( $parse->{verb} ) {
$parse->{invalidVerb} = Foswiki::urlEncode($verb);
}
return $parse;
};
=begin TML
---++ ObjectAttribute subject ->$restSubject
Gets the REST subject parsed from the query path.
This either returns a valid parsed topic name, or undef.
* It does not filter out any illegal characters.
* There is no default Subject.
This is read only.
=cut
=begin TML
---++ ObjectAttribute invalidSubject -> "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
=begin TML
---++ ObjectAttribute verb ->$restVerb
Gets the REST verb parsed from the query path.
This either returns a valid parsed topic name, or undef.
* It does not filter out any illegal characters.
* There is no default Subject.
This is read only.
=cut
=begin TML
---++ ObjectAttribute invalidVerb -> "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
1;
__END__
Module of Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Copyright (C) 2008-2016 Foswiki Contributors. All Rights Reserved.
Foswiki Contributors are listed in the AUTHORS file in the root of this
distribution. NOTE: Please extend that file, not this notice.
This module is based/inspired on Catalyst framework, and also CGI,
CGI::Simple and HTTP::Headers modules. Refer to
http://search.cpan.org/~mramberg/Catalyst-Runtime-5.7010/lib/Catalyst.pm,
http://search.cpan.org/~lds/CGI.pm-3.29/CGI.pm,
http://search.cpan.org/author/ANDYA/CGI-Simple-1.103/lib/CGI/Simple.pm, and
http://search.cpan.org/~gaas/libwww-perl-5.808/lib/HTTP/Headers.pm
for full credits and license details.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version. For
more details read LICENSE in the root of this distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
As per the GPL, removal of this notice is prohibited.