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

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
4
use Carp 'croak';
copy gitweblite soruce code
root authored on 2012-11-23
5
use Mojo::Asset::Memory;
6
use Mojo::Content::Single;
7
use Mojo::DOM;
8
use Mojo::JSON;
9
use Mojo::JSON::Pointer;
10
use Mojo::Parameters;
11
use Mojo::Upload;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
12
use Mojo::Util 'decode';
copy gitweblite soruce code
root authored on 2012-11-23
13

            
14
has content => sub { Mojo::Content::Single->new };
15
has default_charset  => 'UTF-8';
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
16
has max_line_size    => sub { $ENV{MOJO_MAX_LINE_SIZE} || 10240 };
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
17
has max_message_size => sub { defined $ENV{MOJO_MAX_MESSAGE_SIZE} ? $ENV{MOJO_MAX_MESSAGE_SIZE} : 10485760 };
copy gitweblite soruce code
root authored on 2012-11-23
18
has version          => '1.1';
19

            
20
sub body {
21
  my $self = shift;
22

            
23
  # Downgrade multipart content
24
  my $content = $self->content;
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
25
  $content = $self->content(Mojo::Content::Single->new)->content
26
    if $content->is_multipart;
copy gitweblite soruce code
root authored on 2012-11-23
27

            
28
  # Get
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
29
  return $content->asset->slurp unless @_;
copy gitweblite soruce code
root authored on 2012-11-23
30

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
31
  # Set
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
32
  $content->asset(Mojo::Asset::Memory->new->add_chunk(@_));
copy gitweblite soruce code
root authored on 2012-11-23
33

            
34
  return $self;
35
}
36

            
37
sub body_params {
38
  my $self = shift;
39

            
40
  return $self->{body_params} if $self->{body_params};
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
41
  my $params = $self->{body_params} = Mojo::Parameters->new;
42
  $params->charset($self->content->charset || $self->default_charset);
copy gitweblite soruce code
root authored on 2012-11-23
43

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
44
  # "application/x-www-form-urlencoded"
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
45
  my $type = defined $self->headers->content_type ? $self->headers->content_type : '';
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
46
  if ($type =~ m!application/x-www-form-urlencoded!i) {
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
47
    $params->parse($self->content->asset->slurp);
copy gitweblite soruce code
root authored on 2012-11-23
48
  }
49

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
50
  # "multipart/form-data"
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
51
  elsif ($type =~ m!multipart/form-data!i) {
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
52
    $params->append(@$_[0, 1]) for @{$self->_parse_formdata};
copy gitweblite soruce code
root authored on 2012-11-23
53
  }
54

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
55
  return $params;
copy gitweblite soruce code
root authored on 2012-11-23
56
}
57

            
58
sub body_size { shift->content->body_size }
59

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

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
64
sub cookie { shift->_cache(cookies => @_) }
copy gitweblite soruce code
root authored on 2012-11-23
65

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
66
sub cookies { croak 'Method "cookies" not implemented by subclass' }
67

            
copy gitweblite soruce code
root authored on 2012-11-23
68
sub dom {
69
  my $self = shift;
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
70
  return undef if $self->content->is_multipart;
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
71
  my $dom = $self->{dom} ||= Mojo::DOM->new($self->text);
copy gitweblite soruce code
root authored on 2012-11-23
72
  return @_ ? $dom->find(@_) : $dom;
73
}
74

            
75
sub error {
76
  my $self = shift;
77

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
78
  # Set
79
  if (@_) {
80
    $self->{error} = [@_];
81
    return $self->finish;
copy gitweblite soruce code
root authored on 2012-11-23
82
  }
83

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
84
  # Get
85
  return unless my $err = $self->{error};
86
  return wantarray ? @$err : $err->[0];
87
}
copy gitweblite soruce code
root authored on 2012-11-23
88

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
89
sub extract_start_line {
90
  croak 'Method "extract_start_line" not implemented by subclass';
91
}
92

            
93
sub finish {
94
  my $self = shift;
95
  $self->{state} = 'finished';
96
  return $self->{finished}++ ? $self : $self->emit('finish');
copy gitweblite soruce code
root authored on 2012-11-23
97
}
98

            
99
sub fix_headers {
100
  my $self = shift;
101

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
102
  # Content-Length or Connection (unless chunked transfer encoding is used)
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
103
  my $content = $self->content;
104
  return $self if $self->{fix}++ || $content->is_chunked;
copy gitweblite soruce code
root authored on 2012-11-23
105
  my $headers = $self->headers;
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
106
  $content->is_dynamic
copy gitweblite soruce code
root authored on 2012-11-23
107
    ? $headers->connection('close')
108
    : $headers->content_length($self->body_size)
109
    unless $headers->content_length;
110

            
111
  return $self;
112
}
113

            
114
sub get_body_chunk {
115
  my ($self, $offset) = @_;
116

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
117
  $self->emit('progress', 'body', $offset);
copy gitweblite soruce code
root authored on 2012-11-23
118
  my $chunk = $self->content->get_body_chunk($offset);
119
  return $chunk if !defined $chunk || length $chunk;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
120
  $self->finish;
copy gitweblite soruce code
root authored on 2012-11-23
121

            
122
  return $chunk;
123
}
124

            
125
sub get_header_chunk {
126
  my ($self, $offset) = @_;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
127
  $self->emit('progress', 'headers', $offset);
copy gitweblite soruce code
root authored on 2012-11-23
128
  return $self->fix_headers->content->get_header_chunk($offset);
129
}
130

            
131
sub get_start_line_chunk {
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
132
  croak 'Method "get_start_line_chunk" not implemented by subclass';
copy gitweblite soruce code
root authored on 2012-11-23
133
}
134

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
135
sub header_size { shift->fix_headers->content->header_size }
136

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
137
sub headers { shift->content->headers }
copy gitweblite soruce code
root authored on 2012-11-23
138

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
139
sub is_finished { do { my $tmp = shift->{state}; defined $tmp ? $tmp : ''} eq 'finished' }
140

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
141
sub is_limit_exceeded { !!shift->{limit} }
copy gitweblite soruce code
root authored on 2012-11-23
142

            
143
sub json {
144
  my ($self, $pointer) = @_;
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
145
  return undef if $self->content->is_multipart;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
146
  my $data = $self->{json} ||= Mojo::JSON->new->decode($self->body);
147
  return $pointer ? Mojo::JSON::Pointer->new->get($data, $pointer) : $data;
copy gitweblite soruce code
root authored on 2012-11-23
148
}
149

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
150
sub param { shift->body_params->param(@_) }
copy gitweblite soruce code
root authored on 2012-11-23
151

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
152
sub parse {
153
  my ($self, $chunk) = @_;
copy gitweblite soruce code
root authored on 2012-11-23
154

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
155
  # Check message size
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
156
  my $max = $self->max_message_size;
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
157
  return $self->_limit('Maximum message size exceeded', 413)
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
158
    if $max && ($self->{raw_size} += length($chunk = defined $chunk ? $chunk : '')) > $max;
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
159

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
160
  $self->{buffer} .= $chunk;
161

            
162
  # Start line
163
  unless ($self->{state}) {
164

            
165
    # Check line size
166
    my $len = index $self->{buffer}, "\x0a";
167
    $len = length $self->{buffer} if $len < 0;
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
168
    return $self->_limit('Maximum line size exceeded', 431)
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
169
      if $len > $self->max_line_size;
170

            
171
    $self->{state} = 'content' if $self->extract_start_line(\$self->{buffer});
172
  }
173

            
174
  # Content
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
175
  my $state = defined $self->{state} ? $self->{state} : '';
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
176
  $self->content($self->content->parse(delete $self->{buffer}))
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
177
    if $state eq 'content' || $state eq 'finished';
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
178

            
179
  # Check line size
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
180
  return $self->_limit('Maximum line size exceeded', 431)
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
181
    if $self->headers->is_limit_exceeded;
182

            
183
  # Check buffer size
184
  return $self->error('Maximum buffer size exceeded', 400)
185
    if $self->content->is_limit_exceeded;
186

            
187
  return $self->emit('progress')->content->is_finished ? $self->finish : $self;
188
}
copy gitweblite soruce code
root authored on 2012-11-23
189

            
190
sub start_line_size { length shift->build_start_line }
191

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
192
sub text {
193
  my $self    = shift;
194
  my $body    = $self->body;
195
  my $charset = $self->content->charset;
196
  return $charset ? do {my $decoded = decode($charset, $body); defined $decoded ? $decoded : $body} : $body;
197
}
198

            
copy gitweblite soruce code
root authored on 2012-11-23
199
sub to_string {
200
  my $self = shift;
201
  return $self->build_start_line . $self->build_headers . $self->build_body;
202
}
203

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
204
sub upload { shift->_cache(uploads => @_) }
copy gitweblite soruce code
root authored on 2012-11-23
205

            
206
sub uploads {
207
  my $self = shift;
208

            
209
  my @uploads;
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
210
  for my $data (@{$self->_parse_formdata(1)}) {
copy gitweblite soruce code
root authored on 2012-11-23
211
    my $upload = Mojo::Upload->new(
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
212
      name     => $data->[0],
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
213
      filename => $data->[2],
214
      asset    => $data->[1]->asset,
215
      headers  => $data->[1]->headers
copy gitweblite soruce code
root authored on 2012-11-23
216
    );
217
    push @uploads, $upload;
218
  }
219

            
220
  return \@uploads;
221
}
222

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

            
226
  my $buffer = '';
227
  my $offset = 0;
228
  while (1) {
229

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

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

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
236
    $offset += $len;
copy gitweblite soruce code
root authored on 2012-11-23
237
    $buffer .= $chunk;
238
  }
239

            
240
  return $buffer;
241
}
242

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
243
sub _cache {
244
  my ($self, $method, $name) = @_;
copy gitweblite soruce code
root authored on 2012-11-23
245

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
246
  # Cache objects by name
247
  unless ($self->{$method}) {
248
    $self->{$method} = {};
249
    push @{$self->{$method}{$_->name}}, $_ for @{$self->$method};
copy gitweblite soruce code
root authored on 2012-11-23
250
  }
251

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
252
  return unless my $objects = $self->{$method}{$name};
253
  return wantarray ? @$objects : $objects->[0];
254
}
255

            
256
sub _limit {
257
  my $self = shift;
258
  $self->{limit} = 1;
259
  $self->error(@_);
copy gitweblite soruce code
root authored on 2012-11-23
260
}
261

            
262
sub _parse_formdata {
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
263
  my ($self, $upload) = @_;
copy gitweblite soruce code
root authored on 2012-11-23
264

            
265
  my @formdata;
266
  my $content = $self->content;
267
  return \@formdata unless $content->is_multipart;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
268
  my $charset = $content->charset || $self->default_charset;
copy gitweblite soruce code
root authored on 2012-11-23
269

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
270
  # Check all parts recursively
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
271
  my @parts = ($content);
copy gitweblite soruce code
root authored on 2012-11-23
272
  while (my $part = shift @parts) {
273

            
274
    if ($part->is_multipart) {
275
      unshift @parts, @{$part->parts};
276
      next;
277
    }
278

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
279
    next unless my $disposition = $part->headers->content_disposition;
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
280
    my ($filename) = $disposition =~ /[; ]filename\s*=\s*"?((?:\\"|[^"])*)"?/i;
281
    next if ($upload && !defined $filename) || (!$upload && defined $filename);
282
    my ($name) = $disposition =~ /[; ]name\s*=\s*"?((?:\\"|[^";])+)"?/i;
copy gitweblite soruce code
root authored on 2012-11-23
283
    if ($charset) {
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
284
      $name     = do {my $tmp = decode($charset, $name); defined $tmp ? $tmp : $name} if $name;
285
      $filename = do {my $tmp = decode($charset, $filename); defined $tmp ? $tmp : $filename} if $filename;
copy gitweblite soruce code
root authored on 2012-11-23
286
    }
287

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
288
    unless ($upload) {
289
      $part = $part->asset->slurp;
290
      $part = do {my $decoded = decode($charset, $part); defined $decoded ? $decoded : $part} if $charset;
copy gitweblite soruce code
root authored on 2012-11-23
291
    }
292

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
293
    push @formdata, [$name, $part, $filename];
copy gitweblite soruce code
root authored on 2012-11-23
294
  }
295

            
296
  return \@formdata;
297
}
298

            
299
1;
300

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
301
=encoding utf8
302

            
copy gitweblite soruce code
root authored on 2012-11-23
303
=head1 NAME
304

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
305
Mojo::Message - HTTP message base class
copy gitweblite soruce code
root authored on 2012-11-23
306

            
307
=head1 SYNOPSIS
308

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
309
  package Mojo::Message::MyMessage;
copy gitweblite soruce code
root authored on 2012-11-23
310
  use Mojo::Base 'Mojo::Message';
311

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
312
  sub cookies              {...}
313
  sub extract_start_line   {...}
314
  sub get_start_line_chunk {...}
315

            
copy gitweblite soruce code
root authored on 2012-11-23
316
=head1 DESCRIPTION
317

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
318
L<Mojo::Message> is an abstract base class for HTTP messages as described in
319
RFC 2616 and RFC 2388.
copy gitweblite soruce code
root authored on 2012-11-23
320

            
321
=head1 EVENTS
322

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

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
326
=head2 finish
copy gitweblite soruce code
root authored on 2012-11-23
327

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
328
  $msg->on(finish => sub {
329
    my $msg = shift;
copy gitweblite soruce code
root authored on 2012-11-23
330
    ...
331
  });
332

            
333
Emitted after message building or parsing is finished.
334

            
335
  my $before = time;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
336
  $msg->on(finish => sub {
337
    my $msg = shift;
338
    $msg->headers->header('X-Parser-Time' => time - $before);
copy gitweblite soruce code
root authored on 2012-11-23
339
  });
340

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

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
343
  $msg->on(progress => sub {
344
    my $msg = shift;
copy gitweblite soruce code
root authored on 2012-11-23
345
    ...
346
  });
347

            
348
Emitted when message building or parsing makes progress.
349

            
350
  # Building
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
351
  $msg->on(progress => sub {
352
    my ($msg, $state, $offset) = @_;
353
    say qq{Building "$state" at offset $offset};
copy gitweblite soruce code
root authored on 2012-11-23
354
  });
355

            
356
  # Parsing
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
357
  $msg->on(progress => sub {
358
    my $msg = shift;
359
    return unless my $len = $msg->headers->content_length;
360
    my $size = $msg->content->progress;
copy gitweblite soruce code
root authored on 2012-11-23
361
    say 'Progress: ', $size == $len ? 100 : int($size / ($len / 100)), '%';
362
  });
363

            
364
=head1 ATTRIBUTES
365

            
366
L<Mojo::Message> implements the following attributes.
367

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
368
=head2 content
copy gitweblite soruce code
root authored on 2012-11-23
369

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
370
  my $msg = $msg->content;
371
  $msg    = $msg->content(Mojo::Content::Single->new);
copy gitweblite soruce code
root authored on 2012-11-23
372

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
373
Message content, defaults to a L<Mojo::Content::Single> object.
copy gitweblite soruce code
root authored on 2012-11-23
374

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
375
=head2 default_charset
copy gitweblite soruce code
root authored on 2012-11-23
376

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
377
  my $charset = $msg->default_charset;
378
  $msg        = $msg->default_charset('UTF-8');
copy gitweblite soruce code
root authored on 2012-11-23
379

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
380
Default charset used for form-data parsing, defaults to C<UTF-8>.
copy gitweblite soruce code
root authored on 2012-11-23
381

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

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
384
  my $size = $msg->max_line_size;
385
  $msg     = $msg->max_line_size(1024);
copy gitweblite soruce code
root authored on 2012-11-23
386

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
387
Maximum start line size in bytes, defaults to the value of the
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
388
MOJO_MAX_LINE_SIZE environment variable or C<10240>.
copy gitweblite soruce code
root authored on 2012-11-23
389

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
390
=head2 max_message_size
copy gitweblite soruce code
root authored on 2012-11-23
391

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
392
  my $size = $msg->max_message_size;
393
  $msg     = $msg->max_message_size(1024);
copy gitweblite soruce code
root authored on 2012-11-23
394

            
395
Maximum message size in bytes, defaults to the value of the
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
396
MOJO_MAX_MESSAGE_SIZE environment variable or C<10485760>. Setting the value
397
to C<0> will allow messages of indefinite size. Note that increasing this
398
value can also drastically increase memory usage, should you for example
399
attempt to parse an excessively large message body with the L</"body_params">,
400
L</"dom"> or L</"json"> methods.
copy gitweblite soruce code
root authored on 2012-11-23
401

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

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
404
  my $version = $msg->version;
405
  $msg        = $msg->version('1.1');
copy gitweblite soruce code
root authored on 2012-11-23
406

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
407
HTTP version of message, defaults to C<1.1>.
copy gitweblite soruce code
root authored on 2012-11-23
408

            
409
=head1 METHODS
410

            
411
L<Mojo::Message> inherits all methods from L<Mojo::EventEmitter> and
412
implements the following new ones.
413

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

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
416
  my $bytes = $msg->body;
417
  $msg      = $msg->body('Hello!');
copy gitweblite soruce code
root authored on 2012-11-23
418

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
419
Slurp or replace L</"content">, L<Mojo::Content::MultiPart> will be
420
automatically downgraded to L<Mojo::Content::Single>.
copy gitweblite soruce code
root authored on 2012-11-23
421

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
422
=head2 body_params
copy gitweblite soruce code
root authored on 2012-11-23
423

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
424
  my $params = $msg->body_params;
copy gitweblite soruce code
root authored on 2012-11-23
425

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
426
POST parameters extracted from C<application/x-www-form-urlencoded> or
427
C<multipart/form-data> message body, usually a L<Mojo::Parameters> object.
428
Note that this method caches all data, so it should not be called before the
429
entire message body has been received. Parts of the message body need to be
430
loaded into memory to parse POST parameters, so you have to make sure it is
431
not excessively large.
copy gitweblite soruce code
root authored on 2012-11-23
432

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
433
  # Get POST parameter value
434
  say $msg->body_params->param('foo');
copy gitweblite soruce code
root authored on 2012-11-23
435

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

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
438
  my $size = $msg->body_size;
copy gitweblite soruce code
root authored on 2012-11-23
439

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
440
Content size in bytes.
copy gitweblite soruce code
root authored on 2012-11-23
441

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

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
444
  my $bytes = $msg->build_body;
copy gitweblite soruce code
root authored on 2012-11-23
445

            
446
Render whole body.
447

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

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
450
  my $bytes = $msg->build_headers;
copy gitweblite soruce code
root authored on 2012-11-23
451

            
452
Render all headers.
453

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
454
=head2 build_start_line
copy gitweblite soruce code
root authored on 2012-11-23
455

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
456
  my $bytes = $msg->build_start_line;
copy gitweblite soruce code
root authored on 2012-11-23
457

            
458
Render start line.
459

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
460
=head2 cookie
copy gitweblite soruce code
root authored on 2012-11-23
461

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
462
  my $cookie  = $msg->cookie('foo');
463
  my @cookies = $msg->cookie('foo');
copy gitweblite soruce code
root authored on 2012-11-23
464

            
465
Access message cookies, usually L<Mojo::Cookie::Request> or
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
466
L<Mojo::Cookie::Response> objects. Note that this method caches all data, so
467
it should not be called before all headers have been received.
468

            
469
  # Get cookie value
470
  say $msg->cookie('foo')->value;
471

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
472
=head2 cookies
copy gitweblite soruce code
root authored on 2012-11-23
473

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
474
  my $cookies = $msg->cookies;
475

            
476
Access message cookies. Meant to be overloaded in a subclass.
copy gitweblite soruce code
root authored on 2012-11-23
477

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
478
=head2 dom
copy gitweblite soruce code
root authored on 2012-11-23
479

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
480
  my $dom        = $msg->dom;
481
  my $collection = $msg->dom('a[href]');
copy gitweblite soruce code
root authored on 2012-11-23
482

            
483
Turns message body into a L<Mojo::DOM> object and takes an optional selector
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
484
to perform a C<find> on it right away, which returns a L<Mojo::Collection>
485
object. Note that this method caches all data, so it should not be called
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
486
before the entire message body has been received. The whole message body needs
487
to be loaded into memory to parse it, so you have to make sure it is not
488
excessively large.
copy gitweblite soruce code
root authored on 2012-11-23
489

            
490
  # Perform "find" right away
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
491
  say $msg->dom('h1, h2, h3')->text;
copy gitweblite soruce code
root authored on 2012-11-23
492

            
493
  # Use everything else Mojo::DOM has to offer
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
494
  say $msg->dom->at('title')->text;
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
495
  say $msg->dom->html->body->children->type->uniq;
copy gitweblite soruce code
root authored on 2012-11-23
496

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
497
=head2 error
copy gitweblite soruce code
root authored on 2012-11-23
498

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
499
  my $err          = $msg->error;
500
  my ($err, $code) = $msg->error;
501
  $msg             = $msg->error('Parser error');
502
  $msg             = $msg->error('Parser error', 500);
503

            
504
Error and code.
505

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

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
508
  my $bool = $msg->extract_start_line(\$str);
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
509

            
510
Extract start line from string. Meant to be overloaded in a subclass.
511

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

            
514
  $msg = $msg->finish;
515

            
516
Finish message parser/generator.
copy gitweblite soruce code
root authored on 2012-11-23
517

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

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
520
  $msg = $msg->fix_headers;
copy gitweblite soruce code
root authored on 2012-11-23
521

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
522
Make sure message has all required headers.
copy gitweblite soruce code
root authored on 2012-11-23
523

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

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
526
  my $bytes = $msg->get_body_chunk($offset);
copy gitweblite soruce code
root authored on 2012-11-23
527

            
528
Get a chunk of body data starting from a specific position.
529

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

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
532
  my $bytes = $msg->get_header_chunk($offset);
copy gitweblite soruce code
root authored on 2012-11-23
533

            
534
Get a chunk of header data, starting from a specific position.
535

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

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
538
  my $bytes = $msg->get_start_line_chunk($offset);
copy gitweblite soruce code
root authored on 2012-11-23
539

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
540
Get a chunk of start line data starting from a specific position. Meant to be
541
overloaded in a subclass.
copy gitweblite soruce code
root authored on 2012-11-23
542

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

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
545
  my $size = $msg->header_size;
copy gitweblite soruce code
root authored on 2012-11-23
546

            
547
Size of headers in bytes.
548

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

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
551
  my $headers = $msg->headers;
copy gitweblite soruce code
root authored on 2012-11-23
552

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
553
Message headers, usually a L<Mojo::Headers> object.
copy gitweblite soruce code
root authored on 2012-11-23
554

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

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

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
559
Check if message parser/generator is finished.
copy gitweblite soruce code
root authored on 2012-11-23
560

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

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
563
  my $bool = $msg->is_limit_exceeded;
copy gitweblite soruce code
root authored on 2012-11-23
564

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
565
Check if message has exceeded L</"max_line_size"> or L</"max_message_size">.
copy gitweblite soruce code
root authored on 2012-11-23
566

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

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
569
  my $hash  = $msg->json;
570
  my $array = $msg->json;
571
  my $value = $msg->json('/foo/bar');
copy gitweblite soruce code
root authored on 2012-11-23
572

            
573
Decode JSON message body directly using L<Mojo::JSON> if possible, returns
574
C<undef> otherwise. An optional JSON Pointer can be used to extract a specific
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
575
value with L<Mojo::JSON::Pointer>. Note that this method caches all data, so
576
it should not be called before the entire message body has been received.
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
577
The whole message body needs to be loaded into memory to parse it, so you have
578
to make sure it is not excessively large.
copy gitweblite soruce code
root authored on 2012-11-23
579

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
580
  # Extract JSON values
581
  say $msg->json->{foo}{bar}[23];
582
  say $msg->json('/foo/bar/23');
copy gitweblite soruce code
root authored on 2012-11-23
583

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
584
=head2 param
copy gitweblite soruce code
root authored on 2012-11-23
585

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
586
  my @names = $msg->param;
587
  my $foo   = $msg->param('foo');
588
  my @foo   = $msg->param('foo');
copy gitweblite soruce code
root authored on 2012-11-23
589

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
590
Access POST parameters. Note that this method caches all data, so it should
591
not be called before the entire message body has been received. Parts of the
592
message body need to be loaded into memory to parse POST parameters, so you
593
have to make sure it is not excessively large.
copy gitweblite soruce code
root authored on 2012-11-23
594

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

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
597
  $msg = $msg->parse('HTTP/1.1 200 OK...');
copy gitweblite soruce code
root authored on 2012-11-23
598

            
599
Parse message chunk.
600

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
601
=head2 start_line_size
copy gitweblite soruce code
root authored on 2012-11-23
602

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
603
  my $size = $msg->start_line_size;
copy gitweblite soruce code
root authored on 2012-11-23
604

            
605
Size of the start line in bytes.
606

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
607
=head2 text
608

            
609
  my $str = $msg->text;
610

            
611
Retrieve L</"body"> and try to decode it if a charset could be extracted with
612
L<Mojo::Content/"charset">.
613

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
614
=head2 to_string
copy gitweblite soruce code
root authored on 2012-11-23
615

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
616
  my $str = $msg->to_string;
copy gitweblite soruce code
root authored on 2012-11-23
617

            
618
Render whole message.
619

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
620
=head2 upload
copy gitweblite soruce code
root authored on 2012-11-23
621

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
622
  my $upload  = $msg->upload('foo');
623
  my @uploads = $msg->upload('foo');
copy gitweblite soruce code
root authored on 2012-11-23
624

            
625
Access C<multipart/form-data> file uploads, usually L<Mojo::Upload> objects.
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
626
Note that this method caches all data, so it should not be called before the
627
entire message body has been received.
copy gitweblite soruce code
root authored on 2012-11-23
628

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
629
  # Get content of uploaded file
630
  say $msg->upload('foo')->asset->slurp;
copy gitweblite soruce code
root authored on 2012-11-23
631

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
632
=head2 uploads
copy gitweblite soruce code
root authored on 2012-11-23
633

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
634
  my $uploads = $msg->uploads;
copy gitweblite soruce code
root authored on 2012-11-23
635

            
636
All C<multipart/form-data> file uploads, usually L<Mojo::Upload> objects.
637

            
638
=head1 SEE ALSO
639

            
640
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
641

            
642
=cut