⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cgiframe.java

📁 很棒的web服务器源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// CgiFrame.java// $Id: CgiFrame.java,v 1.19 2003/04/30 20:08:23 ylafon Exp $// (c) COPYRIGHT MIT and INRIA, 1996.// Please first read the full copyright statement in file COPYRIGHT.htmlpackage org.w3c.jigsaw.frames ;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.PrintStream;import java.io.InputStreamReader;import java.io.BufferedReader;import java.util.Enumeration;import java.util.Hashtable;import java.util.Vector;import java.net.InetAddress;import java.net.MalformedURLException;import java.net.URL;import org.w3c.tools.resources.Attribute;import org.w3c.tools.resources.AttributeHolder;import org.w3c.tools.resources.AttributeRegistry;import org.w3c.tools.resources.BooleanAttribute;import org.w3c.tools.resources.FileResource;import org.w3c.tools.resources.FramedResource;import org.w3c.tools.resources.InvalidParentException;import org.w3c.tools.resources.LookupResult;import org.w3c.tools.resources.LookupState;import org.w3c.tools.resources.PropertiesAttribute;import org.w3c.tools.resources.ProtocolException;import org.w3c.tools.resources.Resource;import org.w3c.tools.resources.ResourceException;import org.w3c.tools.resources.ResourceFrame;import org.w3c.tools.resources.ServerInterface;import org.w3c.tools.resources.StringArrayAttribute;import org.w3c.tools.resources.StringAttribute;import org.w3c.tools.resources.ProtocolException;import org.w3c.www.mime.MimeHeaderHolder;import org.w3c.www.mime.MimeParser;import org.w3c.www.mime.MimeParserException;import org.w3c.www.mime.MimeParserFactory;import org.w3c.www.mime.MimeType;import org.w3c.jigsaw.http.Client;import org.w3c.jigsaw.http.HTTPException;import org.w3c.jigsaw.http.Reply;import org.w3c.jigsaw.http.Request;import org.w3c.jigsaw.http.httpd;import org.w3c.www.http.HTTP;import org.w3c.www.http.HeaderDescription;import org.w3c.www.http.HttpEntityMessage;import org.w3c.www.http.HttpEntityTag;import org.w3c.www.http.HttpMessage;import org.w3c.www.http.HttpReplyMessage;import org.w3c.www.http.HttpRequestMessage;import org.w3c.jigsaw.auth.AuthFilter;import org.w3c.util.ArrayDictionary;/** * Parsing the CGI output - The CGIHeaderHolder, to hold CGI headers. */class CGIHeaderHolder implements MimeHeaderHolder {    // Status and Location deserve special treatments:    String status   = null;    String location = null;    // Anyway, he is going to pay for using CGI    Hashtable headers = null;    // The MIME parse we are attached to:    MimeParser parser = null;    /**     * The parsing is now about to start, take any appropriate action.     * This hook can return a <strong>true</strong> boolean value to enforce     * the MIME parser into transparent mode (eg the parser will <em>not</em>     * try to parse any headers.     * <p>This hack is primarily defined for HTTP/0.9 support, it might     * also be usefull for other hacks.     * @param parser The Mime parser.     * @return A boolean <strong>true</strong> if the MimeParser shouldn't     * continue the parsing, <strong>false</strong> otherwise.     * @exception IOException if an IO error occurs.     */    public boolean notifyBeginParsing(MimeParser parser)	throws IOException    {	return false;    }    /**     * All the headers have been parsed, take any appropriate actions.     * @param parser The Mime parser.     * @exception IOException if an IO error occurs.     */    public void notifyEndParsing(MimeParser parser)	throws IOException    {	return ;    }    /**     * A new header has been emited by the script.     * If the script is not an NPH, then it <strong>must</strong> at least     * emit one header, so we are safe here, although people may not be safe      * against the spec.     * @param name The header name.     * @param buf The header bytes.     * @param off The begining of the value bytes  in above buffer.     * @param len The length of the value bytes  in above buffer.     * @exception MimeParserException if parsing failed.     */    public void notifyHeader(String name, byte buf[], int off, int len)	throws MimeParserException    {	if ( name.equalsIgnoreCase("status") ) {	    status = new String(buf, 0, off, len);	} else if ( name.equalsIgnoreCase("location") ) {	    location = new String(buf, 0, off, len);	} else {	    String extraval =  new String(buf, 0, off, len);	    if ( headers == null ) {		headers = new Hashtable(11);	    } else {		String val = (String) headers.get(name.toLowerCase());		if ( val != null )		    extraval = val + "," + extraval;	    }	    headers.put(name.toLowerCase(), extraval);	}    }    /**     * Get the status emited by the script.     */    public String getStatus() {	return status;    }    /**     * Get the location header value emited by the script.     */    public String getLocation() {	return location;    }    /**     * Get any header value (except status and location).     * @param name The name of the header to fetch.     * @return The string value of requested header, or <strong>null</strong>     * if header was not defined.     */    public String getValue(String name) {	return (headers == null) ? null : (String) headers.get(name);    }    /**     * Enumerate the headers defined by the holder.     * @return A enumeration of header names, or <strong>null</strong> if no     * header is defined.     */    public Enumeration enumerateHeaders() {	if ( headers == null )	    return null;	return headers.keys();    }    /**     * Get the remaining output of the stream.     * This should be called only once header parsing is done.     */    public InputStream getInputStream() {	return parser.getInputStream();    }    CGIHeaderHolder(MimeParser parser) {	this.parser = parser;    }}/** * Parsing the CGI output - Always create a CGIHeaderHolder. */class CGIHeaderHolderFactory implements MimeParserFactory {    /**     * Create a new header holder to hold the parser's result.     * @param parser The parser that has something to parse.     * @return A MimeParserHandler compliant object.     */    public MimeHeaderHolder createHeaderHolder(MimeParser parser) {	return new CGIHeaderHolder(parser);    }    CGIHeaderHolderFactory() {    }}/** * A simple process feeder class. */class ProcessFeeder extends Thread {    Process      proc    = null ;    OutputStream out     = null ;    InputStream  in      = null ;    int          count = -1 ;    public void run () {	try {	    byte buffer[] = new byte[4096] ;	    int  got      = -1 ;	    	    // Send the data to the target process:	    if ( count >= 0 ) {		while ( (count > 0) && ((got = in.read(buffer)) > 0) ) {		    out.write (buffer, 0, got) ;		    count -= got ;		}	    } else {		while ( (got = in.read(buffer)) > 0 ) {		    out.write (buffer, 0, got) ;		}	    }	} catch (Exception e) {	    System.out.println ("ProcessFeeder: caught exception !") ;	    e.printStackTrace() ;	} finally {	    // Clean up the process:	    try { out.flush() ; } catch (IOException ex) {}	    try { out.close() ; } catch (IOException ex) {}	    try { proc.waitFor() ; } catch (Exception ex) {}	}    }	    ProcessFeeder (Process proc, InputStream in) {	this (proc, in, -1) ;    }	    ProcessFeeder (Process proc, InputStream in, int count) {	this.proc   = proc ;	this.out    = proc.getOutputStream() ;	this.in     = in ;	this.count  = count ;    }}/** * Handle CGI error stream */class ProcessErrorReader extends Thread {    BufferedReader r = null;        public void run() {	String errline = null;	try {	    errline = r.readLine();	    if (errline != null) {		System.err.println("*** CgiError: " + errline);	    }	} catch (Exception ex) {	} finally {	    try { r.close(); } catch (IOException ioex) {}	}    }        ProcessErrorReader(Process proc) {	r = new BufferedReader(new InputStreamReader(proc.getErrorStream()));    }}/** * Handle CGI scripts. */public class CgiFrame extends HTTPFrame {    private final static 	String STATE_EXTRA_PATH = "org.w3c.jigsaw.frames.CgiFrame.extraPath";    /**     * Attribute index - The interpreter to use, if any.     */    protected static int ATTR_INTERPRETER = -1;    /**     * Attribute index - The array of string that makes the command to run.     */    protected static int ATTR_COMMAND = -1 ;    /**     * Attribute index - Does the script takes care of its headers ?     */    protected static int ATTR_NOHEADER = -1 ;    /**     * Attribute index - Does the script generates the form on GET ?     */    protected static int ATTR_GENERATES_FORM = -1 ;    /**     * Attribute index - Do DNS, to fill in REMOTE_HOST env var.     */    protected static int ATTR_REMOTE_HOST = -1;    /**     * Attribute index - Turn the script in debug mode.     */    protected static int ATTR_CGI_DEBUG = -1;    /**     * Attribute index - Additional environment vars     */    protected static int ATTR_ENV = -1;    static {	Attribute a   = null ;	Class     cls = null ;	try {	    cls = Class.forName("org.w3c.jigsaw.frames.CgiFrame") ;	} catch (Exception ex) {	    ex.printStackTrace() ;	    System.exit(1) ;	}	// The interpreter attribute:	a = new StringAttribute("interpreter"				, null				, Attribute.EDITABLE);	ATTR_INTERPRETER = AttributeRegistry.registerAttribute(cls, a);	// The command attribute:	a = new StringArrayAttribute("command"				     , null				     , Attribute.MANDATORY|Attribute.EDITABLE);	ATTR_COMMAND = AttributeRegistry.registerAttribute(cls, a) ;	// The noheader attribute:	a = new BooleanAttribute("noheader"				 , Boolean.FALSE				 , Attribute.EDITABLE) ;	ATTR_NOHEADER = AttributeRegistry.registerAttribute(cls, a) ;	// The generates form attribute	a = new BooleanAttribute("generates-form"				 , Boolean.TRUE				 , Attribute.EDITABLE) ;	ATTR_GENERATES_FORM = AttributeRegistry.registerAttribute(cls, a);	// Registerr the DODNS attribute.	a = new BooleanAttribute("remote-host"				 , null				 , Attribute.EDITABLE);	ATTR_REMOTE_HOST = AttributeRegistry.registerAttribute(cls, a);	// Register the debug mode flag:	a = new BooleanAttribute("cgi-debug"				 , Boolean.FALSE				 , Attribute.EDITABLE);	ATTR_CGI_DEBUG = AttributeRegistry.registerAttribute(cls, a);	// The env attribute:	a = new PropertiesAttribute("environment"				    , null				    , Attribute.EDITABLE);	ATTR_ENV = AttributeRegistry.registerAttribute(cls, a) ;    }    /**     * Get the interpreter to use to execute the script.     * This is most usefull for operating systems that don't have a     * <code>!#</code> convention ala UNIX.     * @return The interpreter to run the script.     */    public String getInterpreter() {	return getString(ATTR_INTERPRETER, null);    }    /**     * Get the command string array.     */    public String[] getCommand() {	return (String[]) getValue(ATTR_COMMAND, null) ;    }    public ArrayDictionary getUserEnv() {	return (ArrayDictionary) getValue(ATTR_ENV, null);    }    /**     * Get the noheader flag.     * @return The boolean value of the noheader flag.     */    public boolean checkNoheaderFlag() {	return getBoolean(ATTR_NOHEADER, false) ;    }    /**     * Get the generates form flag.     * @return The boolean value of the generates form flag.     */    public boolean checkGeneratesFormFlag() {	return getBoolean(ATTR_GENERATES_FORM, true) ;    }    /**     * Get the remote host attribute value.     * If turned on, this flag will enable the REMOTE_HOST env var computation.     * @return A boolean.     */    public boolean checkRemoteHost() {	return getBoolean(ATTR_REMOTE_HOST, false);    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -