1Mojo::Content(3) User Contributed Perl Documentation Mojo::Content(3)
2
3
4
6 Mojo::Content - HTTP content base class
7
9 package Mojo::Content::MyContent;
10 use Mojo::Base 'Mojo::Content';
11
12 sub body_contains {...}
13 sub body_size {...}
14 sub get_body_chunk {...}
15
17 Mojo::Content is an abstract base class for HTTP content containers,
18 based on RFC 7230 <https://tools.ietf.org/html/rfc7230> and RFC 7231
19 <https://tools.ietf.org/html/rfc7231>, like Mojo::Content::MultiPart
20 and Mojo::Content::Single.
21
23 Mojo::Content inherits all events from Mojo::EventEmitter and can emit
24 the following new ones.
25
26 body
27 $content->on(body => sub ($content) {...});
28
29 Emitted once all headers have been parsed and the body starts.
30
31 $content->on(body => sub ($content) {
32 $content->auto_upgrade(0) if $content->headers->header('X-No-MultiPart');
33 });
34
35 drain
36 $content->on(drain => sub ($content, $offset) {...});
37
38 Emitted once all data has been written.
39
40 $content->on(drain => sub ($content) {
41 $content->write_chunk(time);
42 });
43
44 read
45 $content->on(read => sub ($content, $bytes) {...});
46
47 Emitted when a new chunk of content arrives.
48
49 $content->on(read => sub ($content, $bytes) {
50 say "Streaming: $bytes";
51 });
52
54 Mojo::Content implements the following attributes.
55
56 auto_decompress
57 my $bool = $content->auto_decompress;
58 $content = $content->auto_decompress($bool);
59
60 Decompress content automatically if "is_compressed" is true.
61
62 auto_relax
63 my $bool = $content->auto_relax;
64 $content = $content->auto_relax($bool);
65
66 Try to detect when relaxed parsing is necessary.
67
68 headers
69 my $headers = $content->headers;
70 $content = $content->headers(Mojo::Headers->new);
71
72 Content headers, defaults to a Mojo::Headers object.
73
74 max_buffer_size
75 my $size = $content->max_buffer_size;
76 $content = $content->max_buffer_size(1024);
77
78 Maximum size in bytes of buffer for content parser, defaults to the
79 value of the "MOJO_MAX_BUFFER_SIZE" environment variable or 262144
80 (256KiB).
81
82 max_leftover_size
83 my $size = $content->max_leftover_size;
84 $content = $content->max_leftover_size(1024);
85
86 Maximum size in bytes of buffer for pipelined HTTP requests, defaults
87 to the value of the "MOJO_MAX_LEFTOVER_SIZE" environment variable or
88 262144 (256KiB).
89
90 relaxed
91 my $bool = $content->relaxed;
92 $content = $content->relaxed($bool);
93
94 Activate relaxed parsing for responses that are terminated with a
95 connection close.
96
97 skip_body
98 my $bool = $content->skip_body;
99 $content = $content->skip_body($bool);
100
101 Skip body parsing and finish after headers.
102
104 Mojo::Content inherits all methods from Mojo::EventEmitter and
105 implements the following new ones.
106
107 body_contains
108 my $bool = $content->body_contains('foo bar baz');
109
110 Check if content contains a specific string. Meant to be overloaded in
111 a subclass.
112
113 body_size
114 my $size = $content->body_size;
115
116 Content size in bytes. Meant to be overloaded in a subclass.
117
118 boundary
119 my $boundary = $content->boundary;
120
121 Extract multipart boundary from "Content-Type" header.
122
123 charset
124 my $charset = $content->charset;
125
126 Extract charset from "Content-Type" header.
127
128 clone
129 my $clone = $content->clone;
130
131 Return a new Mojo::Content object cloned from this content if possible,
132 otherwise return "undef".
133
134 generate_body_chunk
135 my $bytes = $content->generate_body_chunk(0);
136
137 Generate dynamic content.
138
139 get_body_chunk
140 my $bytes = $content->get_body_chunk(0);
141
142 Get a chunk of content starting from a specific position. Meant to be
143 overloaded in a subclass.
144
145 get_header_chunk
146 my $bytes = $content->get_header_chunk(13);
147
148 Get a chunk of the headers starting from a specific position. Note that
149 this method finalizes the content.
150
151 header_size
152 my $size = $content->header_size;
153
154 Size of headers in bytes. Note that this method finalizes the content.
155
156 headers_contain
157 my $bool = $content->headers_contain('foo bar baz');
158
159 Check if headers contain a specific string. Note that this method
160 finalizes the content.
161
162 is_chunked
163 my $bool = $content->is_chunked;
164
165 Check if "Transfer-Encoding" header indicates chunked transfer
166 encoding.
167
168 is_compressed
169 my $bool = $content->is_compressed;
170
171 Check "Content-Encoding" header for "gzip" value.
172
173 is_dynamic
174 my $bool = $content->is_dynamic;
175
176 Check if content will be dynamically generated, which prevents "clone"
177 from working.
178
179 is_finished
180 my $bool = $content->is_finished;
181
182 Check if parser is finished.
183
184 is_limit_exceeded
185 my $bool = $content->is_limit_exceeded;
186
187 Check if buffer has exceeded "max_buffer_size".
188
189 is_multipart
190 my $bool = $content->is_multipart;
191
192 False, this is not a Mojo::Content::MultiPart object.
193
194 is_parsing_body
195 my $bool = $content->is_parsing_body;
196
197 Check if body parsing started yet.
198
199 leftovers
200 my $bytes = $content->leftovers;
201
202 Get leftover data from content parser.
203
204 parse
205 $content
206 = $content->parse("Content-Length: 12\x0d\x0a\x0d\x0aHello World!");
207
208 Parse content chunk.
209
210 parse_body
211 $content = $content->parse_body('Hi!');
212
213 Parse body chunk and skip headers.
214
215 progress
216 my $size = $content->progress;
217
218 Size of content already received from message in bytes.
219
220 write
221 $content = $content->write;
222 $content = $content->write('');
223 $content = $content->write($bytes);
224 $content = $content->write($bytes => sub {...});
225
226 Write dynamic content non-blocking, the optional drain callback will be
227 executed once all data has been written. Calling this method without a
228 chunk of data will finalize the "headers" and allow for dynamic content
229 to be written later. You can write an empty chunk of data at any time
230 to end the stream.
231
232 # Make sure previous chunk of data has been written before continuing
233 $content->write('He' => sub ($content) {
234 $content->write('llo!' => sub ($content) {
235 $content->write('');
236 });
237 });
238
239 write_chunk
240 $content = $content->write_chunk;
241 $content = $content->write_chunk('');
242 $content = $content->write_chunk($bytes);
243 $content = $content->write_chunk($bytes => sub {...});
244
245 Write dynamic content non-blocking with chunked transfer encoding, the
246 optional drain callback will be executed once all data has been
247 written. Calling this method without a chunk of data will finalize the
248 "headers" and allow for dynamic content to be written later. You can
249 write an empty chunk of data at any time to end the stream.
250
251 # Make sure previous chunk of data has been written before continuing
252 $content->write_chunk('He' => sub ($content) {
253 $content->write_chunk('llo!' => sub ($content) {
254 $content->write_chunk('');
255 });
256 });
257
259 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
260
261
262
263perl v5.34.0 2022-01-21 Mojo::Content(3)