Can hunchentoot host CGI application?
We have a CGI application developed using python, which can be hosted in erlang YAWS easily:
>cat ~/yaws.conf
...
<server 192.168.1.2开发者_如何学运维>
port = 8000
listen = 0.0.0.0
docroot = /media/G/www/qachina/
access_log = false
appmods = <cgi-bin, yaws_appmod_cgi>
</server>
...
Now we want to host the application in a lisp web server. Maybe hunchentoot can do it ?
Sincerely!
Probably something to do with EOL characters.
From Chunga API docs, you can see the read-line* function expects a CR as EOL marker, which is not the default for *nixes. The following should make it work (it works for me):
(setf chunga:*accept-bogus-eols* t)
EDIT: further reading the description of *accept-bogus-eols* I can see why this is the normal behavior of Chunga: the library itself conforms to RFC2616 (HTTP 1.1) in which the HTTP protocol's default EOL marker is CRLF.
I have installed hunchentoot-cgi and tested it. The following is a simple python script file:
>cat cgi-bin/nav.py
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
print "Content-type: text/html\n\n"
print """from Python"""
when i visited http://127.0.0.1:8000/cgi-bin/nav.py, hunchentoot reported:
11111111111111111
**End of file, but expected #\Newline.**
[2011-12-28 13:35:16 [ERROR]] error in handle-cgi-script from URL /cgi-bin/nav.py
127.0.0.1 - [2011-12-28 13:35:16] "GET /cgi-bin/nav.py HTTP/1.1" 200 - "-"
"Opera/9.80 (X11; FreeBSD 8.2-RELEASE i386; U; zh-cn) Presto/2.10.229 Version/11.60"
By hackiing hunchentoot-cgi.lisp, i found the function "handle-cgi-script" reported the error:
(handler-case
(with-input-from-program (in path nil env)
(format t "11111111111111111~%")
(chunga:with-character-stream-semantics
(loop for line = (chunga:read-line* in)
until (equal line "")
do
(format t "22222222222222222:~A~%" line)
(destructuring-bind (key val)
(ppcre:split ": " line :limit 2)
(setf (hunchentoot:header-out key) val))
(format t "22222222222222222~%")))
(format t "33333333333333333~%")
(let ((out (flexi-streams:make-flexi-stream
(tbnl:send-headers)
:external-format tbnl::+latin-1+)))
(copy-stream in out 'character))
(format t "33333333333333333~%"))
(error (error)
(format t "~A~%" error)
(tbnl:log-message* :error "error in handle-cgi-script from URL ~A"
(tbnl:request-uri*))))
Any suggestion is appreciated!
Strange behavior about "merge-pathnames":
* (merge-pathnames "nav.py" "/media/E/myapp/cgi-bin/")
#P"/media/E/myapp/cgi-bin/nav.py"
it works normally in SBCL REPL. However, when hacking "create-cgi-dispatcher-and-handler", i add the following lines:
(defun create-cgi-dispatcher-and-handler (uri-prefix base-path &optional content-type)
;...
(format t "SName=~A SPath=~A BPath=~A~% the path is ~A~%" script-name script-path base-path (merge-pathnames script-path base-path))
;...
Calling it as below:
(pushnew (hunchentoot-cgi::create-cgi-dispatcher-and-handler
"/cgi-bin/"
(make-pathname :name "cgi-bin/" :type nil :version nil :defaults *this-file*)
) *dispatch-table* :test #'equal)
Then visiting http://127.0.0.1:8000/cgi-bin/nav.py, it reports:
SName=/cgi-bin/nav.py SPath=nav.py BPath=/media/E/myapp/cgi-bin/
the path is /media/E/myapp/nav.py
In a short word:
(merge-pathnames "nav.py" "/media/E/myapp/cgi-bin/") returns #P"/media/E/myapp/cgi-bin/nav.py" in REPL. But it returns "/media/E/myapp/nav.py" in hunchentoot-cgi.lisp.
Sincerely!
精彩评论