;; Author: Andrew Hyatt <ahyatt@gmail.com>
;; Keywords: Communication, Websocket, Server
-;; Version: 1.5
+;; Version: 1.6
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
The only acceptable one to websocket is responce code 101.
A t value will be returned on success, and an error thrown
if not."
- (string-match "HTTP/1.1 \\([[:digit:]]+\\)" output)
+ (unless (string-match "^HTTP/1.1 \\([[:digit:]]+\\)" output)
+ (signal 'websocket-invalid-header "Invalid HTTP status line"))
(unless (equal "101" (match-string 1 output))
- (signal 'websocket-received-error-http-response
- (string-to-number (match-string 1 output))))
+ (signal 'websocket-received-error-http-response
+ (string-to-number (match-string 1 output))))
t)
(defun websocket-parse-repeated-field (output field)
(setf (websocket-inflight-input websocket) nil)
;; If we've received the complete header, check to see if we've
;; received the desired handshake.
- (when (and (eq 'connecting (websocket-ready-state websocket))
- (setq header-end-pos (string-match "\r\n\r\n" text))
+ (when (and (eq 'connecting (websocket-ready-state websocket)))
+ (if (and (setq header-end-pos (string-match "\r\n\r\n" text))
(setq start-point (+ 4 header-end-pos)))
- (condition-case err
(progn
- (websocket-verify-response-code text)
- (websocket-verify-headers websocket text)
- (websocket-process-headers (websocket-url websocket) text))
- (error
- (websocket-close websocket)
- (signal (car err) (cdr err))))
- (setf (websocket-ready-state websocket) 'open)
- (websocket-try-callback 'websocket-on-open 'on-open websocket))
+ (condition-case err
+ (progn
+ (websocket-verify-response-code text)
+ (websocket-verify-headers websocket text)
+ (websocket-process-headers (websocket-url websocket) text))
+ (error
+ (websocket-close websocket)
+ (signal (car err) (cdr err))))
+ (setf (websocket-ready-state websocket) 'open)
+ (websocket-try-callback 'websocket-on-open 'on-open websocket))
+ (setf (websocket-inflight-input websocket) text)))
(when (eq 'open (websocket-ready-state websocket))
(websocket-process-input-on-open-ws
websocket (substring text (or start-point 0))))))
(defun* websocket-server (port &rest plist)
"Open a websocket server on PORT.
+If the plist contains a `:host' HOST pair, this value will be
+used to configure the addresses the socket listens on. The symbol
+`local' specifies the local host. If unspecified or nil, the
+socket will listen on all addresses.
+
This also takes a plist of callbacks: `:on-open', `:on-message',
`:on-close' and `:on-error', which operate exactly as documented
in the websocket client function `websocket-open'. Returns the
:log 'websocket-server-accept
:filter-multibyte nil
:plist plist
+ :host (plist-get plist :host)
:service port)))
conn))