Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Return the length of the websocket frame from websocket-read-frame.

  • Loading branch information...
commit 7900cdaed05644be1bac7a0cf01ed767eaf8ee3b 1 parent c619af7
Andrew Hyatt authored May 20, 2012
10  websocket-test.el
@@ -84,9 +84,15 @@
84 84
                                  (:val . [0 70000])))))))
85 85
 
86 86
 (ert-deftest websocket-read-frame ()
87  
-  (should (equal (make-websocket-frame :opcode 'text :payload "Hello")
  87
+  (should (equal (make-websocket-frame :opcode 'text :payload "Hello"
  88
+                                       :length (length websocket-test-hello))
88 89
                  (websocket-read-frame websocket-test-hello)))
89  
-  (should (equal (make-websocket-frame :opcode 'text :payload "Hello")
  90
+  (should (equal (make-websocket-frame :opcode 'text :payload "Hello"
  91
+                                       :length (length websocket-test-hello))
  92
+                 (websocket-read-frame (concat websocket-test-hello
  93
+                                               "should-not-be-read"))))
  94
+  (should (equal (make-websocket-frame :opcode 'text :payload "Hello"
  95
+                                       :length (length websocket-test-masked-hello))
90 96
                  (websocket-read-frame websocket-test-masked-hello)))
91 97
   (dotimes (i (- (length websocket-test-hello) 1))
92 98
     (should-not (websocket-read-frame
20  websocket.el
@@ -123,7 +123,7 @@ many bytes were consumed from the string."
123 123
            (cons (websocket-get-bytes (substring s 1) 2) 3))
124 124
           (t (cons initial-val 1)))))
125 125
 
126  
-(defstruct websocket-frame opcode payload)
  126
+(defstruct websocket-frame opcode payload length)
127 127
 
128 128
 (defun websocket-mask (key data)
129 129
   "Mask string DATA with string KEY according to the RFC.
@@ -149,23 +149,21 @@ the frame finishes.  If the frame is not completed, return NIL."
149 149
     (let* ((opcode (websocket-get-opcode s))
150 150
            (payload-len (websocket-get-payload-len (substring s 1)))
151 151
            (maskp (= 128 (logand 128 (websocket-get-bytes (substring s 1) 1))))
  152
+           (payload-start (+ (if maskp 5 1) (cdr payload-len)))
  153
+           (payload-end (+ payload-start (car payload-len)))
152 154
            (unmasked-payload (progn
153  
-                               (websocket-ensure-length s (+ (if maskp 5 1)
154  
-                                                             (car payload-len)
155  
-                                                             (cdr payload-len)))
156  
-                               (substring
157  
-                                s
158  
-                                (+ (if maskp 5 1) (cdr payload-len))
159  
-                                (+ (if maskp 5 1) (car payload-len)
160  
-                                   (cdr payload-len))))))
  155
+                               (websocket-ensure-length s payload-end)
  156
+                               (substring s payload-start payload-end))))
161 157
       (if maskp
162 158
           (let ((masking-key (substring s (+ 1 (cdr payload-len))
163 159
                                         (+ 5 (cdr payload-len)))))
164 160
             (make-websocket-frame :opcode opcode
165 161
                                   :payload
166  
-                                  (websocket-mask masking-key unmasked-payload)))
  162
+                                  (websocket-mask masking-key unmasked-payload)
  163
+                                  :length payload-end))
167 164
         (make-websocket-frame :opcode opcode
168  
-                              :payload unmasked-payload)))))
  165
+                              :payload unmasked-payload
  166
+                              :length payload-end)))))
169 167
 
170 168
 (defun websocket-open (url filter &optional close-callback)
171 169
   "Open a websocket connection to URL.

0 notes on commit 7900cda

Please sign in to comment.
Something went wrong with that request. Please try again.