Acme Serve is no longer used in Helma.
This commit is contained in:
parent
748c225afe
commit
aa73b41701
9 changed files with 0 additions and 3133 deletions
|
@ -1,313 +0,0 @@
|
|||
// CgiServlet - runs CGI programs
|
||||
//
|
||||
// Copyright (C)1996,1998 by Jef Poskanzer <jef@acme.com>. All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions
|
||||
// are met:
|
||||
// 1. Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in the
|
||||
// documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
// SUCH DAMAGE.
|
||||
//
|
||||
// Visit the ACME Labs Java page for up-to-date versions of this and other
|
||||
// fine Java utilities: http://www.acme.com/java/
|
||||
|
||||
package Acme.Serve;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.http.*;
|
||||
|
||||
/// Runs CGI programs.
|
||||
// <P>
|
||||
// Note: although many implementations of CGI set the working directory of
|
||||
// the subprocess to be the directory containing the executable file, the
|
||||
// CGI spec actually says nothing about the working directory. Since
|
||||
// Java has no method for setting the working directory, this implementation
|
||||
// does not set it.
|
||||
// <P>
|
||||
// <A HREF="/resources/classes/Acme/Serve/CgiServlet.java">Fetch the software.</A><BR>
|
||||
// <A HREF="/resources/classes/Acme.tar.gz">Fetch the entire Acme package.</A>
|
||||
// <P>
|
||||
// @see Acme.Serve.Serve
|
||||
|
||||
public class CgiServlet extends HttpServlet
|
||||
{
|
||||
|
||||
/// Returns a string containing information about the author, version, and
|
||||
// copyright of the servlet.
|
||||
public String getServletInfo()
|
||||
{
|
||||
return "runs CGI programs";
|
||||
}
|
||||
|
||||
/// Services a single request from the client.
|
||||
// @param req the servlet request
|
||||
// @param req the servlet response
|
||||
// @exception ServletException when an exception has occurred
|
||||
public void service( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException
|
||||
{
|
||||
if ( ! ( req.getMethod().equalsIgnoreCase( "get" ) ||
|
||||
req.getMethod().equalsIgnoreCase( "post" ) ) )
|
||||
{
|
||||
res.sendError( HttpServletResponse.SC_NOT_IMPLEMENTED );
|
||||
return;
|
||||
}
|
||||
|
||||
String path = req.getServletPath();
|
||||
if ( path == null || path.charAt( 0 ) != '/' )
|
||||
{
|
||||
res.sendError( HttpServletResponse.SC_BAD_REQUEST );
|
||||
return;
|
||||
}
|
||||
if ( path.indexOf( "/../" ) != -1 || path.endsWith( "/.." ) )
|
||||
{
|
||||
res.sendError( HttpServletResponse.SC_FORBIDDEN );
|
||||
return;
|
||||
}
|
||||
|
||||
// Make a version without the leading /.
|
||||
String pathname = path.substring( 1 );
|
||||
if ( pathname.length() == 0 )
|
||||
pathname = "./";
|
||||
|
||||
dispatchPathname( req, res, path, pathname );
|
||||
}
|
||||
|
||||
|
||||
private void dispatchPathname( HttpServletRequest req, HttpServletResponse res, String path, String pathname ) throws IOException
|
||||
{
|
||||
String filename = pathname.replace( '/', File.separatorChar );
|
||||
if ( filename.charAt( filename.length() - 1 ) == File.separatorChar )
|
||||
filename = filename.substring( 0, filename.length() - 1 );
|
||||
filename = getServletContext().getRealPath( filename );
|
||||
File file = new File( filename );
|
||||
if ( file.exists() )
|
||||
serveFile( req, res, path, filename, file );
|
||||
else
|
||||
res.sendError( HttpServletResponse.SC_NOT_FOUND );
|
||||
}
|
||||
|
||||
|
||||
private void serveFile( HttpServletRequest req, HttpServletResponse res, String path, String filename, File file ) throws IOException
|
||||
{
|
||||
String queryString = req.getQueryString();
|
||||
int contentLength = req.getContentLength();
|
||||
int c;
|
||||
|
||||
log( "running " + path );
|
||||
|
||||
// Make argument list.
|
||||
Vector argVec = new Vector();
|
||||
argVec.addElement( filename );
|
||||
if ( queryString != null && queryString.indexOf( "=" ) == -1 )
|
||||
{
|
||||
Enumeration enum = new StringTokenizer( queryString, "+" );
|
||||
while ( enum.hasMoreElements() )
|
||||
argVec.addElement( (String) enum.nextElement() );
|
||||
}
|
||||
String argList[] = makeList( argVec );
|
||||
|
||||
// Make environment list.
|
||||
Vector envVec = new Vector();
|
||||
envVec.addElement( makeEnv(
|
||||
"PATH", "/usr/local/bin:/usr/ucb:/bin:/usr/bin" ) );
|
||||
envVec.addElement( makeEnv( "GATEWAY_INTERFACE", "CGI/1.1" ) );
|
||||
envVec.addElement( makeEnv(
|
||||
"SERVER_SOFTWARE", getServletContext().getServerInfo() ) );
|
||||
envVec.addElement( makeEnv( "SERVER_NAME", req.getServerName() ) );
|
||||
envVec.addElement( makeEnv(
|
||||
"SERVER_PORT", Integer.toString( req.getServerPort() ) ) );
|
||||
envVec.addElement( makeEnv( "REMOTE_ADDR", req.getRemoteAddr() ) );
|
||||
envVec.addElement( makeEnv( "REMOTE_HOST", req.getRemoteHost() ) );
|
||||
envVec.addElement( makeEnv( "REQUEST_METHOD", req.getMethod() ) );
|
||||
if ( contentLength != -1 )
|
||||
envVec.addElement( makeEnv(
|
||||
"CONTENT_LENGTH", Integer.toString( contentLength ) ) );
|
||||
if ( req.getContentType() != null )
|
||||
envVec.addElement( makeEnv(
|
||||
"CONTENT_TYPE", req.getContentType() ) );
|
||||
envVec.addElement( makeEnv( "SCRIPT_NAME", req.getServletPath() ) );
|
||||
if ( req.getPathInfo() != null )
|
||||
envVec.addElement( makeEnv( "PATH_INFO", req.getPathInfo() ) );
|
||||
if ( req.getPathTranslated() != null )
|
||||
envVec.addElement( makeEnv(
|
||||
"PATH_TRANSLATED", req.getPathTranslated() ) );
|
||||
if ( queryString != null )
|
||||
envVec.addElement( makeEnv( "QUERY_STRING", queryString ) );
|
||||
envVec.addElement( makeEnv( "SERVER_PROTOCOL", req.getProtocol() ) );
|
||||
if ( req.getRemoteUser() != null )
|
||||
envVec.addElement( makeEnv( "REMOTE_USER", req.getRemoteUser() ) );
|
||||
if ( req.getAuthType() != null )
|
||||
envVec.addElement( makeEnv( "AUTH_TYPE", req.getAuthType() ) );
|
||||
Enumeration enum = req.getHeaderNames();
|
||||
while ( enum.hasMoreElements() )
|
||||
{
|
||||
String name = (String) enum.nextElement();
|
||||
String value = req.getHeader( name );
|
||||
if ( value == null )
|
||||
value = "";
|
||||
envVec.addElement( makeEnv(
|
||||
"HTTP_" + name.toUpperCase().replace( '-', '_' ), value ) );
|
||||
}
|
||||
String envList[] = makeList( envVec );
|
||||
|
||||
// Start the command.
|
||||
Process proc = Runtime.getRuntime().exec( argList, envList );
|
||||
|
||||
try
|
||||
{
|
||||
// If it's a POST, copy the request data to the process.
|
||||
if ( req.getMethod().equalsIgnoreCase( "post" ) )
|
||||
{
|
||||
InputStream reqIn = req.getInputStream();
|
||||
OutputStream procOut = proc.getOutputStream();
|
||||
for ( int i = 0; i < contentLength; ++i )
|
||||
{
|
||||
c = reqIn.read();
|
||||
if ( c == -1 )
|
||||
break;
|
||||
procOut.write( c );
|
||||
}
|
||||
procOut.close();
|
||||
}
|
||||
|
||||
// Now read the response from the process.
|
||||
BufferedReader procIn = new BufferedReader( new InputStreamReader(
|
||||
proc.getInputStream() ) );
|
||||
OutputStream resOut = res.getOutputStream();
|
||||
// Some of the headers have to be intercepted and handled.
|
||||
boolean firstLine = true;
|
||||
while ( true )
|
||||
{
|
||||
String line = procIn.readLine();
|
||||
if ( line == null )
|
||||
break;
|
||||
line = line.trim();
|
||||
if ( line.equals( "" ) )
|
||||
break;
|
||||
int colon = line.indexOf( ":" );
|
||||
if ( colon == -1 )
|
||||
{
|
||||
// No colon. If it's the first line, parse it for status.
|
||||
if ( firstLine )
|
||||
{
|
||||
StringTokenizer tok = new StringTokenizer( line, " " );
|
||||
try
|
||||
{
|
||||
switch( tok.countTokens() )
|
||||
{
|
||||
case 2:
|
||||
tok.nextToken();
|
||||
res.setStatus(
|
||||
Integer.parseInt( tok.nextToken() ) );
|
||||
break;
|
||||
case 3:
|
||||
tok.nextToken();
|
||||
res.setStatus(
|
||||
Integer.parseInt( tok.nextToken() ),
|
||||
tok.nextToken() );
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch ( NumberFormatException ignore ) {}
|
||||
}
|
||||
else
|
||||
{
|
||||
// No colon and it's not the first line? Ignore.
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// There's a colon. Check for certain special headers.
|
||||
String name = line.substring( 0, colon );
|
||||
String value = line.substring( colon + 1 ).trim();
|
||||
if ( name.equalsIgnoreCase( "Status" ) )
|
||||
{
|
||||
StringTokenizer tok = new StringTokenizer( value, " " );
|
||||
try
|
||||
{
|
||||
switch( tok.countTokens() )
|
||||
{
|
||||
case 1:
|
||||
res.setStatus(
|
||||
Integer.parseInt( tok.nextToken() ) );
|
||||
break;
|
||||
case 2:
|
||||
res.setStatus(
|
||||
Integer.parseInt( tok.nextToken() ),
|
||||
tok.nextToken() );
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch ( NumberFormatException ignore ) {}
|
||||
}
|
||||
else if ( name.equalsIgnoreCase( "Content-type" ) )
|
||||
{
|
||||
res.setContentType( value );
|
||||
}
|
||||
else if ( name.equalsIgnoreCase( "Content-length" ) )
|
||||
{
|
||||
try
|
||||
{
|
||||
res.setContentLength( Integer.parseInt( value ) );
|
||||
}
|
||||
catch ( NumberFormatException ignore ) {}
|
||||
}
|
||||
else if ( name.equalsIgnoreCase( "Location" ) )
|
||||
{
|
||||
res.setStatus(
|
||||
HttpServletResponse.SC_MOVED_TEMPORARILY );
|
||||
res.setHeader( name, value );
|
||||
}
|
||||
else
|
||||
{
|
||||
// Not a special header. Just set it.
|
||||
res.setHeader( name, value );
|
||||
}
|
||||
}
|
||||
}
|
||||
// Copy the rest of the data uninterpreted.
|
||||
Acme.Utils.copyStream( procIn, resOut );
|
||||
procIn.close();
|
||||
resOut.close();
|
||||
}
|
||||
catch ( IOException e )
|
||||
{
|
||||
//res.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR );
|
||||
// There's some weird bug in Java, when reading from a Process
|
||||
// you get a spurious IOException. We have to ignore it.
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static String makeEnv( String name, String value )
|
||||
{
|
||||
return name + "=" + value;
|
||||
}
|
||||
|
||||
|
||||
private static String[] makeList( Vector vec )
|
||||
{
|
||||
String list[] = new String[vec.size()];
|
||||
for ( int i = 0; i < vec.size(); ++i )
|
||||
list[i] = (String) vec.elementAt( i );
|
||||
return list;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,316 +0,0 @@
|
|||
// FileServlet - servlet similar to a standard httpd
|
||||
//
|
||||
// Copyright (C)1996,1998 by Jef Poskanzer <jef@acme.com>. All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions
|
||||
// are met:
|
||||
// 1. Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in the
|
||||
// documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
// SUCH DAMAGE.
|
||||
//
|
||||
// Visit the ACME Labs Java page for up-to-date versions of this and other
|
||||
// fine Java utilities: http://www.acme.com/java/
|
||||
|
||||
package Acme.Serve;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import java.text.*;
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.http.*;
|
||||
|
||||
/// Servlet similar to a standard httpd.
|
||||
// <P>
|
||||
// Implements the "GET" and "HEAD" methods for files and directories.
|
||||
// Handles index.html.
|
||||
// Redirects directory URLs that lack a trailing /.
|
||||
// Handles If-Modified-Since and Range.
|
||||
// <P>
|
||||
// <A HREF="/resources/classes/Acme/Serve/FileServlet.java">Fetch the software.</A><BR>
|
||||
// <A HREF="/resources/classes/Acme.tar.gz">Fetch the entire Acme package.</A>
|
||||
// <P>
|
||||
// @see Acme.Serve.Serve
|
||||
|
||||
public class FileServlet extends HttpServlet
|
||||
{
|
||||
|
||||
// We keep a single throttle table for all instances of the servlet.
|
||||
// Normally there is only one instance; the exception is subclasses.
|
||||
static Acme.WildcardDictionary throttleTab = null;
|
||||
|
||||
/// Constructor.
|
||||
public FileServlet()
|
||||
{
|
||||
}
|
||||
|
||||
/// Constructor with throttling.
|
||||
// @param throttles filename containing throttle settings
|
||||
// @see ThrottledOutputStream
|
||||
public FileServlet( String throttles ) throws IOException
|
||||
{
|
||||
this();
|
||||
readThrottles( throttles );
|
||||
}
|
||||
|
||||
private void readThrottles( String throttles ) throws IOException
|
||||
{
|
||||
Acme.WildcardDictionary newThrottleTab =
|
||||
ThrottledOutputStream.parseThrottleFile( throttles );
|
||||
if ( throttleTab == null )
|
||||
throttleTab = newThrottleTab;
|
||||
else
|
||||
{
|
||||
// Merge the new one into the old one.
|
||||
Enumeration keys = newThrottleTab.keys();
|
||||
Enumeration elements = newThrottleTab.elements();
|
||||
while ( keys.hasMoreElements() )
|
||||
{
|
||||
Object key = keys.nextElement();
|
||||
Object element = elements.nextElement();
|
||||
throttleTab.put( key, element );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a string containing information about the author, version, and
|
||||
// copyright of the servlet.
|
||||
public String getServletInfo()
|
||||
{
|
||||
return "servlet similar to a standard httpd";
|
||||
}
|
||||
|
||||
|
||||
/// Services a single request from the client.
|
||||
// @param req the servlet request
|
||||
// @param req the servlet response
|
||||
// @exception ServletException when an exception has occurred
|
||||
public void service( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException
|
||||
{
|
||||
boolean headOnly;
|
||||
if ( req.getMethod().equalsIgnoreCase( "get" ) )
|
||||
headOnly = false;
|
||||
else if ( ! req.getMethod().equalsIgnoreCase( "head" ) )
|
||||
headOnly = true;
|
||||
else
|
||||
{
|
||||
res.sendError( HttpServletResponse.SC_NOT_IMPLEMENTED );
|
||||
return;
|
||||
}
|
||||
|
||||
String path = req.getServletPath();
|
||||
if ( path == null || path.charAt( 0 ) != '/' )
|
||||
{
|
||||
res.sendError( HttpServletResponse.SC_BAD_REQUEST );
|
||||
return;
|
||||
}
|
||||
if ( path.indexOf( "/../" ) != -1 || path.endsWith( "/.." ) )
|
||||
{
|
||||
res.sendError( HttpServletResponse.SC_FORBIDDEN );
|
||||
return;
|
||||
}
|
||||
|
||||
// Make a version without the leading /.
|
||||
String pathname = path.substring( 1 );
|
||||
if ( pathname.length() == 0 )
|
||||
pathname = "./";
|
||||
|
||||
dispatchPathname( req, res, headOnly, path, pathname );
|
||||
}
|
||||
|
||||
|
||||
private void dispatchPathname( HttpServletRequest req, HttpServletResponse res, boolean headOnly, String path, String pathname ) throws IOException
|
||||
{
|
||||
String filename = pathname.replace( '/', File.separatorChar );
|
||||
if ( filename.charAt( filename.length() - 1 ) == File.separatorChar )
|
||||
filename = filename.substring( 0, filename.length() - 1 );
|
||||
filename = getServletContext().getRealPath( filename );
|
||||
File file = new File( filename );
|
||||
if ( file.exists() )
|
||||
{
|
||||
if ( ! file.isDirectory() )
|
||||
serveFile( req, res, headOnly, path, filename, file );
|
||||
else
|
||||
{
|
||||
if ( pathname.charAt( pathname.length() - 1 ) != '/' )
|
||||
redirectDirectory( req, res, path, file );
|
||||
else
|
||||
{
|
||||
String indexFilename =
|
||||
filename + File.separatorChar + "index.html";
|
||||
File indexFile = new File( indexFilename );
|
||||
if ( indexFile.exists() )
|
||||
serveFile(
|
||||
req, res, headOnly, path, indexFilename,
|
||||
indexFile );
|
||||
else
|
||||
serveDirectory(
|
||||
req, res, headOnly, path, filename, file );
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( pathname.endsWith( "/index.html" ) )
|
||||
dispatchPathname(
|
||||
req, res, headOnly, path,
|
||||
pathname.substring( 0, pathname.length() - 10 ) );
|
||||
else if ( pathname.equals( "index.html" ) )
|
||||
dispatchPathname( req, res, headOnly, path, "./" );
|
||||
else
|
||||
res.sendError( HttpServletResponse.SC_NOT_FOUND );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected void serveFile( HttpServletRequest req, HttpServletResponse res, boolean headOnly, String path, String filename, File file ) throws IOException
|
||||
{
|
||||
// log( "getting " + path );
|
||||
if ( ! file.canRead() )
|
||||
{
|
||||
res.sendError( HttpServletResponse.SC_FORBIDDEN );
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle If-Modified-Since.
|
||||
res.setStatus( HttpServletResponse.SC_OK );
|
||||
long lastMod = file.lastModified();
|
||||
String ifModSinceStr = req.getHeader( "If-Modified-Since" );
|
||||
long ifModSince = -1;
|
||||
if ( ifModSinceStr != null )
|
||||
{
|
||||
int semi = ifModSinceStr.indexOf( ';' );
|
||||
if ( semi != -1 )
|
||||
ifModSinceStr = ifModSinceStr.substring( 0, semi );
|
||||
try
|
||||
{
|
||||
ifModSince =
|
||||
DateFormat.getDateInstance().parse( ifModSinceStr ).getTime();
|
||||
}
|
||||
catch ( Exception ignore ) {}
|
||||
}
|
||||
if ( ifModSince != -1 && ifModSince >= lastMod )
|
||||
{
|
||||
res.setStatus( HttpServletResponse.SC_NOT_MODIFIED );
|
||||
headOnly = true;
|
||||
}
|
||||
|
||||
String rangeStr = req.getHeader( "Range" );
|
||||
if ( rangeStr != null )
|
||||
{
|
||||
//!!!
|
||||
}
|
||||
|
||||
res.setContentType( getServletContext().getMimeType( filename ) );
|
||||
res.setContentLength( (int) file.length() );
|
||||
res.setDateHeader( "Last-modified", lastMod );
|
||||
OutputStream out = res.getOutputStream();
|
||||
if ( ! headOnly )
|
||||
{
|
||||
// Check throttle.
|
||||
if ( throttleTab != null )
|
||||
{
|
||||
ThrottleItem throttleItem =
|
||||
(ThrottleItem) throttleTab.get( path );
|
||||
if ( throttleItem != null )
|
||||
{
|
||||
// !!! Need to account for multiple simultaneous fetches.
|
||||
out = new ThrottledOutputStream(
|
||||
out, throttleItem.getMaxBps() );
|
||||
}
|
||||
}
|
||||
|
||||
InputStream in = new FileInputStream( file );
|
||||
copyStream( in, out );
|
||||
in.close();
|
||||
}
|
||||
out.close();
|
||||
}
|
||||
|
||||
/// Copy a file from in to out.
|
||||
// Sub-classes can override this in order to do filtering of some sort.
|
||||
public void copyStream( InputStream in, OutputStream out ) throws IOException
|
||||
{
|
||||
Acme.Utils.copyStream( in, out );
|
||||
}
|
||||
|
||||
|
||||
private void serveDirectory( HttpServletRequest req, HttpServletResponse res, boolean headOnly, String path, String filename, File file ) throws IOException
|
||||
{
|
||||
log( "indexing " + path );
|
||||
if ( ! file.canRead() )
|
||||
{
|
||||
res.sendError( HttpServletResponse.SC_FORBIDDEN );
|
||||
return;
|
||||
}
|
||||
res.setStatus( HttpServletResponse.SC_OK );
|
||||
res.setContentType( "text/html" );
|
||||
OutputStream out = res.getOutputStream();
|
||||
if ( ! headOnly )
|
||||
{
|
||||
PrintStream p = new PrintStream( new BufferedOutputStream( out ) );
|
||||
p.println( "<HTML><HEAD>" );
|
||||
p.println( "<TITLE>Index of " + path + "</TITLE>" );
|
||||
p.println( "</HEAD><BODY BGCOLOR=\"#ffffff\">" );
|
||||
p.println( "<H2>Index of " + path + "</H2>" );
|
||||
p.println( "<PRE>" );
|
||||
p.println( "mode bytes last-changed name" );
|
||||
p.println( "<HR>" );
|
||||
String[] names = file.list();
|
||||
Acme.Utils.sortStrings( names );
|
||||
for ( int i = 0; i < names.length; ++i )
|
||||
{
|
||||
String aFilename = filename + File.separatorChar + names[i];
|
||||
File aFile = new File( aFilename );
|
||||
String aFileType;
|
||||
if ( aFile.isDirectory() )
|
||||
aFileType = "d";
|
||||
else if ( aFile.isFile() )
|
||||
aFileType = "-";
|
||||
else
|
||||
aFileType = "?";
|
||||
String aFileRead = ( aFile.canRead() ? "r" : "-" );
|
||||
String aFileWrite = ( aFile.canWrite() ? "w" : "-" );
|
||||
String aFileExe = "-";
|
||||
String aFileSize = Acme.Fmt.fmt( aFile.length(), 8 );
|
||||
String aFileDate =
|
||||
Acme.Utils.lsDateStr( new Date( aFile.lastModified() ) );
|
||||
String aFileDirsuf = ( aFile.isDirectory() ? "/" : "" );
|
||||
String aFileSuf = ( aFile.isDirectory() ? "/" : "" );
|
||||
p.println(
|
||||
aFileType + aFileRead + aFileWrite + aFileExe +
|
||||
" " + aFileSize + " " + aFileDate + " " +
|
||||
"<A HREF=\"" + names[i] + aFileDirsuf + "\">" +
|
||||
names[i] + aFileSuf + "</A>" );
|
||||
}
|
||||
p.println( "</PRE>" );
|
||||
p.println( "<HR>" );
|
||||
ServeUtils.writeAddress( p );
|
||||
p.println( "</BODY></HTML>" );
|
||||
p.flush();
|
||||
}
|
||||
out.close();
|
||||
}
|
||||
|
||||
|
||||
protected void redirectDirectory( HttpServletRequest req, HttpServletResponse res, String path, File file ) throws IOException
|
||||
{
|
||||
log( "redirecting " + path );
|
||||
res.sendRedirect( path + "/" );
|
||||
}
|
||||
|
||||
}
|
|
@ -1,70 +0,0 @@
|
|||
// SampleServlet - trivial servlet
|
||||
//
|
||||
// Copyright (C) 1996 by Jef Poskanzer <jef@acme.com>. All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions
|
||||
// are met:
|
||||
// 1. Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in the
|
||||
// documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
// SUCH DAMAGE.
|
||||
//
|
||||
// Visit the ACME Labs Java page for up-to-date versions of this and other
|
||||
// fine Java utilities: http://www.acme.com/java/
|
||||
|
||||
package Acme.Serve;
|
||||
|
||||
import java.io.*;
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.http.*;
|
||||
|
||||
/// Trivial servlet.
|
||||
// <P>
|
||||
// <A HREF="/resources/classes/Acme/Serve/SampleServlet.java">Fetch the software.</A><BR>
|
||||
// <A HREF="/resources/classes/Acme.tar.gz">Fetch the entire Acme package.</A>
|
||||
|
||||
public class SampleServlet extends HttpServlet
|
||||
{
|
||||
|
||||
/// Returns a string containing information about the author, version, and
|
||||
// copyright of the servlet.
|
||||
public String getServletInfo()
|
||||
{
|
||||
return "trivial servlet";
|
||||
}
|
||||
|
||||
/// Services a single request from the client.
|
||||
// @param req the servlet request
|
||||
// @param req the servlet response
|
||||
// @exception ServletException when an exception has occurred
|
||||
public void service( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException
|
||||
{
|
||||
log( "called" );
|
||||
res.setStatus( HttpServletResponse.SC_OK );
|
||||
res.setContentType( "text/html" );
|
||||
ServletOutputStream p = res.getOutputStream();
|
||||
p.println( "<HTML><HEAD>" );
|
||||
p.println( "<TITLE>Sample Servlet Output</TITLE>" );
|
||||
p.println( "</HEAD><BODY>" );
|
||||
p.println( "<H2>Sample Servlet Output</H2>" );
|
||||
p.println( "<P>Output from a sample servlet." );
|
||||
p.println( "</BODY></HTML>" );
|
||||
p.flush();
|
||||
p.close();
|
||||
}
|
||||
|
||||
}
|
File diff suppressed because it is too large
Load diff
|
@ -1,95 +0,0 @@
|
|||
// ServeUtils - static utilities for minimal Java HTTP server
|
||||
//
|
||||
// Copyright (C) 1996 by Jef Poskanzer <jef@acme.com>. All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions
|
||||
// are met:
|
||||
// 1. Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in the
|
||||
// documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
// SUCH DAMAGE.
|
||||
//
|
||||
// Visit the ACME Labs Java page for up-to-date versions of this and other
|
||||
// fine Java utilities: http://www.acme.com/java/
|
||||
|
||||
package Acme.Serve;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.http.*;
|
||||
|
||||
/// Static utilities for minimal Java HTTP server.
|
||||
// <P>
|
||||
// <A HREF="/resources/classes/Acme/Serve/ServeUtils.java">Fetch the software.</A><BR>
|
||||
// <A HREF="/resources/classes/Acme.tar.gz">Fetch the entire Acme package.</A>
|
||||
|
||||
public class ServeUtils
|
||||
{
|
||||
|
||||
// Server identification.
|
||||
public static final String serverName = "Helma";
|
||||
// we're using the server version from helma.main.Server class.
|
||||
// public static final String serverVersion = "1.2 p1";
|
||||
public static final String serverUrl = "http://helma.org/";
|
||||
|
||||
/// Write a standard-format HTML address for this server.
|
||||
public static void writeAddress( OutputStream o ) throws IOException
|
||||
{
|
||||
PrintStream p = new PrintStream( o );
|
||||
p.println(
|
||||
"<ADDRESS><A HREF=\"" + serverUrl + "\">" +
|
||||
serverName + " " + helma.main.Server.version + "</A></ADDRESS>" );
|
||||
}
|
||||
|
||||
|
||||
/// Get a cookie of a given name.
|
||||
public static String getCookie( HttpServletRequest req, String name )
|
||||
{
|
||||
String h = req.getHeader( "Cookie" );
|
||||
if ( h == null )
|
||||
return null;
|
||||
StringTokenizer st = new StringTokenizer( h, "; " );
|
||||
while ( st.hasMoreTokens() )
|
||||
{
|
||||
String tk = st.nextToken();
|
||||
int eq = tk.indexOf( '=' );
|
||||
String n, v;
|
||||
if ( eq == -1 )
|
||||
{
|
||||
n = tk;
|
||||
v = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
n = tk.substring( 0, eq );
|
||||
v = tk.substring( eq + 1 );
|
||||
}
|
||||
if ( name.equals( n ) )
|
||||
return v;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Set a cookie.
|
||||
public static void setCookie( HttpServletResponse res, String name, String value )
|
||||
{
|
||||
res.setHeader( "Set-Cookie", name + "=" + value );
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,132 +0,0 @@
|
|||
// TestServlet - simple servlet that tests the Servlet API
|
||||
//
|
||||
// Copyright (C) 1996,1998 by Jef Poskanzer <jef@acme.com>. All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions
|
||||
// are met:
|
||||
// 1. Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in the
|
||||
// documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
// SUCH DAMAGE.
|
||||
//
|
||||
// Visit the ACME Labs Java page for up-to-date versions of this and other
|
||||
// fine Java utilities: http://www.acme.com/java/
|
||||
|
||||
package Acme.Serve;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.http.*;
|
||||
|
||||
/// Simple servlet that tests the Servlet API.
|
||||
// Sample output:
|
||||
// <PRE>
|
||||
// getContentLength(): -1
|
||||
// getContentType(): null
|
||||
// getProtocol(): HTTP/1.0
|
||||
// getScheme(): http
|
||||
// getServerName(): www.acme.com
|
||||
// getServerPort(): 1234
|
||||
// getRemoteAddr(): 192.100.66.1
|
||||
// getRemoteHost(): acme.com
|
||||
// getMethod(): GET
|
||||
// getRequestURI(): http://www.acme.com:1234/TestServlet?foo=bar
|
||||
// getServletPath(): /TestServlet
|
||||
// getPathInfo(): null
|
||||
// getPathTranslated(): null
|
||||
// getQueryString(): foo=bar
|
||||
// getRemoteUser(): null
|
||||
// getAuthType(): null
|
||||
//
|
||||
// Parameters:
|
||||
// foo = bar
|
||||
//
|
||||
// Header:
|
||||
// accept: text/html, image/gif, image/jpeg, *; q=.2
|
||||
// user-agent: Java1.0.2
|
||||
// </PRE>
|
||||
// <A HREF="/resources/classes/Acme/Serve/TestServlet.java">Fetch the software.</A><BR>
|
||||
// <A HREF="/resources/classes/Acme.tar.gz">Fetch the entire Acme package.</A>
|
||||
|
||||
public class TestServlet extends HttpServlet
|
||||
{
|
||||
|
||||
/// Returns a string containing information about the author, version, and
|
||||
// copyright of the servlet.
|
||||
public String getServletInfo()
|
||||
{
|
||||
return "simple servlet that tests the Servlet API";
|
||||
}
|
||||
|
||||
/// Services a single request from the client.
|
||||
// @param req the servlet request
|
||||
// @param req the servlet response
|
||||
// @exception ServletException when an exception has occurred
|
||||
public void service( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException
|
||||
{
|
||||
Enumeration en;
|
||||
log( "called" );
|
||||
res.setStatus( HttpServletResponse.SC_OK );
|
||||
res.setContentType( "text/html" );
|
||||
ServletOutputStream p = res.getOutputStream();
|
||||
p.println( "<HTML><HEAD>" );
|
||||
p.println( "<TITLE>Test Servlet Output</TITLE>" );
|
||||
p.println( "</HEAD><BODY>" );
|
||||
p.println( "<H2>Test Servlet Output</H2>" );
|
||||
p.println( "<HR>" );
|
||||
p.println( "<PRE>" );
|
||||
p.println( "getContentLength(): " + req.getContentLength() );
|
||||
p.println( "getContentType(): " + req.getContentType() );
|
||||
p.println( "getProtocol(): " + req.getProtocol() );
|
||||
p.println( "getScheme(): " + req.getScheme() );
|
||||
p.println( "getServerName(): " + req.getServerName() );
|
||||
p.println( "getServerPort(): " + req.getServerPort() );
|
||||
p.println( "getRemoteAddr(): " + req.getRemoteAddr() );
|
||||
p.println( "getRemoteHost(): " + req.getRemoteHost() );
|
||||
p.println( "getMethod(): " + req.getMethod() );
|
||||
p.println( "getRequestURI(): " + req.getRequestURI() );
|
||||
p.println( "getServletPath(): " + req.getServletPath() );
|
||||
p.println( "getPathInfo(): " + req.getPathInfo() );
|
||||
p.println( "getPathTranslated(): " + req.getPathTranslated() );
|
||||
p.println( "getQueryString(): " + req.getQueryString() );
|
||||
p.println( "getRemoteUser(): " + req.getRemoteUser() );
|
||||
p.println( "getAuthType(): " + req.getAuthType() );
|
||||
p.println( "" );
|
||||
p.println( "Parameters:" );
|
||||
en = req.getParameterNames();
|
||||
while ( en.hasMoreElements() )
|
||||
{
|
||||
String name = (String) en.nextElement();
|
||||
p.println( " " + name + " = " + req.getParameter( name ) );
|
||||
}
|
||||
p.println( "" );
|
||||
p.println( "Headers:" );
|
||||
en = req.getHeaderNames();
|
||||
while ( en.hasMoreElements() )
|
||||
{
|
||||
String name = (String) en.nextElement();
|
||||
p.println( " " + name + ": " + req.getHeader( name ) );
|
||||
}
|
||||
p.println( "</PRE>" );
|
||||
p.println( "<HR>" );
|
||||
p.println( "</BODY></HTML>" );
|
||||
p.flush();
|
||||
p.close();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,55 +0,0 @@
|
|||
// ThrottleItem - data item for ThrottledOutputStream
|
||||
//
|
||||
// Copyright (C) 1996 by Jef Poskanzer <jef@acme.com>. All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions
|
||||
// are met:
|
||||
// 1. Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in the
|
||||
// documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
// SUCH DAMAGE.
|
||||
//
|
||||
// Visit the ACME Labs Java page for up-to-date versions of this and other
|
||||
// fine Java utilities: http://www.acme.com/java/
|
||||
|
||||
package Acme.Serve;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
/// Data item for ThrottledOutputStream.
|
||||
// <P>
|
||||
// <A HREF="/resources/classes/Acme/Serve/ThrottleItem.java">Fetch the software.</A><BR>
|
||||
// <A HREF="/resources/classes/Acme.tar.gz">Fetch the entire Acme package.</A>
|
||||
|
||||
public class ThrottleItem
|
||||
{
|
||||
|
||||
private long maxBps;
|
||||
|
||||
/// Constructor.
|
||||
public ThrottleItem( long maxBps )
|
||||
{
|
||||
this.maxBps = maxBps;
|
||||
}
|
||||
|
||||
public long getMaxBps()
|
||||
{
|
||||
return maxBps;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,156 +0,0 @@
|
|||
// ThrottledOutputStream - output stream with throttling
|
||||
//
|
||||
// Copyright (C)1996,1998 by Jef Poskanzer <jef@acme.com>. All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions
|
||||
// are met:
|
||||
// 1. Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in the
|
||||
// documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
// SUCH DAMAGE.
|
||||
//
|
||||
// Visit the ACME Labs Java page for up-to-date versions of this and other
|
||||
// fine Java utilities: http://www.acme.com/java/
|
||||
|
||||
package Acme.Serve;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
/// Output stream with throttling.
|
||||
// <P>
|
||||
// Restricts output to a specified rate. Also includes a static utility
|
||||
// routine for parsing a file of throttle settings.
|
||||
// <P>
|
||||
// <A HREF="/resources/classes/Acme/Serve/ThrottledOutputStream.java">Fetch the software.</A><BR>
|
||||
// <A HREF="/resources/classes/Acme.tar.gz">Fetch the entire Acme package.</A>
|
||||
|
||||
public class ThrottledOutputStream extends FilterOutputStream
|
||||
{
|
||||
|
||||
/// Parses a standard throttle file.
|
||||
// <P>
|
||||
// A throttle file lets you set maximum byte rates on filename patterns.
|
||||
// The format of the throttle file is very simple. A # starts a
|
||||
// comment, and the rest of the line is ignored. Blank lines are ignored.
|
||||
// The rest of the lines should consist of a pattern, whitespace, and a
|
||||
// number. The pattern is a simple shell-style filename pattern, using
|
||||
// ? and *, or multiple such patterns separated by |.
|
||||
// <P>
|
||||
// The numbers in the file are byte rates, specified in units of bytes
|
||||
// per second. For comparison, a v.32b/v.42b modem gives about
|
||||
// 1500/2000 B/s depending on compression, a double-B-channel ISDN line
|
||||
// about 12800 B/s, and a T1 line is about 150000 B/s.
|
||||
// <P>
|
||||
// Example:
|
||||
// <BLOCKQUOTE><PRE>
|
||||
// # throttle file for www.acme.com
|
||||
// * 100000 # limit total web usage to 2/3 of our T1
|
||||
// *.jpg|*.gif 50000 # limit images to 1/3 of our T1
|
||||
// *.mpg 20000 # and movies to even less
|
||||
// jef/* 20000 # jef's pages are too popular
|
||||
// </PRE></BLOCKQUOTE>
|
||||
// <P>
|
||||
// The routine returns a WildcardDictionary. Do a lookup in this
|
||||
// dictionary using a filename, and you'll get back a ThrottleItem
|
||||
// containing the corresponding byte rate.
|
||||
public static Acme.WildcardDictionary parseThrottleFile( String filename ) throws IOException
|
||||
{
|
||||
Acme.WildcardDictionary wcd = new Acme.WildcardDictionary();
|
||||
BufferedReader br = new BufferedReader( new FileReader( filename ) );
|
||||
while ( true )
|
||||
{
|
||||
String line = br.readLine();
|
||||
if ( line == null )
|
||||
break;
|
||||
int i = line.indexOf( '#' );
|
||||
if ( i != -1 )
|
||||
line = line.substring( 0, i );
|
||||
line = line.trim();
|
||||
if ( line.length() == 0 )
|
||||
continue;
|
||||
String[] words = Acme.Utils.splitStr( line );
|
||||
if ( words.length != 2 )
|
||||
throw new IOException( "malformed throttle line: " + line );
|
||||
try
|
||||
{
|
||||
wcd.put(
|
||||
words[0], new ThrottleItem( Long.parseLong( words[1] ) ) );
|
||||
}
|
||||
catch ( NumberFormatException e )
|
||||
{
|
||||
throw new IOException(
|
||||
"malformed number in throttle line: " + line );
|
||||
}
|
||||
}
|
||||
br.close();
|
||||
return wcd;
|
||||
}
|
||||
|
||||
|
||||
private long maxBps;
|
||||
private long bytes;
|
||||
private long start;
|
||||
|
||||
/// Constructor.
|
||||
public ThrottledOutputStream( OutputStream out, long maxBps )
|
||||
{
|
||||
super( out );
|
||||
this.maxBps = maxBps;
|
||||
bytes = 0;
|
||||
start = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
private byte[] oneByte = new byte[1];
|
||||
|
||||
/// Writes a byte. This method will block until the byte is actually
|
||||
// written.
|
||||
// @param b the byte to be written
|
||||
// @exception IOException if an I/O error has occurred
|
||||
public void write( int b ) throws IOException
|
||||
{
|
||||
oneByte[0] = (byte) b;
|
||||
write( oneByte, 0, 1 );
|
||||
}
|
||||
|
||||
/// Writes a subarray of bytes.
|
||||
// @param b the data to be written
|
||||
// @param off the start offset in the data
|
||||
// @param len the number of bytes that are written
|
||||
// @exception IOException if an I/O error has occurred
|
||||
public void write( byte b[], int off, int len ) throws IOException
|
||||
{
|
||||
// Check the throttle.
|
||||
bytes += len;
|
||||
long elapsed = System.currentTimeMillis() - start;
|
||||
long bps = bytes * 1000L / elapsed;
|
||||
if ( bps > maxBps )
|
||||
{
|
||||
// Oops, sending too fast.
|
||||
long wakeElapsed = bytes * 1000L / maxBps;
|
||||
try
|
||||
{
|
||||
Thread.sleep( wakeElapsed - elapsed );
|
||||
}
|
||||
catch ( InterruptedException ignore ) {}
|
||||
}
|
||||
|
||||
// Write the bytes.
|
||||
out.write( b, off, len );
|
||||
}
|
||||
|
||||
}
|
|
@ -1,215 +0,0 @@
|
|||
// FileServlet - servlet similar to a standard httpd
|
||||
//
|
||||
// Copyright (C)1996,1998 by Jef Poskanzer <jef@acme.com>. All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions
|
||||
// are met:
|
||||
// 1. Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in the
|
||||
// documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
// SUCH DAMAGE.
|
||||
//
|
||||
// Visit the ACME Labs Java page for up-to-date versions of this and other
|
||||
// fine Java utilities: http://www.acme.com/java/
|
||||
|
||||
package helma.servlet;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import java.text.*;
|
||||
import Acme.Serve.*;
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.http.*;
|
||||
|
||||
/// Servlet similar to a standard httpd.
|
||||
// <P>
|
||||
// Implements the "GET" and "HEAD" methods for files and directories.
|
||||
// Handles index.html.
|
||||
// Redirects directory URLs that lack a trailing /.
|
||||
// Handles If-Modified-Since and Range.
|
||||
// <P>
|
||||
// <A HREF="/resources/classes/Acme/Serve/FileServlet.java">Fetch the software.</A><BR>
|
||||
// <A HREF="/resources/classes/Acme.tar.gz">Fetch the entire Acme package.</A>
|
||||
// <P>
|
||||
// @see Acme.Serve.Serve
|
||||
|
||||
public class AcmeFileServlet extends FileServlet
|
||||
{
|
||||
|
||||
private File root;
|
||||
|
||||
|
||||
/// Constructor.
|
||||
public AcmeFileServlet(File root)
|
||||
{
|
||||
super ();
|
||||
this.root = root;
|
||||
}
|
||||
|
||||
public void init (ServletConfig config) throws ServletException {
|
||||
super.init (config);
|
||||
// do nothing
|
||||
}
|
||||
|
||||
|
||||
/// Services a single request from the client.
|
||||
// @param req the servlet request
|
||||
// @param req the servlet response
|
||||
// @exception ServletException when an exception has occurred
|
||||
public void service( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException
|
||||
{
|
||||
boolean headOnly;
|
||||
if ( req.getMethod().equalsIgnoreCase( "get" ) )
|
||||
headOnly = false;
|
||||
else if ( ! req.getMethod().equalsIgnoreCase( "head" ) )
|
||||
headOnly = true;
|
||||
else
|
||||
{
|
||||
res.sendError( HttpServletResponse.SC_NOT_IMPLEMENTED );
|
||||
return;
|
||||
}
|
||||
|
||||
String path = req.getServletPath();
|
||||
if ( path == null || path.charAt( 0 ) != '/' )
|
||||
{
|
||||
res.sendError( HttpServletResponse.SC_BAD_REQUEST );
|
||||
return;
|
||||
}
|
||||
if ( path.indexOf( "/../" ) != -1 || path.endsWith( "/.." ) )
|
||||
{
|
||||
res.sendError( HttpServletResponse.SC_FORBIDDEN );
|
||||
return;
|
||||
}
|
||||
|
||||
// Make a version without the leading /.
|
||||
String pathname = path.substring( 1 );
|
||||
if ( pathname.length() == 0 )
|
||||
pathname = "./";
|
||||
|
||||
dispatchPathname( req, res, headOnly, path, pathname );
|
||||
}
|
||||
|
||||
|
||||
protected void dispatchPathname( HttpServletRequest req, HttpServletResponse res, boolean headOnly, String path, String pathname ) throws IOException
|
||||
{
|
||||
String filename = pathname.replace( '/', File.separatorChar );
|
||||
if ( filename.charAt( filename.length() - 1 ) == File.separatorChar )
|
||||
filename = filename.substring( 0, filename.length() - 1 );
|
||||
if (filename.startsWith ("static"))
|
||||
filename = filename.substring ( Math.min (7, filename.length()) );
|
||||
|
||||
File file = new File( root, filename );
|
||||
|
||||
if ( file.exists() )
|
||||
{
|
||||
if ( ! file.isDirectory() )
|
||||
serveFile( req, res, headOnly, path, filename, file );
|
||||
else
|
||||
{
|
||||
if ( pathname.charAt( pathname.length() - 1 ) != '/' )
|
||||
redirectDirectory( req, res, path, file );
|
||||
else
|
||||
{
|
||||
String indexFilename =
|
||||
filename + File.separatorChar + "index.html";
|
||||
File indexFile = new File( indexFilename );
|
||||
if ( indexFile.exists() )
|
||||
serveFile(
|
||||
req, res, headOnly, path, indexFilename,
|
||||
indexFile );
|
||||
else
|
||||
serveDirectory(
|
||||
req, res, headOnly, path, filename, file );
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( pathname.endsWith( "/index.html" ) )
|
||||
dispatchPathname(
|
||||
req, res, headOnly, path,
|
||||
pathname.substring( 0, pathname.length() - 10 ) );
|
||||
else if ( pathname.equals( "index.html" ) )
|
||||
dispatchPathname( req, res, headOnly, path, "./" );
|
||||
else
|
||||
res.sendError( HttpServletResponse.SC_NOT_FOUND );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private void serveDirectory( HttpServletRequest req, HttpServletResponse res, boolean headOnly, String path, String filename, File file ) throws IOException
|
||||
{
|
||||
log( "indexing " + path );
|
||||
if ( ! file.canRead() )
|
||||
{
|
||||
res.sendError( HttpServletResponse.SC_FORBIDDEN );
|
||||
return;
|
||||
}
|
||||
res.setStatus( HttpServletResponse.SC_OK );
|
||||
res.setContentType( "text/html" );
|
||||
OutputStream out = res.getOutputStream();
|
||||
if ( ! headOnly )
|
||||
{
|
||||
PrintStream p = new PrintStream( new BufferedOutputStream( out ) );
|
||||
p.println( "<HTML><HEAD>" );
|
||||
p.println( "<TITLE>Index of " + path + "</TITLE>" );
|
||||
p.println( "</HEAD><BODY BGCOLOR=\"#ffffff\">" );
|
||||
p.println( "<H2>Index of " + path + "</H2>" );
|
||||
p.println( "<PRE>" );
|
||||
p.println( "mode bytes last-changed name" );
|
||||
p.println( "<HR>" );
|
||||
String[] names = file.list();
|
||||
Acme.Utils.sortStrings( names );
|
||||
for ( int i = 0; i < names.length; ++i )
|
||||
{
|
||||
String aFilename = filename + File.separatorChar + names[i];
|
||||
File aFile = new File( aFilename );
|
||||
String aFileType;
|
||||
if ( aFile.isDirectory() )
|
||||
aFileType = "d";
|
||||
else if ( aFile.isFile() )
|
||||
aFileType = "-";
|
||||
else
|
||||
aFileType = "?";
|
||||
String aFileRead = ( aFile.canRead() ? "r" : "-" );
|
||||
String aFileWrite = ( aFile.canWrite() ? "w" : "-" );
|
||||
String aFileExe = "-";
|
||||
String aFileSize = Acme.Fmt.fmt( aFile.length(), 8 );
|
||||
String aFileDate =
|
||||
Acme.Utils.lsDateStr( new Date( aFile.lastModified() ) );
|
||||
String aFileDirsuf = ( aFile.isDirectory() ? "/" : "" );
|
||||
String aFileSuf = ( aFile.isDirectory() ? "/" : "" );
|
||||
p.println(
|
||||
aFileType + aFileRead + aFileWrite + aFileExe +
|
||||
" " + aFileSize + " " + aFileDate + " " +
|
||||
"<A HREF=\"" + names[i] + aFileDirsuf + "\">" +
|
||||
names[i] + aFileSuf + "</A>" );
|
||||
}
|
||||
p.println( "</PRE>" );
|
||||
p.println( "<HR>" );
|
||||
ServeUtils.writeAddress( p );
|
||||
p.println( "</BODY></HTML>" );
|
||||
p.flush();
|
||||
}
|
||||
out.close();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Add table
Reference in a new issue