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
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 "charset_strict"
105 Abort decoding if malformed characters is found in the content.
106 By default you get the substitution character ("\x{FFFD}") in
107 place of malformed characters.
108
109 "raise_error"
110 If TRUE then raise an exception if not able to decode content.
111 Reason might be that the specified "Content-Encoding" or
112 "charset" is not supported. If this option is FALSE, then
113 decoded_content() will return "undef" on errors, but will still
114 set $@.
115
116 "ref"
117 If TRUE then a reference to decoded content is returned. This
118 might be more efficient in cases where the decoded content is
119 identical to the raw content as no data copying is required in
120 this case.
121
122 $mess->decodable
123 HTTP::Message::decodable()
124 This returns the encoding identifiers that decoded_content() can
125 process. In scalar context returns a comma separated string of
126 identifiers.
127
128 This value is suitable for initializing the "Accept-Encoding"
129 request header field.
130
131 $mess->decode
132 This method tries to replace the content of the message with the
133 decoded version and removes the "Content-Encoding" header. Returns
134 TRUE if successful and FALSE if not.
135
136 If the message does not have a "Content-Encoding" header this
137 method does nothing and returns TRUE.
138
139 Note that the content of the message is still bytes after this
140 method has been called and you still need to call decoded_content()
141 if you want to process its content as a string.
142
143 $mess->encode( $encoding, ... )
144 Apply the given encodings to the content of the message. Returns
145 TRUE if successful. The "identity" (non-)encoding is always
146 supported; other currently supported encodings, subject to
147 availability of required additional modules, are "gzip", "deflate",
148 "x-bzip2" and "base64".
149
150 A successful call to this function will set the "Content-Encoding"
151 header.
152
153 Note that "multipart/*" or "message/*" messages can't be encoded
154 and this method will croak if you try.
155
156 $mess->parts
157 $mess->parts( @parts )
158 $mess->parts( \@parts )
159 Messages can be composite, i.e. contain other messages. The
160 composite messages have a content type of "multipart/*" or
161 "message/*". This method give access to the contained messages.
162
163 The argumentless form will return a list of "HTTP::Message"
164 objects. If the content type of $msg is not "multipart/*" or
165 "message/*" then this will return the empty list. In scalar
166 context only the first object is returned. The returned message
167 parts should be regarded as read-only (future versions of this
168 library might make it possible to modify the parent by modifying
169 the parts).
170
171 If the content type of $msg is "message/*" then there will only be
172 one part returned.
173
174 If the content type is "message/http", then the return value will
175 be either an "HTTP::Request" or an "HTTP::Response" object.
176
177 If an @parts argument is given, then the content of the message
178 will be modified. The array reference form is provided so that an
179 empty list can be provided. The @parts array should contain
180 "HTTP::Message" objects. The @parts objects are owned by $mess
181 after this call and should not be modified or made part of other
182 messages.
183
184 When updating the message with this method and the old content type
185 of $mess is not "multipart/*" or "message/*", then the content type
186 is set to "multipart/mixed" and all other content headers are
187 cleared.
188
189 This method will croak if the content type is "message/*" and more
190 than one part is provided.
191
192 $mess->add_part( $part )
193 This will add a part to a message. The $part argument should be
194 another "HTTP::Message" object. If the previous content type of
195 $mess is not "multipart/*" then the old content (together with all
196 content headers) will be made part #1 and the content type made
197 "multipart/mixed" before the new part is added. The $part object
198 is owned by $mess after this call and should not be modified or
199 made part of other messages.
200
201 There is no return value.
202
203 $mess->clear
204 Will clear the headers and set the content to the empty string.
205 There is no return value
206
207 $mess->protocol
208 $mess->protocol( $proto )
209 Sets the HTTP protocol used for the message. The protocol() is a
210 string like "HTTP/1.0" or "HTTP/1.1".
211
212 $mess->clone
213 Returns a copy of the message object.
214
215 $mess->as_string
216 $mess->as_string( $eol )
217 Returns the message formatted as a single string.
218
219 The optional $eol parameter specifies the line ending sequence to
220 use. The default is "\n". If no $eol is given then as_string will
221 ensure that the returned string is newline terminated (even when
222 the message content is not). No extra newline is appended if an
223 explicit $eol is passed.
224
225 $mess->dump( %opt )
226 Returns the message formatted as a string. In void context print
227 the string.
228
229 This differs from "$mess->as_string" in that it escapes the bytes
230 of the content so that it's safe to print them and it limits how
231 much content to print. The escapes syntax used is the same as for
232 Perl's double quoted strings. If there is no content the string
233 "(no content)" is shown in its place.
234
235 Options to influence the output can be passed as key/value pairs.
236 The following options are recognized:
237
238 maxlength => $num
239 How much of the content to show. The default is 512. Set this
240 to 0 for unlimited.
241
242 If the content is longer then the string is chopped at the
243 limit and the string "...\n(### more bytes not shown)"
244 appended.
245
246 prefix => $str
247 A string that will be prefixed to each line of the dump.
248
249 All methods unknown to "HTTP::Message" itself are delegated to the
250 "HTTP::Headers" object that is part of every message. This allows
251 convenient access to these methods. Refer to HTTP::Headers for details
252 of these methods:
253
254 $mess->header( $field => $val )
255 $mess->push_header( $field => $val )
256 $mess->init_header( $field => $val )
257 $mess->remove_header( $field )
258 $mess->remove_content_headers
259 $mess->header_field_names
260 $mess->scan( \&doit )
261
262 $mess->date
263 $mess->expires
264 $mess->if_modified_since
265 $mess->if_unmodified_since
266 $mess->last_modified
267 $mess->content_type
268 $mess->content_encoding
269 $mess->content_length
270 $mess->content_language
271 $mess->title
272 $mess->user_agent
273 $mess->server
274 $mess->from
275 $mess->referer
276 $mess->www_authenticate
277 $mess->authorization
278 $mess->proxy_authorization
279 $mess->authorization_basic
280 $mess->proxy_authorization_basic
281
283 Copyright 1995-2004 Gisle Aas.
284
285 This library is free software; you can redistribute it and/or modify it
286 under the same terms as Perl itself.
287
288
289
290perl v5.10.1 2009-10-03 HTTP::Message(3)