[Prev][Next][Index][Thread]

Re: Who can give me an example for "Form upload"



Rainer Joswig <joswig@vampire.lavielle.com> wrote:
>With the Netscape 2.0 browser it is possible to upload files to
>the server. Here is some code:

>; question: what would I have to do to decode the data I'm getting?
>; this method "hangs" after reading from the stream.
>
>(defmethod respond-to-upload ((url url:http-form) stream)
>  (let ((lines (loop for line = (read-line stream nil nil)
>                     while line
>                     count line
>                       do (princ line))))
>    (http:with-successful-response (stream :html)
>      (html:with-html-document (:stream stream)
>        (html:with-document-preamble (:stream stream)
>          (html:declare-title "done" :stream stream))
>        (html:with-document-body (:stream stream)
>          (princ lines stream))))))
>

This code is a first try at decoding the forms data, replacing the
method above. The forms data is sent from the in parts, separated by the a
boundary line, e.g. "-----1234", which is sent from the client as the first
line.
The last part is followed by the boundary line with "--" appended, e.g.
"-----1234--".
My method returns one respons line for each forms element. The respons line
contains
the first line in each forms part, normally the Content-Disposition MIME header,
and the number of lines in the part body. Since I never try to read past the
last boundary line, my method doesn't "hang" as in Rainer's example.

(defmethod respond-to-upload ((url url:http-form) stream)
  (flet ((read-line ()
           (let ((line (read-line stream nil nil)))
             (when (and (plusp (length line)) (eql (char line 0) #\Linefeed))
               (setf line (subseq line 1)))
             (values line)))
         )
    (let* ((form-boundary (read-line))
           (end-boundary (concatenate 'string form-boundary "--"))
           (lines nil)
           (forms-data nil))
      (loop for line = (read-line)
            when (string= form-boundary line) do
            (progn
              (push (nreverse lines) forms-data)
              (setf lines nil))
            when (string= end-boundary line) do
            (progn
              (push lines forms-data)
              (return))
            do (push line lines))

      (http:with-successful-response (stream :html)
        (html:with-html-document (:stream stream)
          (html:with-document-preamble (:stream stream)
            (html:declare-title "done" :stream stream))
          (html:with-document-body (:stream stream)
            (dolist (form-data (reverse forms-data))
              (format stream "<br>~a: ~a" (car form-data) (length form-data))))
          ))
      ))
  )

Hallvard

Hallvard Traetteberg
Sect. of Information Systems, SINTEF Tele og Data
P.O.Box 124 Blindern, N-0314 Oslo, Norway
Tlf: +47 2206 7983 or +47 2206 7300, Fax: +47 2206 7350
Email: Hallvard.Tretteberg@si.sintef.no