Subject: Re: bug#16198: 24.3.50; [PATCH 1/2] eww: Does not support file upload.

From: Kenjiro NAKAYAMA <[email-masked]>
Date: Thu, 26 Dec 2013 00:09:20 +0900

Thank you Lars,

> Lines shouldn't be longer than 80 characters, and these file names may

But If I include linefeed and indent in following line, the data has
some "\t"s due to the indent. So I think one line is better.

   (format "Content-Disposition: form-data; name=%S; filename=%S\r\nContent-Type: text/plain; ....)

-> With New Line and indent.(Many \t\t\t... are included in the data.)
  Content-Disposition: form-data; name="test"; filename="~/example.txt"\r\n\t\t\t\t\t\t\tContent-Type: text/plain; charset=utf-8\r\n\t\t\t\t\t\t\tContent-Transfer-Encoding: binary\r\n\r\n

-> Non-breaking (The linefeed looks good)
  Content-Disposition: form-data; name="test"; filename="~/example.txt"\r\n
  Content-Type: text/plain; charset=utf-8\r\n
  Content-Transfer-Encoding: binary\r\n\r\n

If I misunderstood your explanation, I am sorry.

Since I made the patch again, I resend.
The patch for the eww.el ([PATCH 2/2]) has no change.

Signed-off-by: Kenjiro NAKAYAMA <[email-masked]>

        * gnus/mm-url.el (mm-url-encode-multipart-form-data):
          Restore to handle "maltipart/form-data" by eww.
        * net/eww.el(eww-form-file(defface)): New defface of file upload form.
        (eww-submit-file): New key map of file upload.
        (eww-form-file): New file upload button and file name context.
        (eww-select-file): Select file and display selected file name.
        (eww-tag-input): Handle input tag of file type.
        (eww-update-field): Add point offset.
        (eww-submit): Add submit with multipart/form-data.
        
---
 lisp/gnus/mm-url.el | 42 ++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 40 insertions(+), 2 deletions(-)

diff --git a/lisp/gnus/mm-url.el b/lisp/gnus/mm-url.el
index 6e83b18..4b5fedb 100644
--- a/lisp/gnus/mm-url.el
+++ b/lisp/gnus/mm-url.el
@@ -416,13 +416,51 @@ spaces.  Die Die Die."
 
 (autoload 'mml-compute-boundary "mml")
 
+(defun mm-url-encode-multipart-form-data (pairs &optional boundary)
+  "Return PAIRS encoded in multipart/form-data."
+  ;; RFC1867
+  ;; Get a good boundary
+  (unless boundary
+    (setq boundary (mml-compute-boundary '())))
+  (concat
+   ;; Start with the boundary
+   "--" boundary "\r\n"
+   ;; Create name value pairs
+   (mapconcat
+    'identity
+    ;; Delete any returned items that are empty
+    (delq nil
+          (mapcar (lambda (data)
+                    (cond ((equal (car data) "file")
+                           ;; For each pair
+                           (format
+                            ;; Encode the name
+                            "Content-Disposition: form-data; name=%S; filename=%S\r\nContent-Type: text/plain; charset=utf-8\r\nContent-Transfer-Encoding: binary\r\n\r\n%s"
+                            (cdr (assoc "name" (cdr data))) (cdr (assoc "filename" (cdr data)))
+                            (cond ((stringp (cdr (assoc "filedata" (cdr data))))
+                                   (cdr (assoc "filedata" (cdr data))))
+                                  ((integerp (cdr (assoc "filedata" (cdr data))))
+                                   (number-to-string (cdr (assoc "filedata" (cdr data))))))))
+                          ((equal (car data) "submit")
+                           "Content-Disposition: form-data; name=\"submit\"\r\n\r\nSubmit\r\n")
+                          (t
+                           (format
+                            "Content-Disposition: form-data;name=%S\r\n\r\n%s\r\n"
+                            (car data) (concat (mm-url-form-encode-xwfu (cdr data)))
+                            ))))
+                  pairs))
+    ;; use the boundary as a separator
+    (concat "\r\n--" boundary "\r\n"))
+   ;; put a boundary at the end.
+   "--" boundary "--\r\n"))
+
 (defun mm-url-remove-markup ()
   "Remove all HTML markup, leaving just plain text."
   (goto-char (point-min))
   (while (search-forward "<!--" nil t)
     (delete-region (match-beginning 0)
-		   (or (search-forward "-->" nil t)
-		       (point-max))))
+                   (or (search-forward "-->" nil t)
+                       (point-max))))
   (goto-char (point-min))
   (while (re-search-forward "<[^>]+>" nil t)
     (replace-match "" t t)))
-- 
1.8.3.1

Regards,

Kenjiro

[email-masked] writes:

> Kenjiro NAKAYAMA <[email-masked]> writes:
>
> Looks good.  One bit that could perhaps be changed is this:
>
>> +                            "Content-Disposition: form-data; name=\"" (cdr (assoc "name" (cdr data))) "\"; filename=\"" (cdr (assoc "filename" (cdr data))) "\"\r\n"
>
> Lines shouldn't be longer than 80 characters, and these file names may
> perhaps contain the " character, which would make these specs invalid?
>
> It's usually best to use `format' with %S in these cases:
>
> (setq file "foo\"bar")
>
> (insert (concat "name=\"" file "\""))
> name="foo"bar"
>
> (insert (format "name=%S" file))
> name="foo\"bar"