emacs-elpa-diffs
[Top][All Lists]
Advanced

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

[elpa] 24/119: parse application/x-www-form-urlencoded post data


From: Eric Schulte
Subject: [elpa] 24/119: parse application/x-www-form-urlencoded post data
Date: Mon, 10 Mar 2014 16:57:07 +0000

eschulte pushed a commit to branch master
in repository elpa.

commit 1b272794e2b2efd3a388ad1fcac29c7399376d66
Author: Eric Schulte <address@hidden>
Date:   Wed Dec 18 23:47:11 2013 -0700

    parse application/x-www-form-urlencoded post data
---
 NOTES               |    7 ++++---
 emacs-web-server.el |   26 ++++++++++++++++++++------
 2 files changed, 24 insertions(+), 9 deletions(-)

diff --git a/NOTES b/NOTES
index 2024910..07e7305 100644
--- a/NOTES
+++ b/NOTES
@@ -1,7 +1,7 @@
                                                            -*- org -*-
 
 * Notes
-* Tasks [3/8]
+* Tasks [4/8]
 ** DONE Handle POST requests
 1. read standard for POST data
 2. parse multi-line headers with boundaries
@@ -19,11 +19,12 @@ include an easy error handler like the 404 handler
 ** TODO better parsing of multipart form blocks
 parse more than just the content-type headers.
 
-** TODO non-multipart form data
+** DONE non-multipart form data
 e.g., parameter strings
 
-** TODO some more convenience functionality [4/5]
+** TODO some more convenience functionality [5/6]
 - [ ] strip and parse URL query string
+- [X] parse urlencoded post data
 - [X] think about defaulting to (name . content) for form elements
 - [X] maybe don't require a non-nil return to cancel the connection,
       instead only keep open if :keep-open is returned
diff --git a/emacs-web-server.el b/emacs-web-server.el
index efcfb5c..d24da78 100644
--- a/emacs-web-server.el
+++ b/emacs-web-server.el
@@ -155,7 +155,17 @@ function.
             (cond
              ;; Double \r\n outside of post data means we are done
              ;; w/headers and should call the handler.
-             ((= last-index index)
+             ((and (not boundary) (= last-index index))
+              (throw 'finished-parsing-headers t))
+             ;; Parse a URL
+             ((eq boundary :application/x-www-form-urlencoded)
+              (mapc (lambda (p)
+                      (when (string-match "=" p)
+                        (setcdr (last headers)
+                                (list (cons (substring p 0 (match-beginning 0))
+                                            (substring p (match-end 0)))))))
+                    (split-string (ews-trim (substring pending last-index))
+                                  "&" 'omit-nulls))
               (throw 'finished-parsing-headers t))
              ;; Build up multipart data.
              (boundary
@@ -173,11 +183,15 @@ function.
                 (if (and (caar this) (eql (caar this) :CONTENT-TYPE))
                     (cl-destructuring-bind (type &rest data)
                         (mail-header-parse-content-type (cdar this))
-                      (unless (string= type "multipart/form-data")
-                        (ews-error proc "TODO: handle content type: %S" type))
-                      (when (assoc 'boundary data)
-                        (setq boundary (cdr (assoc 'boundary data)))
-                        (setq delimiter (concat "\r\n--" boundary))))
+                      (cond
+                       ((string= type "multipart/form-data")
+                        (when (assoc 'boundary data)
+                          (setq boundary (cdr (assoc 'boundary data)))
+                          (setq delimiter (concat "\r\n--" boundary))))
+                       ((string= type "application/x-www-form-urlencoded")
+                        (setq boundary (intern (concat ":" (downcase type)))))
+                       (:otherwise
+                        (ews-error proc "TODO: handle content type: %S" 
type))))
                   (setcdr (last headers) this)))))
             (setq last-index tmp)))
         (setq leftover (ews-trim (substring pending last-index)))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]