Skip to content

Commit

Permalink
cleaned up buffer api
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 29, 2009
1 parent b8842b1 commit c5e5545
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 56 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ This file documents the revision history for Perl extension Mojo.

0.991243 2009-07-28 00:00:00
- Made proxy support more portable.
- Cleaned up internal Mojo APIs.

0.991242 2009-07-27 00:00:00
- Cleaned up the whole script system, this change is mostly backwards
Expand Down
74 changes: 47 additions & 27 deletions lib/Mojo/Buffer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,55 +11,69 @@ use bytes;

__PACKAGE__->attr('raw_length', default => 0);

sub new {
my $self = shift->SUPER::new();
$self->add_chunk(join '', @_) if @_;
$self->{buffer} ||= '';
return $self;
}

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

# Raw length
$self->raw_length($self->raw_length + length $chunk);
$self->{buffer} .= $chunk;

# Store
$self->{_buffer} .= $chunk;

return $self;
}

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

# Search
return index $self->{_buffer}, $chunk;
}

sub empty {
my $self = shift;
my $buffer = $self->{buffer};
$self->{buffer} = '';
my $self = shift;

# Cleanup
my $buffer = $self->{_buffer};
$self->{_buffer} = '';

return $buffer;
}

sub get_line {
my $self = shift;

# No full line in buffer
return unless $self->{buffer} =~ /\x0d?\x0a/;
return unless $self->{_buffer} =~ /\x0d?\x0a/;

# Locate line ending
my $pos = index $self->{buffer}, "\x0a";
my $pos = index $self->{_buffer}, "\x0a";

# Extract line and ending
my $line = substr $self->{buffer}, 0, $pos + 1, '';
my $line = substr $self->{_buffer}, 0, $pos + 1, '';
$line =~ s/(\x0d?\x0a)\z//;

return $line;
}

sub length {
my $self = shift;
$self->{buffer} ||= '';
return length $self->{buffer};
$self->{_buffer} ||= '';
return length $self->{_buffer};
}

sub remove {
my ($self, $length) = @_;
return substr $self->{buffer}, 0, $length, '';
my ($self, $length, $chunk) = @_;

# Chunk to replace?
$chunk ||= '';

# Extract and replace
$self->{_buffer} ||= '';
return substr $self->{_buffer}, 0, $length, $chunk;
}

sub to_string { shift->{buffer} || '' }
sub to_string { shift->{_buffer} || '' }

1;
__END__
Expand All @@ -72,7 +86,7 @@ Mojo::Buffer - A Simple In-Memory Buffer
use Mojo::Buffer;
my $buffer = Mojo::Buffer->new('foo');
my $buffer = Mojo::Buffer->new;
$buffer->add_chunk('bar');
my $foo = $buffer->remove(3);
my $bar = $buffer->empty;
Expand All @@ -85,13 +99,10 @@ L<Mojo::Buffer> is a simple in-memory buffer.
L<Mojo::Buffer> implements the following attributes.
=head2 C<length>
my $length = $buffer->length;
=head2 C<raw_length>
my $raw_length = $buffer->raw_length;
my $length = $buffer->raw_length;
$buffer = $buffer->raw_length;
=head1 METHODS
Expand All @@ -107,17 +118,26 @@ the following new ones.
$buffer = $buffer->add_chunk('foo');
=head2 C<contains>
my $position = $buffer->contains('something');
=head2 C<empty>
my $string = $buffer->empty;
my $chunk = $buffer->empty;
=head2 C<get_line>
my $line = $buffer->get_line;
=head2 C<length>
my $length = $buffer->length;
=head2 C<remove>
my $string = $buffer->remove(4);
my $chunk = $buffer->remove(4);
my $chunk = $buffer->remove(4, 'abcd');
=head2 C<to_string>
Expand Down
11 changes: 8 additions & 3 deletions lib/Mojo/Content.pm
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ sub build_headers {

sub body_contains {
my ($self, $chunk) = @_;
return $self->file->contains($chunk);

# Found
return 1 if $self->file->contains($chunk) >= 0;

# Not found
return 0;
}

sub body_length { shift->file->length }
Expand Down Expand Up @@ -203,7 +208,7 @@ sub parse_until_body {

# Parser started
if ($self->is_state('start')) {
my $length = length($self->filter_buffer->{buffer});
my $length = $self->filter_buffer->length;
my $raw_length = $self->filter_buffer->raw_length;
my $raw_header_length = $raw_length - $length;
$self->raw_header_length($raw_header_length);
Expand Down Expand Up @@ -236,7 +241,7 @@ sub _parse_headers {
$self->headers->buffer($self->filter_buffer);
$self->headers->parse;

my $length = length($self->headers->buffer->{buffer});
my $length = $self->headers->buffer->length;
my $raw_length = $self->headers->buffer->raw_length;
my $raw_header_length = $raw_length - $length;

Expand Down
27 changes: 13 additions & 14 deletions lib/Mojo/Content/MultiPart.pm
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ use Mojo::File;
__PACKAGE__->attr('parts', default => sub { [] });

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

# Check parts
my $found = 0;
for my $part (@{$self->parts}) {
my $headers = $part->build_headers;
$found += 1 if $headers =~ /$bytestream/g;
$found += $part->body_contains($bytestream);
$found += 1 if $headers =~ /$chunk/g;
$found += $part->body_contains($chunk);
}
return $found ? 1 : 0;
}
Expand Down Expand Up @@ -178,22 +178,21 @@ sub _parse_multipart {
sub _parse_multipart_body {
my ($self, $boundary) = @_;

my $pos = index $self->buffer->{buffer}, "\x0d\x0a--$boundary";
my $pos = $self->buffer->contains("\x0d\x0a--$boundary");

# Make sure we have enough buffer to detect end boundary
if ($pos < 0) {
my $length =
length($self->buffer->{buffer}) - (length($boundary) + 8);
my $length = $self->buffer->length - (length($boundary) + 8);
return unless $length > 0;

# Store chunk
my $chunk = substr $self->buffer->{buffer}, 0, $length, '';
my $chunk = $self->buffer->remove($length);
$self->parts->[-1] = $self->parts->[-1]->parse($chunk);
return;
}

# Store chunk
my $chunk = substr $self->buffer->{buffer}, 0, $pos, '';
my $chunk = $self->buffer->remove($pos);
$self->parts->[-1] = $self->parts->[-1]->parse($chunk);
$self->state('multipart_boundary');
return 1;
Expand All @@ -203,17 +202,17 @@ sub _parse_multipart_boundary {
my ($self, $boundary) = @_;

# Begin
if (index($self->buffer->{buffer}, "\x0d\x0a--$boundary\x0d\x0a") == 0) {
substr $self->buffer->{buffer}, 0, length($boundary) + 6, '';
if ($self->buffer->contains("\x0d\x0a--$boundary\x0d\x0a") == 0) {
$self->buffer->remove(length($boundary) + 6);
push @{$self->parts}, Mojo::Content->new(relaxed => 1);
$self->state('multipart_body');
return 1;
}

# End
my $end = "\x0d\x0a--$boundary--";
if (index($self->buffer->{buffer}, $end) == 0) {
$self->buffer->{buffer} =~ s/^$end//;
if ($self->buffer->contains($end) == 0) {
$self->buffer->remove(length $end);
$self->done;
}

Expand All @@ -224,9 +223,9 @@ sub _parse_multipart_preamble {
my ($self, $boundary) = @_;

# Replace preamble with CRLF
my $pos = index $self->buffer->{buffer}, "--$boundary";
my $pos = $self->buffer->contains("--$boundary");
unless ($pos < 0) {
substr $self->buffer->{buffer}, 0, $pos, "\x0d\x0a";
$self->buffer->remove($pos, "\x0d\x0a");
$self->state('multipart_boundary');
return 1;
}
Expand Down
19 changes: 16 additions & 3 deletions lib/Mojo/File.pm
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ __PACKAGE__->attr(
my $sum = b(time . rand(999999999))->md5_sum;
$file = "$base.$sum";
}

$self->path($file);

# Enable automatic cleanup
$self->cleanup(1);

# Open for read/write access
Expand All @@ -61,6 +62,8 @@ __PACKAGE__->attr(
sub DESTROY {
my $self = shift;
my $file = $self->path;

# Cleanup
unlink $file if $self->cleanup && -f $file;
}

Expand Down Expand Up @@ -96,7 +99,7 @@ sub contains {
$offset += $read;
$window .= $buffer;
my $pos = index $window, $bytestream;
return 1 if $pos >= 0;
return $pos if $pos >= 0;
substr $window, 0, $read, '';
}

Expand All @@ -106,8 +109,11 @@ sub contains {
sub copy_to {
my ($self, $path) = @_;
my $src = $self->path;

# Copy
File::Copy::copy($src, $path)
or croak qq/Can't copy file "$src" to "$path": $!/;

return $self;
}

Expand All @@ -125,6 +131,7 @@ sub get_chunk {
sub length {
my $self = shift;

# File size
my $file = $self->path;
return -s $file if $file;

Expand All @@ -134,8 +141,11 @@ sub length {
sub move_to {
my ($self, $path) = @_;
my $src = $self->path;

# Move
File::Copy::move($src, $path)
or croak qq/Can't move file "$src" to "$path": $!/;

return $self;
}

Expand Down Expand Up @@ -169,6 +179,9 @@ Mojo::File - File
$file->add_chunk('World!');
print $file->slurp;
my $file = Mojo::File->new(path => '/foo/bar.txt');
print $file->slurp;
=head1 DESCRIPTION
L<Mojo::File> is a container for files.
Expand Down Expand Up @@ -203,7 +216,7 @@ following new ones.
=head2 C<contains>
my $contains = $file->contains('random string');
my $position = $file->contains('random string');
=head2 C<copy_to>
Expand Down
7 changes: 5 additions & 2 deletions lib/Mojo/File/Memory.pm
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ sub add_chunk {
return $self;
}

sub contains { index(shift->{content}, shift) >= 0 ? 1 : 0 }
sub contains { index shift->{content}, shift }

sub copy_to { shift->_write_to_file(@_) }

Expand All @@ -41,9 +41,12 @@ sub slurp { shift->content }

sub _write_to_file {
my ($self, $path) = @_;

# Write
my $file = IO::File->new;
$file->open("> $path") or croak qq/Can't' open file "$path": $!/;
$file->syswrite($self->{content});

return $self;
}

Expand Down Expand Up @@ -87,7 +90,7 @@ the following new ones.
=head2 C<contains>
my $contains = $file->contains('random string');
my $position = $file->contains('random string');
=head2 C<copy_to>
Expand Down
Loading

0 comments on commit c5e5545

Please sign in to comment.