Permalink
Browse files

Add Form.import and Form.export

This essentially facilitates serialization and deserialization of a Form.form
into a string. The existing Form.dump function is useful as a human-readable
format, but not as a machine-readable one. This pair of functions just builds
and then reads back a GET-style query string.
  • Loading branch information...
jwatzman authored and j4cbo committed Feb 25, 2011
1 parent f964f43 commit a829f6334e15ab7e772be33a6ea50ea2ac8a7377
Showing with 28 additions and 2 deletions.
  1. +3 −0 web/form.sig
  2. +25 −2 web/form.sml
View
@@ -8,6 +8,9 @@ signature FORM = sig
val get: form -> string -> string option
val getAll: form -> string -> string list
val export: form -> string
val import: string -> form
val dump: form -> string
end
View
@@ -36,6 +36,28 @@ structure Form : FORM = struct
end
(* val import: string -> form
*
* Builds a form from a query string.
*)
fun import str = foldl add_value Map.empty (parseVars str)
(* val export: form -> string
*
* Exports a form into a query string format suitable for use by the "import"
* function. May not properly preserve the ordering of keys with multiple
* values.
*)
fun export form = String.concatWith "&" (
map (fn (k, vs) =>
String.concatWith "&" (
map (fn v => k ^ "=" ^ (WebUtil.urlencode v)) vs
)
) (Map.listItemsi form)
)
(* val load: Web.request -> form
*
* Parse all form variables out of a request.
@@ -44,7 +66,7 @@ structure Form : FORM = struct
* of application/x-www-form-urlencoded data, that content is loaded as well.
*)
fun load (req: Web.request) = let
val form = foldl add_value Map.empty (parseVars (#query_string req))
val form = import (#query_string req)
val content_type = case WebUtil.server_header "CONTENT_TYPE" req of
SOME ct => SOME ct
| NONE => WebUtil.http_header "HTTP_CONTENT_TYPE" req
@@ -77,7 +99,8 @@ structure Form : FORM = struct
(* val dump: form -> string
*
* Return a multiline string of all keys and values in the form.
* Return a multiline string of all keys and values in the form which is
* nominally human-readable and useful for debugging.
*)
fun dump form = String.concat (
map (fn (k, vs) =>

0 comments on commit a829f63

Please sign in to comment.