File: Traefik.pm

package info (click to toggle)
lemonldap-ng 2.21.2%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 28,024 kB
  • sloc: perl: 77,414; javascript: 25,284; xml: 6,473; makefile: 1,303; sh: 453; sql: 159; python: 53; php: 26
file content (59 lines) | stat: -rw-r--r-- 1,469 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package Lemonldap::NG::Handler::Server::Traefik;

use strict;
use Mouse;
use Lemonldap::NG::Handler::Server::Main;

our $VERSION = '2.19.0';

extends 'Lemonldap::NG::Handler::PSGI';

sub init {
    my $self = shift;
    $self->api('Lemonldap::NG::Handler::Server::Main');
    my $tmp = $self->SUPER::init(@_);
}

sub _run {
    my $self = shift;

    # Create regular _authAndTrace PSGI app
    my $app = $self->psgiAdapter(
        sub {
            my $req = $_[0];
            return $self->_authAndTrace($req);
        }
    );

    # Middleware to set correct values for Traefik
    return sub {
        my $env = $_[0];
        $env->{HTTP_HOST}   = $env->{HTTP_X_FORWARDED_HOST};
        $env->{REQUEST_URI} = $env->{HTTP_X_FORWARDED_URI};
        return $app->($env);
    }
}

sub handler {
    my ( $self, $req ) = @_;
    my @convertedHdrs =
      ( 'Content-Length' => 0, Cookie => ( $req->env->{HTTP_COOKIE} // '' ) );
    while ( my ( $k, $v ) = splice( @{ $req->{respHeaders} }, 0, 2 ) ) {
        if ( $k =~ /^(?:Deleteheader\d+|Cookie)$/ ) {
            next;
        }
        else {
            push @convertedHdrs, $k, $v;
        }
    }

    # Echo the Authorization header to compensate the fact that Traefik removes
    # it from the incoming HTTP request
    if ( my $authorization = $req->headers->header('Authorization') ) {
        push @convertedHdrs, "Authorization" => $authorization;
    }

    return [ 200, \@convertedHdrs, [] ];
}

1;