Emacs25.1之后UrlHttpError

.title { text-align: center; margin-bottom: .2em }
.subtitle { text-align: center; font-size: medium; font-weight: bold; margin-top: 0 }
.todo { font-family: monospace; color: red }
.done { font-family: monospace; color: green }
.priority { font-family: monospace; color: orange }
.tag { background-color: #eee; font-family: monospace; padding: 2px; font-size: 80%; font-weight: normal }
.timestamp { color: #bebebe }
.timestamp-kwd { color: #5f9ea0 }
.org-right { margin-left: auto; margin-right: 0px; text-align: right }
.org-left { margin-left: 0px; margin-right: auto; text-align: left }
.org-center { margin-left: auto; margin-right: auto; text-align: center }
.underline { text-decoration: underline }
#postamble p,#preamble p { font-size: 90%; margin: .2em }
p.verse { margin-left: 3% }
pre { border: 1px solid #ccc; padding: 8pt; font-family: monospace; overflow: auto; margin: 1.2em }
pre.src { position: relative; overflow: visible; padding-top: 1.2em }
pre.src::before { display: none; position: absolute; background-color: white; top: -10px; right: 10px; padding: 3px; border: 1px solid black }
pre.src:hover::before { display: inline }
pre.src-asymptote::before { content: "Asymptote" }
pre.src-awk::before { content: "Awk" }
pre.src-C::before { content: "C" }
pre.src-clojure::before { content: "Clojure" }
pre.src-css::before { content: "CSS" }
pre.src-D::before { content: "D" }
pre.src-ditaa::before { content: "ditaa" }
pre.src-dot::before { content: "Graphviz" }
pre.src-calc::before { content: "Emacs Calc" }
pre.src-emacs-lisp::before { content: "Emacs Lisp" }
pre.src-fortran::before { content: "Fortran" }
pre.src-gnuplot::before { content: "gnuplot" }
pre.src-haskell::before { content: "Haskell" }
pre.src-java::before { content: "Java" }
pre.src-js::before { content: "Javascript" }
pre.src-latex::before { content: "LaTeX" }
pre.src-ledger::before { content: "Ledger" }
pre.src-lisp::before { content: "Lisp" }
pre.src-lilypond::before { content: "Lilypond" }
pre.src-lua::before { content: "Lua" }
pre.src-matlab::before { content: "MATLAB" }
pre.src-mscgen::before { content: "Mscgen" }
pre.src-ocaml::before { content: "Objective Caml" }
pre.src-octave::before { content: "Octave" }
pre.src-org::before { content: "Org mode" }
pre.src-oz::before { content: "OZ" }
pre.src-plantuml::before { content: "Plantuml" }
pre.src-processing::before { content: "Processing.js" }
pre.src-python::before { content: "Python" }
pre.src-R::before { content: "R" }
pre.src-ruby::before { content: "Ruby" }
pre.src-sass::before { content: "Sass" }
pre.src-scheme::before { content: "Scheme" }
pre.src-screen::before { content: "Gnu Screen" }
pre.src-sed::before { content: "Sed" }
pre.src-sh::before { content: "shell" }
pre.src-sql::before { content: "SQL" }
pre.src-sqlite::before { content: "SQLite" }
pre.src-forth::before { content: "Forth" }
pre.src-io::before { content: "IO" }
pre.src-J::before { content: "J" }
pre.src-makefile::before { content: "Makefile" }
pre.src-maxima::before { content: "Maxima" }
pre.src-perl::before { content: "Perl" }
pre.src-picolisp::before { content: "Pico Lisp" }
pre.src-scala::before { content: "Scala" }
pre.src-shell::before { content: "Shell Script" }
pre.src-ebnf2ps::before { content: "ebfn2ps" }
pre.src-cpp::before { content: "C++" }
pre.src-abc::before { content: "ABC" }
pre.src-coq::before { content: "Coq" }
pre.src-groovy::before { content: "Groovy" }
pre.src-bash::before { content: "bash" }
pre.src-csh::before { content: "csh" }
pre.src-ash::before { content: "ash" }
pre.src-dash::before { content: "dash" }
pre.src-ksh::before { content: "ksh" }
pre.src-mksh::before { content: "mksh" }
pre.src-posh::before { content: "posh" }
pre.src-ada::before { content: "Ada" }
pre.src-asm::before { content: "Assembler" }
pre.src-caml::before { content: "Caml" }
pre.src-delphi::before { content: "Delphi" }
pre.src-html::before { content: "HTML" }
pre.src-idl::before { content: "IDL" }
pre.src-mercury::before { content: "Mercury" }
pre.src-metapost::before { content: "MetaPost" }
pre.src-modula-2::before { content: "Modula-2" }
pre.src-pascal::before { content: "Pascal" }
pre.src-ps::before { content: "PostScript" }
pre.src-prolog::before { content: "Prolog" }
pre.src-simula::before { content: "Simula" }
pre.src-tcl::before { content: "tcl" }
pre.src-tex::before { content: "TeX" }
pre.src-plain-tex::before { content: "Plain TeX" }
pre.src-verilog::before { content: "Verilog" }
pre.src-vhdl::before { content: "VHDL" }
pre.src-xml::before { content: "XML" }
pre.src-nxml::before { content: "XML" }
pre.src-conf::before { content: "Configuration File" }
table { border-collapse: collapse }
caption.t-above { caption-side: top }
caption.t-bottom { caption-side: bottom }
td,th { vertical-align: top }
th.org-right { text-align: center }
th.org-left { text-align: center }
th.org-center { text-align: center }
td.org-right { text-align: right }
td.org-left { text-align: left }
td.org-center { text-align: center }
dt { font-weight: bold }
.footpara { display: inline }
.footdef { margin-bottom: 1em }
.figure { padding: 1em }
.figure p { text-align: center }
.inlinetask { padding: 10px; border: 2px solid gray; margin: 10px; background: #ffffcc }
#org-div-home-and-up { text-align: right; font-size: 70%; white-space: nowrap }
textarea { }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00 }
.org-info-js_info-navigation { border-style: none }
#org-info-js_console-label { font-size: 10px; font-weight: bold; white-space: nowrap }
.org-info-js_search-highlight { background-color: #ffff00; color: #000000; font-weight: bold }
.org-svg { width: 90% }
pre.src { background-color: #002b36; color: #839496 }
pre.src { background-color: #002b36; color: #839496 }
pre.src { background-color: #002b36; color: #839496 }
pre.src { background-color: #002b36; color: #839496 }
pre.src { background-color: #002b36; color: #839496 }
pre.src { background-color: #002b36; color: #839496 }
pre.src { background-color: #002b36; color: #839496 }
pre.src { background-color: #002b36; color: #839496 }
pre.src { background-color: #002b36; color: #839496 }
pre.src { background-color: #002b36; color: #839496 }

Emacs25.1之后UrlHttpError

Table of Contents

  • 1. 引言
  • 2. 正文
  • 3. 参考资源

1 引言

当我们更新到Emacs25.1或者之后的版本后,原来可以使用的org2blog,或者其他类似使用metaweblog包的功能都无法使用了,会报一个"error: url-http-create-request: Multibyte text in HTTP request" 的错误信息。

这个问题出现之后,由于我用的是cnblogs插件,这个插件作者很久没有更新了,我首先想到的是这个插件是不是出问题了。然后,我把这个插件查找了,发现不是这个里面的问题。于是在google上搜索了一下,发现其他有好多人也在问这个问题(主要是日本的,说明emacs在日本使用普及度应该比中国要高很多)。

2 正文

在网上并没有找到解决方案,于是,我就决定自己去查看一下Emacs的url-http的源码:

c:/emacs/share/emacs/26.0.50/lisp/url/url-http.el.gz

然后,发现在第395行的位置,有这么一段代码:

;; Bug#23750
(unless (= (string-bytes request)
           (length request))
  (error "Multibyte text in HTTP request: %s" request))

无疑就是这个地方报的错,为什么会报错呢?

去看一下之前版本的代码就知道了,原来是没有这几行代码的,作者也标明了,加入这几行代码是为了解决 Bug#23750,而这个bug我也去查了一下,应该是json.el中的一个bug,也就是说,如果代码不在这个地方检查报错,后面如果要使用到json中的函数的话,同样也会报错,并且据介绍,会更加隐晦,更难查找(去搜索json中的中文问题)。所以,作者在这里提前报错。

那这个错误到底是什么呢?

就是说 (string-bytes request)(length request) 的返回值不一样的时候,就会报错。而中文字符的string-bytes,如果用utf-8来进行编码的话,字符长度是变化的,中文的编码平均大概是每个字会占用3个bytes,而 length 函数返回的会是1。那结果当然是不相等,当然会报错了。

怎么解决呢?一个简单的方法是:

;; Bug#23750
(setq request (url-http--encode-string request))
(unless (= (string-bytes request)
           (length request))
  (error "Multibyte text in HTTP request: %s" request))

其中的 url-http--encode-string 也是作者新增的:

(defun url-http--encode-string (s)
  (if (multibyte-string-p s)
      (encode-coding-string s ‘us-ascii)
    s))

把传进来的中文先转换为‘us-ascii编码,这样,两种函数返回值就是相等的了。 当然,为什么可以这样,把中文转换为ascii,照理压根儿就不可行吧?这里没有想明白。但是可以工作了,是不是作用只是把这个判断跳过去了,后面使用json还是会报错?这个目前还没有去测试。 但是,确实是可以工作了,我目前的这个文章就是使用这个来发布的。

完整的函数,把它添加到配置文件中去就可以了:

(defun url-http-create-request (&optional ref-url)
  "Create an HTTP request for `url-http-target-url‘, referred to by REF-URL."
  (let* ((extra-headers)
         (request nil)
         (no-cache (cdr-safe (assoc "Pragma" url-http-extra-headers)))
         (using-proxy url-http-proxy)
         (proxy-auth (if (or (cdr-safe (assoc "Proxy-Authorization"
                                              url-http-extra-headers))
                             (not using-proxy))
                         nil
                       (let ((url-basic-auth-storage
                              ‘url-http-proxy-basic-auth-storage))
                         (url-get-authentication url-http-proxy nil ‘any nil))))
         (real-fname (url-filename url-http-target-url))
         (host (url-http--encode-string (url-host url-http-target-url)))
         (auth (if (cdr-safe (assoc "Authorization" url-http-extra-headers))
                   nil
                 (url-get-authentication (or
                                          (and (boundp ‘proxy-info)
                                               proxy-info)
                                          url-http-target-url) nil ‘any nil))))
    (if (equal "" real-fname)
        (setq real-fname "/"))
    (setq no-cache (and no-cache (string-match "no-cache" no-cache)))
    (if auth
        (setq auth (concat "Authorization: " auth "\r\n")))
    (if proxy-auth
        (setq proxy-auth (concat "Proxy-Authorization: " proxy-auth "\r\n")))

    ;; Protection against stupid values in the referrer
    (if (and ref-url (stringp ref-url) (or (string= ref-url "file:nil")
                                           (string= ref-url "")))
        (setq ref-url nil))

    ;; We do not want to expose the referrer if the user is paranoid.
    (if (or (memq url-privacy-level ‘(low high paranoid))
            (and (listp url-privacy-level)
                 (memq ‘lastloc url-privacy-level)))
        (setq ref-url nil))

    ;; url-http-extra-headers contains an assoc-list of
    ;; header/value pairs that we need to put into the request.
    (setq extra-headers (mapconcat
                         (lambda (x)
                           (concat (car x) ": " (cdr x)))
                         url-http-extra-headers "\r\n"))
    (if (not (equal extra-headers ""))
        (setq extra-headers (concat extra-headers "\r\n")))

    ;; This was done with a call to `format‘.  Concatenating parts has
    ;; the advantage of keeping the parts of each header together and
    ;; allows us to elide null lines directly, at the cost of making
    ;; the layout less clear.
    (setq request
          (concat
             ;; The request
             (or url-http-method "GET") " "
             (url-http--encode-string
              (if using-proxy (url-recreate-url url-http-target-url) real-fname))
             " HTTP/" url-http-version "\r\n"
             ;; Version of MIME we speak
             "MIME-Version: 1.0\r\n"
             ;; (maybe) Try to keep the connection open
             "Connection: " (if (or using-proxy
                                    (not url-http-attempt-keepalives))
                                "close" "keep-alive") "\r\n"
                                ;; HTTP extensions we support
             (if url-extensions-header
                 (format
                  "Extension: %s\r\n" url-extensions-header))
             ;; Who we want to talk to
             (if (/= (url-port url-http-target-url)
                     (url-scheme-get-property
                      (url-type url-http-target-url) ‘default-port))
                 (format
                  "Host: %s:%d\r\n" (puny-encode-domain host)
                  (url-port url-http-target-url))
               (format "Host: %s\r\n" (puny-encode-domain host)))
             ;; Who its from
             (if url-personal-mail-address
                 (concat
                  "From: " url-personal-mail-address "\r\n"))
             ;; Encodings we understand
             (if (or url-mime-encoding-string
                     ;; MS-Windows loads zlib dynamically, so recheck
                     ;; in case they made it available since
                     ;; initialization in url-vars.el.
                     (and (eq ‘system-type ‘windows-nt)
                          (fboundp ‘zlib-available-p)
                          (zlib-available-p)
                          (setq url-mime-encoding-string "gzip")))
                 (concat
                  "Accept-encoding: " url-mime-encoding-string "\r\n"))
             (if url-mime-charset-string
                 (concat
                  "Accept-charset: "
                  (url-http--encode-string url-mime-charset-string)
                  "\r\n"))
             ;; Languages we understand
             (if url-mime-language-string
                 (concat
                  "Accept-language: " url-mime-language-string "\r\n"))
             ;; Types we understand
             "Accept: " (or url-mime-accept-string "*/*") "\r\n"
             ;; User agent
             (url-http-user-agent-string)
             ;; Proxy Authorization
             proxy-auth
             ;; Authorization
             auth
             ;; Cookies
             (when (url-use-cookies url-http-target-url)
               (url-http--encode-string
                (url-cookie-generate-header-lines
                 host real-fname
                 (equal "https" (url-type url-http-target-url)))))
             ;; If-modified-since
             (if (and (not no-cache)
                      (member url-http-method ‘("GET" nil)))
                 (let ((tm (url-is-cached url-http-target-url)))
                   (if tm
                       (concat "If-modified-since: "
                               (url-get-normalized-date tm) "\r\n"))))
             ;; Whence we came
             (if ref-url (concat
                          "Referer: " ref-url "\r\n"))
             extra-headers
             ;; Length of data
             (if url-http-data
                 (concat
                  "Content-length: " (number-to-string
                                      (length url-http-data))
                  "\r\n"))
             ;; End request
             "\r\n"
             ;; Any data
             url-http-data))
    ;; Bug#23750
    (setq request (url-http--encode-string request))
    (unless (= (string-bytes request)
               (length request))
      (error "Multibyte text in HTTP request: %s" request))
    (url-http-debug "Request is: \n%s" request)
    request))

3 参考资源

《使用org-mode写cnblogs博客》:

http://www.cnblogs.com/yangwen0228/p/6238723.html

配置github地址:

https://github.com/yangwen0228/unimacs

Date: 2017-01-08 10:50

Created: 2017-01-08 周日 11:37

Validate

时间: 2024-10-05 10:04:19

Emacs25.1之后UrlHttpError的相关文章

牛x的JavaScript编辑器你知道几个

英文:Martin Heller  译文:葡萄城控件 学习过程中遇到什么问题或者想获取学习资源的话,欢迎加入学习交流群343599877,我们一起学前端! 对于JavaScript程序员来说,目前有很多很棒的工具可供选择.本文将会讨论10个优秀的支持JavaScript,HTML5和CSS开发,并且可以使用Markdown进行文档编写的文本编辑器.为什么使用编辑器而不是IDE进行JavaScript编程?原因就是速度快. 编辑器和IDE之间的本质区别在于:IDE不但可以调试,并且可以对代码进行概

更新archlinux

有个上网本,虽然配置很差,但是安装的是arch,这不长时间不滚动更新出问题了, 1 :: Proceed with installation? [Y/n] 2 (247/247) checking keys in keyring [##################################################] 100% 3 (247/247) checking package integrity [#####################################

emacs安装

一.下载地址: http://ftp.gnu.org/pub/gnu/emacs/    下载最新的即可,我下载的是emacs-25.2.tar.xz. 二.解压后配置安装 tar xvf emacs-25.2.tar.gz cd emacs-25.2 ./configure -prefix=/usr/local/emacs  -with-x-toolkit=gtk 提示是缺少X windows系统的开发库.如果不想在图形界面下使用Emacs,可以使用--without-x选项进行编译(废话),

Top 10 JavaScript编辑器,你在用哪个?

欢迎大家持续关注葡萄城控件技术团队博客,更多更好的原创文章尽在这里~~ 对于JavaScript程序员来说,目前有很多很棒的工具可供选择.文本将会讨论10个优秀的支持JavaScript,HTML5和CSS开发,并且可以使用Markdown进行文档编写的文本编辑器.为什么使用编辑器而不是IDE进行JavaScript编程?原因就是速度快. 编辑器和IDE之间的本质区别在于:IDE不但可以调试,并且可以对代码进行概要分析,IDE还支持应用程序的生命周期管理(ALM)系统.我们在这里讨论的许多编辑器

windows+vagrant+python+emacs开发环境配置

背景说明 公司的电脑一般都是windows系统的.但是windows系统如果配置开发环境,在安装一些依赖的时候经常会抓狂.Vagrant是一个基于Ruby的工具,用于创建和部署虚拟机.通过vagrant工具在windows系统上拉起一台linux系统的虚拟机,在上面安装emacs,然后通过 mobaxterm 工具的X server 功能,在windows上远程打开虚拟机中的emacs GUI界面. vagrant vagrant下载地址:https://www.vagrantup.com/ 从

emacs手动安装、解决不能使用中文输入法

emacs的安装 emacs的下载,解压 wget http://mirrors.ustc.edu.cn/gnu/emacs/emacs-25.3.tar.gz tar -zxf emacs-25.3.tar.gz -C /opt/modules cd /opt/modules/emacs-25.3/ 编译安装 ./configure --prefix=/usr/local --with-x-toolkit=gtk 这里会报错. You seem to be running X, but no

Win10使用Linux子系统使用adb|win10子系统配置adb环境变量

1.win10安装ubuntu 18.04或Ubuntu发行版 搜索:Microsoft Store ---->搜索:Ubuntu //下载即可 ---->下载完之后安装,启动即可. 搜索:ubuntu启动即可. 注意:源最好不要替换 2.安装所需的库 安装emacs的cscope + autocomplete需要的lib库 # sudo apt-get install byacc flex g++ emacs25 3.启动Linux子系统配置adb 必须保证Win10和Linux系统的adb