1Mojo::Content(3)      User Contributed Perl Documentation     Mojo::Content(3)
2
3
4

NAME

6       Mojo::Content - HTTP content base class
7

SYNOPSIS

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

DESCRIPTION

17       Mojo::Content is an abstract base class for HTTP content containers,
18       based on RFC 7230 <http://tools.ietf.org/html/rfc7230> and RFC 7231
19       <http://tools.ietf.org/html/rfc7231>, like Mojo::Content::MultiPart and
20       Mojo::Content::Single.
21

EVENTS

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 {
28           my $content = shift;
29           ...
30         });
31
32       Emitted once all headers have been parsed and the body starts.
33
34         $content->on(body => sub {
35           my $content = shift;
36           $content->auto_upgrade(0) if $content->headers->header('X-No-MultiPart');
37         });
38
39   drain
40         $content->on(drain => sub {
41           my ($content, $offset) = @_;
42           ...
43         });
44
45       Emitted once all data has been written.
46
47         $content->on(drain => sub {
48           my $content = shift;
49           $content->write_chunk(time);
50         });
51
52   read
53         $content->on(read => sub {
54           my ($content, $bytes) = @_;
55           ...
56         });
57
58       Emitted when a new chunk of content arrives.
59
60         $content->on(read => sub {
61           my ($content, $bytes) = @_;
62           say "Streaming: $bytes";
63         });
64

ATTRIBUTES

66       Mojo::Content implements the following attributes.
67
68   auto_decompress
69         my $bool = $content->auto_decompress;
70         $content = $content->auto_decompress($bool);
71
72       Decompress content automatically if "is_compressed" is true.
73
74   auto_relax
75         my $bool = $content->auto_relax;
76         $content = $content->auto_relax($bool);
77
78       Try to detect when relaxed parsing is necessary.
79
80   headers
81         my $headers = $content->headers;
82         $content    = $content->headers(Mojo::Headers->new);
83
84       Content headers, defaults to a Mojo::Headers object.
85
86   max_buffer_size
87         my $size = $content->max_buffer_size;
88         $content = $content->max_buffer_size(1024);
89
90       Maximum size in bytes of buffer for content parser, defaults to the
91       value of the "MOJO_MAX_BUFFER_SIZE" environment variable or 262144
92       (256KiB).
93
94   max_leftover_size
95         my $size = $content->max_leftover_size;
96         $content = $content->max_leftover_size(1024);
97
98       Maximum size in bytes of buffer for pipelined HTTP requests, defaults
99       to the value of the "MOJO_MAX_LEFTOVER_SIZE" environment variable or
100       262144 (256KiB).
101
102   relaxed
103         my $bool = $content->relaxed;
104         $content = $content->relaxed($bool);
105
106       Activate relaxed parsing for responses that are terminated with a
107       connection close.
108
109   skip_body
110         my $bool = $content->skip_body;
111         $content = $content->skip_body($bool);
112
113       Skip body parsing and finish after headers.
114

METHODS

116       Mojo::Content inherits all methods from Mojo::EventEmitter and
117       implements the following new ones.
118
119   body_contains
120         my $bool = $content->body_contains('foo bar baz');
121
122       Check if content contains a specific string. Meant to be overloaded in
123       a subclass.
124
125   body_size
126         my $size = $content->body_size;
127
128       Content size in bytes. Meant to be overloaded in a subclass.
129
130   boundary
131         my $boundary = $content->boundary;
132
133       Extract multipart boundary from "Content-Type" header.
134
135   charset
136         my $charset = $content->charset;
137
138       Extract charset from "Content-Type" header.
139
140   clone
141         my $clone = $content->clone;
142
143       Return a new Mojo::Content object cloned from this content if possible,
144       otherwise return "undef".
145
146   generate_body_chunk
147         my $bytes = $content->generate_body_chunk(0);
148
149       Generate dynamic content.
150
151   get_body_chunk
152         my $bytes = $content->get_body_chunk(0);
153
154       Get a chunk of content starting from a specific position. Meant to be
155       overloaded in a subclass.
156
157   get_header_chunk
158         my $bytes = $content->get_header_chunk(13);
159
160       Get a chunk of the headers starting from a specific position. Note that
161       this method finalizes the content.
162
163   header_size
164         my $size = $content->header_size;
165
166       Size of headers in bytes. Note that this method finalizes the content.
167
168   headers_contain
169         my $bool = $content->headers_contain('foo bar baz');
170
171       Check if headers contain a specific string. Note that this method
172       finalizes the content.
173
174   is_chunked
175         my $bool = $content->is_chunked;
176
177       Check if "Transfer-Encoding" header indicates chunked transfer
178       encoding.
179
180   is_compressed
181         my $bool = $content->is_compressed;
182
183       Check "Content-Encoding" header for "gzip" value.
184
185   is_dynamic
186         my $bool = $content->is_dynamic;
187
188       Check if content will be dynamically generated, which prevents "clone"
189       from working.
190
191   is_finished
192         my $bool = $content->is_finished;
193
194       Check if parser is finished.
195
196   is_limit_exceeded
197         my $bool = $content->is_limit_exceeded;
198
199       Check if buffer has exceeded "max_buffer_size".
200
201   is_multipart
202         my $bool = $content->is_multipart;
203
204       False, this is not a Mojo::Content::MultiPart object.
205
206   is_parsing_body
207         my $bool = $content->is_parsing_body;
208
209       Check if body parsing started yet.
210
211   leftovers
212         my $bytes = $content->leftovers;
213
214       Get leftover data from content parser.
215
216   parse
217         $content
218           = $content->parse("Content-Length: 12\x0d\x0a\x0d\x0aHello World!");
219
220       Parse content chunk.
221
222   parse_body
223         $content = $content->parse_body('Hi!');
224
225       Parse body chunk and skip headers.
226
227   progress
228         my $size = $content->progress;
229
230       Size of content already received from message in bytes.
231
232   write
233         $content = $content->write;
234         $content = $content->write('');
235         $content = $content->write($bytes);
236         $content = $content->write($bytes => sub {...});
237
238       Write dynamic content non-blocking, the optional drain callback will be
239       executed once all data has been written. Calling this method without a
240       chunk of data will finalize the "headers" and allow for dynamic content
241       to be written later. You can write an empty chunk of data at any time
242       to end the stream.
243
244         # Make sure previous chunk of data has been written before continuing
245         $content->write('He' => sub {
246           my $content = shift;
247           $content->write('llo!' => sub {
248             my $content = shift;
249             $content->write('');
250           });
251         });
252
253   write_chunk
254         $content = $content->write_chunk;
255         $content = $content->write_chunk('');
256         $content = $content->write_chunk($bytes);
257         $content = $content->write_chunk($bytes => sub {...});
258
259       Write dynamic content non-blocking with chunked transfer encoding, the
260       optional drain callback will be executed once all data has been
261       written. Calling this method without a chunk of data will finalize the
262       "headers" and allow for dynamic content to be written later. You can
263       write an empty chunk of data at any time to end the stream.
264
265         # Make sure previous chunk of data has been written before continuing
266         $content->write_chunk('He' => sub {
267           my $content = shift;
268           $content->write_chunk('llo!' => sub {
269             my $content = shift;
270             $content->write_chunk('');
271           });
272         });
273

SEE ALSO

275       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
276
277
278
279perl v5.30.1                      2020-01-30                  Mojo::Content(3)
Impressum