branch: externals/websocket
commit 4ca406fb962cc36ac5712a19566b52c6a56d5238
Merge: 33d0406901 058e8f4696
Author: Andrew Hyatt <[email protected]>
Commit: Andrew Hyatt <[email protected]>
Merge branch 'custom-headers'
---
websocket-test.el | 14 +++++++++-----
websocket.el | 20 +++++++++++++++-----
2 files changed, 24 insertions(+), 10 deletions(-)
diff --git a/websocket-test.el b/websocket-test.el
index 604eb87919..cfb2db00fd 100644
--- a/websocket-test.el
+++ b/websocket-test.el
@@ -207,18 +207,22 @@
(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"))
@@ -226,7 +230,7 @@
(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"
@@ -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
diff --git a/websocket.el b/websocket.el
index 854e69c9fa..355e70b962 100644
--- a/websocket.el
+++ b/websocket.el
@@ -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
@@ -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))
@@ -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))
@@ -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))
@@ -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