for review: fix to cgi.m

Tyson Dowd trd at cs.mu.OZ.AU
Tue Oct 13 14:01:21 AEST 1998


Hi,

Here's a fix I made a long time ago and forgot to commit.

===================================================================


Estimated hours taken: 1

Fix a few problems in the Mercury cgi library.

extras/cgi/cgi.m:
	Add cgi__maybe_get_form which processes forms
	if they are called using POST, otherwise it will return "no".
	This allows the form to handle the error itself, perhaps
	by giving the input form.

	Also fix a bug in printing the error message -- the 
	content type must be set even for error messages.
	

cvs server: Diffing .
Index: cgi.m
===================================================================
RCS file: /home/mercury1/repository/mercury/extras/cgi/cgi.m,v
retrieving revision 1.2
diff -u -r1.2 cgi.m
--- cgi.m	1997/07/15 14:46:52	1.2
+++ cgi.m	1998/10/13 03:51:21
@@ -31,6 +31,17 @@
 :- pred cgi__get_form(maybe(assoc_list(string, string))::out,
 			io__state::di, io__state::uo) is det. 
 
+% cgi__maybe_get_form(MaybeFormEntries):
+%	This procedure should be called form within a CGI program
+%	that *may* be invoked with a METHOD of POST.
+%	If all goes well, it will return the form entries.
+%	If not invoked using a METHOD of POST, it will return `no'.
+%	If something goes wrong, it will print an appropriate HTML-formatted
+%	error message to stdout, call io__set_exit_status(1),
+%	and return `no'.
+:- pred cgi__maybe_get_form(maybe(assoc_list(string, string))::out,
+			io__state::di, io__state::uo) is det. 
+
 %-----------------------------------------------------------------------------%
 
 :- implementation.
@@ -38,10 +49,16 @@
 
 %-----------------------------------------------------------------------------%
 
+cgi__maybe_get_form(FormEntries) -->
+    io__get_environment_var("REQUEST_METHOD", REQUEST_METHOD),
+    ( { REQUEST_METHOD \= yes("POST") } ->
+	{ FormEntries = no }
+    ;
+        cgi__get_form_contents(FormEntries)
+    ).
+
 cgi__get_form(FormEntries) -->
     io__get_environment_var("REQUEST_METHOD", REQUEST_METHOD),
-    io__get_environment_var("CONTENT_TYPE", CONTENT_TYPE),
-    io__get_environment_var("CONTENT_LENGTH", CONTENT_LENGTH),
     ( { REQUEST_METHOD \= yes("POST") } ->
     	cgi__error([
 	    "This script should be referenced with a ",
@@ -51,7 +68,17 @@
 	        "/fill-out-forms/overview.html"">forms overview</A>.\n"
 	]),
 	{ FormEntries = no }
-    ; { CONTENT_TYPE \= yes("application/x-www-form-urlencoded") } ->
+    ;
+        cgi__get_form_contents(FormEntries)
+    ).
+
+:- pred cgi__get_form_contents(maybe(assoc_list(string, string))::out,
+			io__state::di, io__state::uo) is det. 
+
+cgi__get_form_contents(FormEntries) -->
+    io__get_environment_var("CONTENT_TYPE", CONTENT_TYPE),
+    io__get_environment_var("CONTENT_LENGTH", CONTENT_LENGTH),
+    ( { CONTENT_TYPE \= yes("application/x-www-form-urlencoded") } ->
     	cgi__error([
 	    "This script can only be used to decode form results.\n",
 	    "It should be referenced with a <code>CONTENT_TYPE</code> of ",
@@ -194,6 +221,7 @@
 :- pred cgi__error(list(string)::in, io__state::di, io__state::uo) is det.
 cgi__error(MessageList) -->
 	{ string__append_list(MessageList, Message) },
+	output_content_type_html,
 	html__output_html(html([title(text("CGI Error Message"))],
 		(heading(1, text("CGI Error")),
 		 markup(Message)))),


-- 
Those who would give up essential liberty to purchase a little temporary
safety deserve neither liberty nor safety.     - Benjamin Franklin

Tyson Dowd   <tyson at tyse.net>   http://tyse.net



More information about the developers mailing list