📄 httprequest.java
字号:
package servlet.http;
import java.io.*;
import java.util.*;
import javax.servlet.ServletInputStream;
import javax.servlet.http.*;
import servlet.ServletConnection;
// Referenced classes of package servlet.http:
// HttpInputStream, HttpRequestLine, HttpRequestListener, HttpResponse,
// HttpSessionImpl, InvalidRequestException, MessageBytes, MessageString,
// MimeHeaders, SessionContextImpl
public class HttpRequest
implements HttpServletRequest, Cloneable
{
public HttpRequest()
{
scheme = "http";
sessionCookieName = "session";
line = new HttpRequestLine();
headers = new MimeHeaders();
in = new HttpInputStream();
attributeDictionaries = new Vector();
nullHashtable = new Hashtable();
isFilterRequest = false;
}
public Cookie[] getCookies()
{
String as[] = null;
as = getHeaders().getHeaders("Cookie");
if(as == null)
return null;
Vector vector = new Vector();
for(int i = 0; i < as.length; i++)
{
int j = 0;
StringTokenizer stringtokenizer = new StringTokenizer(as[i], ",;");
Cookie cookie = null;
while(stringtokenizer.hasMoreElements())
{
String s = null;
String s1 = null;
StringTokenizer stringtokenizer1 = new StringTokenizer(stringtokenizer.nextToken(), "=");
if(!stringtokenizer1.hasMoreElements())
break;
s = stringtokenizer1.nextToken().trim();
if(!stringtokenizer1.hasMoreElements())
break;
s1 = stringtokenizer1.nextToken().trim();
if(stringtokenizer1.hasMoreElements())
break;
if(s.charAt(0) == '$')
{
if(s.equalsIgnoreCase("$Version"))
j = Integer.parseInt(s1);
else
if(s.equalsIgnoreCase("$Domain") && cookie != null)
cookie.setDomain(s1);
else
if(s.equalsIgnoreCase("$Path") && cookie != null)
cookie.setPath(s1);
}
else
{
try
{
cookie = new Cookie(s, s1);
cookie.setVersion(j);
vector.addElement(cookie);
}
catch(IllegalArgumentException ex) {}
}
}
}
Cookie acookie[] = new Cookie[vector.size()];
for(int k = 0; k < acookie.length; k++)
acookie[k] = (Cookie)vector.elementAt(k);
return acookie;
}
public void init(ServletConnection servletconnection)
throws IOException
{
con = servletconnection;
keepAlive = true;
init();
attributeDictionaries.removeAllElements();
in.init(servletconnection.getInputStream());
}
private void init()
{
mimeType = null;
reader = null;
gotReader = false;
gotInputStream = false;
params = null;
session = null;
charEncoding = null;
requestedSessionID = null;
assignedRequestURI = null;
isRequestURIAssigned = false;
requestPath = null;
servletPath.reset();
remoteUser.reset();
pathInfo.reset();
headers.clear();
int i = attributeDictionaries.size();
for(int j = 0; j < i; j++)
{
Object obj = attributeDictionaries.elementAt(j);
if(obj instanceof HttpRequestListener)
((HttpRequestListener)obj).resetHttpRequest(this);
}
}
public boolean next()
throws IOException, InvalidRequestException
{
for(boolean flag = false; !flag;)
{
in.next();
try
{
if(!line.parse(in))
return false;
flag = true;
}
catch(IllegalArgumentException ex)
{
if(line.line == null || line.line.length() != 0)
throw new InvalidRequestException();
}
}
init();
if(line.isFullRequest())
{
try
{
headers.read(in);
}
catch(IllegalArgumentException ex)
{
throw new InvalidRequestException();
}
MessageBytes messagebytes = line.getMethod();
if(messagebytes.equals("GET") || messagebytes.equals("HEAD"))
length = -1;
else
length = headers.getIntHeader("Content-Length");
if(length != -1)
in.setContentLength(length);
if(keepAlive)
{
String s = getHeader("Connection");
if(s != null)
{
if(getProtocol().equals("HTTP/1.0"))
{
if(!s.equalsIgnoreCase("Keep-Alive"))
keepAlive = false;
}
else
if(s.equalsIgnoreCase("Close"))
keepAlive = false;
}
else
if(getProtocol().equals("HTTP/1.0"))
keepAlive = false;
}
}
else
{
keepAlive = false;
length = -1;
}
return true;
}
public void finish()
throws IOException
{
in.finish();
}
public void reset()
throws IOException
{
init();
in.resets();
line.reset();
}
public boolean isFullRequest()
{
return line.isFullRequest();
}
public boolean isProxyRequest()
{
return line.isProxyRequest();
}
public int getTotalBytes()
{
return in.getTotal();
}
public MimeHeaders getHeaders()
{
return headers;
}
public void setKeepAlive(boolean flag)
{
keepAlive = flag;
}
public boolean getKeepAlive()
{
return keepAlive;
}
public MessageBytes getRequestPath()
{
if(requestPath != null)
return requestPath;
else
return line.getRequestPath();
}
public void setRequestPath(String s)
{
byte abyte0[] = new byte[s.length()];
s.getBytes(0, abyte0.length, abyte0, 0);
requestPath = new MessageBytes(abyte0, 0, abyte0.length);
}
public MessageBytes getRequestLine()
{
return line.getRequestLine();
}
public void setServletPath(String s)
{
servletPath.setString(s);
}
public void setServletPath(byte abyte0[], int i, int j)
{
servletPath.setBytes(abyte0, i, j);
}
public void setPathInfo(String s)
{
pathInfo.setString(s);
}
public void setPathInfo(byte abyte0[], int i, int j)
{
pathInfo.setBytes(abyte0, i, j);
}
public int getContentLength()
{
return length;
}
public String getContentType()
{
if(mimeType == null)
mimeType = headers.getHeader("Content-Type");
return mimeType;
}
public String getProtocol()
{
return line.getProtocol().toString();
}
public String getScheme()
{
return scheme;
}
public void setScheme(String s)
{
scheme = s;
}
public String getVersion()
{
String s = getProtocol();
if(s == null)
return null;
int i = s.indexOf(47);
if(i == -1)
return null;
else
return s.substring(i + 1);
}
public int getMajorVersion()
{
String s = getVersion();
if(s == null)
return 0;
int i = s.indexOf(46);
if(i == -1)
return 0;
else
return Integer.parseInt(s.substring(0, i));
}
public int getMinorVersion()
{
String s = getVersion();
if(s == null)
return 0;
int i = s.indexOf(46);
if(i == -1)
return 0;
else
return Integer.parseInt(s.substring(i + 1));
}
public String getServerName()
{
String s = getHeader("Host");
if(s == null)
{
s = con.getServerName();
}
else
{
int i = s.indexOf(58);
if(i != -1)
s = s.substring(0, i);
}
return s;
}
public int getServerPort()
{
return con.getServerPort();
}
public String getRemoteAddr()
{
return con.getRemoteAddr();
}
public String getRemoteHost()
{
return con.getRemoteHost();
}
public ServletInputStream getInputStream()
throws IOException
{
if(gotReader)
{
throw new IllegalStateException("can't mix text and binary input");
}
else
{
gotInputStream = true;
return in;
}
}
public BufferedReader getReader()
throws UnsupportedEncodingException
{
if(gotInputStream)
throw new IllegalStateException("can't mix text and binary input");
if(reader == null)
{
gotReader = true;
String s = HttpResponse.getContentCharset(getContentType());
InputStreamReader inputstreamreader = new InputStreamReader(in, s);
reader = new BufferedReader(inputstreamreader);
}
return reader;
}
public String getParameter(String s)
{
if(params == null)
params = getParametersFromRequest();
String as[] = (String[])params.get(s);
if(as == null)
return null;
String s1 = as[0];
for(int i = 1; i < as.length; i++)
s1 = s1 + "," + as[i];
return s1;
}
public String[] getParameterValues(String s)
{
if(params == null)
params = getParametersFromRequest();
return (String[])params.get(s);
}
public Enumeration getParameterNames()
{
if(params == null)
params = getParametersFromRequest();
return params.keys();
}
public void setSessionContext(SessionContextImpl sessioncontextimpl)
{
sessionContext = sessioncontextimpl;
}
public void setResponse(HttpResponse httpresponse)
{
response = httpresponse;
}
public HttpSession getSession(boolean flag)
{
if(session != null)
return session;
if(isRequestedSessionIdValid())
return session;
if(flag)
{
Cookie cookie = createSessionCookie();
session = new HttpSessionImpl(sessionContext, cookie);
if(cookiesSupported())
session.setCookieSupported(true);
}
else
{
session = null;
}
response.setHttpSession(session);
return session;
}
protected boolean cookiesSupported()
{
return getHeaders().find("Cookie") != null;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -