gitprep / mojo / lib / Mojo / Content.pm /
Newer Older
587 lines | 14.355kb
copy gitweblite soruce code
root authored on 2012-11-23
1
package Mojo::Content;
2
use Mojo::Base 'Mojo::EventEmitter';
3

            
4
use Carp 'croak';
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
5
BEGIN {eval {require Compress::Raw::Zlib; import Compress::Raw::Zlib qw(WANT_GZIP Z_STREAM_END)}}
copy gitweblite soruce code
root authored on 2012-11-23
6
use Mojo::Headers;
7

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
8
has [qw(auto_relax relaxed skip_body)];
9
has headers           => sub { Mojo::Headers->new };
10
has max_buffer_size   => sub { $ENV{MOJO_MAX_BUFFER_SIZE} || 262144 };
copy gitweblite soruce code
root authored on 2012-11-23
11
has max_leftover_size => sub { $ENV{MOJO_MAX_LEFTOVER_SIZE} || 262144 };
12

            
13
sub body_contains {
14
  croak 'Method "body_contains" not implemented by subclass';
15
}
16

            
17
sub body_size { croak 'Method "body_size" not implemented by subclass' }
18

            
19
sub boundary {
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
20
  return undef unless my $type = shift->headers->content_type;
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
21
  $type =~ m!multipart.*boundary\s*=\s*(?:"([^"]+)"|([\w'(),.:?\-+/]+))!i
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
22
    and return defined $1 ? $1 : $2;
23
  return undef;
copy gitweblite soruce code
root authored on 2012-11-23
24
}
25

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
26
sub build_body    { shift->_build('get_body_chunk') }
27
sub build_headers { shift->_build('get_header_chunk') }
copy gitweblite soruce code
root authored on 2012-11-23
28

            
29
sub charset {
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
30
  my $type = do { my $type = shift->headers->content_type; defined $type ? $type : ''};
31
  return $type =~ /charset\s*=\s*"?([^"\s;]+)"?/i ? $1 : undef;
copy gitweblite soruce code
root authored on 2012-11-23
32
}
33

            
34
sub clone {
35
  my $self = shift;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
36
  return undef if $self->is_dynamic;
copy gitweblite soruce code
root authored on 2012-11-23
37
  return $self->new(headers => $self->headers->clone);
38
}
39

            
40
sub generate_body_chunk {
41
  my ($self, $offset) = @_;
42

            
43
  $self->emit(drain => $offset)
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
44
    if !delete $self->{delay} && !length(defined $self->{body_buffer} ? $self->{body_buffer} : '');
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
45
  my $chunk = do { my $tmp = delete $self->{body_buffer}; defined $tmp ? $tmp : '' };
copy gitweblite soruce code
root authored on 2012-11-23
46
  return $self->{eof} ? '' : undef unless length $chunk;
47

            
48
  return $chunk;
49
}
50

            
51
sub get_body_chunk {
52
  croak 'Method "get_body_chunk" not implemented by subclass';
53
}
54

            
55
sub get_header_chunk {
56
  my ($self, $offset) = @_;
57

            
58
  unless (defined $self->{header_buffer}) {
59
    my $headers = $self->headers->to_string;
60
    $self->{header_buffer}
61
      = $headers ? "$headers\x0d\x0a\x0d\x0a" : "\x0d\x0a";
62
  }
63

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
64
  return substr $self->{header_buffer}, $offset, 131072;
copy gitweblite soruce code
root authored on 2012-11-23
65
}
66

            
67
sub header_size { length shift->build_headers }
68

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
69
sub is_chunked { !!shift->headers->transfer_encoding }
copy gitweblite soruce code
root authored on 2012-11-23
70

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
71
sub is_compressed { (do {my $tmp = shift->headers->content_encoding; defined $tmp ? $tmp : ''}) =~ /^gzip$/i }
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
72

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
73
sub is_dynamic { $_[0]{dynamic} && !defined $_[0]->headers->content_length }
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
74

            
75
sub is_finished { my $tmp = shift->{state}; (defined $tmp ? $tmp : '') eq 'finished' }
copy gitweblite soruce code
root authored on 2012-11-23
76

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
77
sub is_limit_exceeded { !!shift->{limit} }
copy gitweblite soruce code
root authored on 2012-11-23
78

            
79
sub is_multipart {undef}
80

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
81
sub is_parsing_body { my $tmp = shift->{state}; (defined $tmp ? $tmp : '') eq 'body' }
copy gitweblite soruce code
root authored on 2012-11-23
82

            
83
sub leftovers { shift->{buffer} }
84

            
85
sub parse {
86
  my $self = shift;
87

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
88
  # Headers
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
89
  $self->_parse_until_body(@_);
copy gitweblite soruce code
root authored on 2012-11-23
90
  return $self if $self->{state} eq 'headers';
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
91
  $self->emit('body') unless $self->{body}++;
copy gitweblite soruce code
root authored on 2012-11-23
92

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
93
  # Chunked content
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
94
  $self->{real_size} = defined $self->{real_size} ? $self->{real_size} : 0;
95
  if ($self->is_chunked && $self->{state} ne 'headers') {
copy gitweblite soruce code
root authored on 2012-11-23
96
    $self->_parse_chunked;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
97
    $self->{state} = 'finished' if (defined $self->{chunk_state} ? $self->{chunk_state} : '') eq 'finished';
copy gitweblite soruce code
root authored on 2012-11-23
98
  }
99

            
100
  # Not chunked, pass through to second buffer
101
  else {
102
    $self->{real_size} += length $self->{pre_buffer};
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
103
    my $limit = $self->is_finished
104
      && length($self->{buffer}) > $self->max_leftover_size;
105
    $self->{buffer} .= $self->{pre_buffer} unless $limit;
copy gitweblite soruce code
root authored on 2012-11-23
106
    $self->{pre_buffer} = '';
107
  }
108

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
109
  # No content
110
  if ($self->skip_body) {
111
    $self->{state} = 'finished';
112
    return $self;
113
  }
114

            
115
  # Relaxed parsing
116
  my $headers = $self->headers;
117
  if ($self->auto_relax) {
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
118
    my $connection = defined $headers->connection ? $headers->connection : '';
119
    my $len        = defined $headers->content_length ? $headers->content_length : '';
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
120
    $self->relaxed(1)
121
      if !length $len && ($connection =~ /close/i || $headers->content_type);
122
  }
123

            
copy gitweblite soruce code
root authored on 2012-11-23
124
  # Chunked or relaxed content
125
  if ($self->is_chunked || $self->relaxed) {
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
126
    $self->{size} += length($self->{buffer} = defined $self->{buffer} ? $self->{buffer} : '');
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
127
    $self->_uncompress($self->{buffer});
128
    $self->{buffer} = '';
copy gitweblite soruce code
root authored on 2012-11-23
129
  }
130

            
131
  # Normal content
132
  else {
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
133
    my $len = $headers->content_length || 0;
copy gitweblite soruce code
root authored on 2012-11-23
134
    $self->{size} ||= 0;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
135
    if ((my $need = $len - $self->{size}) > 0) {
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
136
      my $len = length $self->{buffer};
137
      my $chunk = substr $self->{buffer}, 0, $need > $len ? $len : $need, '';
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
138
      $self->_uncompress($chunk);
copy gitweblite soruce code
root authored on 2012-11-23
139
      $self->{size} += length $chunk;
140
    }
141
    $self->{state} = 'finished' if $len <= $self->progress;
142
  }
143

            
144
  return $self;
145
}
146

            
147
sub parse_body {
148
  my $self = shift;
149
  $self->{state} = 'body';
150
  return $self->parse(@_);
151
}
152

            
153
sub progress {
154
  my $self = shift;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
155
  return 0 unless my $state = $self->{state};
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
156
  return 0 unless $state eq 'body' || $state eq 'finished';
copy gitweblite soruce code
root authored on 2012-11-23
157
  return $self->{raw_size} - ($self->{header_size} || 0);
158
}
159

            
160
sub write {
161
  my ($self, $chunk, $cb) = @_;
162

            
163
  $self->{dynamic} = 1;
164
  if (defined $chunk) { $self->{body_buffer} .= $chunk }
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
165
  else                { $self->{delay} = 1 }
copy gitweblite soruce code
root authored on 2012-11-23
166
  $self->once(drain => $cb) if $cb;
167
  $self->{eof} = 1 if defined $chunk && $chunk eq '';
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
168

            
169
  return $self;
copy gitweblite soruce code
root authored on 2012-11-23
170
}
171

            
172
sub write_chunk {
173
  my ($self, $chunk, $cb) = @_;
174
  $self->headers->transfer_encoding('chunked') unless $self->is_chunked;
175
  $self->write(defined $chunk ? $self->_build_chunk($chunk) : $chunk, $cb);
176
  $self->{eof} = 1 if defined $chunk && $chunk eq '';
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
177
  return $self;
copy gitweblite soruce code
root authored on 2012-11-23
178
}
179

            
180
sub _build {
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
181
  my ($self, $method) = @_;
copy gitweblite soruce code
root authored on 2012-11-23
182

            
183
  my $buffer = '';
184
  my $offset = 0;
185
  while (1) {
186

            
187
    # No chunk yet, try again
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
188
    next unless defined(my $chunk = $self->$method($offset));
copy gitweblite soruce code
root authored on 2012-11-23
189

            
190
    # End of part
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
191
    last unless my $len = length $chunk;
copy gitweblite soruce code
root authored on 2012-11-23
192

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
193
    $offset += $len;
copy gitweblite soruce code
root authored on 2012-11-23
194
    $buffer .= $chunk;
195
  }
196

            
197
  return $buffer;
198
}
199

            
200
sub _build_chunk {
201
  my ($self, $chunk) = @_;
202

            
203
  # End
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
204
  return "\x0d\x0a0\x0d\x0a\x0d\x0a" if length $chunk == 0;
copy gitweblite soruce code
root authored on 2012-11-23
205

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
206
  # First chunk has no leading CRLF
207
  my $crlf = $self->{chunks}++ ? "\x0d\x0a" : '';
208
  return $crlf . sprintf('%x', length $chunk) . "\x0d\x0a$chunk";
copy gitweblite soruce code
root authored on 2012-11-23
209
}
210

            
211
sub _parse_chunked {
212
  my $self = shift;
213

            
214
  # Trailing headers
215
  return $self->_parse_chunked_trailing_headers
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
216
    if (defined $self->{chunk_state} ? $self->{chunk_state} : '') eq 'trailing_headers';
copy gitweblite soruce code
root authored on 2012-11-23
217

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
218
  while (my $len = length $self->{pre_buffer}) {
copy gitweblite soruce code
root authored on 2012-11-23
219

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
220
    # Start new chunk (ignore the chunk extension)
221
    unless ($self->{chunk_len}) {
222
      last
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
223
        unless $self->{pre_buffer} =~ s/^(?:\x0d?\x0a)?([0-9a-fA-F]+).*\x0a//;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
224
      next if $self->{chunk_len} = hex $1;
copy gitweblite soruce code
root authored on 2012-11-23
225

            
226
      # Last chunk
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
227
      $self->{chunk_state} = 'trailing_headers';
228
      last;
copy gitweblite soruce code
root authored on 2012-11-23
229
    }
230

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
231
    # Remove as much as possible from payload
232
    $len = $self->{chunk_len} if $self->{chunk_len} < $len;
233
    $self->{buffer} .= substr $self->{pre_buffer}, 0, $len, '';
234
    $self->{real_size} += $len;
235
    $self->{chunk_len} -= $len;
copy gitweblite soruce code
root authored on 2012-11-23
236
  }
237

            
238
  # Trailing headers
239
  $self->_parse_chunked_trailing_headers
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
240
    if (defined $self->{chunk_state} ? $self->{chunk_state} : '') eq 'trailing_headers';
241

            
242
  # Check buffer size
243
  $self->{limit} = $self->{state} = 'finished'
244
    if length(defined $self->{pre_buffer} ? $self->{pre_buffer} : '') > $self->max_buffer_size;
copy gitweblite soruce code
root authored on 2012-11-23
245
}
246

            
247
sub _parse_chunked_trailing_headers {
248
  my $self = shift;
249

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
250
  my $headers = $self->headers->parse(delete $self->{pre_buffer});
251
  return unless $headers->is_finished;
252
  $self->{chunk_state} = 'finished';
253

            
254
  # Replace Transfer-Encoding with Content-Length
255
  $headers->remove('Transfer-Encoding');
256
  $headers->content_length($self->{real_size}) unless $headers->content_length;
copy gitweblite soruce code
root authored on 2012-11-23
257
}
258

            
259
sub _parse_headers {
260
  my $self = shift;
261

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
262
  my $headers = $self->headers->parse(delete $self->{pre_buffer});
263
  return unless $headers->is_finished;
264
  $self->{state} = 'body';
265

            
266
  # Take care of leftovers
267
  my $leftovers = $self->{pre_buffer} = $headers->leftovers;
268
  $self->{header_size} = $self->{raw_size} - length $leftovers;
269
  $self->emit('body') unless $self->{body}++;
270
}
271

            
272
sub _parse_until_body {
273
  my ($self, $chunk) = @_;
274

            
275
  $self->{raw_size} += length($chunk = defined $chunk ? $chunk : '');
276
  $self->{pre_buffer} .= $chunk;
277

            
278
  unless ($self->{state}) {
279
    $self->{header_size} = $self->{raw_size} - length $self->{pre_buffer};
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
280
    $self->{state}       = 'headers';
copy gitweblite soruce code
root authored on 2012-11-23
281
  }
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
282
  $self->_parse_headers if (defined $self->{state} ? $self->{state} : '') eq 'headers';
283
}
284

            
285
sub _uncompress {
286
  my ($self, $chunk) = @_;
287

            
288
  # No compression
289
  return $self->emit(read => $chunk) unless $self->is_compressed;
290

            
291
  # Uncompress
292
  $self->{post_buffer} .= $chunk;
293
  my $gz = $self->{gz} = defined $self->{gz} ? $self->{gz} : 
294
    Compress::Raw::Zlib::Inflate->new(WindowBits => WANT_GZIP());
295
  my $status = $gz->inflate(\$self->{post_buffer}, my $out);
296
  $self->emit(read => $out) if defined $out;
297

            
298
  # Replace Content-Encoding with Content-Length
299
  $self->headers->content_length($gz->total_out)->remove('Content-Encoding')
300
    if $status == Z_STREAM_END();
301

            
302
  # Check buffer size
303
  $self->{limit} = $self->{state} = 'finished'
304
    if length(defined $self->{post_buffer} ? $self->{post_buffer} : '') > $self->max_buffer_size;
copy gitweblite soruce code
root authored on 2012-11-23
305
}
306

            
307
1;
308

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
309
=encoding utf8
310

            
copy gitweblite soruce code
root authored on 2012-11-23
311
=head1 NAME
312

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
313
Mojo::Content - HTTP content base class
copy gitweblite soruce code
root authored on 2012-11-23
314

            
315
=head1 SYNOPSIS
316

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
317
  package Mojo::Content::MyContent;
copy gitweblite soruce code
root authored on 2012-11-23
318
  use Mojo::Base 'Mojo::Content';
319

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
320
  sub body_contains  {...}
321
  sub body_size      {...}
322
  sub get_body_chunk {...}
323

            
copy gitweblite soruce code
root authored on 2012-11-23
324
=head1 DESCRIPTION
325

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
326
L<Mojo::Content> is an abstract base class for HTTP content as described in
327
RFC 2616.
copy gitweblite soruce code
root authored on 2012-11-23
328

            
329
=head1 EVENTS
330

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
331
L<Mojo::Content> inherits all events from L<Mojo::EventEmitter> and can emit
332
the following new ones.
copy gitweblite soruce code
root authored on 2012-11-23
333

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
334
=head2 body
copy gitweblite soruce code
root authored on 2012-11-23
335

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
336
  $content->on(body => sub {
337
    my $content = shift;
copy gitweblite soruce code
root authored on 2012-11-23
338
    ...
339
  });
340

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
341
Emitted once all headers have been parsed and the body starts.
copy gitweblite soruce code
root authored on 2012-11-23
342

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
343
  $content->on(body => sub {
copy gitweblite soruce code
root authored on 2012-11-23
344
    my $content = shift;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
345
    $content->auto_upgrade(0) if $content->headers->header('X-No-MultiPart');
copy gitweblite soruce code
root authored on 2012-11-23
346
  });
347

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
348
=head2 drain
copy gitweblite soruce code
root authored on 2012-11-23
349

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
350
  $content->on(drain => sub {
351
    my ($content, $offset) = @_;
copy gitweblite soruce code
root authored on 2012-11-23
352
    ...
353
  });
354

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
355
Emitted once all data has been written.
copy gitweblite soruce code
root authored on 2012-11-23
356

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
357
  $content->on(drain => sub {
copy gitweblite soruce code
root authored on 2012-11-23
358
    my $content = shift;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
359
    $content->write_chunk(time);
copy gitweblite soruce code
root authored on 2012-11-23
360
  });
361

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
362
=head2 read
copy gitweblite soruce code
root authored on 2012-11-23
363

            
364
  $content->on(read => sub {
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
365
    my ($content, $bytes) = @_;
copy gitweblite soruce code
root authored on 2012-11-23
366
    ...
367
  });
368

            
369
Emitted when a new chunk of content arrives.
370

            
371
  $content->unsubscribe('read');
372
  $content->on(read => sub {
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
373
    my ($content, $bytes) = @_;
374
    say "Streaming: $bytes";
copy gitweblite soruce code
root authored on 2012-11-23
375
  });
376

            
377
=head1 ATTRIBUTES
378

            
379
L<Mojo::Content> implements the following attributes.
380

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
381
=head2 auto_relax
copy gitweblite soruce code
root authored on 2012-11-23
382

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
383
  my $bool = $content->auto_relax;
384
  $content = $content->auto_relax($bool);
copy gitweblite soruce code
root authored on 2012-11-23
385

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
386
Try to detect when relaxed parsing is necessary.
copy gitweblite soruce code
root authored on 2012-11-23
387

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
388
=head2 headers
copy gitweblite soruce code
root authored on 2012-11-23
389

            
390
  my $headers = $content->headers;
391
  $content    = $content->headers(Mojo::Headers->new);
392

            
393
Content headers, defaults to a L<Mojo::Headers> object.
394

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
395
=head2 max_buffer_size
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
396

            
397
  my $size = $content->max_buffer_size;
398
  $content = $content->max_buffer_size(1024);
399

            
400
Maximum size in bytes of buffer for content parser, defaults to the value of
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
401
the MOJO_MAX_BUFFER_SIZE environment variable or C<262144>.
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
402

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
403
=head2 max_leftover_size
copy gitweblite soruce code
root authored on 2012-11-23
404

            
405
  my $size = $content->max_leftover_size;
406
  $content = $content->max_leftover_size(1024);
407

            
408
Maximum size in bytes of buffer for pipelined HTTP requests, defaults to the
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
409
value of the MOJO_MAX_LEFTOVER_SIZE environment variable or C<262144>.
copy gitweblite soruce code
root authored on 2012-11-23
410

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
411
=head2 relaxed
copy gitweblite soruce code
root authored on 2012-11-23
412

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
413
  my $bool = $content->relaxed;
414
  $content = $content->relaxed($bool);
copy gitweblite soruce code
root authored on 2012-11-23
415

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
416
Activate relaxed parsing for responses that are terminated with a connection
417
close.
418

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
419
=head2 skip_body
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
420

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
421
  my $bool = $content->skip_body;
422
  $content = $content->skip_body($bool);
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
423

            
424
Skip body parsing and finish after headers.
copy gitweblite soruce code
root authored on 2012-11-23
425

            
426
=head1 METHODS
427

            
428
L<Mojo::Content> inherits all methods from L<Mojo::EventEmitter> and
429
implements the following new ones.
430

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
431
=head2 body_contains
copy gitweblite soruce code
root authored on 2012-11-23
432

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
433
  my $bool = $content->body_contains('foo bar baz');
copy gitweblite soruce code
root authored on 2012-11-23
434

            
435
Check if content contains a specific string. Meant to be overloaded in a
436
subclass.
437

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
438
=head2 body_size
copy gitweblite soruce code
root authored on 2012-11-23
439

            
440
  my $size = $content->body_size;
441

            
442
Content size in bytes. Meant to be overloaded in a subclass.
443

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
444
=head2 boundary
copy gitweblite soruce code
root authored on 2012-11-23
445

            
446
  my $boundary = $content->boundary;
447

            
448
Extract multipart boundary from C<Content-Type> header.
449

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
450
=head2 build_body
copy gitweblite soruce code
root authored on 2012-11-23
451

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
452
  my $str = $content->build_body;
copy gitweblite soruce code
root authored on 2012-11-23
453

            
454
Render whole body.
455

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
456
=head2 build_headers
copy gitweblite soruce code
root authored on 2012-11-23
457

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
458
  my $str = $content->build_headers;
copy gitweblite soruce code
root authored on 2012-11-23
459

            
460
Render all headers.
461

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
462
=head2 charset
copy gitweblite soruce code
root authored on 2012-11-23
463

            
464
  my $charset = $content->charset;
465

            
466
Extract charset from C<Content-Type> header.
467

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
468
=head2 clone
copy gitweblite soruce code
root authored on 2012-11-23
469

            
470
  my $clone = $content->clone;
471

            
472
Clone content if possible, otherwise return C<undef>.
473

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
474
=head2 generate_body_chunk
copy gitweblite soruce code
root authored on 2012-11-23
475

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
476
  my $bytes = $content->generate_body_chunk(0);
copy gitweblite soruce code
root authored on 2012-11-23
477

            
478
Generate dynamic content.
479

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
480
=head2 get_body_chunk
copy gitweblite soruce code
root authored on 2012-11-23
481

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
482
  my $bytes = $content->get_body_chunk(0);
copy gitweblite soruce code
root authored on 2012-11-23
483

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
484
Get a chunk of content starting from a specific position. Meant to be
copy gitweblite soruce code
root authored on 2012-11-23
485
overloaded in a subclass.
486

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
487
=head2 get_header_chunk
copy gitweblite soruce code
root authored on 2012-11-23
488

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
489
  my $bytes = $content->get_header_chunk(13);
copy gitweblite soruce code
root authored on 2012-11-23
490

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
491
Get a chunk of the headers starting from a specific position.
copy gitweblite soruce code
root authored on 2012-11-23
492

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
493
=head2 header_size
copy gitweblite soruce code
root authored on 2012-11-23
494

            
495
  my $size = $content->header_size;
496

            
497
Size of headers in bytes.
498

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
499
=head2 is_chunked
copy gitweblite soruce code
root authored on 2012-11-23
500

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
501
  my $bool = $content->is_chunked;
copy gitweblite soruce code
root authored on 2012-11-23
502

            
503
Check if content is chunked.
504

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
505
=head2 is_compressed
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
506

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
507
  my $bool = $content->is_compressed;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
508

            
509
Check if content is C<gzip> compressed.
510

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
511
=head2 is_dynamic
copy gitweblite soruce code
root authored on 2012-11-23
512

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
513
  my $bool = $content->is_dynamic;
copy gitweblite soruce code
root authored on 2012-11-23
514

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
515
Check if content will be dynamically generated, which prevents L</"clone">
516
from working.
copy gitweblite soruce code
root authored on 2012-11-23
517

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
518
=head2 is_finished
copy gitweblite soruce code
root authored on 2012-11-23
519

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
520
  my $bool = $content->is_finished;
copy gitweblite soruce code
root authored on 2012-11-23
521

            
522
Check if parser is finished.
523

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
524
=head2 is_limit_exceeded
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
525

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
526
  my $bool = $content->is_limit_exceeded;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
527

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
528
Check if buffer has exceeded L</"max_buffer_size">.
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
529

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
530
=head2 is_multipart
copy gitweblite soruce code
root authored on 2012-11-23
531

            
532
  my $false = $content->is_multipart;
533

            
534
False.
535

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
536
=head2 is_parsing_body
copy gitweblite soruce code
root authored on 2012-11-23
537

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
538
  my $bool = $content->is_parsing_body;
copy gitweblite soruce code
root authored on 2012-11-23
539

            
540
Check if body parsing started yet.
541

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
542
=head2 leftovers
copy gitweblite soruce code
root authored on 2012-11-23
543

            
544
  my $bytes = $content->leftovers;
545

            
546
Get leftover data from content parser.
547

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
548
=head2 parse
copy gitweblite soruce code
root authored on 2012-11-23
549

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
550
  $content
551
    = $content->parse("Content-Length: 12\x0d\x0a\x0d\x0aHello World!");
copy gitweblite soruce code
root authored on 2012-11-23
552

            
553
Parse content chunk.
554

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
555
=head2 parse_body
copy gitweblite soruce code
root authored on 2012-11-23
556

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
557
  $content = $content->parse_body('Hi!');
copy gitweblite soruce code
root authored on 2012-11-23
558

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
559
Parse body chunk and skip headers.
copy gitweblite soruce code
root authored on 2012-11-23
560

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
561
=head2 progress
copy gitweblite soruce code
root authored on 2012-11-23
562

            
563
  my $size = $content->progress;
564

            
565
Size of content already received from message in bytes.
566

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
567
=head2 write
copy gitweblite soruce code
root authored on 2012-11-23
568

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
569
  $content = $content->write($bytes);
570
  $content = $content->write($bytes => sub {...});
copy gitweblite soruce code
root authored on 2012-11-23
571

            
572
Write dynamic content non-blocking, the optional drain callback will be
573
invoked once all data has been written.
574

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
575
=head2 write_chunk
copy gitweblite soruce code
root authored on 2012-11-23
576

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
577
  $content = $content->write_chunk($bytes);
578
  $content = $content->write_chunk($bytes => sub {...});
copy gitweblite soruce code
root authored on 2012-11-23
579

            
580
Write dynamic content non-blocking with C<chunked> transfer encoding, the
581
optional drain callback will be invoked once all data has been written.
582

            
583
=head1 SEE ALSO
584

            
585
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
586

            
587
=cut