url-misc.el 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. ;;; url-misc.el --- Misc Uniform Resource Locator retrieval code
  2. ;; Copyright (C) 1996-1999, 2002, 2004-2015 Free Software Foundation,
  3. ;; Inc.
  4. ;; Keywords: comm, data, processes
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Code:
  17. (require 'url-vars)
  18. (require 'url-parse)
  19. (autoload 'Info-goto-node "info" "" t)
  20. (autoload 'man "man" nil t)
  21. ;;;###autoload
  22. (defun url-man (url)
  23. "Fetch a Unix manual page URL."
  24. (man (url-filename url))
  25. nil)
  26. ;;;###autoload
  27. (defun url-info (url)
  28. "Fetch a GNU Info URL."
  29. ;; Fetch an info node
  30. (let* ((fname (url-filename url))
  31. (node (url-unhex-string (or (url-target url) "Top"))))
  32. (if (and fname node)
  33. (Info-goto-node (concat "(" fname ")" node))
  34. (error "Malformed url: %s" (url-recreate-url url)))
  35. nil))
  36. (defun url-do-terminal-emulator (type server port user)
  37. (switch-to-buffer
  38. (apply
  39. 'make-term
  40. (format "%s%s" (if user (concat user "@") "") server)
  41. (cond ((eq type 'rlogin) "rlogin")
  42. ((eq type 'telnet) "telnet")
  43. ((eq type 'tn3270) "tn3270")
  44. (t (error "Unknown terminal emulator required: %s" type)))
  45. nil
  46. (cond ((eq type 'rlogin)
  47. (if user (list server "-l" user) (list server)))
  48. ((eq type 'telnet)
  49. (if port (list server port) (list server)))
  50. ((eq type 'tn3270)
  51. (list server))))))
  52. ;;;###autoload
  53. (defun url-generic-emulator-loader (url)
  54. (let* ((type (intern (downcase (url-type url))))
  55. (server (url-host url))
  56. (name (url-user url))
  57. (port (number-to-string (url-port url))))
  58. (url-do-terminal-emulator type server port name))
  59. nil)
  60. ;;;###autoload
  61. (defalias 'url-rlogin 'url-generic-emulator-loader)
  62. ;;;###autoload
  63. (defalias 'url-telnet 'url-generic-emulator-loader)
  64. ;;;###autoload
  65. (defalias 'url-tn3270 'url-generic-emulator-loader)
  66. ;; RFC 2397
  67. ;;;###autoload
  68. (defun url-data (url)
  69. "Fetch a data URL (RFC 2397)."
  70. (let ((mediatype nil)
  71. ;; The mediatype may need to be hex-encoded too -- see the RFC.
  72. (desc (url-unhex-string (url-filename url)))
  73. (encoding "8bit")
  74. (data nil))
  75. (save-excursion
  76. (if (not (string-match "\\([^,]*\\)?," desc))
  77. (error "Malformed data URL: %s" desc)
  78. (setq mediatype (match-string 1 desc)
  79. data (url-unhex-string (substring desc (match-end 0))))
  80. (if (and mediatype (string-match ";base64\\'" mediatype))
  81. (setq mediatype (substring mediatype 0 (match-beginning 0))
  82. encoding "base64"))
  83. (if (or (null mediatype)
  84. (eq ?\; (aref mediatype 0)))
  85. (setq mediatype (concat "text/plain" mediatype))))
  86. (set-buffer (generate-new-buffer " *url-data*"))
  87. (mm-disable-multibyte)
  88. (insert (format "Content-Length: %d\n" (length data))
  89. "Content-Type: " mediatype "\n"
  90. "Content-Transfer-Encoding: " encoding "\n"
  91. "\n")
  92. (if data (insert data))
  93. (current-buffer))))
  94. (provide 'url-misc)
  95. ;;; url-misc.el ends here