This repository has been archived by the owner on Jun 25, 2020. It is now read-only.
/
Request.pm6
308 lines (276 loc) · 7.82 KB
/
Request.pm6
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
use v6;
unit class HTTP::Client::Request;
## This is the request class. It represents a request to an HTTP server.
use MIME::Base64;
#### Private constants
constant MULTIPART = 'multipart/form-data';
constant URLENCODED = 'application/x-www-form-urlencoded';
constant $CRLF = "\x0D\x0A";
#### Immutable public members.
has $.method; ## The HTTP Method for the request.
has $.client; ## Our parent HTTP::Client object.
#### Private members.
has $!proto; ## The protocol we will be connecting to.
has $!host; ## The host we are going to connect to.
has $!port; ## The port we are going to connect to.
has $!path; ## The path we are going to get.
has $!user; ## Username, if needed, for Authentication.
has $!pass; ## Password, if needed, for Authentication.
has $!auth; ## Auth type, can be Basic or Digest.
has $!type = URLENCODED; ## Default to urlencoded forms.
has $!query = ''; ## Part to add after the URL.
has $!data = ''; ## The data body for POST/PUT.
has @!headers; ## Extra headers in Pair format, for sending.
has $!boundary; ## A unique boundary, set on first use.
#### Grammars
## A grammar representing a URL, as per our usage anyway.
## This is temporary until the URI library is working under "master"
## then we'll move to using that instead, as it is far more complete.
grammar URL {
regex TOP {
^
<proto>
'://'
[<auth>'@']?
<host>
[':'<port>]?
<path>
$
}
token proto { \w+ }
token host { [\w|'.'|'-']+ }
token port { \d+ }
token user { \w+ } ## That's right, simplest usernames only.
token pass { [\w|'-'|'+'|'%']+ } ## Fairly simple passwords only too.
token auth { <user> ':' <pass> } ## This assumes Basic Auth.
regex path { .* }
}
#### Public Methods
## Encode a username and password into Base64 for Basic Auth.
method base64encode ($user, $pass) {
my $mime = MIME::Base64.new();
my $encoded = $mime.encode_base64($user~':'~$pass);
return $encoded;
}
## Parse a URL into host, port and path.
method url ($url) {
my $match = URL.parse($url);
if ($match) {
$!proto = ~$match<proto>;
$!host = ~$match<host>;
if ($match<port>) {
my $port = ~$match<port>;
# $*ERR.say: "port is "~$port.perl;
$!port = +$port;
# $*ERR.say: "Setting port to $!port";
}
if (~$match<path>) {
$!path = ~$match<path>;
} else {
$!path = '/';
}
if ($match<auth>) {
## The only auth we support via URL is Basic.
$!auth = 'Basic';
$!user = $match<auth><user>;
$!pass = $match<auth><pass>;
}
}
}
## Get the protocol
method protocol {
return $!proto;
}
## Get the hostname
method host {
return $!host;
}
## Get the custom port.
## If this is not set, the library requesting it should use
## whatever is the default port for the protocol.
method port {
return $!port;
}
## Get the path. If this is not set, you should use '/' or whatever
## makes sense in your application.
method path {
return $!path;
}
## Use multipart POST/PUT.
method multipart {
$!type = MULTIPART;
}
## Use urlencoded POST/PUT.
method urlencoded {
$!type = URLENCODED;
}
## Use some custom type. May be useful for some web services.
method set-type ($type) {
$!type = $type;
}
## Build a query (query string, or urlencoded form data.)
method build-query ($query is rw, %queries) {
for %queries.kv -> $name, $value {
if $query {
$query ~= '&';
}
my $val; ## Storage for the value, in case of array.
if $value ~~ Array {
$val = $value.join('&'~$name~'='); ## It looks evil, but it works.
}
else {
$val = $value;
}
$query ~= "$name=$val";
}
}
## Add query fields.
method add-query (*%queries) {
self.build-query($!query, %queries);
}
## Generate something fairly random.
method !randomstr {
my $num = time * 1000.rand.Int;
for 1..6.rand.Int+2 {
my $ran = 1000000.rand.Int;
if ($ran % 2) == 0 {
$num += $ran;
}
else {
$num -= $ran;
}
}
my $str = $num.base(36);
if 2.rand.Int {
$str.=lc;
}
return $str;
}
## Get the boundary (generate it if needed.)
method boundary {
if $!boundary { return $!boundary; }
$!boundary = (for 1..4 { self!randomstr }).join;
return $!boundary;
}
## Add data fields.
method add-field (*%queries) {
## First off, this only works on POST and PUT.
if $.method ne 'POST' | 'PUT' {
return self.add-query(|%queries);
}
if $!type eq URLENCODED {
self.build-query($!data, %queries);
}
elsif $!type eq MULTIPART {
for %queries.kv -> $name, $value {
if ($value ~~ Array) {
for @($value) -> $subval {
self.add-part($subval, :$name);
}
}
else {
self.add-part($value, :$name);
}
}
}
}
## Make a multipart section.
method make-part (
$boundary, $value, :$type, :$binary, :$disp='form-data', *%conf
) {
my $part = "--$boundary$CRLF";
$part ~= "Content-Disposition: $disp";
for %conf.kv -> $key, $val {
$part ~= "; $key=\"$val\"";
}
$part ~= $CRLF; ## End of disposition header.
if $type {
$part ~= "Content-Type: $type$CRLF";
}
if $binary {
$part ~= "Content-Transfer-Encoding: binary$CRLF";
}
$part ~= $CRLF; ## End of headers.
$part ~= $value ~ $CRLF;
return $part;
}
## Add a multipart section to our data.
method add-part ($value, :$type, :$binary, :$disp='form-data', *%conf) {
if $!type ne MULTIPART { return; } ## We only work on multipart.
$!data ~= self.make-part($.boundary, $value, :$type, :$binary, :$disp, |%conf);
}
## Add a file upload
method add-file (:$name!, :$filename!, :$content!, :$type, :$binary) {
self.add-part($content, :$type, :$binary, :$name, :$filename);
}
## Set the data directly (may be useful for some web services.)
method set-content ($content) {
$!data = $content;
}
## Add an extra header
method add-header (Pair $pair) {
@!headers.push: $pair;
}
## See if a given header exists
method has-header ($name) {
for @!headers -> $header {
if $header.key eq $name { return True; }
}
return False;
}
## The method that actually builds the Request
## that will be sent to the HTTP Server.
method Str {
my $version = $.client.http-version;
my $output = "$.method $!path HTTP/$version$CRLF";
self.add-header('Connection'=>'close');
if ! self.has-header('User-Agent') {
my $useragent = $.client.user-agent;
self.add-header('User-Agent'=>$useragent);
}
if $!port {
self.add-header('Host'=>$!host~':'~$!port);
}
else {
self.add-header('Host'=>$!host);
}
if ! self.has-header('Accept') {
## The following is a hideous workaround for a bug in vim
## which breaks the perl6 plugin. It is there for my editing sanity
## only, and does not affect the end result.
my $star = '*';
self.add-header('Accept'=>"$star/$star");
}
if $.method eq 'POST' | 'PUT' {
self.add-header('Content-Type'=>$!type);
}
if $!auth {
if $!auth eq 'Basic' { ## Only one we're supporting right now.
my $authstring = self.base64encode($!user, $!pass);
self.add-header('Authorization'=>"Basic $authstring");
}
}
if $!data {
if $!type eq MULTIPART {
## End our default boundary.
$!data ~= "--{$!boundary}--$CRLF";
}
my $length = $!data.chars;
self.add-header('Content-Length'=>$length);
}
## Okay, add the headers.
for @!headers -> $header {
$output ~= "{$header.key}: {$header.value}$CRLF";
}
if $!data {
$output ~= $CRLF; ## Add a blank line, notifying the end of headers.
$output ~= $!data; ## Add the data.
}
$output ~= $CRLF;
return $output;
}
## Execute the request. This is actually just a convenience
## wrapper for do-request() in the HTTP::Client class.
method run (:$follow) {
$.client.do-request(self, :$follow);
}