How to trim all incoming values in an HTTP Post request

htmlhttphttp-request

I have an HTML form, with more than 30 fields in it. When this form is submitted to the server, all of the values of the fields should be trimmed. In other words, no value should start/end with any kind of whitespace (\b).

There are many options available for me:

  1. Manually trim each value
  2. Looping over values and trim each one
  3. Creating a framework (something like ASP.NET's HTTP Module) to sit at the way of any HTTP Post request, to trim the incoming value.

The third option seems the best one. However, I don't know if it is really the best one or not? And I also don't know how to do it. Is it possible at all? What are other solutions?

Best Answer

If you don't really care about security you may consider trimming the fields with Javascript when the form submit button is clicked (event *onsubmit" to implement). Now, this is of course easily bypassable since it's client server code.

Otherwise, for a server-side solution, all your solutions work. I'm not sure about the module but I think it will perform this trimming for all applications on the server, right? So you may consider that factor if your application is not the only one deployed.

Finally, but you may have considered that already, you can properly do the trimming using HTTP request listeners.

In J2EE (or whatever it is called now), it would consist in implementing your own ServletRequestListener and requestInitialized method. The advantage here is that you can package this listener in a simple JAR and add it (and its configuration in web.xml) only for the applications that require that.

In .NET, it would consist in overriding the Application_BeginRequest method in Global.asax.cs. So it would work only for the application you wrote the code for.