How to get http header correctly with websocket http request

89 views
Skip to first unread message

Mert Öztürk

unread,
Nov 18, 2015, 5:21:51 AM11/18/15
to ChicagoBoss
Hello,

I am trying to get hostname with websocket connection on the following function: handle_join(ServiceName, WebSocketId, State)
I tried with  Req:header(host), Req:header(hostname), Req:header("host"), Req:header("hostname"), but i get the following error;

tt_service_websocket (<0.1698.0>) starting...
12:01:52.853 [error] ** Boss Service Handler tt_service_websocket terminating in join/0
   for the reason error:undef
ServiceUrl: "/websocket/service"
WebSocketId: <0.1696.0>
SessionId  : undefined
State    : {state,{dict,0,16,16,8,80,48,{[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]},{{[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]}}}}
** Stacktrace: [{http_req,header,[host,{http_req,#Port<0.39188>,ranch_tcp,keepalive,<0.1696.0>,<<"GET">>,'HTTP/1.1',{{127,0,0,1},52131},<<"localhost">>,undefined,8001,<<"/websocket/service">>,undefined,<<>>,undefined,[],[{<<"host">>,<<"localhost:8001">>},{<<"connection">>,<<"Upgrade">>},{<<"pragma">>,<<"no-cache">>},{<<"cache-control">>,<<"no-cache">>},{<<"upgrade">>,<<"websocket">>},{<<"origin">>,<<"http://localhost:8001">>},{<<"sec-websocket-version">>,<<"13">>},{<<"user-agent">>,<<"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36">>},{<<"accept-encoding">>,<<"gzip, deflate, sdch">>},{<<"accept-language">>,<<"en-US,en;q=0.8">>},{<<"cookie">>,<<"_ga=GA1.1.93174808.1447451951">>},{<<"sec-websocket-key">>,<<"olKXNasMxISW/Xn6fPZ9Yg==">>},{<<"sec-websocket-extensions">>,<<"permessage-deflate; client_max_window_bits">>}],[{<<"sec-websocket-extensions">>,[{<<"permessage-deflate">>,[<<"client_max_window_bits">>]}]},{<<"upgrade">>,[<<"websocket">>]},{<<"connection">>,[<<"upgrade">>]}],undefined,[{websocket_compress,false},{websocket_version,13}],waiting,undefined,<<>>,false,waiting,[],<<>>,undefined}],[]},{tt_service_websocket,handle_join,4,[{file,"c:/Users/Asus/Desktop/tt/src/websocket/tt_service_websocket.erl"},{line,23}]},{boss_service_worker,handle_cast,2,[{file,"src/boss/boss_service_worker.erl"},{line,123}]},{gen_server,handle_msg,5,[{file,"gen_server.erl"},{line,599}]},{proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,237}]}]

12:01:52.853 [error] gen_server tt_service_websocket terminated with reason: bad return value: ok
12:01:52.853 [error] CRASH REPORT Process <0.1686.0> with 0 neighbours exited with reason: bad return value: ok in gen_server:terminate/6 line 737
12:01:52.853 [error] Supervisor {global,boss_service_sup} had child tt_service_websocket started with boss_service_worker:start_link(tt_service_websocket, <<"/websocket/service">>) at <0.1686.0> exit with reason bad return value: ok in context child_terminated

I think i get the cowboy req object in the error code. Is there a correct way to get hostname through websocket connection and use it in my websocket service code as a security and confirmation?

Thanks
Mert




Dmitry Polyanovsky

unread,
Nov 18, 2015, 6:31:36 AM11/18/15
to ChicagoBoss
Hi,

first of all, which CB version you are using?

Mert Öztürk

unread,
Nov 18, 2015, 7:01:07 AM11/18/15
to ChicagoBoss
Hello,

I am using 0.8.12

18 Kasım 2015 Çarşamba 13:31:36 UTC+2 tarihinde Dmitry Polyanovsky yazdı:
Message has been deleted

Mert Öztürk

unread,
Nov 18, 2015, 8:09:22 AM11/18/15
to ChicagoBoss
Hello Dimitry

Actually after your question, i realized that i am using an old version of CB and decided to upgrade OTP 17 to 18 and CB to the latest alpha release in the given link below;
When i try start-server.bat after creating a new project, it stays forever while ==>boss (compile)
I am using windows 10. Is it about the OTP 18?

Thanks
Mert

18 Kasım 2015 Çarşamba 13:31:36 UTC+2 tarihinde Dmitry Polyanovsky yazdı:
Hi,

Dmitry Polyanovsky

unread,
Nov 18, 2015, 2:04:11 PM11/18/15
to ChicagoBoss
Hi,

try to use latest HEAD. windows is not something I can test, but we could solve it too, if there is any problem.

Mert Öztürk

unread,
Nov 18, 2015, 2:42:00 PM11/18/15
to ChicagoBoss
Hello,

May i know which version of CB should i use and which erlang version?
I came up here from the past conversation (https://groups.google.com/forum/#!msg/chicagoboss/-W4cOraeHvU/Rk-g6Mp-GSIJ) and saw that this is implemented almost 2 years ago.
Now i check my boss files (api-websocket.html, boss_mochicow_handler and boss_service_worker) they are the same as modified, and still cant get my hostname from my myapp_service_websocket.erl while handle_join function with Req:header(host)
If i know the version of min CB version and erlang version and also how to implement this, i can make it.

Thanks
Mert

18 Kasım 2015 Çarşamba 21:04:11 UTC+2 tarihinde Dmitry Polyanovsky yazdı:

Dmitry Polyanovsky

unread,
Nov 19, 2015, 1:02:45 AM11/19/15
to chica...@googlegroups.com
Hi,
for Erlang 17 & 18 use current HEAD
for anything below use legacy branch or v0.8.x releases



--
You received this message because you are subscribed to the Google Groups "ChicagoBoss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chicagoboss...@googlegroups.com.
Visit this group at http://groups.google.com/group/chicagoboss.
To view this discussion on the web visit https://groups.google.com/d/msgid/chicagoboss/1a0e1086-718d-4939-bcc5-03fc8cbd799a%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Mert Öztürk

unread,
Nov 19, 2015, 11:32:51 AM11/19/15
to ChicagoBoss
Hello,

So for Erlang  17 & 18 and current HEAD, i can see hostname like this with websocket connection on handle join?;
io:format(~p~n,[Req:header(host)]),

Thanks
Mert


19 Kasım 2015 Perşembe 08:02:45 UTC+2 tarihinde Dmitry Polyanovsky yazdı:

Mert Öztürk

unread,
Nov 19, 2015, 1:58:21 PM11/19/15
to ChicagoBoss
Also, maybe this is an amateur question but may i know how to use the latest HEAD in my CB application? :)

19 Kasım 2015 Perşembe 08:02:45 UTC+2 tarihinde Dmitry Polyanovsky yazdı:

Dmitry Polyanovsky

unread,
Nov 22, 2015, 3:00:08 PM11/22/15
to ChicagoBoss
with something like {boss, ".*", {git, "git://github.com/ChicagoBoss/ChicagoBoss.git", "HEAD"}}

Mert Öztürk

unread,
Nov 23, 2015, 4:31:40 AM11/23/15
to ChicagoBoss
thanks

22 Kasım 2015 Pazar 22:00:08 UTC+2 tarihinde Dmitry Polyanovsky yazdı:

graeme defty

unread,
Nov 28, 2015, 11:51:05 PM11/28/15
to ChicagoBoss
Hi,

I hit this same problem, and I think it may be to do with a new version of simple_bridge (maybe?) and the face that headers seem now to be binaries instead of strings.

If you use Req:header(<<"host">>) you may find it works.

I did something similar and it works for me (tm).

You will also get a binary returned, of course, so you will have to deal with it appropriately.

I hope this helps.

g

graeme defty

unread,
Nov 28, 2015, 11:51:15 PM11/28/15
to ChicagoBoss
Hi,

I hit this same problem, and I think it may be to do with a new version of simple_bridge (maybe?) and the face that headers seem now to be binaries instead of strings.

If you use Req:header(<<"host">>) you may find it works.

I did something similar and it works for me (tm).

You will also get a binary returned, of course, so you will have to deal with it appropriately.

I hope this helps.

g




On Wednesday, November 18, 2015 at 5:21:51 PM UTC+7, Mert Öztürk wrote:

Mert Öztürk

unread,
Dec 4, 2015, 6:00:32 PM12/4/15
to ChicagoBoss
Hello Graeme,

I still couldnt get it working also using with binary :(
Which erlang and cb version are you using?

Mert


29 Kasım 2015 Pazar 06:51:15 UTC+2 tarihinde graeme defty yazdı:

Graeme Defty

unread,
Dec 4, 2015, 7:32:53 PM12/4/15
to chica...@googlegroups.com
Hi,

I am on the latest HEAD of CB and Erlang/OTP R18.

If you print out the Req you should be able to see what's needed. I relied on a good, old-fashioned 'print' statement (well ... it was an 'io:format/1' function, in fact  :-) )

graeme



--
You received this message because you are subscribed to the Google Groups "ChicagoBoss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chicagoboss...@googlegroups.com.
Visit this group at http://groups.google.com/group/chicagoboss.

Mert Öztürk

unread,
Dec 5, 2015, 6:09:20 AM12/5/15
to ChicagoBoss
Thanks Graeme,

I will try it as soon as possible.

5 Aralık 2015 Cumartesi 02:32:53 UTC+2 tarihinde graeme defty yazdı:
Reply all
Reply to author
Forward
0 new messages