File: Content.pm

package info (click to toggle)
libmojolicious-perl 0.999926-1%2Bsqueeze2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,672 kB
  • ctags: 949
  • sloc: perl: 17,391; makefile: 4
file content (473 lines) | stat: -rw-r--r-- 9,755 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# Copyright (C) 2008-2010, Sebastian Riedel.

package Mojo::Content;

use strict;
use warnings;

use base 'Mojo::Stateful';

use Carp 'croak';
use Mojo::ByteStream;
use Mojo::Filter::Chunked;
use Mojo::Headers;

use constant CHUNK_SIZE => $ENV{MOJO_CHUNK_SIZE} || 8192;

__PACKAGE__->attr([qw/body_cb filter/]);
__PACKAGE__->attr([qw/buffer filter_buffer/] => sub { Mojo::ByteStream->new }
);
__PACKAGE__->attr(headers => sub { Mojo::Headers->new });
__PACKAGE__->attr(raw_header_size => 0);
__PACKAGE__->attr(relaxed         => 0);

sub body_contains {
    croak 'Method "body_contains" not implemented by subclass';
}

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

# Operator! Give me the number for 911!
sub build_body {
    my $self = shift;

    my $body   = '';
    my $offset = 0;
    while (1) {
        my $chunk = $self->get_body_chunk($offset);

        # No content yet, try again
        next unless defined $chunk;

        # End of content
        last unless length $chunk;

        # Content
        $offset += length $chunk;
        $body .= $chunk;
    }

    return $body;
}

sub build_headers {
    my $self = shift;

    my $headers = '';
    my $offset  = 0;
    while (1) {
        my $chunk = $self->get_header_chunk($offset);

        # No headers yet, try again
        next unless defined $chunk;

        # End of headers
        last unless length $chunk;

        # Headers
        $offset += length $chunk;
        $headers .= $chunk;
    }

    return $headers;
}

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

    # Shortcut
    return '' unless $self->body_cb;

    # Remove written
    my $buffer = $self->buffer;
    my $written = $offset - ($buffer->raw_size - $buffer->size);
    $buffer->remove($written);

    # Fill buffer
    if (!$self->{_eof} && $buffer->size < CHUNK_SIZE) {

        # Generate
        my $chunk = $self->body_cb->($self, $buffer->raw_size);

        # EOF
        if (defined $chunk && !length $chunk) { $self->{_eof} = 1 }

        # Buffer chunk
        else { $buffer->add_chunk($chunk) }
    }

    # Get chunk
    my $chunk = $buffer->to_string;

    # Pause or EOF
    return $self->{_eof} ? '' : undef unless length $chunk;

    return $chunk;
}

sub get_body_chunk {
    croak 'Method "get_body_chunk" not implemented by subclass';
}

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

    # Normal headers
    my $copy = $self->_build_headers;
    return substr($copy, $offset, CHUNK_SIZE);
}

sub has_leftovers {
    my $self = shift;

    # Leftovers
    return 1 if $self->buffer->size || $self->filter_buffer->size;

    # Empty buffer
    return;
}

sub header_size { length shift->build_headers }

sub is_chunked {
    my $self = shift;

    # Chunked
    my $encoding = $self->headers->transfer_encoding || '';
    return $encoding =~ /chunked/i ? 1 : 0;
}

sub is_multipart {
    my $self = shift;

    # Multipart
    my $type = $self->headers->content_type || '';
    return $type =~ /multipart/i ? 1 : 0;
}

sub leftovers {
    my $self = shift;

    # Chunked leftovers are in the filter buffer, and so are those from a
    # HEAD request
    return $self->filter_buffer->to_string if $self->filter_buffer->size;

    # Normal leftovers
    return $self->buffer->to_string;
}

sub parse {
    my ($self, $chunk) = @_;

    # Buffer
    my $fbuffer = $self->filter_buffer;
    $fbuffer->add_chunk($chunk);

    # Parse headers
    $self->parse_until_body;

    # Still parsing headers
    return $self if $self->is_state('headers');

    # Chunked, need to filter
    if ($self->is_chunked && !$self->is_state('headers')) {

        # Initialize filter
        $self->filter(
            Mojo::Filter::Chunked->new(
                headers       => $self->headers,
                input_buffer  => $fbuffer,
                output_buffer => $self->buffer
            )
        ) unless $self->filter;

        # Filter
        $self->filter->parse;
        $self->done if $self->filter->is_done;
    }

    # Not chunked, pass through
    else { $self->buffer($fbuffer) }

    # Custom body parser
    if (my $cb = $self->body_cb) {

        # Chunked or relaxed content
        if ($self->is_chunked || $self->relaxed) {
            $self->$cb($self->buffer->empty);
        }

        # Normal content
        else {

            # Need
            my $length = $self->headers->content_length || 0;
            $self->{_size} ||= 0;
            my $need = $length - $self->{_size};

            # Slurp
            if ($need > 0) {
                my $chunk = $self->buffer->remove($need);
                $self->{_size} = $self->{_size} + length $chunk;
                $self->$cb($chunk);
            }

            # Done
            $self->done if $length <= $self->raw_body_size;
        }
    }

    # Leftovers
    if ($self->is_done) {
        $self->state('done_with_leftovers') if $self->has_leftovers;
    }

    return $self;
}

sub parse_until_body {
    my ($self, $chunk) = @_;

    # Buffer
    my $fbuffer = $self->filter_buffer;
    $fbuffer->add_chunk($chunk);

    # Parser started
    if ($self->is_state('start')) {

        # Update size
        my $length            = $fbuffer->size;
        my $raw_length        = $fbuffer->raw_size;
        my $raw_header_length = $raw_length - $length;
        $self->raw_header_size($raw_header_length);

        # Headers
        $self->state('headers');
    }

    # Parse headers
    $self->_parse_headers if $self->is_state('headers');

    return $self;
}

sub raw_body_size {
    my $self = shift;

    # Calculate
    my $length        = $self->filter_buffer->raw_size;
    my $header_length = $self->raw_header_size;
    return $length - $header_length;
}

sub _build_headers {
    my $self = shift;

    # Build
    my $headers = $self->headers->to_string;

    # Empty
    return "\x0d\x0a" unless $headers;

    return "$headers\x0d\x0a\x0d\x0a";
}

sub _parse_headers {
    my $self = shift;

    # Parse
    my $headers = $self->headers;
    $headers->buffer($self->filter_buffer);
    $headers->parse;

    # Update size
    my $buffer            = $headers->buffer;
    my $length            = $buffer->size;
    my $raw_length        = $buffer->raw_size;
    my $raw_header_length = $raw_length - $length;
    $self->raw_header_size($raw_header_length);

    # Done
    $self->state('body') if $headers->is_done;
}

1;
__END__

=head1 NAME

Mojo::Content - HTTP 1.1 Content Base Class

=head1 SYNOPSIS

    use base 'Mojo::Content';

=head1 DESCRIPTION

L<Mojo::Content> is an abstract base class for HTTP 1.1 content as described
in RFC 2616.

=head1 ATTRIBUTES

L<Mojo::Content> inherits all attributes from L<Mojo::Stateful> and
implements the following new ones.

=head2 C<body_cb>

    my $cb = $content->body_cb;

    $counter = 1;
    $content = $content->body_cb(sub {
        my $self  = shift;
        my $chunk = '';
        $chunk    = "hello world!" if $counter == 1;
        $chunk    = "hello world2!\n\n" if $counter == 2;
        $counter++;
        return $chunk;
    });

Content generator callback.

=head2 C<buffer>

    my $buffer = $content->buffer;
    $content   = $content->buffer(Mojo::ByteStream->new);

Parser buffer.

=head2 C<filter>

    my $filter = $content->filter;
    $content   = $content->filter(Mojo::Filter::Chunked->new);

Input filter.

=head2 C<filter_buffer>

    my $filter_buffer = $content->filter_buffer;
    $content          = $content->filter_buffer(Mojo::ByteStream->new);

Input buffer for filtering.

=head2 C<headers>

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

The headers.

=head2 C<relaxed>

    my $relaxed = $content->relaxed;
    $content    = $content->relaxed(1);

Activate relaxed filtering for HTTP 0.9.

=head2 C<raw_header_size>

    my $size = $content->raw_header_size;

Raw size of headers in bytes.

=head1 METHODS

L<Mojo::Content> inherits all methods from L<Mojo::Stateful> and implements
the following new ones.

=head2 C<body_contains>

    my $found = $content->body_contains('foo bar baz');

Check if content contains a specific string.

=head2 C<body_size>

    my $size = $content->body_size;

Content size in bytes.

=head2 C<build_body>

    my $string = $content->build_body;

Render whole body.

=head2 C<build_headers>

    my $string = $content->build_headers;

Render all headers.

=head2 C<generate_body_chunk>

    my $chunk = $content->generate_body_chunk(0);

Generate content from C<body_cb>.

=head2 C<get_body_chunk>

    my $chunk = $content->get_body_chunk(0);

Get a chunk of content starting from a specfic position.

=head2 C<get_header_chunk>

    my $chunk = $content->get_header_chunk(13);

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

=head2 C<has_leftovers>

    my $leftovers = $content->has_leftovers;

Check if there are leftovers in the buffer.

=head2 C<header_size>

    my $size = $content->header_size;

Size of headers in bytes.

=head2 C<is_chunked>

    my $chunked = $content->is_chunked;

Chunked transfer encoding.

=head2 C<is_multipart>

    my $multipart = $content->is_multipart;

Multipart content.

=head2 C<leftovers>

    my $bytes = $content->leftovers;

Leftovers for next HTTP message in buffer.

=head2 C<parse>

    $content = $content->parse("Content-Length: 12\r\n\r\nHello World!");

Parse content.

=head2 C<parse_until_body>

    $content = $content->parse_until_body(
        "Content-Length: 12\r\n\r\nHello World!"
    );

Parse and stop after headers.

=head2 C<raw_body_size>

    my $size = $content->raw_body_size;

Raw size of body in bytes.

=head1 SEE ALSO

L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicious.org>.

=cut