Skip to content

Commit

Permalink
add basic support for signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 8, 2015
1 parent 7004bb5 commit 968537b
Show file tree
Hide file tree
Showing 18 changed files with 102 additions and 102 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Expand Up @@ -4,8 +4,6 @@ perl:
- "5.18"
- "5.16"
- "5.14"
- "5.12"
- "5.10"
env:
- "HARNESS_OPTIONS=j9 TEST_EV=1 TEST_POD=1 TEST_SOCKS=1 TEST_TLS=1"
install:
Expand Down
12 changes: 4 additions & 8 deletions Makefile.PL
Expand Up @@ -5,10 +5,8 @@ use warnings;

use ExtUtils::MakeMaker;

# Pod::Simple 3.09 first shipped with Perl 5.11.2
# JSON::PP 2.27103 first shipped with Perl 5.13.9
# Time::Local 1.2 first shipped with Perl 5.13.9
# IO::Socket::IP 0.26 first shipped with Perl 5.19.8
# Functon::Parmeters 1.0605 is required up to Perl 5.20.0
WriteMakefile(
NAME => 'Mojolicious',
VERSION_FROM => 'lib/Mojolicious.pm',
Expand All @@ -19,7 +17,7 @@ WriteMakefile(
dynamic_config => 0,
'meta-spec' => {version => 2},
no_index => {directory => ['examples', 't']},
prereqs => {runtime => {requires => {perl => '5.010001'}}},
prereqs => {runtime => {requires => {perl => '5.014'}}},
resources => {
bugtracker => {web => 'https://github.com/kraih/mojo/issues'},
homepage => 'http://mojolicio.us',
Expand All @@ -33,10 +31,8 @@ WriteMakefile(
},
},
PREREQ_PM => {
'IO::Socket::IP' => '0.26',
'JSON::PP' => '2.27103',
'Pod::Simple' => '3.09',
'Time::Local' => '1.2'
$^V >= 5.020000 ? () : ('Function::Parameters' => '1.0605'),
'IO::Socket::IP' => '0.26'
},
EXE_FILES => ['script/hypnotoad', 'script/mojo', 'script/morbo'],
test => {TESTS => 't/*.t t/*/*.t'}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -19,7 +19,7 @@
detection, first class Unicode support and much more for you to
discover.
* Very clean, portable and object-oriented pure-Perl API with no hidden
magic and no requirements besides Perl 5.20.0 (versions as old as 5.10.1
magic and no requirements besides Perl 5.20.0 (versions as old as 5.14.0
can be used too, but may require additional CPAN modules to be installed)
* Full stack HTTP and WebSocket client/server implementation with IPv6, TLS,
SNI, IDNA, HTTP/SOCKS5 proxy, Comet (long polling), keep-alive, connection
Expand Down
26 changes: 15 additions & 11 deletions lib/Mojo/Base.pm
Expand Up @@ -8,9 +8,6 @@ use feature ();
# No imports because we get subclassed, a lot!
use Carp ();

# Only Perl 5.14+ requires it on demand
use IO::Handle ();

# Supported on Perl 5.22+
my $NAME
= eval { require Sub::Util; Sub::Util->can('set_subname') } || sub { $_[1] };
Expand Down Expand Up @@ -87,7 +84,17 @@ sub import {

# Mojo modules are strict!
$_->import for qw(strict warnings utf8);
feature->import(':5.10');
feature->import(':5.14');

# Core signatures for Perl 5.20+ and Function::Parameters for everything else
if ($^V >= 5.020000) {
feature->import('signatures');
warnings->unimport('experimental::signatures');
}
else {
require Function::Parameters;
Function::Parameters->import({sub => 'function_strict'});
}
}

sub new {
Expand Down Expand Up @@ -138,7 +145,7 @@ Mojo::Base - Minimal base class for Mojo projects
L<Mojo::Base> is a simple base class for L<Mojo> projects with fluent
interfaces.
# Automatically enables "strict", "warnings", "utf8" and Perl 5.10 features
# Automatically enables "strict", "warnings", "utf8" and Perl 5.14 features
use Mojo::Base -strict;
use Mojo::Base -base;
use Mojo::Base 'SomeBaseClass';
Expand All @@ -149,15 +156,13 @@ All three forms save a lot of typing.
use strict;
use warnings;
use utf8;
use feature ':5.10';
use IO::Handle ();
use feature ':5.14';
# use Mojo::Base -base;
use strict;
use warnings;
use utf8;
use feature ':5.10';
use IO::Handle ();
use feature ':5.14';
use Mojo::Base;
push @ISA, 'Mojo::Base';
sub has { Mojo::Base::attr(__PACKAGE__, @_) }
Expand All @@ -166,8 +171,7 @@ All three forms save a lot of typing.
use strict;
use warnings;
use utf8;
use feature ':5.10';
use IO::Handle ();
use feature ':5.14';
require SomeBaseClass;
push @ISA, 'SomeBaseClass';
use Mojo::Base;
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Collection.pm
Expand Up @@ -42,7 +42,7 @@ sub grep {
}

sub join {
Mojo::ByteStream->new(join $_[1] // '', map {"$_"} @{$_[0]});
Mojo::ByteStream->new(CORE::join $_[1] // '', map {"$_"} @{$_[0]});
}

sub last { shift->[-1] }
Expand All @@ -63,7 +63,7 @@ sub reduce {
goto &List::Util::reduce;
}

sub reverse { $_[0]->new(reverse @{$_[0]}) }
sub reverse { $_[0]->new(CORE::reverse(@{$_[0]})) }

sub shuffle { $_[0]->new(List::Util::shuffle @{$_[0]}) }

Expand Down
8 changes: 5 additions & 3 deletions lib/Mojo/JSON.pm
Expand Up @@ -6,7 +6,7 @@ use Carp 'croak';
use Exporter 'import';
use JSON::PP ();
use Mojo::Util;
use Scalar::Util 'blessed';
use Scalar::Util qw(blessed set_prototype);

our @EXPORT_OK = qw(decode_json encode_json false from_json j to_json true);

Expand All @@ -33,7 +33,8 @@ sub decode_json {

sub encode_json { Mojo::Util::encode 'UTF-8', _encode_value(shift) }

sub false () {JSON::PP::false}
sub false {JSON::PP::false}
BEGIN { set_prototype \&false, '' }

sub from_json {
my $err = _decode(\my $value, shift, 1);
Expand All @@ -47,7 +48,8 @@ sub j {

sub to_json { _encode_value(shift) }

sub true () {JSON::PP::true}
sub true {JSON::PP::true}
BEGIN { set_prototype \&true, '' }

sub _decode {
my $valueref = shift;
Expand Down
73 changes: 31 additions & 42 deletions lib/Mojo/Message/Request.pm
Expand Up @@ -10,8 +10,7 @@ has method => 'GET';
has url => sub { Mojo::URL->new };
has 'reverse_proxy';

sub clone {
my $self = shift;
sub clone ($self) {

# Dynamic requests cannot be cloned
return undef unless my $content = $self->content->clone;
Expand All @@ -26,29 +25,27 @@ sub clone {
return $clone;
}

sub cookies {
my $self = shift;
sub cookies ($self, @cookies) {

# Parse cookies
my $headers = $self->headers;
return [map { @{Mojo::Cookie::Request->parse($_)} } $headers->cookie]
unless @_;
unless @cookies;

# Add cookies
my @cookies = $headers->cookie || ();
for my $cookie (@_) {
my @new = $headers->cookie || ();
for my $cookie (@cookies) {
$cookie = Mojo::Cookie::Request->new($cookie) if ref $cookie eq 'HASH';
push @cookies, $cookie;
push @new, $cookie;
}
$headers->cookie(join('; ', @cookies));
$headers->cookie(join('; ', @new));

return $self;
}

sub every_param { shift->params->every_param(@_) }
sub every_param ($self, @args) { $self->params->every_param(@args) }

sub extract_start_line {
my ($self, $bufref) = @_;
sub extract_start_line ($self, $bufref) {

# Ignore any leading empty lines
return undef unless $$bufref =~ s/^\s*(.*?)\x0d?\x0a//;
Expand All @@ -60,9 +57,8 @@ sub extract_start_line {
return !!($1 eq 'CONNECT' ? $url->authority($2) : $url->parse($2));
}

sub fix_headers {
my $self = shift;
$self->{fix} ? return $self : $self->SUPER::fix_headers(@_);
sub fix_headers ($self) {
$self->{fix} ? return $self : $self->SUPER::fix_headers;

# Host
my $url = $self->url;
Expand All @@ -82,40 +78,37 @@ sub fix_headers {
return $self;
}

sub get_start_line_chunk {
my ($self, $offset) = @_;
sub get_start_line_chunk ($self, $offset) {
$self->_start_line->emit(progress => 'start_line', $offset);
return substr $self->{start_buffer}, $offset, 131072;
}

sub is_handshake { lc($_[0]->headers->upgrade // '') eq 'websocket' }
sub is_handshake ($self) { lc($self->headers->upgrade // '') eq 'websocket' }

sub is_secure {
my $url = shift->url;
sub is_secure ($self) {
my $url = $self->url;
return ($url->protocol || $url->base->protocol) eq 'https';
}

sub is_xhr {
(shift->headers->header('X-Requested-With') // '') =~ /XMLHttpRequest/i;
sub is_xhr ($self) {
($self->headers->header('X-Requested-With') // '') =~ /XMLHttpRequest/i;
}

sub param { shift->params->param(@_) }
sub param ($self, @args) { $self->params->param(@args) }

sub params {
my $self = shift;
sub params ($self) {
return $self->{params}
||= $self->body_params->clone->append($self->query_params);
}

sub parse {
my $self = shift;
sub parse ($self, @args) {

# Parse CGI environment
my $env = @_ > 1 ? {@_} : ref $_[0] eq 'HASH' ? $_[0] : undef;
my $env = @args > 1 ? {@args} : ref $args[0] eq 'HASH' ? $args[0] : undef;
$self->env($env)->_parse_env($env) if $env;

# Parse normal message
my @args = $env ? () : @_;
@args = $env ? () : @args;
if (($self->{state} // '') ne 'cgi') { $self->SUPER::parse(@args) }

# Parse CGI content
Expand Down Expand Up @@ -146,24 +139,21 @@ sub parse {
return $self;
}

sub proxy {
my $self = shift;
return $self->{proxy} unless @_;
$self->{proxy} = !$_[0] || ref $_[0] ? shift : Mojo::URL->new(shift);
sub proxy ($self, $proxy = undef) {
return $self->{proxy} unless defined $proxy;
$self->{proxy} = !$proxy || ref $proxy ? $proxy : Mojo::URL->new($proxy);
return $self;
}

sub query_params { shift->url->query }
sub query_params ($self) { $self->url->query }

sub start_line_size { length shift->_start_line->{start_buffer} }
sub start_line_size ($self) { length $self->_start_line->{start_buffer} }

sub _parse_basic_auth {
return undef unless my $header = shift;
return $header =~ /Basic (.+)$/ ? b64_decode $1 : undef;
sub _parse_basic_auth ($header) {
return $header ? $header =~ /Basic (.+)$/ ? b64_decode $1 : undef : undef;
}

sub _parse_env {
my ($self, $env) = @_;
sub _parse_env ($self, $env) {

# Extract headers
my $headers = $self->headers;
Expand Down Expand Up @@ -223,8 +213,7 @@ sub _parse_env {
$self->{state} = 'cgi';
}

sub _start_line {
my $self = shift;
sub _start_line ($self) {

return $self if defined $self->{start_buffer};

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Template.pm
Expand Up @@ -335,7 +335,7 @@ content with the L<Mojolicious> renderer.
=head1 SYNTAX
For all templates L<strict>, L<warnings>, L<utf8> and Perl 5.10 features are
For all templates L<strict>, L<warnings>, L<utf8> and Perl 5.14 features are
automatically enabled.
<% Perl code %>
Expand Down
5 changes: 3 additions & 2 deletions lib/Mojo/Util.pm
Expand Up @@ -10,6 +10,7 @@ use Exporter 'import';
use IO::Poll qw(POLLIN POLLPRI);
use List::Util 'min';
use MIME::Base64 qw(decode_base64 encode_base64);
use Scalar::Util 'set_prototype';
use Symbol 'delete_package';
use Time::HiRes ();

Expand Down Expand Up @@ -259,11 +260,12 @@ sub squish {
return $str;
}

sub steady_time () {
sub steady_time {
MONOTONIC
? Time::HiRes::clock_gettime(Time::HiRes::CLOCK_MONOTONIC())
: Time::HiRes::time;
}
BEGIN { set_prototype \&steady_time, '' }

sub tablify {
my $rows = shift;
Expand Down Expand Up @@ -378,7 +380,6 @@ sub _encoding {
$CACHE{$_[0]} //= find_encoding($_[0]) // croak "Unknown encoding '$_[0]'";
}

# Supported on Perl 5.14+
sub _global_destruction {
defined ${^GLOBAL_PHASE} && ${^GLOBAL_PHASE} eq 'DESTRUCT';
}
Expand Down
6 changes: 4 additions & 2 deletions lib/Mojolicious/Command/eval.pm
Expand Up @@ -14,8 +14,10 @@ sub run {

# Run code against application
my $app = $self->app;
no warnings;
my $result = eval "package main; sub app; local *app = sub { \$app }; $code";
no strict 'refs';
no warnings 'redefine';
local *{'main::app'} = sub {$app};
my $result = eval "package main; $code";
return $@ ? die $@ : $result unless defined $result && ($v1 || $v2);
$v2 ? print($app->dumper($result)) : say $result;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Guides/FAQ.pod
Expand Up @@ -70,7 +70,7 @@ community and receive bug fixes, which are currently 5.22.x and 5.20.x.
L<Mojolicious> follows this model and fully supports these two release series.
In addition we will also keep the distribution installable up to a certain
legacy version that we deem worthy of supporting, but not specifically optimize
for it, this is currently 5.10.1.
for it, this is currently 5.14.0.

=head2 Do I need to clean my environment before testing Mojolicious?

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Guides/Rendering.pod
Expand Up @@ -65,7 +65,7 @@ L<Mojolicious> includes a minimalistic but very powerful template system out of
the box called Embedded Perl or C<ep> for short. It allows the embedding of
Perl code right into actual content using a small set of special tags and line
start characters. For all templates L<strict>, L<warnings>, L<utf8> and Perl
5.10 features are automatically enabled.
5.14 features are automatically enabled.

<% Perl code %>
<%= Perl expression, replaced with XML escaped result %>
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Guides/Tutorial.pod
Expand Up @@ -14,7 +14,7 @@ applications.
=head2 Hello World

A simple Hello World application can look like this, L<strict>, L<warnings>,
L<utf8> and Perl 5.10 features are automatically enabled and a few functions
L<utf8> and Perl 5.14 features are automatically enabled and a few functions
imported when you use L<Mojolicious::Lite>, turning your script into a full
featured web application.

Expand Down

0 comments on commit 968537b

Please sign in to comment.