1HTTP::Message(3) User Contributed Perl Documentation HTTP::Message(3)
2
3
4
6 HTTP::Message - HTTP style message (base class)
7
9 version 6.26
10
12 use base 'HTTP::Message';
13
15 An "HTTP::Message" object contains some headers and a content body.
16 The following methods are available:
17
18 $mess = HTTP::Message->new
19 $mess = HTTP::Message->new( $headers )
20 $mess = HTTP::Message->new( $headers, $content )
21 This constructs a new message object. Normally you would want
22 construct "HTTP::Request" or "HTTP::Response" objects instead.
23
24 The optional $header argument should be a reference to an
25 "HTTP::Headers" object or a plain array reference of key/value
26 pairs. If an "HTTP::Headers" object is provided then a copy of it
27 will be embedded into the constructed message, i.e. it will not be
28 owned and can be modified afterwards without affecting the message.
29
30 The optional $content argument should be a string of bytes.
31
32 $mess = HTTP::Message->parse( $str )
33 This constructs a new message object by parsing the given string.
34
35 $mess->headers
36 Returns the embedded "HTTP::Headers" object.
37
38 $mess->headers_as_string
39 $mess->headers_as_string( $eol )
40 Call the as_string() method for the headers in the message. This
41 will be the same as
42
43 $mess->headers->as_string
44
45 but it will make your program a whole character shorter :-)
46
47 $mess->content
48 $mess->content( $bytes )
49 The content() method sets the raw content if an argument is given.
50 If no argument is given the content is not touched. In either case
51 the original raw content is returned.
52
53 If the "undef" argument is given, the content is reset to its
54 default value, which is an empty string.
55
56 Note that the content should be a string of bytes. Strings in perl
57 can contain characters outside the range of a byte. The "Encode"
58 module can be used to turn such strings into a string of bytes.
59
60 $mess->add_content( $bytes )
61 The add_content() methods appends more data bytes to the end of the
62 current content buffer.
63
64 $mess->add_content_utf8( $string )
65 The add_content_utf8() method appends the UTF-8 bytes representing
66 the string to the end of the current content buffer.
67
68 $mess->content_ref
69 $mess->content_ref( \$bytes )
70 The content_ref() method will return a reference to content buffer
71 string. It can be more efficient to access the content this way if
72 the content is huge, and it can even be used for direct
73 manipulation of the content, for instance:
74
75 ${$res->content_ref} =~ s/\bfoo\b/bar/g;
76
77 This example would modify the content buffer in-place.
78
79 If an argument is passed it will setup the content to reference
80 some external source. The content() and add_content() methods will
81 automatically dereference scalar references passed this way. For
82 other references content() will return the reference itself and
83 add_content() will refuse to do anything.
84
85 $mess->content_charset
86 This returns the charset used by the content in the message. The
87 charset is either found as the charset attribute of the
88 "Content-Type" header or by guessing.
89
90 See
91 <http://www.w3.org/TR/REC-html40/charset.html#spec-char-encoding>
92 for details about how charset is determined.
93
94 $mess->decoded_content( %options )
95 Returns the content with any "Content-Encoding" undone and for
96 textual content the raw content encoded to Perl's Unicode strings.
97 If the "Content-Encoding" or "charset" of the message is unknown
98 this method will fail by returning "undef".
99
100 The following options can be specified.
101
102 "charset"
103 This override the charset parameter for text content. The
104 value "none" can used to suppress decoding of the charset.
105
106 "default_charset"
107 This override the default charset guessed by content_charset()
108 or if that fails "ISO-8859-1".
109
110 "alt_charset"
111 If decoding fails because the charset specified in the Content-
112 Type header isn't recognized by Perl's Encode module, then try
113 decoding using this charset instead of failing. The
114 "alt_charset" might be specified as "none" to simply return the
115 string without any decoding of charset as alternative.
116
117 "charset_strict"
118 Abort decoding if malformed characters is found in the content.
119 By default you get the substitution character ("\x{FFFD}") in
120 place of malformed characters.
121
122 "raise_error"
123 If TRUE then raise an exception if not able to decode content.
124 Reason might be that the specified "Content-Encoding" or
125 "charset" is not supported. If this option is FALSE, then
126 decoded_content() will return "undef" on errors, but will still
127 set $@.
128
129 "ref"
130 If TRUE then a reference to decoded content is returned. This
131 might be more efficient in cases where the decoded content is
132 identical to the raw content as no data copying is required in
133 this case.
134
135 $mess->decodable
136 HTTP::Message::decodable()
137 This returns the encoding identifiers that decoded_content() can
138 process. In scalar context returns a comma separated string of
139 identifiers.
140
141 This value is suitable for initializing the "Accept-Encoding"
142 request header field.
143
144 $mess->decode
145 This method tries to replace the content of the message with the
146 decoded version and removes the "Content-Encoding" header. Returns
147 TRUE if successful and FALSE if not.
148
149 If the message does not have a "Content-Encoding" header this
150 method does nothing and returns TRUE.
151
152 Note that the content of the message is still bytes after this
153 method has been called and you still need to call decoded_content()
154 if you want to process its content as a string.
155
156 $mess->encode( $encoding, ... )
157 Apply the given encodings to the content of the message. Returns
158 TRUE if successful. The "identity" (non-)encoding is always
159 supported; other currently supported encodings, subject to
160 availability of required additional modules, are "gzip", "deflate",
161 "x-bzip2" and "base64".
162
163 A successful call to this function will set the "Content-Encoding"
164 header.
165
166 Note that "multipart/*" or "message/*" messages can't be encoded
167 and this method will croak if you try.
168
169 $mess->parts
170 $mess->parts( @parts )
171 $mess->parts( \@parts )
172 Messages can be composite, i.e. contain other messages. The
173 composite messages have a content type of "multipart/*" or
174 "message/*". This method give access to the contained messages.
175
176 The argumentless form will return a list of "HTTP::Message"
177 objects. If the content type of $msg is not "multipart/*" or
178 "message/*" then this will return the empty list. In scalar
179 context only the first object is returned. The returned message
180 parts should be regarded as read-only (future versions of this
181 library might make it possible to modify the parent by modifying
182 the parts).
183
184 If the content type of $msg is "message/*" then there will only be
185 one part returned.
186
187 If the content type is "message/http", then the return value will
188 be either an "HTTP::Request" or an "HTTP::Response" object.
189
190 If a @parts argument is given, then the content of the message will
191 be modified. The array reference form is provided so that an empty
192 list can be provided. The @parts array should contain
193 "HTTP::Message" objects. The @parts objects are owned by $mess
194 after this call and should not be modified or made part of other
195 messages.
196
197 When updating the message with this method and the old content type
198 of $mess is not "multipart/*" or "message/*", then the content type
199 is set to "multipart/mixed" and all other content headers are
200 cleared.
201
202 This method will croak if the content type is "message/*" and more
203 than one part is provided.
204
205 $mess->add_part( $part )
206 This will add a part to a message. The $part argument should be
207 another "HTTP::Message" object. If the previous content type of
208 $mess is not "multipart/*" then the old content (together with all
209 content headers) will be made part #1 and the content type made
210 "multipart/mixed" before the new part is added. The $part object
211 is owned by $mess after this call and should not be modified or
212 made part of other messages.
213
214 There is no return value.
215
216 $mess->clear
217 Will clear the headers and set the content to the empty string.
218 There is no return value
219
220 $mess->protocol
221 $mess->protocol( $proto )
222 Sets the HTTP protocol used for the message. The protocol() is a
223 string like "HTTP/1.0" or "HTTP/1.1".
224
225 $mess->clone
226 Returns a copy of the message object.
227
228 $mess->as_string
229 $mess->as_string( $eol )
230 Returns the message formatted as a single string.
231
232 The optional $eol parameter specifies the line ending sequence to
233 use. The default is "\n". If no $eol is given then as_string will
234 ensure that the returned string is newline terminated (even when
235 the message content is not). No extra newline is appended if an
236 explicit $eol is passed.
237
238 $mess->dump( %opt )
239 Returns the message formatted as a string. In void context print
240 the string.
241
242 This differs from "$mess->as_string" in that it escapes the bytes
243 of the content so that it's safe to print them and it limits how
244 much content to print. The escapes syntax used is the same as for
245 Perl's double quoted strings. If there is no content the string
246 "(no content)" is shown in its place.
247
248 Options to influence the output can be passed as key/value pairs.
249 The following options are recognized:
250
251 maxlength => $num
252 How much of the content to show. The default is 512. Set this
253 to 0 for unlimited.
254
255 If the content is longer then the string is chopped at the
256 limit and the string "...\n(### more bytes not shown)"
257 appended.
258
259 no_content => $str
260 Replaces the "(no content)" marker.
261
262 prefix => $str
263 A string that will be prefixed to each line of the dump.
264
265 All methods unknown to "HTTP::Message" itself are delegated to the
266 "HTTP::Headers" object that is part of every message. This allows
267 convenient access to these methods. Refer to HTTP::Headers for details
268 of these methods:
269
270 $mess->header( $field => $val )
271 $mess->push_header( $field => $val )
272 $mess->init_header( $field => $val )
273 $mess->remove_header( $field )
274 $mess->remove_content_headers
275 $mess->header_field_names
276 $mess->scan( \&doit )
277
278 $mess->date
279 $mess->expires
280 $mess->if_modified_since
281 $mess->if_unmodified_since
282 $mess->last_modified
283 $mess->content_type
284 $mess->content_encoding
285 $mess->content_length
286 $mess->content_language
287 $mess->title
288 $mess->user_agent
289 $mess->server
290 $mess->from
291 $mess->referer
292 $mess->www_authenticate
293 $mess->authorization
294 $mess->proxy_authorization
295 $mess->authorization_basic
296 $mess->proxy_authorization_basic
297
299 Gisle Aas <gisle@activestate.com>
300
302 This software is copyright (c) 1994 by Gisle Aas.
303
304 This is free software; you can redistribute it and/or modify it under
305 the same terms as the Perl 5 programming language system itself.
306
307
308
309perl v5.32.0 2020-09-10 HTTP::Message(3)