Merge pull request #1060 from emqtt/develop

Add a 'websocket_protocol_header' option to handle WebSocket connection from WeChat
This commit is contained in:
Feng Lee 2017-05-19 16:59:34 +08:00 committed by GitHub
commit b98ea3ec8e
3 changed files with 21 additions and 1 deletions

View File

@ -119,6 +119,9 @@ mqtt.max_clientid_len = 1024
## Max Packet Size Allowed, 64K by default. ## Max Packet Size Allowed, 64K by default.
mqtt.max_packet_size = 64KB mqtt.max_packet_size = 64KB
## Check Websocket Protocol Header. Enum: on, off
mqtt.websocket_protocol_header = on
##-------------------------------------------------------------------- ##--------------------------------------------------------------------
## MQTT Connection ## MQTT Connection
##-------------------------------------------------------------------- ##--------------------------------------------------------------------

View File

@ -346,6 +346,11 @@ end}.
{max_packet_size, cuttlefish:conf_get("mqtt.max_packet_size", Conf)}] {max_packet_size, cuttlefish:conf_get("mqtt.max_packet_size", Conf)}]
end}. end}.
{mapping, "mqtt.websocket_protocol_header", "emqttd.websocket_protocol_header", [
{default, on},
{datatype, flag}
]}.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% MQTT Connection %% MQTT Connection
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------

View File

@ -59,7 +59,7 @@ handle_request('POST', "/mqtt/publish", Req) ->
handle_request('GET', "/mqtt", Req) -> handle_request('GET', "/mqtt", Req) ->
lager:info("WebSocket Connection from: ~s", [Req:get(peer)]), lager:info("WebSocket Connection from: ~s", [Req:get(peer)]),
Upgrade = Req:get_header_value("Upgrade"), Upgrade = Req:get_header_value("Upgrade"),
Proto = Req:get_header_value("Sec-WebSocket-Protocol"), Proto = check_protocol_header(Req),
case {is_websocket(Upgrade), Proto} of case {is_websocket(Upgrade), Proto} of
{true, "mqtt" ++ _Vsn} -> {true, "mqtt" ++ _Vsn} ->
emqttd_ws:handle_request(Req); emqttd_ws:handle_request(Req);
@ -83,6 +83,18 @@ handle_request(Method, Path, Req) ->
lager:error("Unexpected HTTP Request: ~s ~s", [Method, Path]), lager:error("Unexpected HTTP Request: ~s ~s", [Method, Path]),
Req:not_found(). Req:not_found().
check_protocol_header(Req) ->
case emqttd:env(websocket_protocol_header, false) of
true -> get_protocol_header(Req);
false -> "mqtt-v3.1.1"
end.
get_protocol_header(Req) ->
case Req:get_header_value("EMQ-WebSocket-Protocol") of
undefined -> Req:get_header_value("Sec-WebSocket-Protocol");
Proto -> Proto
end.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% HTTP Publish %% HTTP Publish
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------