/
Header.pod
285 lines (168 loc) · 8.5 KB
/
Header.pod
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
=TITLE HTTP::Header
=SUBTITLE Object representing an individual HTTP header
=begin SYNOPSIS
use HTTP::Headers;
my $headers = HTTP::Headers.new;
my $ct = $headers.Content-Type;
$ct.value = 'text/html';
$ct.charset = 'UTF-8';
$headers.Allow = 'GET', 'POST';
$headers.Allow.push: 'PUT', 'DELETE';
$headers.Content-Length.init(42);
=end SYNOPSIS
=begin DESCRIPTION
This class provides additional tools for working with and manipulating a header and the values associated with it.
=end DESCRIPTION
=begin pod
=head1 Methods
=head2 method name
has $.name
This is the name of the header being manipulated. It is immutable. The name may be provided in one of two forms. It may be set to a member of the C<HTTP::Header::Standard::Name> enumeration or to a regular C<Str>. Either one will stringify to the canonical header name, i.e., the name given in an RFC or the header name with all words in Title-Case.
=head2 method key
method key returns Str
This is a stringified, lowercase version of the header name.
=head2 method values
has @.values is rw
At the core of a header is simply the list of values stored. Most headers only store a single value. According to RFC, only headers that can be set to comma-separated values are allowed to have multiple values. As such, when a single value is requested (e.g., see the C<method value>), these values will be joined together using a comma.
$headers.Allow = "GET", "POST";
say $headers.Allow.value;
# GET, POST
Normally, however, these values will generate one header line each using the same header name.
say $headers.Allow.as-string;
# Allow: GET
# Allow: POST
=head2 method value
method value is rw
Forces the header to operate as a single value. It can be used safely for reading and writing.
$headers.Allow = "GET", "POST";
say $headers.Allow.value;
# GET, POST
$headers.Allow.value = "PUT, DELETE";
say $headers.as-string;
# Allow: PUT, DELETE
=head2 method push
method push(*@values)
This method may be used to push additional values onto the end of the list of values already added to the header.
$headers.Allow = "GET";
$headers.Allow.push: "POST";
say ~$headers.Allow;
# GET, POST
=head2 method unshift
method unshift(*@values)
This method may be used to shift additional values onto the start of the list of values.
$headers.Allow = "GET";
$headers.Allow.unshift: "POST";
say ~$headers.Allow;
# POST, GET
=head2 method shift
method shift
Removes the first element from the values associated with this header and returns it.
$headers.Allow = "GET", "POST";
say $headers.Allow.shift;
# GET
=head2 method pop
method pop
Removes the last element from the values associated with this header and returns it.
$headers.Allow = "GET", "POST";
say $headers.Allow.pop;
# POST
=head2 method init
method init(*@values)
Sets the values list for this header to the given values if and only if no values have been set yet.
$headers.Allow.init("GET", "POST");
$headers.Content-Type = "text/html";
$headers.Content-Type.init("text/xml");
say $headers.as-string;
# Allow: GET
# Allow: POST
# Content-Type: text/html
=head2 method as-string
method as-string(Str :$eol = "\n")
Returns the header as a string. If there are multiple values, this will output those values separated by C<$eol> with the header name.
$headers.Allow = "GET", "POST";
say $headers.Allow.as-string;
# Allow: GET
# Allow: POST
say $headers.Allow.as-string(:eol("; "));
# Allow: GET; Allow: POST
This method is called by the C<as-string> method of L<HTTP::Headers> to output each header.
=head2 method Bool
method Bool
Returns C<True> if there are values set or C<False> otherwise.
=head2 method Str
This is the same as calling L</method value> to read the value. Note that this is B<not> the same as L</as-string>. The reason for this is just because it makes more sense to have the header stringify to the values in most cases:
$headers.Content-Type = "text/html";
say ~$headers.Content-Type;
# text/html
The parallel between those two statements is more obvious than the alternative.
=head2 method list
method list
This is a synonym for L</prepared-values>. It returns a finalized version of the values.
=head2 method postcircumfix:<[ ]>
method postcircumfix:<[ ]>($index)
You may use the [] operator to lookup values on the header. Basically, these are synonymous:
$header.values[*] === $header[*]
This is B<experimental> and might be removed or changed in the future.
=head2 method prepared-values
method prepared-values
Some headers may be set to values that are more easily dealt with as other kinds of values. For example, a C<Date> header might be most conveniently set from a L<DateTime> or L<Instant> object. Similarly, a C<Retry-After> column could be set to those or to a L<Duration>. Any header can be set to one of these values. When the value is output via L</value> or L</list> or L</as-string>, etc., the value will be converted to an appropriate string.
Currently only these types are supported:
=item * L<DateTime> is formatted as ddd, mmm dd yyyy HH:MM:ss GMT
=item * L<Instant> is converted to L<DateTime> and is formatted thes ame.
=item * L<Duration> is formatted as the number of seconds stored in it.
B<NOTE:> See the L</Caveats> for details on possible interactions between this method and others.
=head1 Primary/Parameter Convenience Methods
B<NOTE:> See the L</Caveats> for details on possible interactions between these methods and others.
=head2 method primary
method primary is rw
Returns the primary value of the header. Certain headers, such as C<Content-Type> allow for a main value followed by parameters. This grabs the main value only.
$headers.Content-Type = "text/html; charset=UTF-8";
say $headers.Content-Type.primary;
# text/html
$headers.Content-Type.primary = "text/xml";
say ~$headers.Content-Type;
# text/xml; charset=UTF-8
=head2 method params
method params
Returns all the parameter-like bits in the header as a hash.
$headers.Content-Type = "text/html; charset=UTF-8";
say $headers.Content-Type.params.perl;
# ("charset" => "UTF-8").hash
=head2 method param
method param($name) is rw
Provides a convenient way to write the parameters of a header.
$headers.Content-Type = "text/html";
$headers.Content-Type.param("charset") = "UTF-8";
say $headers.as-string;
# Content-Type: text/html; charset=UTF-8
$headers.Content-Type.param("charset") = "ISO-8859-1";
say $headers.as-string;
# Content-Type: text/html; charset=ISO-8859-1
Setting the value to an undefined value will delete the parameter.
$headers.Content-Type.param("charset") = Nil;
say $headers.as-string;
# Content-Type: text/html
=head1 Content-Type Convenience Methods
When the header's name is C<Content-Type>, some additional methods have been provided for your convenience.
=head2 method charset
method charset is rw
This is a convenient shortcut to the "charset" parameter.
$headers.Content-Type = "text/html";
$headers.Content-Type.charset = "UTF-8";
say $headers.as-string;
# Content-Type: text/html; charset=UTF-8
=head2 method is-text
method is-text returns Bool
Returns a true value if the C<Content-Type> is set to a text MIME type.
=head2 method is-html
method is-html returns Bool
Returns a true value if the C<Content-Type> is set to an HTML MIME type.
=head2 method is-xhtml
method is-xhtml returns Bool
Returns a true value if the C<Content-Type> is set to an XHTML MIME type.
=head1 Caveats
It should be noted that different kinds of headers require different sorts of interactions. These tools will allow certain interactions that will not behave well. This section is intended to highlight this issue.
It is generally assumed that the use of L</method primary>, L</method params>, and L</method param> and similar methods will be used on single-valued headers. These methods will work less well when multiple values are present.
Similarly, values that are prepared using L</method prepared-values>, may interact in unexpectd ways with L</method primary>, L</method params>, and L</method param> methods as well.
These interactions do not limit what you are able to do with this object, as these methods are only provided for convenience and making typical code look nicer. If you get bad interactions with these or other convenience methods, do not use them.
=end pod