Skip to content

Instantly share code, notes, and snippets.

@sharifulin
Created May 17, 2013 10:36
Show Gist options
  • Save sharifulin/5598328 to your computer and use it in GitHub Desktop.
Save sharifulin/5598328 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use common::sense;
use lib qw(/tk/lib /tk/mojo/lib);
$ENV{MOJO_APP} ||= 'Proxy';
use Mojo::Commands; Mojo::Commands->start;
package Proxy;
use common::sense;
use base 'Mojo';
use Data::Dumper;
sub handler {
my($self, $tx) = @_;
my $new = Mojo::Transaction::Single->new;
$new->req->parse( $tx->req->to_string );
$new->req->headers->remove('Host');
$new->req->url->parse( 'http://mobile.maps.yandex.net' . $tx->req->url );
$new->req->headers->content_length($new->req->headers->content_length + 1) if $new->req->body;
$self->client->queue($new, sub {
my($c, $tx2) = @_;
say join "\n", $tx2->req->url, scalar $tx2->res->headers->content_length, '';
{
open my $fh, '>', 'cache/' . $tx2->req->method . '_'. _clean($tx2->req->url) . '_' . time or die $!;
print $fh join "\n============\n", $tx2->req, $tx2->res;
}
$tx->res->parse( $tx2->res->to_string );
})->process;
}
sub _clean {
for (my $s = shift){
s{http://.*?/}{};
s/\W+/_/sg;
s/_+/_/sg;
return $_;
}
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment