| 1 | package HTTP::Tiny::PreserveHostHeader; | ||||||||||||
| 2 | |||||||||||||
| 3 | =head1 NAME | ||||||||||||
| 4 | |||||||||||||
| 5 | HTTP::Tiny::PreserveHostHeader - preserve Host header on requests | ||||||||||||
| 6 | |||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||
| 8 | |||||||||||||
| 9 | =for markdown ```perl | ||||||||||||
| 10 | |||||||||||||
| 11 | use HTTP::Tiny::PreserveHostHeader; | ||||||||||||
| 12 | |||||||||||||
| 13 | my $response = HTTP::Tiny::PreserveHostHeader->new->get( | ||||||||||||
| 14 | 'http://example.com', { | ||||||||||||
| 15 | headers => { | ||||||||||||
| 16 | Host => 'example.net', | ||||||||||||
| 17 | } | ||||||||||||
| 18 | } | ||||||||||||
| 19 | ); | ||||||||||||
| 20 | |||||||||||||
| 21 | =for markdown ``` | ||||||||||||
| 22 | |||||||||||||
| 23 | =head1 DESCRIPTION | ||||||||||||
| 24 | |||||||||||||
| 25 | This module extends L<HTTP::Tiny> and allows to preserve original C<Host> | ||||||||||||
| 26 | header from HTTP request. | ||||||||||||
| 27 | |||||||||||||
| 28 | The L<HTTP::Tiny> is strictly compatible with HTTP 1.1 spec, section 14.23: | ||||||||||||
| 29 | |||||||||||||
| 30 | =over | ||||||||||||
| 31 | |||||||||||||
| 32 | The Host field value MUST represent the naming authority of the origin | ||||||||||||
| 33 | server or gateway given by the original URL. | ||||||||||||
| 34 | |||||||||||||
| 35 | =back | ||||||||||||
| 36 | |||||||||||||
| 37 | It means that L<HTTP::Tiny> always rewrite C<Host> header to the value | ||||||||||||
| 38 | taken from URL. | ||||||||||||
| 39 | |||||||||||||
| 40 | Some non-standard HTTP clients, such as reverse HTTP proxy, need to override | ||||||||||||
| 41 | C<Host> header to other value. | ||||||||||||
| 42 | |||||||||||||
| 43 | =for readme stop | ||||||||||||
| 44 | |||||||||||||
| 45 | =cut | ||||||||||||
| 46 | |||||||||||||
| 47 | 1 | ▶ | use 5.006; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 48 | |||||||||||||
| 49 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 50 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 51 | |||||||||||||
| 52 | 1 | our $VERSION = '0.0101'; | |||||||||||
| 53 | |||||||||||||
| 54 | 1 | ▶ | use parent qw(HTTP::Tiny); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 55 | |||||||||||||
| 56 | ## no critic(Subroutines::ProhibitUnusedPrivateSubroutines) | ||||||||||||
| 57 | sub _prepare_headers_and_cb { | ||||||||||||
| 58 | 0 | ▶ | my ($self, $request, $args, $url, $auth) = @_; | ||||||||||
|
Subroutine
_prepare_headers_and_cb: not called CC=4 SCAR=30.0
| |||||||||||||
| 59 | |||||||||||||
| 60 | 0 | my $host; | |||||||||||
| 61 | |||||||||||||
| 62 | 0 | ▶ | while (my ($k, $v) = each %{ $args->{headers} }) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 63 | 0 | ▶ | if (lc $k eq 'host') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 64 | 0 | $host = $v; | |||||||||||
| 65 | 0 | delete $args->{headers}{$k}; | |||||||||||
| 66 | } | ||||||||||||
| 67 | } | ||||||||||||
| 68 | |||||||||||||
| 69 | 0 | $self->SUPER::_prepare_headers_and_cb($request, $args, $url, $auth); | |||||||||||
| 70 | |||||||||||||
| 71 | 0 | ▶ | $request->{headers}{host} = $host if $host; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 72 | |||||||||||||
| 73 | 0 | return; | |||||||||||
| 74 | } | ||||||||||||
| 75 | |||||||||||||
| 76 | ## no critic(Subroutines::ProhibitUnusedPrivateSubroutines) | ||||||||||||
| 77 | sub _agent { | ||||||||||||
| 78 | 0 | ▶ | my ($self) = @_; | ||||||||||
|
Subroutine
_agent: not called CC=3 SCAR=24.8
| |||||||||||||
| 79 | 0 | ▶ | my $class = ref $self ? ref $self : $self; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 80 | 0 | (my $default_agent = $class) =~ s{::}{-}g; | |||||||||||
| 81 | ## no critic(Subroutines::ProtectPrivateSubs) | ||||||||||||
| 82 | 0 | ▶ | return $default_agent . "/" . ($class->VERSION || 0) . " " . HTTP::Tiny->_agent; | ||||||||||
|
Condition0%0/2
$class->VERSION || 0
MC/DC0%0/1
$class->VERSION || 0
$class->VERSION
Truth table0%0/2
A
A= $class->VERSION
| |||||||||||||
| 83 | } | ||||||||||||
| 84 | |||||||||||||
| 85 | 1 | 1; | |||||||||||
| 86 | |||||||||||||
| 87 | =for readme continue | ||||||||||||
| 88 | |||||||||||||
| 89 | =head1 SEE ALSO | ||||||||||||
| 90 | |||||||||||||
| 91 | L<HTTP::Tiny>, L<https://github.com/chansen/p5-http-tiny/pull/34>. | ||||||||||||
| 92 | |||||||||||||
| 93 | =head1 BUGS | ||||||||||||
| 94 | |||||||||||||
| 95 | If you find the bug or want to implement new features, please report it at | ||||||||||||
| 96 | L<https://github.com/dex4er/perl-HTTP-Tiny-PreserveHostHeader/issues> | ||||||||||||
| 97 | |||||||||||||
| 98 | The code repository is available at | ||||||||||||
| 99 | L<http://github.com/dex4er/perl-HTTP-Tiny-PreserveHostHeader> | ||||||||||||
| 100 | |||||||||||||
| 101 | =head1 AUTHOR | ||||||||||||
| 102 | |||||||||||||
| 103 | Piotr Roszatycki <dexter@cpan.org> | ||||||||||||
| 104 | |||||||||||||
| 105 | =head1 LICENSE | ||||||||||||
| 106 | |||||||||||||
| 107 | Copyright (c) 2014-2016, 2023 Piotr Roszatycki <dexter@cpan.org>. | ||||||||||||
| 108 | |||||||||||||
| 109 | This is free software; you can redistribute it and/or modify it under | ||||||||||||
| 110 | the same terms as perl itself. | ||||||||||||
| 111 | |||||||||||||
| 112 | See L<http://dev.perl.org/licenses/artistic.html> | ||||||||||||