为什么python脚本不能从hunchentoot-cgi获取post参数?

时间:2012-01-07 03:27:41

标签: lisp common-lisp hunchentoot

hunchentoot-cgi现在基本上可以与hunchentoot-1.2.2一起使用了一些修改:

  1. #'handle-cgi-script中,将:external-format tbnl::+latin-1+替换为:external-format tbnl::+utf-8+
  2. 正如WhiteCat建议的那样,使用#'make-pathname如下:

    (pushnew (hunchentoot-cgi::create-cgi-dispatcher-and-handler
              "/cgi-bin/"
              (make-pathname :directory '(:absolute "media" "E"
                                                    "myapp" "cgi-bin")))
             dispatch-table :test #'equal)
    
  3. 但是,我不明白为什么python脚本无法获取POSTed参数,即访问http://127.0.0:8000/cgi-bin/login.py?cmd=view时,login.py可以成功获取参数cmd的值,但是当发布时以下表单,login.py无法获取所有发布的值(无论是否隐藏):

    <html><body>
      <form method='POST' action='cgi-bin/login.py'>
        <input type='text' name='userid'>
        <input type='password' name='userpwd'>
        <input type='submit' value='Login'>
        <input type='hidden' name='cmd' value='view'>
      </form>
    </body></html>
    

    我猜hunchentoot-cgi没有根据环境将发布的参数传递给python脚本:

    • GET http://127.0.0.1:8000/cgi-bin/nav.py?userid=xyz&cmd=view
      传递给python脚本的环境是:

      (SERVER_SOFTWARE=hunchentoot/1.2.2
       SERVER_NAME=127.0.0.1 GATEWAY_INTERFACE=CGI/1.1
       SERVER_PROTOCOL=HTTP/1.1 SERVER_PORT=8000 REQUEST_METHOD=GET
       CONTENT_TYPE=text/html CONTENT_LENGTH=NIL
       SCRIPT_NAME=/cgi-bin/nav.py QUERY_STRING=userid=xyz&cmd=view
       REMOTE_ADDR=127.0.0.1 HTTP_HOST=NIL
       REQUEST_URI=/cgi-bin/nav.py?userid=xyz&cmd=view SERVER_ADDR=NIL
       HTTP_USER_AGENT=Mozilla/5.0 (X11; Linux i686; rv:9.0.1) Gecko/20100101 Firefox/9.0.1
       HTTP_REFERER=http://127.0.0.1:8000/)
      
    • 发布到/cgi-bin/nav.py

      (SERVER_SOFTWARE=hunchentoot/1.2.2
       SERVER_NAME=127.0.0.1 GATEWAY_INTERFACE=CGI/1.1
       SERVER_PROTOCOL=HTTP/1.1 SERVER_PORT=8000 REQUEST_METHOD=POST
       CONTENT_TYPE=text/html CONTENT_LENGTH=NIL
       POST_PARAMETERS=((userid . xyz) (userpwd . 123) (cmd . view))
       SCRIPT_NAME=/cgi-bin/nav.py QUERY_STRING=NIL REMOTE_ADDR=127.0.0.1     
       HTTP_HOST=NIL REQUEST_URI=/cgi-bin/nav.py SERVER_ADDR=NIL
       HTTP_USER_AGENT=Mozilla/5.0 (X11; Linux i686; rv:9.0.1) Gecko/20100101 Firefox/9.0.1
      

    我认为“CONTENT_LENGTH = NIL”是错误的,不是吗?

    请点亮我,谢谢!

2 个答案:

答案 0 :(得分:0)

在hunchentoot-cgi.lisp乱糟糟的黑客攻击后,现在可以正常工作了。但是,希望Cyrus Harmon(http://cyrusharmon.org/blog)能够发布下一个规范版本。

>diff cgi.lisp /media/E/RnD/clisp/hunchentoot-cgi/hunchentoot-cgi.lisp
71c71
<   (let ((time (or (file-write-date path) (get-universal-time))) (query-string (content-length nil))
---
>   (let ((time (or (file-write-date path) (get-universal-time))))
77,83d76
< (if (member (request-method *request*) *methods-for-post-parameters* :test #'eq)
<     (progn
<         (setq query-string (format nil "~{~A~^&~}" (mapcar (lambda (x) (format nil "~A=~A" (car x) (tbnl:url-encode (cdr x)))) (tbnl:post-parameters*))))
<         (setq content-length (parse-integer (header-in :content-length *request*)))
<     )
<     (setq query-string (tbnl:query-string*)))
< 
87c80,81
<                       . ,(format nil "hunchentoot/~A" hunchentoot-asd:*hunchentoot version*))
---
>                       . (format nil "hunchentoot/~A"
>                                 hunchentoot-asd:*hunchentoot-version*))
97c91,92
<                     #+nil ("REMOTE_HOST" . "FIXME!")
---
>                      ("QUERY_STRING" . ,(tbnl:query-string*))
>                      #+nil ("REMOTE_HOST" . "FIXME!")
101,106c96
<                      ("PATH" . ,(sb-unix::posix-getenv "PATH"))
<                      ("QUERY_STRING" . ,query-string)
<                      ("CONTENT_TYPE" . ,(header-in :content-type *request*))
<                      ("CONTENT_LENGTH" . ,content-length)
<                      ;("HTTP_COOKIE" . ,(tbnl:cookies-out*))
<                      ("HTTP_COOKIE" . "userid=xxx")
---
>                          
112,113c102,103
<                      ("HTTP_REFERER" . ,(tbnl:referer))))))
< (format t "ENV is ~A~%" env)
---
>                      ("HTTP_REFERER" . ,(tbnl:referer))))))      
>       
125c115
<           :external-format tbnl::+utf-8+)))
---
>           :external-format tbnl::+latin-1+)))                   
128d117
< (format t "ERROR: ~A~%" error)                   ("HTTP_REFERER" . (tbnl:referer))))))
< (format t "ENV is ~A~%" env)

此致!

答案 1 :(得分:0)

旧版本的hunchentoot-cgi根本不支持CGI数据。尝试0.3或更高版本并在github上提出问题,如果这仍然不适合你。