public class Request implements HttpServletRequest
{
protected Socket Connect; // Socket
der Verbindung
protected Context Env;
protected InStream Stream; // ServletInputStream
protected String Method; // HTTP-Methode
protected String Filename, UnTrans; //
Request-URI, absolut und relativ
protected String Pathinfo; // PATH_INFO
protected String Query = "";// Query-String
protected String Version = ""; // HTTP-Version
protected Hashtable Param; //
Parameter aus QueryString
protected Hashtable Header;
// weitere Parameter aus HTTP-Header
// Werte sind Vector-Objekte
public Request(Socket connect, Context env)
throws IOException
{
Connect = connect;
Env = env;
Stream = new InStream(Connect.getInputStream());
// Einlesen des HTTP-Header
String get, key, value;
// erste Zeile
get = Stream.readLine();
java.util.StringTokenizer st =
new java.util.StringTokenizer(get);
Method = st.nextToken();
UnTrans = st.nextToken();
if (UnTrans.indexOf("?") != -1)
{
Query = UnTrans.substring(UnTrans.indexOf("?")+1,
UnTrans.length());
UnTrans = UnTrans.substring(0,UnTrans.indexOf("?"));
}
Filename = Env.getRealPath(UnTrans);
Pathinfo = Env.GetPathInfo(UnTrans);
if (st.hasMoreTokens())
Version = st.nextToken();
if (!Version.startsWith("HTTP/1"))
Version = "HTTP/0.9";
Header = new Hashtable(2);
// Header-Felder
while(Stream.ready())
{
get = Stream.readLine();
if (get.trim().equals(""))
break;
key = get.substring( 0,
get.indexOf(":")).toLowerCase().trim();
value = get.substring(get.indexOf(":")+1,
get.length()).toLowerCase().trim();
if (Header.containsKey(key))
{ // weitere Werte (nicht erster)
Vector vec = (Vector)Header.get(key);
vec.addElement(value);
}
else
{ // erster Wert
Vector vec = new Vector(1,1);
vec.addElement(value);
Header.put(key,vec);
}
}
if (!Method.toUpperCase().equals("POST") || !Stream.ready())
Param = javax.servlet.http.HttpUtils.parseQueryString(Query);
else
Param = javax.servlet.http.HttpUtils.parsePostData(
Stream.available(), Stream);
}
public int getContentLength() //
CONTENT_LENGTH
{
int value = getIntHeader("content-length");
if (value == -1)
return 0;
else return value;
}
public String getContentType()
// CONTENT_TYPE
{ return Env.getMimeType(Filename); }
public String getProtocol() //
SERVER_PROTOCOLL
{ return Version; }
public String getScheme()
{ return "http"; }
public String getServerName() //
SERVER_NAME
{ return Connect.getLocalAddress(
).getHostName().toLowerCase(); }
public int getServerPort() //
SERVER_PORT
{ return Connect.getLocalPort(); }
public String getRemoteAddr() // REMOTE_ADDR
{ return Connect.getInetAddress().getHostAddress(); }
public String getRemoteHost() // REMOTE_HOST
{ return Connect.getInetAddress().getHostName(); }
public String getRealPath(String path)
{ // absoluter Pfad zu path
return Filename;
}
public ServletInputStream getInputStream()
throws IOException
{ // Stream zum Lesen
return (ServletInputStream)Stream;
}
public String getParameter(String name)
{
String[] vec = getParameterValues(name);
if (vec == null)
return null;
else
return vec[0];
}
public String[] getParameterValues(String name)
{ return (String[])Param.get(name); }
public Enumeration getParameterNames()
{ return Param.keys(); }
public Object getAttribute(String name)
{ // keine zusatzlichen Infos vorhanden
return null;
}
public String getMethod() //
REQUEST_METHOD
{ return Method; }
public String getRequestURI() // URL
der Anfrage
{
return HttpUtils.getRequestURL(this).toString();
}
public String getServletPath() // SCRIPT_NAME
{ return Env.GetServletPath(UnTrans); }
public String getPathInfo() //
PATH_INFO
{ return Pathinfo; }
public String getPathTranslated() //
PATH_TRANSLATED
{ return Filename; }
public String getQueryString() // QUERY_STRING
{ return Query; }
public String getRemoteUser() // REMOTE_USER
{ return ""; }
public String getAuthType() //
AUTH_TYPE
{
String str = getHeader("authorization");
return str.substring(0, str.indexOf(" "));
}
public String getHeader(String name)
{ // HTTP-Header-Feld als String
Vector vec = (Vector)Header.get(name.toLowerCase());
if (vec != null)
return (String) vec.firstElement();
else return null;
}
public int getIntHeader(String name)
{ // HTTP-Header-Feld als Integer
Vector vec = (Vector)Header.get(name.toLowerCase());
if (vec != null)
return Integer.parseInt((String) vec.firstElement());
else return -1;
}
public long getDateHeader(String name)
{ // HTTP-Header-Feld als Zeit
Vector vec = (Vector)Header.get(name.toLowerCase());
if (vec != null)
return Date.parse((String) vec.firstElement());
else return -1;
}
public Enumeration getHeaderNames()
{
return Header.keys();
}
}