+}
+
+=method build_headers_from_env
+
+Supplement the headers-building logic from L<Plack::App::Proxy> to maintain
+the complete list of proxies in C<X-Forwarded-For> and to set the following
+headers if they are not already set: C<X-Forwarded-Proto> to the value of
+C<psgi.url_scheme>, C<X-Real-IP> to the value of C<REMOTE_ADDR>, and C<Host>
+to the host and port number of a URI (if given).
+
+This is called internally.
+
+=cut
+
+sub build_headers_from_env {
+ my ($self, $env, $req, $uri) = @_;
+
+ my $headers = $self->SUPER::build_headers_from_env($env, $req);
+
+ # if x-forwarded-for already existed, append the remote address; the super
+ # method fails to maintain a list of mutiple proxies
+ if (my $forwarded_for = $env->{HTTP_X_FORWARDED_FOR}) {
+ $headers->{'X-Forwarded-For'} = "$forwarded_for, $env->{REMOTE_ADDR}";
+ }
+
+ # the super method depends on the user agent to add the host header if it
+ # is missing, so set the host if it needs to be set
+ if ($uri && !$headers->{'Host'}) {
+ $headers->{'Host'} = $uri->host_port;
+ }
+
+ $headers->{'X-Forwarded-Proto'} ||= $env->{'psgi.url_scheme'};
+ $headers->{'X-Real-IP'} ||= $env->{REMOTE_ADDR};
+
+ $headers;
+}