Skip to content

Instantly share code, notes, and snippets.

@tsucchi
Created November 22, 2018 02:56
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tsucchi/582f2b5eb0cea09236348b95d15e5e9e to your computer and use it in GitHub Desktop.
sub reproxy {
my ($c, $proxy_base_url) = @_;
my $path = $c->req->path;
my $query = $c->req->uri->query;
my $url = $proxy_base_url . '/' . $path;
if (defined $query ) {
$url .= '?' . $query;
}
$c->log->debug("proxy : $url");
my $furl = Furl->new(
agent => $c->req->user_agent,
timeout => $ENV{HTTP_TIMEOUT},
max_redirects => 0, #Furlはリダイレクトを追わないでブラウザにリダイレクトさせる
);
my $body = $c->_create_body();
my $req_for_proxy = HTTP::Request->new($c->req->method, $url, $c->req->headers, $body);
my $raw_cookie = $ENV{HTTP_COOKIE} // $ENV{COOKIE};
$req_for_proxy->header( Cookie => $raw_cookie );
$req_for_proxy->header( 'Content-Length' => length $body );
my $res = $furl->request($req_for_proxy);
my $response_header = $res->headers->as_http_headers;
$response_header->remove_header('content-encoding');# encoding gzip は Furl が解決しているため削除
$c->res->headers($response_header);
$c->res->header( content_length => length $res->body ); #gzipではなくなっているので、解凍後のlengthを指定
$c->res->status($res->status);
$c->res->body($res->body);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment