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 con‐
19 struct "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( $content )
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( $data )
55 The add_content() methods appends more data to the end of the cur‐
56 rent content buffer.
57
58 $mess->content_ref
59 $mess->content_ref( \$content )
60 The content_ref() method will return a reference to content buffer
61 string. It can be more efficient to access the content this way if
62 the content is huge, and it can even be used for direct manipula‐
63 tion of the content, for instance:
64
65 ${$res->content_ref} =~ s/\bfoo\b/bar/g;
66
67 This example would modify the content buffer in-place.
68
69 If an argument is passed it will setup the content to reference
70 some external source. The content() and add_content() methods will
71 automatically dereference scalar references passed this way. For
72 other references content() will return the reference itself and
73 add_content() will refuse to do anything.
74
75 $mess->decoded_content( %options )
76 Returns the content with any "Content-Encoding" undone and strings
77 mapped to perl's Unicode strings. If the "Content-Encoding" or
78 "charset" of the message is unknown this method will fail by
79 returning "undef".
80
81 The following options can be specified.
82
83 "charset"
84 This override the charset parameter for text content. The
85 value "none" can used to suppress decoding of the charset.
86
87 "default_charset"
88 This override the default charset of "ISO-8859-1".
89
90 "raise_error"
91 If TRUE then raise an exception if not able to decode content.
92 Reason might be that the specified "Content-Encoding" or
93 "charset" is not supported. If this option is FALSE, then
94 decode_content() will return "undef" on errors, but will still
95 set $@.
96
97 "ref"
98 If TRUE then a reference to decoded content is returned. This
99 might be more efficient in cases where the decoded content is
100 identical to the raw content as no data copying is required in
101 this case.
102
103 $mess->parts
104 $mess->parts( @parts )
105 $mess->parts( \@parts )
106 Messages can be composite, i.e. contain other messages. The com‐
107 posite messages have a content type of "multipart/*" or "mes‐
108 sage/*". This method give access to the contained messages.
109
110 The argumentless form will return a list of "HTTP::Message"
111 objects. If the content type of $msg is not "multipart/*" or "mes‐
112 sage/*" then this will return the empty list. In scalar context
113 only the first object is returned. The returned message parts
114 should be regarded as are read only (future versions of this
115 library might make it possible to modify the parent by modifying
116 the parts).
117
118 If the content type of $msg is "message/*" then there will only be
119 one part returned.
120
121 If the content type is "message/http", then the return value will
122 be either an "HTTP::Request" or an "HTTP::Response" object.
123
124 If an @parts argument is given, then the content of the message
125 will modified. The array reference form is provided so that an
126 empty list can be provided. The @parts array should contain
127 "HTTP::Message" objects. The @parts objects are owned by $mess
128 after this call and should not be modified or made part of other
129 messages.
130
131 When updating the message with this method and the old content type
132 of $mess is not "multipart/*" or "message/*", then the content type
133 is set to "multipart/mixed" and all other content headers are
134 cleared.
135
136 This method will croak if the content type is "message/*" and more
137 than one part is provided.
138
139 $mess->add_part( $part )
140 This will add a part to a message. The $part argument should be
141 another "HTTP::Message" object. If the previous content type of
142 $mess is not "multipart/*" then the old content (together with all
143 content headers) will be made part #1 and the content type made
144 "multipart/mixed" before the new part is added. The $part object
145 is owned by $mess after this call and should not be modified or
146 made part of other messages.
147
148 There is no return value.
149
150 $mess->clear
151 Will clear the headers and set the content to the empty string.
152 There is no return value
153
154 $mess->protocol
155 $mess->protocol( $proto )
156 Sets the HTTP protocol used for the message. The protocol() is a
157 string like "HTTP/1.0" or "HTTP/1.1".
158
159 $mess->clone
160 Returns a copy of the message object.
161
162 $mess->as_string
163 $mess->as_string( $eol )
164 Returns the message formatted as a single string.
165
166 The optional $eol parameter specifies the line ending sequence to
167 use. The default is "\n". If no $eol is given then as_string will
168 ensure that the returned string is newline terminated (even when
169 the message content is not). No extra newline is appended if an
170 explicit $eol is passed.
171
172 All methods unknown to "HTTP::Message" itself are delegated to the
173 "HTTP::Headers" object that is part of every message. This allows con‐
174 venient access to these methods. Refer to HTTP::Headers for details of
175 these methods:
176
177 $mess->header( $field => $val )
178 $mess->push_header( $field => $val )
179 $mess->init_header( $field => $val )
180 $mess->remove_header( $field )
181 $mess->remove_content_headers
182 $mess->header_field_names
183 $mess->scan( \&doit )
184
185 $mess->date
186 $mess->expires
187 $mess->if_modified_since
188 $mess->if_unmodified_since
189 $mess->last_modified
190 $mess->content_type
191 $mess->content_encoding
192 $mess->content_length
193 $mess->content_language
194 $mess->title
195 $mess->user_agent
196 $mess->server
197 $mess->from
198 $mess->referer
199 $mess->www_authenticate
200 $mess->authorization
201 $mess->proxy_authorization
202 $mess->authorization_basic
203 $mess->proxy_authorization_basic
204
206 Copyright 1995-2004 Gisle Aas.
207
208 This library is free software; you can redistribute it and/or modify it
209 under the same terms as Perl itself.
210
211
212
213perl v5.8.8 2004-04-06 HTTP::Message(3)