CGI Module for JavaScript

malte on 2005-04-26T08:14:55

It only works for GET, but still a small step toward what should have been in JavaScript 1.0

function CGI() {
	
	var query = location.search.substr(1,256);
	
	var pairs = query.split("&");
	
	for(var i in pairs) {
		var pair = pairs[i].split("=");
		this[pair[0]] = pair[1]
	}
	
	CGI.prototype.param = function (name) { 
		var value = this[name]
		if(value == null) {
			return ""
		}
		return value 
	}
	
}


There are obviously bugs in this code (encoding) wise but it works for most purposes.


Matt Wright Lives!

davorg on 2005-04-26T10:34:29

Wow. It's like it's 1997 all over again.

You might like to read some of the criticism of Matt's CGI decoder that you'll find all over the web. And then incorporate fixes for those problems in your code.

Two obvious problems that spring to mind:

* No support for the ';' parameter separator which is now recommended over '&'.

* No support for multiple parameters with the same name.

Re:Matt Wright Lives!

malte on 2005-04-26T10:42:45

I know. Thats why I said there are bugs in there. However, if you know you will only get & separators and all you want to transport via the URL is some integers or booleans values, than it works pretty well.

As opposed to Matt's decoder there is no security hole in that code.

All the bugs you mentioned are easily fixed. My point was, that having a thing like the CGI object in JavaScript is nice :) That's all.

Re:Matt Wright Lives!

Reuven M. Lerner on 2005-05-10T03:42:01

Why do you think that Matt Wright created the "foreach my $pair (@pairs)" snippet? I wrote it; it appeared in my form-mail program, which was distributed in late 1993 or early 1994.

Needless to say, this was when HTML forms were still brand new, and having a form that sent e-mail was considered novel. Once forms and CGI were more established, and once there were libraries (cgi-lib.pl and CGI.pm) that parsed parameters for you, why would you ever do it yourself in production code?

I still have the e-mail from Matt Wright (then 16 years old) in which he apologized for having taken my form-mail program, removed the GPL, replaced my name with his, added several security issues, and distributed it on the Web. He also agreed to stop distributing form-mail, and all of the other CGI programs in his collection, because the problems were making it not worthwhile. Needless to say, he didn't follow through on his promise to me -- and caused a lot of pain for many years to come.

Re:Matt Wright Lives!

davorg on 2005-05-11T11:59:10

Sorry. You're right. I knew that Matt's program was based on your code. We've discussed it before in email (when I found that UK ISP Demon were still distributing your original program).

At least Matt now mentions the nms versions of these programs on his site. He even distributes slightly altered versions of them, tho' the links are hard to find and he refuses to link directly to our site.

You know...

sigzero on 2005-04-26T12:13:24

I was always of the mind that Javascript should have been extended further (db libraries, cgi, etc.).

When I was first learning it I was thinking it was a nice first language but it needed just a bit more.

Re:

Aristotle on 2005-04-28T12:20:16

Use location.search.substr(1); instead of throwing away anything beyond the 256th character.

Split on /[;&]/, not "&".

Call decodeURIComponent() on your split strings.

Use this[pair[0]].push(pair[1]) instead of this[pair[0]] = pair[1]. Remember to check for non-existent elements and assign them a new Array if needed, since JS does not have autovivification.