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