📄 httpserver.java
字号:
// Server properties related constants
public static final int DEFAULT_BUFFER_SIZE = 512;
public static final String DEFAULT_DOC_ROOT = "/docs";
public static final int DEFAULT_PORT = 80;
public static final int DEFAULT_MAX_HANDLERS = 5;
public static final int DEFAULT_MAX_LOG_SIZE = 10000;
public static final int DEFAULT_REQUEST_TIMEOUT = 10;
public static final boolean DEFAULT_STACK_TRACE = false;
// Servlet constants
// public static final String DEFAULT_SERVLET_PROPS = "servlets.properties"; // TINI Omit
public static final String DEFAULT_SERVLET_PROPS = "/etc/servlets.props"; // TINI Include
// Session constants
public static final int SESSION_TYPE_COOKIE = 0;
public static final int SESSION_TYPE_URL = 1;
public static final int SESSION_TYPE_NONE = 2;
public static final String SESSION_TYPES[] = { "Cookie", "URL" };
public static final String DEFAULT_SERVLET_NAME = "com.smartsc.http.DefaultServlet";
public static final String DEFAULT_SESSION_NAME = "JSESSIONID";
public static final int DEFAULT_SESSION_TIMEOUT = 60*60; // 1 hour
public static final String DEFAULT_SESSION_TYPE = SESSION_TYPES[0];
// Server-related property names
public static final String PROP_BACKLOG = "server.backlog";
public static final String PROP_BUFFER_SIZE = "server.bufferSize";
public static final String PROP_DOC_ROOT = "server.docRoot";
public static final String PROP_INIT_PARAMS = "server.initParams";
public static final String PROP_LOG_FILE = "server.logFile";
public static final String PROP_MAIL_FROM = "server.mail.from";
public static final String PROP_MAIL_TO = "server.mail.to";
public static final String PROP_MAX_HANDLERS = "server.maxHandlers";
public static final String PROP_MAX_LOG_SIZE = "server.maxLogSize";
public static final String PROP_MIME_TYPES_FILE = "server.mimeTypesFile";
public static final String PROP_PORT = "server.port";
public static final String PROP_REQUEST_TIMEOUT = "server.requestTimeout";
public static final String PROP_STACK_TRACE = "server.stackTrace";
public static final String PROP_TRANSFER_LOG = "server.transferLog";
public static final String PROP_VERBOSE = "server.verbose";
// Servlet-related property names
public static final String PROP_SERVLET_PROPS = "servlet.propFile";
// Session-related property names
public static final String PROP_SESSION_NAME = "session.name";
public static final String PROP_SESSION_TIMEOUT = "session.timeout";
public static final String PROP_SESSION_TYPE = "session.type";
protected Properties props;
protected static Properties defaultProps = new Properties();
static
{
// Server default properties
defaultProps.put(
PROP_BUFFER_SIZE, String.valueOf( DEFAULT_BUFFER_SIZE));
defaultProps.put(
PROP_DOC_ROOT, DEFAULT_DOC_ROOT);
defaultProps.put(
PROP_MAX_HANDLERS, String.valueOf( DEFAULT_MAX_HANDLERS));
defaultProps.put(
PROP_REQUEST_TIMEOUT, String.valueOf( DEFAULT_REQUEST_TIMEOUT));
defaultProps.put(
PROP_STACK_TRACE, String.valueOf( DEFAULT_STACK_TRACE));
defaultProps.put(
PROP_PORT, String.valueOf( DEFAULT_PORT));
// Servlet default properties
defaultProps.put(
PROP_SERVLET_PROPS, DEFAULT_SERVLET_PROPS);
// Session default properties
defaultProps.put(
PROP_SESSION_TYPE, String.valueOf( DEFAULT_SESSION_TYPE));
defaultProps.put(
PROP_SESSION_NAME, DEFAULT_SESSION_NAME);
defaultProps.put(
PROP_SESSION_TIMEOUT, String.valueOf( DEFAULT_SESSION_TIMEOUT));
}
protected int getBufferSize()
{
return props.getIntProperty( PROP_BUFFER_SIZE);
}
protected String getDocRoot()
{
return props.getProperty( PROP_DOC_ROOT);
}
protected boolean isMailingOversizedLogs()
{
return props.getProperty( PROP_MAIL_TO) != null;
}
protected boolean isStackTraceEnabled()
{
return props.getBooleanProperty( PROP_STACK_TRACE);
}
protected int getSessionType()
{
String sessionTypeName = props.getProperty( PROP_SESSION_TYPE);
for( int i = 0; i < SESSION_TYPES.length; ++i)
{
if( SESSION_TYPES[i].equalsIgnoreCase( sessionTypeName))
return i;
}
return SESSION_TYPE_NONE;
}
protected String getSessionName()
{
return props.getProperty( PROP_SESSION_NAME);
}
protected int getSessionTimeout()
{
return props.getIntProperty( PROP_SESSION_TIMEOUT);
}
private File logFile;
private PrintWriter logWriter;
private File transferLogFile;
private PrintWriter transferLogWriter;
private Hashtable mailHeaders;
// TODO Support https?
public static final String DEFAULT_SCHEME = "http";
private ServerSocket serverSocket;
private String serverName;
private int handlerCount;
private int maxHandlers = DEFAULT_MAX_HANDLERS;
// String attributeName --> String attributeValue
private Hashtable attributes = new Hashtable();
// String servletName --> NamedDispatcher
private Hashtable namedDispatchers = new Hashtable();
// servlets.properties stuff
public static final String CODE = ".code";
public static final String INIT_ARGS = ".initArgs";
public static final String PRELOAD = ".preload";
public static final String MAPPING = ".mapping";
private final Properties servletProperties = new Properties();
protected String getServletProperty( String key)
{
return servletProperties.getProperty( key);
}
protected String getServletProperty( String key, String defaultValue)
{
String s = servletProperties.getProperty( key);
if( s == null) s = defaultValue;
return s;
}
// Mappings
protected Hashtable exactServletMappings = new Hashtable();
protected Hashtable prefixServletMappings = new Hashtable();
protected Hashtable extensionServletMappings = new Hashtable();
protected String defaultServletName = DEFAULT_SERVLET_NAME;
// Use java.util.Properties instead of com.smartsc.util.Properties
// because we don't need the fancy getter methods of the latter.
private static java.util.Properties
extToMimeType = new java.util.Properties();
static
{
extToMimeType.put( ".class", "application/octet-stream");
extToMimeType.put( ".css", "text/css");
extToMimeType.put( ".htm", "text/html");
extToMimeType.put( ".html", "text/html");
extToMimeType.put( ".gif", "image/gif");
extToMimeType.put( ".gz", "application/octet-stream");
extToMimeType.put( ".jar", "application/octet-stream");
extToMimeType.put( ".jpg", "image/jpeg");
extToMimeType.put( ".jpeg", "image/jpeg");
extToMimeType.put( ".js", "application/x-javascript");
extToMimeType.put( ".tini", "application/octet-stream");
extToMimeType.put( ".wml", "text/vnd.wap.wml");
extToMimeType.put( ".zip", "application/octet-stream");
}
// Use java.util.Properties instead of com.smartsc.util.Properties
// because we don't need the fancy getter methods of the latter.
private java.util.Properties
servletContextInitParams = new java.util.Properties();
// From javax.servlet.ServletContext
public Object getAttribute(String name)
{
return attributes.get( name);
}
public Enumeration getAttributeNames()
{
return attributes.keys();
}
public ServletContext getContext(String uripath)
{
return null;
}
public String getInitParameter( String name)
{
return (String)servletContextInitParams.get( name);
}
public Enumeration getInitParameterNames()
{
return servletContextInitParams.keys();
}
public int getMajorVersion()
{
return JSDK_MAJOR_VERSION;
}
public String getMimeType(String file)
{
return getContentTypeFor( file);
}
public int getMinorVersion()
{
return JSDK_MINOR_VERSION;
}
public String getRealPath(String path)
{
StringBuffer realPath = new StringBuffer( getDocRoot());
if( path == null)
return null;
// TODO path aliases
// TODO prevent .. from going above docRoot
if( !path.startsWith( "/"))
{
realPath.append( "/");
}
realPath.append( path);
return realPath.toString();
}
public RequestDispatcher getRequestDispatcher(String urlPath)
{
// Separate query string
String queryString = null;
int qryIdx = urlPath.indexOf( '?' );
if( qryIdx != -1 )
{
queryString = urlPath.substring( qryIdx + 1 );
urlPath = urlPath.substring( 0, qryIdx );
}
// Get servlet path and name
String[] servletPathAndName = new String[2];
getServletPathAndName( urlPath, servletPathAndName );
// Get NamedDispatcher
NamedRequestDispatcher nd = (NamedRequestDispatcher)
getNamedDispatcher( servletPathAndName[1] );
if( nd == null )
{
return null;
}
// Get path info portion of request path
String pathInfo = null;
int servletPathLength = servletPathAndName[0].length();
if( servletPathLength < urlPath.length() )
{
pathInfo = urlPath.substring( servletPathLength );
}
return new PathRequestDispatcher(
nd, urlPath, servletPathAndName[0], pathInfo, queryString );
}
public RequestDispatcher getNamedDispatcher( String name )
{
NamedRequestDispatcher namedDispatcher = null;
synchronized( namedDispatchers )
{
// Is it already loaded?
namedDispatcher = (NamedRequestDispatcher)
namedDispatchers.get( name );
// If not already loaded
if( namedDispatcher == null)
{
Servlet servlet = getServletFromName( name);
if( servlet != null )
{
namedDispatcher =
new NamedRequestDispatcher( name, servlet );
namedDispatchers.put( name, namedDispatcher );
}
}
}
return namedDispatcher;
}
/* TODO.17
protected RequestDispatcher getRequestDispatcher(
Servlet servlet, String urlpath)
{
if( servlet == null)
return null;
RequestDispatcher rd = new
com.smartsc.http.RequestDispatcher( servlet, urlpath);
return rd;
}
*/
public URL getResource(String path)
throws MalformedURLException
{
// TODO Support URL resources
return null;
}
public InputStream getResourceAsStream(String path)
{
// Build filename
String filename = getRealPath( path);
// Construct new file object
File file = new File( filename);
// If file does not exist
// or can not be read
// or is a directory
if( !file.exists()
|| !file.canRead()
|| file.isDirectory() )
{
return null;
}
// Get length
long length = file.length();
// If file does not exist
// or can not be read
// or has zero length
if( !file.exists()
|| !file.canRead()
|| length == 0 )
{
return null;
}
FileInputStream in = null;
try { in = new FileInputStream(file); }
catch( FileNotFoundException fnfe) {}
return in;
}
public String getServerInfo()
{
return SERVER_INFO;
}
/** @deprecated */
public Servlet getServlet(String name)
{
return null;
}
/** @deprecated */
public Enumeration getServletNames()
{
return new Vector(0).elements();
}
/** @deprecated */
public Enumeration getServlets()
{
return new Vector(0).elements();
}
/** @deprecated */
public void log( Exception exception, String msg)
{
log( msg, exception);
}
public void log( String msg)
{
// TODO Add timestamp
if( msg != null && msg.length() > 0)
{
synchronized( logWriter)
{
logWriter.println( msg);
}
}
}
public void log( String msg, Throwable throwable)
{
synchronized( logWriter)
{
log( msg);
if( isStackTraceEnabled())
{
throwable.printStackTrace( logWriter);
}
else
{
// TODO Indicate that stack tracing is disabled?
log( throwable.toString() );
}
}
}
public void log( Throwable t)
{
log( t.getClass().getName() + ": " + t.getMessage(), t);
}
public void removeAttribute(String name)
{
attributes.remove( name);
}
public void setAttribute(String name, Object o)
{
attributes.put( name, o);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -