Skip to content

Commit

Permalink
Add headers parameter for HTTP::WebSocket constructors (#4227)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamtrilling authored and Martin Verzilli committed Jun 26, 2017
1 parent c6e2077 commit 11b1e3c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
1 change: 1 addition & 0 deletions spec/std/http/web_socket_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -374,4 +374,5 @@ describe HTTP::WebSocket do
typeof(HTTP::WebSocket.new(URI.parse("ws://localhost")))
typeof(HTTP::WebSocket.new("localhost", "/"))
typeof(HTTP::WebSocket.new("ws://localhost"))
typeof(HTTP::WebSocket.new(URI.parse("ws://localhost"), headers: HTTP::Headers{"X-TEST_HEADER" => "some-text"}))
end
10 changes: 6 additions & 4 deletions src/http/web_socket.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ class HTTP::WebSocket
# HTTP::WebSocket.new(URI.parse("ws://websocket.example.com/chat")) # Creates a new WebSocket to `websocket.example.com`
# HTTP::WebSocket.new(URI.parse("wss://websocket.example.com/chat")) # Creates a new WebSocket with TLS to `websocket.example.com`
# HTTP::WebSocket.new(URI.parse("http://websocket.example.com:8080/chat")) # Creates a new WebSocket to `websocket.example.com` on port `8080`
# HTTP::WebSocket.new(URI.parse("ws://websocket.example.com/chat"), # Creates a new WebSocket to `websocket.example.com` with an Authorization header
# HTTP::Headers{"Authorization" => "Bearer authtoken"})
# ```
def self.new(uri : URI | String)
new(Protocol.new(uri))
def self.new(uri : URI | String, headers = HTTP::Headers.new)
new(Protocol.new(uri, headers: headers))
end

# Opens a new websocket to the target host. This will also handle the handshake
Expand All @@ -35,8 +37,8 @@ class HTTP::WebSocket
# HTTP::WebSocket.new("websocket.example.com", "/chat") # Creates a new WebSocket to `websocket.example.com`
# HTTP::WebSocket.new("websocket.example.com", "/chat", tls: true) # Creates a new WebSocket with TLS to `ẁebsocket.example.com`
# ```
def self.new(host : String, path : String, port = nil, tls = false)
new(Protocol.new(host, path, port, tls))
def self.new(host : String, path : String, port = nil, tls = false, headers = HTTP::Headers.new)
new(Protocol.new(host, path, port, tls, headers))
end

def on_ping(&@on_ping : String ->)
Expand Down
7 changes: 3 additions & 4 deletions src/http/web_socket/protocol.cr
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ class HTTP::WebSocket::Protocol
end
end

def self.new(host : String, path : String, port = nil, tls = false)
def self.new(host : String, path : String, port = nil, tls = false, headers = HTTP::Headers.new)
{% if flag?(:without_openssl) %}
if tls
raise "WebSocket TLS is disabled because `-D without_openssl` was passed at compile time"
Expand All @@ -259,7 +259,6 @@ class HTTP::WebSocket::Protocol
end
{% end %}

headers = HTTP::Headers.new
headers["Host"] = "#{host}:#{port}"
headers["Connection"] = "Upgrade"
headers["Upgrade"] = "websocket"
Expand All @@ -278,12 +277,12 @@ class HTTP::WebSocket::Protocol
new(socket, masked: true)
end

def self.new(uri : URI | String)
def self.new(uri : URI | String, headers = HTTP::Headers.new)
uri = URI.parse(uri) if uri.is_a?(String)

if (host = uri.host) && (path = uri.path)
tls = uri.scheme == "https" || uri.scheme == "wss"
return new(host, path, uri.port, tls)
return new(host, path, uri.port, tls, headers)
end

raise ArgumentError.new("No host or path specified which are required.")
Expand Down

0 comments on commit 11b1e3c

Please sign in to comment.