Skip to content

Commit

Permalink
Merge branch 'custom-headers'
Browse files Browse the repository at this point in the history
  • Loading branch information
ahyatt committed Nov 14, 2017
2 parents 33d0406 + 058e8f4 commit 4ca406f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
14 changes: 9 additions & 5 deletions websocket-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -207,26 +207,30 @@
(host localpart secure) ""))
(should (equal (concat base-headers "\r\n")
(websocket-create-headers "ws://www.example.com/path"
"key" nil nil)))
"key" nil nil nil)))
(should (equal (concat base-headers
"Sec-WebSocket-Protocol: protocol\r\n\r\n")
(websocket-create-headers "ws://www.example.com/path"
"key" '("protocol") nil)))
"key" '("protocol") nil nil)))
(should (equal
(concat base-headers
"Sec-WebSocket-Extensions: ext1; a; b=2, ext2\r\n\r\n")
(websocket-create-headers "ws://www.example.com/path"
"key" nil
'(("ext1" . ("a" "b=2"))
("ext2"))))))
("ext2")) nil)))
(should (equal
(concat base-headers "Foo: bar\r\nBaz: boo\r\n\r\n")
(websocket-create-headers "ws://www.example.com/path"
"key" nil nil '(("Foo" . "bar") ("Baz" . "boo"))))))
(flet ((url-cookie-generate-header-lines
(host localpart secure)
(should (equal host "www.example.com:123"))
(should (equal localpart "/path"))
(should secure)
"Cookie: foo=bar\r\n"))
(should (equal (websocket-create-headers "wss://www.example.com:123/path"
"key" nil nil)
"key" nil nil nil)
(concat
"Host: www.example.com:123\r\n"
"Upgrade: websocket\r\n"
Expand All @@ -237,7 +241,7 @@
(should
(string-match
"Host: www.example.com:123\r\n"
(websocket-create-headers "ws://www.example.com:123/path" "key" nil nil)))))
(websocket-create-headers "ws://www.example.com:123/path" "key" nil nil nil)))))

(ert-deftest websocket-process-headers ()
(flet ((url-cookie-handle-set-cookie
Expand Down
20 changes: 15 additions & 5 deletions websocket.el
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ connecting or open."
(cl-defun websocket-open (url &key protocols extensions (on-open 'identity)
(on-message (lambda (_w _f))) (on-close 'identity)
(on-error 'websocket-default-error-handler)
(nowait nil))
(nowait nil) (custom-header-alist nil))
"Open a websocket connection to URL, returning the `websocket' struct.
The PROTOCOL argument is optional, and setting it will declare to
the server that this client supports the protocols in the list
Expand Down Expand Up @@ -683,6 +683,11 @@ describing the problem with the frame.
`nowait': If NOWAIT is true, return without waiting for the
connection to complete.
`custom-headers-alist': An alist of custom headers to pass to the
server. The car is the header name, the cdr is the header value.
These are different from the extensions because it is not related
to the websocket protocol.
"
(let* ((name (format "websocket to %s" url))
(url-struct (url-generic-parse-url url))
Expand Down Expand Up @@ -738,7 +743,8 @@ connection to complete.
(websocket-debug websocket "Sending handshake, key: %s, acceptance: %s"
key (websocket-accept-string websocket))
(process-send-string conn
(websocket-create-headers url key protocols extensions))
(websocket-create-headers
url key protocols extensions custom-header-alist))
(websocket-debug websocket "Websocket opened")
websocket))

Expand Down Expand Up @@ -906,9 +912,10 @@ connection, which should be kept in order to pass to
(not (eq 'closed (websocket-ready-state websocket))))
(websocket-try-callback 'websocket-on-close 'on-close websocket)))))))

(defun websocket-create-headers (url key protocol extensions)
"Create connections headers for the given URL, KEY, PROTOCOL and EXTENSIONS.
These are defined as in `websocket-open'."
(defun websocket-create-headers (url key protocol extensions custom-headers-alist)
"Create connections headers for the given URL, KEY, PROTOCOL, and EXTENSIONS.
Additionally, the CUSTOM-HEADERS-ALIST is passed from the client.
All these parameters are defined as in `websocket-open'."
(let* ((parsed-url (url-generic-parse-url url))
(host-port (if (url-port-if-non-default parsed-url)
(format "%s:%s" (url-host parsed-url) (url-port parsed-url))
Expand Down Expand Up @@ -939,6 +946,9 @@ These are defined as in `websocket-open'."
(mapconcat 'identity (cdr ext) "; "))))
extensions ", ")))
(when cookie-header cookie-header)
(concat (mapconcat (lambda (cons) (format "%s: %s" (car cons) (cdr cons)))
custom-headers-alist "\r\n")
(when custom-headers-alist "\r\n"))
"\r\n")
host-port
key
Expand Down

0 comments on commit 4ca406f

Please sign in to comment.