Newer Older
593 lines | 14.383kb
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;
21
  $type =~ m!multipart.*boundary=(?:"([^"]+)"|([\w'(),.:?\-+/]+))!i
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 {
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
30
  my $type = shift->headers->content_type || '';
31
  return $type =~ /charset="?([^"\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

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
67
sub has_leftovers { !!length shift->leftovers }
copy gitweblite soruce code
root authored on 2012-11-23
68

            
69
sub header_size { length shift->build_headers }
70

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

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
73
sub is_compressed { (shift->headers->content_encoding || '') =~ /^gzip$/i }
74

            
75
sub is_dynamic { $_[0]->{dynamic} && !defined $_[0]->headers->content_length }
76

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

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

            
81
sub is_multipart {undef}
82

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

            
85
sub leftovers { shift->{buffer} }
86

            
87
sub parse {
88
  my $self = shift;
89

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

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

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

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

            
117
  # Relaxed parsing
118
  my $headers = $self->headers;
119
  if ($self->auto_relax) {
120
    my $connection = $headers->connection || '';
121
    my $len = defined $headers->content_length ? $headers->content_length : '';
122
    $self->relaxed(1)
123
      if !length $len && ($connection =~ /close/i || $headers->content_type);
124
  }
125

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

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

            
146
  return $self;
147
}
148

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

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

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

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

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

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

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

            
185
  my $buffer = '';
186
  my $offset = 0;
187
  while (1) {
188

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

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

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

            
199
  return $buffer;
200
}
201

            
202
sub _build_chunk {
203
  my ($self, $chunk) = @_;
204

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

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

            
213
sub _parse_chunked {
214
  my $self = shift;
215

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

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

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

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

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

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

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

            
249
sub _parse_chunked_trailing_headers {
250
  my $self = shift;
251

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

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

            
261
sub _parse_headers {
262
  my $self = shift;
263

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

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

            
274
sub _parse_until_body {
275
  my ($self, $chunk) = @_;
276

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

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

            
287
sub _uncompress {
288
  my ($self, $chunk) = @_;
289

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

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

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

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

            
309
1;
310

            
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

            
383
  my $relax = $content->auto_relax;
384
  $content  = $content->auto_relax(1);
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
401
the C<MOJO_MAX_BUFFER_SIZE> environment variable or C<262144>.
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
409
value of the C<MOJO_MAX_LEFTOVER_SIZE> environment variable or C<262144>.
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

            
413
  my $relaxed = $content->relaxed;
414
  $content    = $content->relaxed(1);
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

            
421
  my $skip = $content->skip_body;
422
  $content = $content->skip_body(1);
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

            
433
  my $success = $content->body_contains('foo bar baz');
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

            
452
  my $string = $content->build_body;
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

            
458
  my $string = $content->build_headers;
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

            
484
Get a chunk of content starting from a specfic position. Meant to be
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

            
491
Get a chunk of the headers starting from a specfic position.
492

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

            
495
  my $success = $content->has_leftovers;
496

            
497
Check if there are leftovers.
498

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

            
501
  my $size = $content->header_size;
502

            
503
Size of headers in bytes.
504

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

            
507
  my $success = $content->is_chunked;
508

            
509
Check if content is chunked.
510

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

            
513
  my $success = $content->is_compressed;
514

            
515
Check if content is C<gzip> compressed.
516

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

            
519
  my $success = $content->is_dynamic;
520

            
521
Check if content will be dynamically generated, which prevents C<clone> from
522
working.
523

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

            
526
  my $success = $content->is_finished;
527

            
528
Check if parser is finished.
529

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

            
532
  my $success = $content->is_limit_exceeded;
533

            
534
Check if buffer has exceeded C<max_buffer_size>.
535

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

            
538
  my $false = $content->is_multipart;
539

            
540
False.
541

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

            
544
  my $success = $content->is_parsing_body;
545

            
546
Check if body parsing started yet.
547

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

            
550
  my $bytes = $content->leftovers;
551

            
552
Get leftover data from content parser.
553

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

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

            
559
Parse content chunk.
560

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

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

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

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

            
569
  my $size = $content->progress;
570

            
571
Size of content already received from message in bytes.
572

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

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

            
578
Write dynamic content non-blocking, the optional drain callback will be
579
invoked once all data has been written.
580

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

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

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

            
589
=head1 SEE ALSO
590

            
591
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
592

            
593
=cut