Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
5.8 KiB

  1. ;;; ein-websocket.el --- Wrapper of websocket.el -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2012- Takafumi Arakaki
  3. ;; Author: Takafumi Arakaki <aka.tkf at gmail.com>
  4. ;; This file is NOT part of GNU Emacs.
  5. ;; ein-websocket.el is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; ein-websocket.el is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with ein-websocket.el. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;;; Code:
  18. (require 'websocket)
  19. (require 'ein-core)
  20. (require 'ein-classes)
  21. (require 'url-cookie)
  22. (require 'request)
  23. ;; Fix issues reading cookies in request when using curl backend
  24. (defun fix-request-netscape-cookie-parse (_next-method)
  25. "Parse Netscape/Mozilla cookie format."
  26. (goto-char (point-min))
  27. (let ((tsv-re (concat "^\\="
  28. (cl-loop repeat 6 concat "\\([^\t\n]+\\)\t")
  29. "\\(.*\\)"))
  30. cookies)
  31. (forward-line 3) ;; Skip header (first three lines)
  32. (while
  33. (and
  34. (cond
  35. ((re-search-forward "^\\=$" nil t))
  36. ((re-search-forward tsv-re)
  37. (push (cl-loop for i from 1 to 7 collect (match-string i))
  38. cookies)
  39. t))
  40. (= (forward-line 1) 0)
  41. (not (= (point) (point-max)))))
  42. (setq cookies (nreverse cookies))
  43. (cl-loop for (domain flag path secure expiration name value) in cookies
  44. collect (list domain
  45. (equal flag "TRUE")
  46. path
  47. (equal secure "TRUE")
  48. (string-to-number expiration)
  49. name
  50. value))))
  51. (defsubst ein:websocket-store-cookie (c host-port url-filename securep)
  52. (url-cookie-store (car c) (cdr c) nil host-port url-filename securep))
  53. (defun ein:maybe-get-jhconn-user (url)
  54. (let ((paths (cl-rest (split-string (url-filename (url-generic-parse-url url)) "/"))))
  55. (if (string= (car paths) "user")
  56. (format "/%s/%s/" (car paths) (cadr paths))
  57. "")))
  58. ;;(advice-add 'request--netscape-cookie-parse :around #'fix-request-netscape-cookie-parse)
  59. (defun ein:websocket--prepare-cookies (url)
  60. "Websocket gets its cookies using the url-cookie API, so we need to copy over
  61. any cookies that are made and stored during the contents API calls via
  62. emacs-request."
  63. (let* ((parsed-url (url-generic-parse-url url))
  64. (host-port (if (url-port-if-non-default parsed-url)
  65. (format "%s:%s" (url-host parsed-url) (url-port parsed-url))
  66. (url-host parsed-url)))
  67. (securep (string-match "^wss://" url))
  68. (read-cookies-func (lambda (path)
  69. (request-cookie-alist
  70. (url-host parsed-url) path securep)))
  71. (cookies (cl-loop repeat 4
  72. for cand = (mapcan read-cookies-func
  73. `("/" "/hub/"
  74. ,(ein:maybe-get-jhconn-user url)))
  75. until (cl-some (lambda (x) (string= "_xsrf" (car x))) cand)
  76. do (ein:log 'info
  77. "ein:websocket--prepare-cookies: no _xsrf among %s, retrying."
  78. cand)
  79. do (sleep-for 0 300)
  80. finally return cand)))
  81. (dolist (c cookies)
  82. (ein:websocket-store-cookie c host-port
  83. (car (url-path-and-query parsed-url)) securep))))
  84. (defun ein:websocket (url kernel on-message on-close on-open)
  85. (ein:websocket--prepare-cookies url)
  86. (let* ((ws (websocket-open url
  87. :on-open on-open
  88. :on-message on-message
  89. :on-close on-close
  90. :on-error (lambda (ws action err)
  91. (ein:log 'info "WS action [%s] %s (%s)"
  92. err action (websocket-url ws)))))
  93. (websocket (make-ein:$websocket :ws ws :kernel kernel :closed-by-client nil)))
  94. (setf (websocket-client-data ws) websocket)
  95. websocket))
  96. (defun ein:websocket-open-p (websocket)
  97. (eql (websocket-ready-state (ein:$websocket-ws websocket)) 'open))
  98. (defun ein:websocket-send (websocket text)
  99. ;; (ein:log 'info "WS: Sent message %s" text)
  100. (condition-case-unless-debug err
  101. (websocket-send-text (ein:$websocket-ws websocket) text)
  102. (error (message "Error %s on sending websocket message %s." err text))))
  103. (defun ein:websocket-close (websocket)
  104. (setf (ein:$websocket-closed-by-client websocket) t)
  105. (websocket-close (ein:$websocket-ws websocket)))
  106. (defun ein:websocket-send-shell-channel (kernel msg)
  107. (cond ((= (ein:$kernel-api-version kernel) 2)
  108. (ein:websocket-send
  109. (ein:$kernel-shell-channel kernel)
  110. (json-encode msg)))
  111. ((>= (ein:$kernel-api-version kernel) 3)
  112. (ein:websocket-send
  113. (ein:$kernel-websocket kernel)
  114. (json-encode (plist-put msg :channel "shell"))))))
  115. (defun ein:websocket-send-stdin-channel (kernel msg)
  116. (cond ((= (ein:$kernel-api-version kernel) 2)
  117. (ein:log 'warn "Stdin messages only supported with IPython 3."))
  118. ((>= (ein:$kernel-api-version kernel) 3)
  119. (ein:websocket-send
  120. (ein:$kernel-websocket kernel)
  121. (json-encode (plist-put msg :channel "stdin"))))))
  122. (provide 'ein-websocket)
  123. ;;; ein-websocket.el ends here