adapt seval-handler to new response philosophy

FIXME: problems with input port in POST request
This commit is contained in:
interp 2002-08-28 15:41:52 +00:00
parent 7fd23a0ceb
commit a2aed3280e
2 changed files with 73 additions and 34 deletions

View File

@ -38,6 +38,15 @@
(syntax-rules () (syntax-rules ()
((do/timeout secs body ...) (do/timeout* secs (lambda () body ...))))) ((do/timeout secs body ...) (do/timeout* secs (lambda () body ...)))))
(define-condition-type 'seval-error '())
(define seval-error? (condition-predicate 'seval-error))
(define-condition-type 'seval-no-content-length-error '(seval-error))
(define seval-no-content-length-error?
(condition-predicate 'seval-no-content-length-error))
(define-condition-type 'seval-no-program '(seval-error))
(define seval-no-program? (condition-predicate 'seval-no-program))
;;; The path handler for seval ops. ;;; The path handler for seval ops.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@ -46,37 +55,65 @@
(let ((request-method (request:method req))) (let ((request-method (request:method req)))
(cond (cond
((string=? request-method "POST") ; Could do others also. ((string=? request-method "POST") ; Could do others also.
(call-with-current-continuation
(lambda (exit)
(begin
(with-handler
(lambda (condition more)
(exit
(cond
((seval-no-content-length-error? condition)
(make-http-error-response
http-status/bad-request req
"No `Content-length:' field in POST request."))
((seval-no-program? condition)
(make-http-error-response http-status/bad-request req
"No program in entity body."))
(else
(make-http-error-response
http-status/internal-error req
"Unknown error while evaluating seval-expression."
condition)))))
(lambda ()
(seval path req)))))))
(else
(make-http-error-response http-status/method-not-allowed req)))))
(let ((modern-protocol? (not (v0.9-request? req)))) (define (seval path req)
(if modern-protocol? (let ((sexp (read-request-sexp req)))
(begin (http-syslog (syslog-level debug) "read sexp: ~a" sexp)
(begin-http-header #t 200) (make-response
(write-string "Content-type: text/html\r\n\r\n"))) http-status/ok
(with-tag #t HEAD () (status-code->text http-status/ok)
(newline) (time)
(emit-title #t "Scheme program output")) "text/html"
(newline)) '()
(make-writer-body
(with-tag #t BODY () (lambda (port options)
(newline) (with-tag port HEAD ()
(let ((sexp (read-request-sexp req))) (newline port)
(do/timeout (emit-title port "Scheme program output"))
10 (newline port)
(receive vals
;; Do the computation. (with-tag port BODY ()
(begin (emit-header #t 2 "Output from execution") (newline port)
(newline) (do/timeout
(with-tag #t PRE () 10
(newline) (receive vals
(force-output) ; In case we're gunned down. ;; Do the computation.
(eval-safely sexp))) (begin (emit-header port 2 "Output from execution")
(newline port)
;; Pretty-print the returned value(s). (with-tag port PRE ()
(emit-header #t 2 "Return value(s)") (newline port)
(with-tag #t PRE () (force-output port); In case we're gunned down.
(for-each p vals))))))) (eval-safely sexp)))
(else (http-error http-status/method-not-allowed #f req))))) ;; Pretty-print the returned value(s).
(emit-header port 2 "Return value(s)")
(with-tag port PRE ()
(for-each (lambda (val) (p val port))
vals))))))))))
;;; Read an HTTP request entity body from stdin. The Content-length: ;;; Read an HTTP request entity body from stdin. The Content-length:
@ -97,11 +134,11 @@
(string-length cl-str))) (string-length cl-str)))
0)) ; All whitespace?? -- WTF. 0)) ; All whitespace?? -- WTF.
(qs (read-string cl)) ; Read in CL chars, (qs (read-string cl)) ; Read in CL chars,
;; FIXME where is the input port?
(q (parse-html-form-query qs)) ; and parse them up. (q (parse-html-form-query qs)) ; and parse them up.
(s (cond ((assoc "program" q) => cdr) (s (cond ((assoc "program" q) => cdr)
(else (error "No program in entity body."))))) (else (signal 'seval-no-program)))))
(http-syslog (syslog-level debug) (http-syslog (syslog-level debug)
"Seval sexp:~%~s~%" s) "Seval sexp: ~s" s)
(read (make-string-input-port s))))) (read (make-string-input-port s)))))
(else (http-error http-status/bad-request req (else (signal 'seval-no-content-length-error))))
"No Content-length: field in POST request."))))

View File

@ -448,6 +448,7 @@
(open receiving ; MV return (RECEIVE and VALUES) (open receiving ; MV return (RECEIVE and VALUES)
scsh-utilities ; index scsh-utilities ; index
srfi-13 srfi-13
srfi-1 ; fold
let-opt ; let-optionals let-opt ; let-optionals
crlf-io ; read-crlf-line crlf-io ; read-crlf-line
ascii ; ascii->char ascii ; ascii->char
@ -804,6 +805,7 @@
srfi-13 ; STRING-SKIP srfi-13 ; STRING-SKIP
rfc822 rfc822
toothless-eval ; EVAL-SAFELY toothless-eval ; EVAL-SAFELY
conditions signals
handle ; IGNORE-ERROR handle ; IGNORE-ERROR
parse-html-forms ; PARSE-HTML-FORM-QUERY parse-html-forms ; PARSE-HTML-FORM-QUERY
threads ; SLEEP threads ; SLEEP