Permalink
Browse files

Add a function to calculate the accept header expected.

  • Loading branch information...
1 parent 763cb60 commit b5bbce12dc2dfdc3bcfee13e702b9d99f113b84e @ahyatt committed May 7, 2012
Showing with 17 additions and 0 deletions.
  1. +7 −0 websocket-test.el
  2. +10 −0 websocket.el
View
@@ -76,7 +76,14 @@
(websocket-test-get-filtered-response
'("HTTP 1.1" "\0foo\377")))))
+(ert-deftest websocket-calculate-accept ()
+ ;; This example comes straight from RFC 6455
+ (should
+ (equal "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="
+ (websocket-calculate-accept "dGhlIHNhbXBsZSBub25jZQ=="))))
+
(ert-run-tests-interactively 'websocket-genbytes-length)
(ert-run-tests-interactively 'websocket-filter-basic)
(ert-run-tests-interactively 'websocket-filter-inflight-packets)
(ert-run-tests-interactively 'websocket-filter-first-response)
+(ert-run-tests-interactively 'websocket-calculate-accept)
View
@@ -41,6 +41,10 @@
The buffer is ` *websocket URL debug*' where URL is the
URL of the connection.")
+(defconst websocket-guid "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
+ "The websocket GUID as defined in RFC 6455. Do not change
+ unless the RFC changes.")
+
(defun websocket-genbytes ()
"Generate bytes used at the end of the handshake."
(let ((s " "))
@@ -71,6 +75,12 @@ URL of the connection.")
max-num-str)))
(cons max-num-str num)))
+(defun websocket-calculate-accept (key)
+ "Calculate the expect value of the accept header.
+This is based on the KEY from the Sec-WebSocket-Key header."
+ (base64-encode-string
+ (sha1 (concat key websocket-guid) nil nil t)))
+
(defun websocket-open (url filter &optional close-callback)
"Open a websocket connection to URL.
Websocket packets are sent as the only argument to FILTER, and if

0 comments on commit b5bbce1

Please sign in to comment.