Skip to content

Instantly share code, notes, and snippets.

/croak.diff Secret

Created August 17, 2016 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/6cb0ed7ad64c91c05facd6f49a41df53 to your computer and use it in GitHub Desktop.
Save anonymous/6cb0ed7ad64c91c05facd6f49a41df53 to your computer and use it in GitHub Desktop.
diff --git a/lib/Mojo/UserAgent.pm b/lib/Mojo/UserAgent.pm
index ebe9537..692d2f2 100644
--- a/lib/Mojo/UserAgent.pm
+++ b/lib/Mojo/UserAgent.pm
@@ -3,6 +3,7 @@ use Mojo::Base 'Mojo::EventEmitter';
# "Fry: Since when is the Internet about robbing people of their privacy?
# Bender: August 6, 1991."
+use Carp 'croak';
use Mojo::IOLoop;
use Mojo::Util qw(monkey_patch term_escape);
use Mojo::UserAgent::CookieJar;
@@ -48,6 +49,10 @@ sub start {
# Fork-safety
$self->_cleanup->server->restart unless ($self->{pid} //= $$) eq $$;
+ my $proto = $tx->req->url->protocol;
+ croak qq/Unsupported protocol "$proto"/
+ if $proto && $proto ne 'http' && $proto ne 'https';
+
# Non-blocking
if ($cb) {
warn "-- Non-blocking request (@{[_url($tx)]})\n" if DEBUG;
diff --git a/t/mojo/user_agent.t b/t/mojo/user_agent.t
index de5e604..99ce618 100644
--- a/t/mojo/user_agent.t
+++ b/t/mojo/user_agent.t
@@ -592,4 +592,8 @@ Mojo::IOLoop->delay(
)->wait;
is scalar @$result, 2, 'two active connections';
+# Unsupported protocol
+eval { $ua->get('file:///etc/passwd') };
+like $@, qr/Unsupported protocol "file"/, 'right error';
+
done_testing();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment