📄 serve.java
字号:
/// Sets the status code and message for this response.
// @param resCode the status code
// @param resMessage the status message
public void setStatus( int resCode, String resMessage )
{
this.resCode = resCode;
this.resMessage = resMessage;
}
/// Sets the status code and a default message for this response.
// @param resCode the status code
public void setStatus( int resCode )
{
switch ( resCode )
{
case SC_CONTINUE: setStatus( resCode, "Continue" ); break;
case SC_SWITCHING_PROTOCOLS:
setStatus( resCode, "Switching protocols" ); break;
case SC_OK: setStatus( resCode, "Ok" ); break;
case SC_CREATED: setStatus( resCode, "Created" ); break;
case SC_ACCEPTED: setStatus( resCode, "Accepted" ); break;
case SC_NON_AUTHORITATIVE_INFORMATION:
setStatus( resCode, "Non-authoritative" ); break;
case SC_NO_CONTENT: setStatus( resCode, "No content" ); break;
case SC_RESET_CONTENT: setStatus( resCode, "Reset content" ); break;
case SC_PARTIAL_CONTENT:
setStatus( resCode, "Partial content" ); break;
case SC_MULTIPLE_CHOICES:
setStatus( resCode, "Multiple choices" ); break;
case SC_MOVED_PERMANENTLY:
setStatus( resCode, "Moved permanentently" ); break;
case SC_MOVED_TEMPORARILY:
setStatus( resCode, "Moved temporarily" ); break;
case SC_SEE_OTHER: setStatus( resCode, "See other" ); break;
case SC_NOT_MODIFIED: setStatus( resCode, "Not modified" ); break;
case SC_USE_PROXY: setStatus( resCode, "Use proxy" ); break;
case SC_BAD_REQUEST: setStatus( resCode, "Bad request" ); break;
case SC_UNAUTHORIZED: setStatus( resCode, "Unauthorized" ); break;
case SC_PAYMENT_REQUIRED:
setStatus( resCode, "Payment required" ); break;
case SC_FORBIDDEN: setStatus( resCode, "Forbidden" ); break;
case SC_NOT_FOUND: setStatus( resCode, "Not found" ); break;
case SC_METHOD_NOT_ALLOWED:
setStatus( resCode, "Method not allowed" ); break;
case SC_NOT_ACCEPTABLE:
setStatus( resCode, "Not acceptable" ); break;
case SC_PROXY_AUTHENTICATION_REQUIRED:
setStatus( resCode, "Proxy auth required" ); break;
case SC_REQUEST_TIMEOUT:
setStatus( resCode, "Request timeout" ); break;
case SC_CONFLICT: setStatus( resCode, "Conflict" ); break;
case SC_GONE: setStatus( resCode, "Gone" ); break;
case SC_LENGTH_REQUIRED:
setStatus( resCode, "Length required" ); break;
case SC_PRECONDITION_FAILED:
setStatus( resCode, "Precondition failed" ); break;
case SC_REQUEST_ENTITY_TOO_LARGE:
setStatus( resCode, "Request entity too large" ); break;
case SC_REQUEST_URI_TOO_LONG:
setStatus( resCode, "Request URI too large" ); break;
case SC_UNSUPPORTED_MEDIA_TYPE:
setStatus( resCode, "Unsupported media type" ); break;
case SC_INTERNAL_SERVER_ERROR:
setStatus( resCode, "Internal server error" ); break;
case SC_NOT_IMPLEMENTED:
setStatus( resCode, "Not implemented" ); break;
case SC_BAD_GATEWAY: setStatus( resCode, "Bad gateway" ); break;
case SC_SERVICE_UNAVAILABLE:
setStatus( resCode, "Service unavailable" ); break;
case SC_GATEWAY_TIMEOUT:
setStatus( resCode, "Gateway timeout" ); break;
case SC_HTTP_VERSION_NOT_SUPPORTED:
setStatus( resCode, "HTTP version not supported" ); break;
default: setStatus( resCode, "" ); break;
}
}
/// Sets the value of a header field.
// @param name the header field name
// @param value the header field value
public void setHeader( String name, String value )
{
resHeaderNames.addElement( name );
resHeaderValues.addElement( value );
}
/// Sets the value of an integer header field.
// @param name the header field name
// @param value the header field integer value
public void setIntHeader( String name, int value )
{
setHeader( name, Integer.toString( value ) );
}
/// Sets the value of a long header field.
// @param name the header field name
// @param value the header field long value
public void setLongHeader( String name, long value )
{
setHeader( name, Long.toString( value ) );
}
/// Sets the value of a date header field.
// @param name the header field name
// @param value the header field date value
public void setDateHeader( String name, long value )
{
setHeader( name, to1123String( new Date( value ) ) );
}
private static final String[] weekdays =
{ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
/// Converts a Date into an RFC-1123 string.
private static String to1123String( Date date )
{
// We have to go through some machinations here to get the
// correct day of the week in GMT. getDay() gives the day in
// local time. getDate() gives the day of the month in local
// time. toGMTString() gives a formatted string in GMT. So, we
// extract the day of the month from the GMT string, and if it
// doesn't match the local one we change the local day of the
// week accordingly.
//
// The Date class sucks.
int localDay = date.getDay();
int localDate = date.getDate();
String gmtStr = date.toGMTString();
int blank = gmtStr.indexOf( ' ' );
int gmtDate = Integer.parseInt( gmtStr.substring( 0, blank ) );
int gmtDay;
if ( gmtDate > localDate || ( gmtDate < localDate && gmtDate == 1 ) )
gmtDay = ( localDay + 1 ) % 7;
else if ( localDate > gmtDate || ( localDate < gmtDate && localDate == 1 ) )
gmtDay = ( localDay + 6 ) % 7;
else
gmtDay = localDay;
return weekdays[gmtDay] + ( gmtDate < 10 ? ", 0" : ", " ) + gmtStr;
}
private boolean headersWritten = false;
/// Writes the status line and message headers for this response to the
// output stream.
// @exception IOException if an I/O error has occurred
void writeHeaders() throws IOException
{
if ( headersWritten )
return;
headersWritten = true;
if ( reqMime )
{
out.println( reqProtocol + " " + resCode + " " + resMessage );
for ( int i = 0; i < resHeaderNames.size(); ++i )
{
String name = (String) resHeaderNames.elementAt( i );
String value = (String) resHeaderValues.elementAt( i );
if ( value != null ) // just in case
out.println( name + ": " + value );
}
out.println( "" );
out.flush();
}
}
/// Writes an error response using the specified status code and message.
// @param resCode the status code
// @param resMessage the status message
// @exception IOException if an I/O error has occurred
public void sendError( int resCode, String resMessage ) throws IOException
{
setStatus( resCode, resMessage );
realSendError();
}
/// Writes an error response using the specified status code and a default
// message.
// @param resCode the status code
// @exception IOException if an I/O error has occurred
public void sendError( int resCode ) throws IOException
{
setStatus( resCode );
realSendError();
}
private void realSendError() throws IOException
{
setContentType( "text/html" );
out.println( "<HTML><HEAD>" );
out.println( "<TITLE>" + resCode + " " + resMessage + "</TITLE>" );
out.println( "</HEAD><BODY BGCOLOR=\"#99cc99\">" );
out.println( "<H2>" + resCode + " " + resMessage + "</H2>" );
String ua = getHeader( "user-agent" );
if ( ua != null && Acme.Utils.match( "*MSIE*", ua ) )
{
out.println( "<!--" );
for ( int i = 0; i < 6; ++i )
out.println( "Padding so that MSIE deigns to show this error instead of its own canned one." );
out.println( "-->" );
}
out.println( "<HR>" );
ServeUtils.writeAddress( out );
out.println( "</BODY></HTML>" );
out.flush();
}
/// Sends a redirect message to the client using the specified redirect
// location URL.
// @param location the redirect location URL
// @exception IOException if an I/O error has occurred
public void sendRedirect( String location ) throws IOException
{
setHeader( "Location", location );
sendError( SC_MOVED_TEMPORARILY );
}
// URL session-encoding stuff. Not implemented, but the API is here
// for compatibility.
/// Encodes the specified URL by including the session ID in it, or, if
// encoding is not needed, returns the URL unchanged. The
// implementation of this method should include the logic to determine
// whether the session ID needs to be encoded in the URL. For example,
// if the browser supports cookies, or session tracking is turned off,
// URL encoding is unnecessary.
// <P>
// All URLs emitted by a Servlet should be run through this method.
// Otherwise, URL rewriting cannot be used with browsers which do not
// support cookies.
public String encodeUrl( String url )
{
return url;
}
/// Encodes the specified URL for use in the sendRedirect method or, if
// encoding is not needed, returns the URL unchanged. The
// implementation of this method should include the logic to determine
// whether the session ID needs to be encoded in the URL. Because the
// rules for making this determination differ from those used to
// decide whether to encode a normal link, this method is seperate
// from the encodeUrl method.
// <P>
// All URLs sent to the HttpServletResponse.sendRedirect method should be
// run through this method. Otherwise, URL rewriting cannot be used with
// browsers which do not support cookies.
public String encodeRedirectUrl( String url )
{
return url;
}
}
class ServeInputStream extends ServletInputStream
{
private InputStream in;
public ServeInputStream( InputStream in )
{
this.in = in;
}
public int readLine( byte[] b, int off, int len ) throws IOException
{
int off2 = off;
while ( off2 - off < len )
{
int r = read();
if ( r == -1 )
{
if (off2 == off )
return -1;
break;
}
if ( r == 13 )
continue;
if ( r == 10 )
break;
b[off2] = (byte) r;
++off2;
}
return off2 - off;
}
public int read() throws IOException
{
return in.read();
}
public int read( byte[] b, int off, int len ) throws IOException
{
return in.read( b, off, len );
}
public int available() throws IOException
{
return in.available();
}
public void close() throws IOException
{
in.close();
}
}
class ServeOutputStream extends ServletOutputStream
{
private PrintStream out;
private ServeConnection conn;
public ServeOutputStream( OutputStream out, ServeConnection conn )
{
this.out = new PrintStream( out );
this.conn = conn;
}
public void write( int b ) throws IOException
{
conn.writeHeaders();
out.write( b );
}
public void write( byte[] b, int off, int len ) throws IOException
{
conn.writeHeaders();
out.write( b, off, len );
}
public void flush() throws IOException
{
conn.writeHeaders();
out.flush();
}
public void close() throws IOException
{
conn.writeHeaders();
out.close();
}
public void print( String s ) throws IOException
{
conn.writeHeaders();
out.print( s );
}
public void print( int i ) throws IOException
{
conn.writeHeaders();
out.print( i );
}
public void print( long l ) throws IOException
{
conn.writeHeaders();
out.print( l );
}
public void println( String s ) throws IOException
{
conn.writeHeaders();
out.println( s );
}
public void println( int i ) throws IOException
{
conn.writeHeaders();
out.println( i );
}
public void println( long l ) throws IOException
{
conn.writeHeaders();
out.println( l );
}
public void println() throws IOException
{
conn.writeHeaders();
out.println();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -