package org.pagecentric.util;
import java.util.*;
import java.io.IOException;
public class HTTPContext
{
private dictionary request;
private dictionary server;
private dictionary post;
private dictionary get;
public HTTPContext()
{
this.request = new dictionary();
this.server = new dictionary();
this.post = new dictionary();
this.get = new dictionary();
initialise();
}
public dictionary getRequest()
{
return request;
}
public void populateServer( dictionary request, Map<String,String> env )
{
Set<Map.Entry<String,String> > set = env.entrySet();
Iterator<Map.Entry<String,String> > it = set.iterator();
while ( it.hasNext() )
{
Map.Entry<String,String> e = it.next();
String key = e.getKey();
String val = e.getValue();
request.put( key, val );
}
}
public void initialise()
{
Map<String,String> map = System.getenv();
populateServer( this.server, map );
populateRequest( this.request, this.get );
pouplatePost ( this.request, this.post );
}
public void populateRequest( dictionary request, dictionary get )
{
String query_string = this.server.get( "QUERY_STRING" );
if ( null != query_string )
{
StringTokenizer st = new StringTokenizer( query_string, "&", false );
while ( st.hasMoreTokens() )
{
String keyval = st.nextToken();
int del = keyval.indexOf( "=" );
String key = Input.sanitise( keyval.substring( 0, del ) );
String val = Input.sanitise( keyval.substring( del + 1 ) );
Input i;
request.put( key, val );
get.put( key, val );
}
}
}
public void pouplatePost( dictionary request, dictionary post )
{
StringBuffer sb = new StringBuffer();
String key = null;
String val = null;
int ch;
try
{
boolean loop = true;
while ( loop )
{
ch = System.in.read();
switch ( ch )
{
case '=':
key = sb.toString();
sb.setLength( 0 );
break;
case -1:
loop = false;
case '&':
if ( null == key )
{
key = Input.sanitise( sb.toString() );
val = new String();
}
else
{
val = Input.sanitise( sb.toString() );
}
sb.setLength( 0 );
post.put( key, val );
key = null;
val = null;
break;
default:
sb.append( (char) ch );
}
}
}
catch ( IOException ex )
{
}
}
public dictionary getPost()
{
return post;
}
}