Mojo::Content::Single(3)

NAME
Mojo::Content::Single - HTTP content

SYNOPSIS
  use Mojo::Content::Single;

  my $single = Mojo::Content::Single->new;
  $single->parse("Content-Length: 12\x0d\x0a\x0d\x0aHello World!");
  say $single->headers->content_length;

DESCRIPTION
Mojo::Content::Single is a container for HTTP content, based on RFC 7230 <https://tools.ietf.org/html/rfc7230> and RFC 7231 <https://tools.ietf.org/html/rfc7231>.

EVENTS
Mojo::Content::Single inherits all events from Mojo::Content and can emit the following new ones.

  upgrade
  $single->on(upgrade => sub ($single, $multi) {...});

Emitted when content gets upgraded to a Mojo::Content::MultiPart object.

  $single->on(upgrade => sub ($single, $multi) {
    return unless $multi->headers->content_type =~ /multipart\/([^;]+)/i;
    say "Multipart: $1";
  });

ATTRIBUTES
Mojo::Content::Single inherits all attributes from Mojo::Content and implements the following new ones.

  asset
  my $asset = $single->asset;
  $single   = $single->asset(Mojo::Asset::Memory->new);

The actual content, defaults to a Mojo::Asset::Memory object with "auto_upgrade" in Mojo::Asset::Memory enabled.

  auto_upgrade
  my $bool = $single->auto_upgrade;
  $single  = $single->auto_upgrade($bool);

Try to detect multipart content and automatically upgrade to a Mojo::Content::MultiPart object, defaults to a true value.

METHODS
Mojo::Content::Single inherits all methods from Mojo::Content and implements the following new ones.

  body_contains
  my $bool = $single->body_contains('1234567');

Check if content contains a specific string.

  body_size
  my $size = $single->body_size;

Content size in bytes.

  clone
  my $clone = $single->clone;

Return a new Mojo::Content::Single object cloned from this content if possible, otherwise return "undef".

  get_body_chunk
  my $bytes = $single->get_body_chunk(0);

Get a chunk of content starting from a specific position. Note that it might not be possible to get the same chunk twice if content was generated dynamically.

  new
  my $single = Mojo::Content::Single->new;
  my $single = Mojo::Content::Single->new(asset => Mojo::Asset::File->new);
  my $single = Mojo::Content::Single->new({asset => Mojo::Asset::File->new});

Construct a new Mojo::Content::Single object and subscribe to event "read" in Mojo::Content with default content parser.

  parse
  $single = $single->parse("Content-Length: 12\x0d\x0a\x0d\x0aHello World!");
  my $multi = $single->parse("Content-Type: multipart/form-data\x0d\x0a\x0d\x0a");

Parse content chunk and upgrade to Mojo::Content::MultiPart object if necessary.

SEE ALSO
Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.