URL解码Common Lisp中的字符串

时间:2012-07-02 11:48:47

标签: url common-lisp drakma

我有一个包含转义网址的字符串:

http%3A%2F%2Fexample.com%3Ffoo%3Dbar+baz

我正在尝试将其解码为以下内容:

http://example.com?foo=bar+baz

但是,我找不到Drakma导出的任何合适的功能。我可以像这样 en 代码:

* url-string

"http://example.com?foo=bar+baz"
* (drakma:url-encode url-string :utf-8)

"http%3A%2F%2Fexample.com%3Ffoo%3Dbar%2Bbaz"

...所以我认为我走在正确的轨道上。如果有人能提供正确方向的推动我会很感激:-)我正在使用SBCL 1.0.54,它是在64位Linux Mint 13上从源代码构建的。

如果它有助于澄清我正在尝试做什么,在Ruby中,我会做以下事情:

> uri_string = "http%3A%2F%2Fexample.com%3Ffoo%3Dbar+baz"
 => "http%3A%2F%2Fexample.com%3Ffoo%3Dbar+baz" 
> URI.decode uri_string
 => "http://example.com?foo=bar+baz" 

2 个答案:

答案 0 :(得分:12)

快速SLIME会话。

CL-USER> (ql-dist:system-apropos "url")

#<SYSTEM curly / curly-20120407-git / quicklisp 2012-05-20>
#<SYSTEM curly.test / curly-20120407-git / quicklisp 2012-05-20>
#<SYSTEM do-urlencode / do-urlencode-20120407-git / quicklisp 2012-05-20>
#<SYSTEM url-rewrite / url-rewrite-0.1.1 / quicklisp 2012-05-20>

CL-USER> (ql:quickload :do-urlencode)

C-C C-d P do-urlencode RET

DO-URLENCODE:URLDECODE
  Function: (not documented)
DO-URLENCODE:URLENCODE
  Function: (not documented)
DO-URLENCODE:URLENCODE-MALFORMED-STRING
  Type: (not documented)
DO-URLENCODE:URLENCODE-MALFORMED-STRING-STRING
  Generic Function: (not documented)

CL-USER> (do-urlencode:urldecode "http%3A%2F%2Fexample.com%3Ffoo%3Dbar%2Bbaz")

"http://example.com?foo=bar+baz"

答案 1 :(得分:1)

找到另一个解决方案,Quri,一个带有编码和解码实用程序的uri处理库(用于uri和参数):

(url-decode "http%3A%2F%2Fexample.com%3Ffoo%3Dbar%2Bbaz")
;; "http://example.com?foo=bar+baz"

使用(ql:quickload :quri)安装,然后选择(use-package :quri)

(我的来源:lisp-lang.org's recommend librariesawesone-cl)。