phoenixframework / websock

A specification for Elixir apps to service WebSocket connections
MIT License
75 stars 5 forks source link

Issue setting a specific header response when using WebSockAdapter.upgrade #13

Closed gevera closed 1 year ago

gevera commented 1 year ago

Hey, Mat.

Quick question. I want to implement the ocpp protocol version 1.6j in Phoenix using WebSockets.

Now in specification, it is stated that:

Upon receiving the Charge Point’s request, the Central System has to finish the handshake with a response as described in [RFC6455]. The following OCPP-J-specific conditions apply: • If the Central System does not recognize the charge point identifier in the URL path, it SHOULD send an HTTP response with status 404 and abort the WebSocket connection as described in [RFC6455]. • If the Central System does not agree to using one of the subprotocols offered by the client, it MUST complete the WebSocket handshake with a response without a Sec-WebSocket-Protocol header and then immediately close the WebSocket connection. So if the Central System accepts the above example request and agrees to using OCPP 1.6J with the Charge Point, the Central System’s response will look as follows:

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
Sec-WebSocket-Protocol: ocpp1.6

The bold parts are found as such in every WebSocket handshake response, the other parts are specific to this example. The role of the Sec-WebSocket-Accept header is explained in [RFC6455]. The Sec-WebSocket-Protocol header indicates that the server will be using OCPP1.6J on this connection.


So basically I need to set in the reply a specific header, mainly "sec-websocket-protocol": "ocpp1.6" Since Websock

Here is my implementation:

defmodule MyApp.WebSocketController do
  alias MyApp.Websocket.Ocpp.ProtocolHandler
  use MyApp, :controller

  @timeout 60_000

  def websocket(conn, params) do

    conn
    |> put_resp_header("sec-websocket-protocol", "ocpp1.6")
    |> WebSockAdapter.upgrade(
      ProtocolHandler,
      params,
      timeout: @timeout
    )
  end
end

Upon reviewing the response, this is what I get

 query_params: %{},                                                                                                                                                                           
  query_string: "",                                                                                                                                                                            
  remote_ip: {127, 0, 0, 1},                                                                                                                                                                   
  req_cookies: %{},                                                                                                                                                                            
  req_headers: [                                                                                                                                                                               
    {"authorization", "Basic VEVTVENQMTpURVNUQ1Ax"},                                                                                                                                           
    {"connection", "Upgrade"},                                                                                                                                                                 
    {"host", "localhost:3333"},
    {"sec-websocket-extensions", "permessage-deflate; client_max_window_bits"},                
    {"sec-websocket-key", "3F4psnvie6paAUxH5qzcUw=="},                                         
    {"sec-websocket-protocol", "ocpp1.6"},                                                     
    {"sec-websocket-version", "13"},
    {"upgrade", "websocket"}
  ],         
  request_path: "/ws/ocpp/1.6j/TESTCP1",
  resp_body: nil,                                                                              
  resp_cookies: %{},                                                                           
  resp_headers: [                                                                              
    {"cache-control", "max-age=0, private, must-revalidate"},
    {"x-request-id", "F31uOJucbjNfEDcAAAKF"},                                                  
    {"referrer-policy", "strict-origin-when-cross-origin"},                                    
    {"x-content-type-options", "nosniff"},                                                     
    {"x-download-options", "noopen"},        
    {"x-frame-options", "SAMEORIGIN"},
    {"x-permitted-cross-domain-policies", "none"}              
  ],                                                                                           
  scheme: :http,                                                                               
  script_name: [],                       
  secret_key_base: :...,
  state: :unset,                         
  status: nil     

I wonder what am I missing. I believe there should be a better way of injecting the headers Thanks

gevera commented 1 year ago

Never mind. Just implemented the response in a plug thru a pipeline. Thanks