[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/websocket 350ea63 01/16: 2013-09-15 Andrew Hyatt <addre

From: Stefan Monnier
Subject: [elpa] externals/websocket 350ea63 01/16: 2013-09-15 Andrew Hyatt <address@hidden>
Date: Fri, 27 Nov 2020 17:47:17 -0500 (EST)

branch: externals/websocket
commit 350ea63785ca65555b15344999583c1ae5d8885b
Author: Andrew Hyatt <ahyatt@gmail.com>
Commit: Stefan Monnier <monnier@iro.umontreal.ca>

    2013-09-15 Andrew Hyatt  <ahyatt@gmail.com>
            * websocket.el (websocket-mask-frames, websocket-encode-frame,
              websocket, websocket-send): Get rid of
              `websocket-mask-frames' variable, which was a mistake, and
              make masking mandatory for clients and forbidden for
            * websocket-test.el (websocket-encode-frame,
              websocket-outer-filter): Alter tests to pass new argument
              for `websocket-encode-frame'.
            * websocket.el (websocket-close, websocket-open,
              websocket-server-accept): Only call the on-close callback on
              process state change if the process is now closed or
              similar, and always call the on-close callback from
            * websocket-test.el (websocket-close): Add an on-close callback
              which the `websocket-close' method now expects.
            * magit-functiona-test.el: Assert on ready-state, and increase
              the sleep time for the wss test.
 websocket-functional-test.el |  3 ++-
 websocket-test.el            | 37 ++++++++++++++++++-------------------
 websocket.el                 | 35 ++++++++++++++++++-----------------
 3 files changed, 38 insertions(+), 37 deletions(-)

diff --git a/websocket-functional-test.el b/websocket-functional-test.el
index af1a4ee..6a9481f 100644
--- a/websocket-functional-test.el
+++ b/websocket-functional-test.el
@@ -107,9 +107,10 @@
         wstest-msgs nil)
   (sleep-for 0.3)
   (assert (websocket-openp wstest-ws))
+  (assert (eq 'open (websocket-ready-state wstest-ws)))
   (assert (null wstest-msgs))
   (websocket-send-text wstest-ws "Hi!")
-  (sleep-for 0.3)
+  (sleep-for 1)
   (assert (equal (car wstest-msgs) "Hi!"))
   (websocket-close wstest-ws))
diff --git a/websocket-test.el b/websocket-test.el
index 3cc51f9..5de21d3 100644
--- a/websocket-test.el
+++ b/websocket-test.el
@@ -282,38 +282,36 @@
 (ert-deftest websocket-encode-frame ()
   ;; We've tested websocket-read-frame, now we can use that to help
   ;; test websocket-encode-frame.
-  (let ((websocket-mask-frames nil))
-    (should (equal
+  (should (equal
-              (make-websocket-frame :opcode 'text :payload "Hello" :completep 
-    (dolist (len '(200 70000))
-      (let ((long-string (make-string len ?x)))
-        (should (equal long-string
-                       (websocket-frame-payload
-                        (websocket-read-frame
-                         (websocket-encode-frame
-                          (make-websocket-frame :opcode 'text
-                                                :payload long-string)))))))))
-  (let ((websocket-mask-frames t))
-    (flet ((websocket-genbytes (n) (substring websocket-test-masked-hello 2 
+              (make-websocket-frame :opcode 'text :payload "Hello" :completep 
t) nil)))
+  (dolist (len '(200 70000))
+        (let ((long-string (make-string len ?x)))
+          (should (equal long-string
+                         (websocket-frame-payload
+                          (websocket-read-frame
+                           (websocket-encode-frame
+                            (make-websocket-frame :opcode 'text
+                                                  :payload long-string) 
+  (flet ((websocket-genbytes (n) (substring websocket-test-masked-hello 2 6)))
       (should (equal websocket-test-masked-hello
                       (make-websocket-frame :opcode 'text :payload "Hello"
-                                            :completep t))))))
+                                            :completep t) t))))
      (websocket-encode-frame (make-websocket-frame :opcode 'text
                                                    :payload "Hello"
-                                                   :completep nil)))))
+                                                   :completep nil) t))))
   (dolist (opcode '(close ping pong))
     (should (equal
                (websocket-encode-frame (make-websocket-frame :opcode opcode
-                                                             :completep 
+                                                             :completep t) 
 (ert-deftest websocket-close ()
   (let ((sent-frames)
@@ -326,7 +324,8 @@
       (websocket-close (websocket-inner-create
                         :conn "fake-conn"
                         :url t
-                        :accept-string t))
+                        :accept-string t
+                        :on-close 'identity))
       (should (equal sent-frames (list
                                   (make-websocket-frame :opcode 'close
                                                         :completep t))))
@@ -349,8 +348,8 @@
-           (websocket-encode-frame frame1)
-           (websocket-encode-frame frame2))))
+           (websocket-encode-frame frame1 t)
+           (websocket-encode-frame frame2 t))))
     (flet ((websocket-process-frame
             (websocket frame)
             (lexical-let ((frame frame))
diff --git a/websocket.el b/websocket.el
index 7ea9c93..564e041 100644
--- a/websocket.el
+++ b/websocket.el
@@ -111,11 +111,6 @@ URL of the connection.")
   "The websocket GUID as defined in RFC 6455.
 Do not change unless the RFC changes.")
-(defvar websocket-mask-frames t
-  "If true, we mask frames as defined in the spec.
-This is recommended to be true, and some servers will refuse to
-communicate with unmasked clients.")
 (defvar websocket-callback-debug-on-error nil
   "If true, when an error happens in a client callback, invoke the debugger.
 Having this on can cause issues with missing frames if the debugger is
@@ -295,13 +290,14 @@ Otherwise we throw the error 
   (when (< (length s) n)
     (throw 'websocket-incomplete-frame nil)))
-(defun websocket-encode-frame (frame)
-  "Encode the FRAME struct to the binary representation."
+(defun websocket-encode-frame (frame should-mask)
+  "Encode the FRAME struct to the binary representation.
+We mask the frame or not, depending on SHOULD-MASK."
   (let* ((opcode (websocket-frame-opcode frame))
          (payload (websocket-frame-payload frame))
          (fin (websocket-frame-completep frame))
          (payloadp (memq opcode '(continuation text binary)))
-         (mask-key (when websocket-mask-frames  (websocket-genbytes 4))))
+         (mask-key (when should-mask (websocket-genbytes 4))))
     (apply 'unibyte-string
            (append (list
                     (logior (cond ((eq opcode 'continuation) 0)
@@ -314,7 +310,7 @@ Otherwise we throw the error `websocket-incomplete-frame'."
                    (when payloadp
-                       (if websocket-mask-frames 128 0)
+                       (if should-mask 128 0)
                        (cond ((< (length payload) 126) (length payload))
                              ((< (length payload) 65536) 126)
                              (t 127)))))
@@ -323,11 +319,10 @@ Otherwise we throw the error 
                                           (cond ((< (length payload) 126) 1)
                                                 ((< (length payload) 65536) 2)
                                                 (t 8))) nil))
-                   (when (and payloadp websocket-mask-frames)
+                   (when (and payloadp should-mask)
                      (append mask-key nil))
                    (when payloadp
-                     (append (if websocket-mask-frames
-                                 (websocket-mask mask-key payload)
+                     (append (if should-mask (websocket-mask mask-key payload)
@@ -542,7 +537,8 @@ the `websocket-error' condition."
   (unless (websocket-openp websocket)
     (signal 'websocket-closed frame))
   (process-send-string (websocket-conn websocket)
-                       (websocket-encode-frame frame)))
+                       ;; We mask only when we're a client, following the spec.
+                       (websocket-encode-frame frame (not (websocket-server-p 
 (defun websocket-openp (websocket)
   "Check WEBSOCKET and return non-nil if it is open, and either
@@ -554,6 +550,7 @@ connecting or open."
 (defun websocket-close (websocket)
   "Close WEBSOCKET and erase all the old websocket data."
   (websocket-debug websocket "Closing websocket")
+  (websocket-try-callback 'websocket-on-close 'on-close websocket)
   (when (websocket-openp websocket)
     (websocket-send websocket
                     (make-websocket-frame :opcode 'close
@@ -692,9 +689,10 @@ describing the problem with the frame.
      (lambda (process change)
        (let ((websocket (process-get process :websocket)))
-         (websocket-debug websocket
-                          "State change to %s" change)
-         (unless (eq 'closed (websocket-ready-state websocket))
+         (websocket-debug websocket "State change to %s" change)
+         (when (and
+                (member (process-status process) '(closed failed exit signal))
+                (not (eq 'closed (websocket-ready-state websocket))))
            (websocket-try-callback 'websocket-on-close 'on-close websocket)))))
     (set-process-query-on-exit-flag conn nil)
     (process-send-string conn
@@ -827,6 +825,7 @@ connection, which should be kept in order to pass to
              :server-conn server
              :conn client
              :url client
+             :server-p t
              :on-open (or (process-get server :on-open) 'identity)
              :on-message (or (process-get server :on-message) (lambda (_ws 
              :on-close (lexical-let ((user-method
@@ -849,7 +848,9 @@ connection, which should be kept in order to pass to
      (lambda (process change)
        (let ((websocket (process-get process :websocket)))
          (websocket-debug websocket "State change to %s" change)
-         (unless (eq 'closed (websocket-ready-state websocket))
+         (when (and
+                (member (process-status process) '(closed failed exit signal))
+                (not (eq 'closed (websocket-ready-state websocket))))
            (websocket-try-callback 'websocket-on-close 'on-close 
 (defun websocket-create-headers (url key protocol extensions)

reply via email to

[Prev in Thread] Current Thread [Next in Thread]