;;; websocket.el --- Emacs WebSocket client and server
-;; Copyright (c) 2013 Free Software Foundation, Inc.
+;; Copyright (c) 2013, 2016 Free Software Foundation, Inc.
-;; Author: Andrew Hyatt <ahyatt at gmail dot com>
-;; Maintainer: Andrew Hyatt <ahyatt at gmail dot com>
+;; Author: Andrew Hyatt <ahyatt@gmail.com>
;; Keywords: Communication, Websocket, Server
-;; Version: 1.4
+;; Version: 1.5
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
(require 'bindat)
(require 'url-parse)
+(require 'url-cookie)
(eval-when-compile (require 'cl))
;;; Code:
accept-string
(inflight-input nil))
-(defvar websocket-version "1.4"
+(defvar websocket-version "1.5"
"Version numbers of this version of websocket.el.")
(defvar websocket-debug nil
EXTENSIONS can be NIL if none are in use. An example value would
be '(\"deflate-stream\" . (\"mux\" \"max-channels=4\")).
+Cookies that are set via `url-cookie-store' will be used during
+communication with the server, and cookies received from the
+server will be stored in the same cookie storage that the
+`url-cookie' package uses.
+
Optionally you can specify
ON-OPEN, ON-MESSAGE and ON-CLOSE callbacks as well.
(websocket-debug websocket "Websocket opened")
websocket))
+(defun websocket-process-headers (url headers)
+ "On opening URL, process the HEADERS sent from the server."
+ (when (string-match "Set-Cookie: \(.*\)\r\n" headers)
+ ;; The url-current-object is assumed to be set by
+ ;; url-cookie-handle-set-cookie.
+ (let ((url-current-object (url-generic-parse-url url)))
+ (url-cookie-handle-set-cookie (match-string 1 headers)))))
+
(defun websocket-outer-filter (websocket output)
"Filter the WEBSOCKET server's OUTPUT.
This will parse headers and process frames repeatedly until there
(condition-case err
(progn
(websocket-verify-response-code text)
- (websocket-verify-headers websocket text))
+ (websocket-verify-headers websocket text)
+ (websocket-process-headers (websocket-url websocket) text))
(error
(websocket-close websocket)
(signal (car err) (cdr err))))
(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'."
- (format (concat "Host: %s\r\n"
- "Upgrade: websocket\r\n"
- "Connection: Upgrade\r\n"
- "Sec-WebSocket-Key: %s\r\n"
- "Sec-WebSocket-Version: 13\r\n"
- (when protocol
- (concat
- (mapconcat (lambda (protocol)
- (format "Sec-WebSocket-Protocol: %s" protocol))
- protocol "\r\n")
- "\r\n"))
- (when extensions
- (format "Sec-WebSocket-Extensions: %s\r\n"
- (mapconcat
- (lambda (ext)
- (concat (car ext)
- (when (cdr ext) "; ")
- (when (cdr ext)
- (mapconcat 'identity (cdr ext) "; "))))
- extensions ", ")))
- "\r\n")
- (url-host (url-generic-parse-url url))
- key
- protocol))
+ (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))
+ (url-host parsed-url)))
+ (cookie-header (url-cookie-generate-header-lines
+ host-port (car (url-path-and-query parsed-url))
+ (equal (url-type parsed-url) "wss"))))
+ (format (concat "Host: %s\r\n"
+ "Upgrade: websocket\r\n"
+ "Connection: Upgrade\r\n"
+ "Sec-WebSocket-Key: %s\r\n"
+ "Sec-WebSocket-Version: 13\r\n"
+ (when protocol
+ (concat
+ (mapconcat
+ (lambda (protocol)
+ (format "Sec-WebSocket-Protocol: %s" protocol))
+ protocol "\r\n")
+ "\r\n"))
+ (when extensions
+ (format "Sec-WebSocket-Extensions: %s\r\n"
+ (mapconcat
+ (lambda (ext)
+ (concat
+ (car ext)
+ (when (cdr ext) "; ")
+ (when (cdr ext)
+ (mapconcat 'identity (cdr ext) "; "))))
+ extensions ", ")))
+ (when cookie-header cookie-header)
+ "\r\n")
+ host-port
+ key
+ protocol)))
(defun websocket-get-server-response (websocket client-protocols client-extensions)
"Get the websocket response from client WEBSOCKET."