Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

host header invalid for http+unix:// url values #382

Closed
fp opened this issue Jan 28, 2017 · 2 comments · Fixed by #383
Closed

host header invalid for http+unix:// url values #382

fp opened this issue Jan 28, 2017 · 2 comments · Fixed by #383

Comments

@fp
Copy link

fp commented Jan 28, 2017

TLDR; when sending a http+unix:// request, the host: header should be url encoded.

Assuming a request like the following is made is made to a unix domain socket:

    Uri = <<"http+unix://%2ftmp%2ferllambda.sock/erllambda/erllambda_test">>,
    Headers = [{<<"accept">>, <<"application/json">>}],
    case hackney:request( get, Uri, Headers, <<>>, [] ) of
        {ok, Status, RespHeaders, Ref} ->
            {ok, Body} = hackney:body( Ref ),
            DecodeBody = decode_body( Body ),
            {ok, Status, RespHeaders, DecodeBody};
        Otherwise -> Otherwise
    end.

This results in a 400 response from cowboy (correctly) because the host header is sent as the unix domain socket file name, but not url encoded. The full trace:

[hackney trace 80 <0.494.0> 2017:01:28 17:32:04 4144] request 
   Content: [{module,hackney},
             {line,308},
             {method,get},
             {url,{hackney_url,hackney_local_tcp,http_unix,
                               <<"/tmp/erllambda.sock">>,
                               <<"/erllambda/erllambda_test">>,
                               <<"/erllambda/erllambda_test">>,<<>>,<<>>,
                               "/tmp/erllambda.sock",0,<<>>,<<>>}},
             {headers,[{<<"accept">>,<<"application/json">>}]},
             {body,<<>>},
             {options,[]}]

[hackney trace 60 <0.494.0> 2017:01:28 17:32:04 4144] request without proxy 
   Content: [{module,hackney},{line,689}]

[hackney trace 60 <0.494.0> 2017:01:28 17:32:04 4144] connect 
   Content: [{module,hackney_connect},
             {line,33},
             {transport,hackney_local_tcp},
             {host,"/tmp/erllambda.sock"},
             {port,0},
             {dynamic,true}]

[hackney trace 80 <0.494.0> 2017:01:28 17:32:04 4145] no socket in the pool 
   Content: [{module,hackney_connect},{line,200},{pool,default}]

[hackney trace 80 <0.494.0> 2017:01:28 17:32:04 4147] new connection 
   Content: [{module,hackney_connect},{line,248}]

[hackney trace 40 <0.494.0> 2017:01:28 17:32:04 4147] perform request 
   Content: [{module,hackney_request},
             {line,97},
             {header_data,<<"GET /erllambda/erllambda_test HTTP/1.1\r\naccept: application/json\r\nUser-Agent: hackney/1.6.5\r\nHost: /tmp/erllambda.sock\r\n\r\n">>},
             {perform_all,true},
             {expect,false}]

[hackney trace 80 <0.494.0> 2017:01:28 17:32:04 4147] got response 
   Content: [{module,hackney},
             {line,372},
             {response,
                 {ok,400,
                     [{<<"content-length">>,<<"0">>}],
                     {client,
                         {1485,624724,144075},
                         {metrics_ng,metrics_dummy},
                         hackney_local_tcp,"/tmp/erllambda.sock",0,
                         <<"/tmp/erllambda.sock:0">>,[],#Port<0.16734>,
                         {default,#Ref<0.0.3.8154>,
                             {"/tmp/erllambda.sock",0,hackney_local_tcp},
                             <0.496.0>,hackney_local_tcp},
                         #Ref<0.0.3.8154>,true,hackney_pool,5000,false,5,
                         false,5,nil,nil,
                         {hparser,response,4096,10,0,on_body,<<>>,
                             {1,1},
                             undefined,[],0,undefined,undefined,undefined,
                             undefined,waiting},
                         connected,waiting,nil,normal,false,false,false,
                         undefined,false,#Fun<hackney_request.send.2>,waiting,
                         nil,4096,<<>>,[],
                         {1,1},
                         0,nil,nil,<<"GET">>,<<"/erllambda/erllambda_test">>,
                         nil}}},
             {client,
                 {client,
                     {1485,624724,144075},
                     {metrics_ng,metrics_dummy},
                     hackney_local_tcp,"/tmp/erllambda.sock",0,
                     <<"/tmp/erllambda.sock:0">>,[],#Port<0.16734>,
                     {default,#Ref<0.0.3.8154>,
                         {"/tmp/erllambda.sock",0,hackney_local_tcp},
                         <0.496.0>,hackney_local_tcp},
                     #Ref<0.0.3.8154>,true,hackney_pool,5000,false,5,false,5,
                     nil,nil,nil,connected,start,nil,normal,false,false,false,
                     undefined,false,nil,waiting,nil,4096,<<>>,[],undefined,
                     nil,nil,nil,nil,undefined,nil}}]

[hackney trace 80 <0.496.0> 2017:01:28 17:32:04 4147] checkin: socket is not ok~n 
   Content: [{module,hackney_pool},
             {line,286},
             {socket,#Port<0.16734>},
             {peername,{error,einval}}]
@fp
Copy link
Author

fp commented Jan 28, 2017

PR to fix this coming later today.

fp pushed a commit to fp/hackney that referenced this issue Jan 28, 2017
When sending the `host` header for a request to a unix domain socket
the value must be url encoded, since most such values have invalid
slash characters. (fixes benoitc#382)
fp pushed a commit to fp/hackney that referenced this issue Jan 28, 2017
When sending the `host` header for a request to a unix domain socket
the value must be url encoded, since most such values have invalid
slash characters. (fixes benoitc#382)
@fp
Copy link
Author

fp commented Jan 28, 2017

This fixed makes the hackney side of things correct, and also requires a corresponding changes to cow_http_hd:reg_name/2 to accept the percent encoded value according to the specs: ninenines/cowlib#53

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant