This repository has been archived by the owner on Jun 25, 2020. It is now read-only.
/
Response.pm6
174 lines (156 loc) · 4.21 KB
/
Response.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
use v6;
unit class HTTP::Client::Response;
## This is the response class. It represents a response from an HTTP server.
use HTTP::Status;
## Private constants
constant $CRLF = "\x0D\x0A";
## Public members.
has $.client; ## Our parent HTTP::Client object.
has $.status; ## The HTTP status code (numeric only.)
has $.message; ## The text HTTP status message.
has $.protocol; ## HTTP proto/version returned by server.
## Private members
has @!headers; ## The server response headers. An Array of Pairs.
## Use $response.headers() or $response.header()
## to get the headers.
has @!content; ## The body of the message from the server.
## Use $response.contents() for the array, or
## $response.content() for a string.
## We override new, and expect the response from the server
## to be passed in, as well as a copy of our HTTP::Client object.
method new ($server_response, $client) {
my @content = $server_response.split($CRLF);
my $status_line = @content.shift;
my ($protocol, $status, $message) = $status_line.split(/\s/);
if ! $message {
$message = get_http_status_msg($status);
}
my @headers;
while @content {
my $line = @content.shift;
last if $line eq ''; ## End of headers.
my ($name, $value) = $line.split(': ');
my $header = $name => $value;
@headers.push: $header;
}
# $*ERR.say: "Contents after: "~@content.perl;
# $*ERR.say: "Headers after: "~@headers.perl;
self.bless(
:$client, :$status, :$message, :$protocol ##,
# :headers(@headers), :content(@content) ## Bug? These aren't set.
)!initialize(@headers, @content);
}
## The initialize method is a hack, due to private members not being set.
## in the bless method.
method !initialize ($headers, $content) {
@!headers := @($headers);
@!content := @($content);
return self;
}
multi method headers () {
return @!headers;
}
multi method headers ($wanted) {
my @matched;
my $raw = False;
if $wanted ~~ Regex { $raw = True; }
for @!headers -> $header {
if $header.key ~~ $wanted {
if $raw {
@matched.push: $header;
}
else {
@matched.push: $header.value;
}
}
}
return @matched;
}
method header ($wanted) {
my $raw = False;
if $wanted ~~ Regex { $raw = True; }
for @!headers -> $header {
if $header.key ~~ $wanted {
if $raw {
return $header;
}
else {
return $header.value;
}
}
}
return; ## Sorry, we didn't find anything.
}
## de-chunking algorithm stolen shamelessly from LWP::Simple
method dechunk (@contents) {
my $transfer = self.header('Transfer-Encoding');
if ! $transfer || $transfer !~~ /:i chunked/ {
## dechunking only to be done if Transfer-Encoding says so.
return @contents;
}
my @con = ();
while @contents {
# Chunk start: length as hex word
my $length = @contents.shift;
## Chunk length is hex and could contain extensions.
## See RFC2616, 3.6.1 -- e.g. '5f32; xxx=...'
if $length ~~ /^ \w+ / {
$length = :16($length);
}
else {
last;
}
if $length == 0 {
last;
}
## Continue reading for '$length' bytes
while $length > 0 && @contents {
my $line = @contents.shift;
@con.push($line);
$length -= $line.chars; #.bytes, not .chars
}
}
return @con;
}
method contents (Bool :$dechunk=True) {
# $*ERR.say: "contents -- "~@!content.perl;
if $dechunk {
return self.dechunk(@!content);
}
return @!content;
}
method content (Bool :$dechunk=True) {
return self.contents(:$dechunk).join($CRLF);
}
method success (:$loose) {
if $loose {
if $.status ~~ /^2/ {
return True;
}
}
else {
if $.status ~~ /^200$/ {
return True;
}
}
return False;
}
method redirect (:$loose, :$url) {
if $loose {
if $.status ~~ /^3/ {
if $url {
return self.header('Location');
}
return True;
}
}
else {
if $.status ~~ /30 <[12]>/ {
if $url {
return self.header('Location');
}
return True;
}
}
return False;
}