anchor.diff:    next(Req3, State, fun append_qs/2).
   
   append_qs(Req, State) ->
  +  {Path, _} = cowboy_req:meta(backend_path, Req),
     case cowboy_req:qs(Req) of
       {<<>>, _} ->
  -      next(Req, State, fun forwarded_header_prefix/2);
  +      Req2 = cowboy_req:set_meta(backend_path_qs, Path, Req),
  +      next(Req2, State, fun forwarded_header_prefix/2);
       {QS, _} ->
  -      {Path, _} = cowboy_req:meta(backend_path, Req),
         Req2 = cowboy_req:set_meta(backend_path_qs, <<Path/binary, "?", QS/binary>>, Req),
         next(Req2, State, fun forwarded_header_prefix/2)
     end.
  
positive.code: 
  %% Formatting
  format_forwarded_headers(Req, {Proto, Host, Port, Path}) ->
    [
      format_forwarded_proto(Req, Proto),
      format_forwarded_host(Req, Host),
      format_forwarded_port(Req, Port),
      format_forwarded_path(Req, Path)
    ].
  
  format_forwarded_proto(Req, Header) ->
    {Header, case cowboy_req:header(Header, Req) of
      {undefined, _} ->
        case cowboy_req:get([transport], Req) of
          [ranch_tcp] ->
            <<"http">>;
          _ ->
            <<"https">>
        end;
      {Proto, _} ->
        Proto
    end}.
  
  format_forwarded_host(Req, Header) ->
    {Header, case cowboy_req:header(Header, Req) of
      {undefined, _} ->
        [Host] = cowboy_req:get([host], Req),
        Host;
      {Host, _} ->
        Host
    end}.
  
  format_forwarded_port(Req, Header) ->
    {Header, case cowboy_req:header(Header, Req) of
      {undefined, _} ->
        [Port] = cowboy_req:get([port], Req),
        integer_to_binary(Port);
      {Port, _} ->
        Port
    end}.
  
  format_forwarded_path(Req, Header) ->
    {Header, case cowboy_req:header(Header, Req) of
      {undefined, _} ->
        [Path] = cowboy_req:get([path], Req),
        {BackendPath, _} = cowboy_req:meta(backend_path, Req),
        binary:part(Path, {0, byte_size(Path) - byte_size(BackendPath)});
      {Path, _} ->
        Path
    end}.
  
  %% internal.
  
  path_join(BasePath, undefined) ->
    BasePath;
  path_join(<<>>, Parts) ->
    [<<"/">>, io_join(lists:reverse(Parts), <<"/">>, [])];
  path_join(<<"/">>, Parts) ->
  
negative.code: 
  proxy_terminate(Req, #state{handler = Handler, handler_state = HandlerState}) ->
    case erlang:function_exported(Handler, proxy_terminate, 2) of
      true ->
        ok = Handler:proxy_terminate(cowboy_req:lock(Req), HandlerState);
      false ->
        ok
    end.
  