forwardframe.java

来自「很棒的web服务器源代码」· Java 代码 · 共 639 行 · 第 1/2 页

JAVA
639
字号
	Reply reply = request.makeReply(rep.getStatus());	// get rid of "by default" headers wchich SHOULD NOT be modified	reply.setHeaderValue(Reply.H_SERVER, null);        // Duplicate reply header values:	Enumeration e = rep.enumerateHeaderDescriptions();	while ( e.hasMoreElements() ) {	    HeaderDescription d = (HeaderDescription) e.nextElement();	    HeaderValue       v = rep.getHeaderValue(d);	    if ( v != null )		reply.setHeaderValue(d, v);	}	// Get rid of hop by hop headers:	reply.setHeaderValue(Reply.H_CONNECTION, null);	reply.setHeaderValue(Reply.H_PROXY_CONNECTION, null);	reply.setHeaderValue(Reply.H_PROXY_AUTHENTICATE, null);	reply.setHeaderValue(Reply.H_PUBLIC, null);	reply.setHeaderValue(Reply.H_TRANSFER_ENCODING, null);	reply.setHeaderValue(Reply.H_UPGRADE, null);	reply.setHeaderValue(Reply.H_TRAILER, null);	reply.removeHeader("keep-alive");	// Get rid of the fields enumerated in the connection header:	String conn[] = rep.getConnection();	if (conn != null) {	    for (int i = 0 ; i < conn.length ; i++) 		reply.removeHeader(conn[i]);	}	// check if nasty people are mixing Content-Length and chunking	if (reply.getContentLength() >= 0 ) {	    String te[] = rep.getTransferEncoding() ;	    if ( te != null ) {		for (int i = 0 ; i < te.length ; i++) {		    if (te[i].equals("chunked")) {			reply.setContentLength(-1);		    }		}	    }	}	// Update the via route:	reply.addVia(getVia());	// Update the reply output stream:	try {	    reply.setStream(rep.getInputStream());	} catch (Exception ex) {};	// if reply is using bad HTTP version we should close it	if (rep.getMajorVersion() == 0) {	    reply.setKeepConnection(false);	}	// if HTTP/1.0 and no Content-Length also...	if ((rep.getMajorVersion() == 1) && 	    (rep.getMajorVersion() == 0) &&	    (reply.getContentLength() == -1)) {	    reply.setKeepConnection(false);	}	// check the age	int age = rep.getAge();	if (age >= 0 ) {	    // check if it is an heuristic expiration without a warning	    if (age > 86400) {		if ((rep.getExpires() == -1) && (rep.getMaxAge() == -1) &&		    (rep.getSMaxAge() == -1)) {		    // rfc2616: 13.2.4 on behalf of a bad upstream server		    HttpWarning w[] = rep.getWarning();		    boolean doit = true;		    if (w != null) {			for (int i=0; doit && (i<w.length); i++) {			    doit = (w[i].getStatus() != 113);			}		    }		    if (doit) {			reply.addWarning(WARN_HEURISTIC);		    }		}	    }	}	reply.setProxy(true);	return reply;    }    /**     * Perform the given proxied request.     * @param request The request to perform.     * @return A Reply instance.     * @exception org.w3c.tools.resources.ProtocolException if processing     * the request failed.     * @exception org.w3c.tools.resources.ResourceException if the resource     * got a fatal error.     */    public ReplyInterface perform(RequestInterface ri) 	throws org.w3c.tools.resources.ProtocolException,	       org.w3c.tools.resources.ResourceException    {	Request request = (Request) ri;	Reply   reply  = null;	boolean stated = false;	// check the expectations	if (!checkExpect(request)) {	    reply = createDefaultReply(request, HTTP.EXPECTATION_FAILED);	    reply.setContent("The requested expectation could not be"+			     " met by the resource");	    return reply;	}	// Perform the request:	try {	    if (request.getMaxForwards() != -1) { // 14.31 decrement the value		if (request.getMaxForwards() == 0) {		    if (request.getMethod().equals("TRACE") ||			request.getMethod().equals("OPTIONS"))			return super.perform(request);		}	    } 	    org.w3c.www.protocol.http.Request     req = dupRequest(request);	    org.w3c.www.protocol.http.Reply       rep = null;	    // Perform the request	    rep = manager.runRequest(req);	    // Dump back the client reply into a server reply:	    reply = dupReply(request, rep);	    // Update statistics:	    updateStatistics(req);	    stated = true;	    // Trace the request (debug):	    if ( getTraceRequest() ) {//		if ( req.hasState(CacheFilter.STATE_HOW) ) {//		    System.out.println(request.getURL()+": "+//				       CacheFilter.getHow(req));//		}//		if ( req.hasState(micp) )//		    System.out.println(req.getURL()+": (icp) "+//				       req.getState(micp));	    }	} catch (HTTPException shex) { 	    // An server-side HTTP-related error happened	    if (debug) {		System.out.println("HTTP Server Exception while running"				   +" request:");		shex.printStackTrace();	    }	    // Make sure the request is accounted:	    if ( ! stated ) {		synchronized(this) {		    reqcount++;		    reqerred++;		}		stated = true;	    }	    Reply r = (Reply) shex.getReply();	    if (reply == null) {		// Send an appropriate error message back:		reply = request.makeReply(HTTP.BAD_GATEWAY);		boolean showerr = 		    props.getBoolean(httpd.DISPLAY_URL_ON_ERROR_P, false);				if (showerr) {		    reply.setContent("An HTTP error occured while getting: "				     + "<p><strong>"+request.getURL()				     + "</strong>"				     + "<p>Details \""+shex.getMessage()+"\"."				     + "<hr>Generated by <i>"				     + getServer().getURL());		} else {		    reply.setContent("An HTTP error occured while getting the "				     + "requested URI.</p>"				     + "<hr>Generated by <i>"				     + getServer().getURL());		}		reply.setContentType(org.w3c.www.mime.MimeType.TEXT_HTML);	    }	} catch (HttpInvalidValueException iex) { 	    // An server-side HTTP-related error happened	    if (debug) {		System.out.println("Parsing Exception while running"				   +" request:");		iex.printStackTrace();	    }	    // Make sure the request is accounted:	    if ( ! stated ) {		synchronized(this) {		    reqcount++;		    reqerred++;		}		stated = true;	    }	    reply = request.makeReply(HTTP.BAD_GATEWAY);	    boolean showerr = 		props.getBoolean(httpd.DISPLAY_URL_ON_ERROR_P, false);	    	    if (showerr) {		reply.setContent("An HTTP error occured while getting: "				 + "<p><strong>"+request.getURL()				 + "</strong>"				 + "<p>Details \""+iex.getMessage()+"\"."				 + "<hr>Generated by <i>"				 + getServer().getURL());	    } else {		reply.setContent("An HTTP error occured while getting the "				 + "requested URI.</p>"				 + "<hr>Generated by <i>"				 + getServer().getURL());	    }	    reply.setContentType(org.w3c.www.mime.MimeType.TEXT_HTML);	} catch (HttpException hex) { 	    // An HTTP-related error happened	    if (debug) {		System.out.println("HTTP Exception while running request:");		hex.printStackTrace();	    }	    // Make sure the request is accounted:	    if ( ! stated ) {		synchronized(this) {		    reqcount++;		    reqerred++;		}		stated = true;	    }	    org.w3c.www.protocol.http.Reply rep = hex.getReply();	    if (rep != null) {		try {		    reply = dupReply(request, rep);		} catch (IOException ioex) {		    reply = null;		}	    }	    if (reply == null) {		// Send an appropriate error message back:		String msg = hex.getMessage();		if (msg.startsWith("Unable to contact target server ")) {		    reply = request.makeReply(HTTP.GATEWAY_TIMEOUT);		} else {		    reply = request.makeReply(HTTP.BAD_GATEWAY);		}		boolean showerr = 		    props.getBoolean(httpd.DISPLAY_URL_ON_ERROR_P, false);				if (showerr) {		    reply.setContent("An HTTP error occured while getting: "				     + "<p><strong>"+request.getURL()				     + "</strong>"				     + "<p>Details \""+hex.getMessage()+"\"."				     + "<hr>Generated by <i>"				     + getServer().getURL());		} else {		    reply.setContent("An HTTP error occured while getting the "				     + "requested URI.</p>"				     + "<hr>Generated by <i>"				     + getServer().getURL());		}		reply.setContentType(org.w3c.www.mime.MimeType.TEXT_HTML);	    }	} catch (Exception ex) {	    // Debug trace (when debuggging):	    if ( debug ) {		System.out.println("Exception while running request:");		ex.printStackTrace();	    }	    // Make sure the request is accounted:	    if ( ! stated ) {		synchronized(this) {		    reqcount++;		    reqerred++;		}		stated = true;	    }	    // Send an appropriate error message back:	    reply = request.makeReply(HTTP.GATEWAY_TIMEOUT);	    boolean showerr = props.getBoolean(httpd.DISPLAY_URL_ON_ERROR_P, 					       false);	    	    if (showerr) {		reply.setContent("An HTTP error occured while getting: "				 + "<p><strong>"+request.getURL()+"</strong>"				 + "<p>Details \""+ex.getMessage()+"\"."				 + "<hr>Generated by <i>"				 + getServer().getURL());	    } else {		reply.setContent("An HTTP error occured while getting the "				 + "requested URI.</p>"				 + "<hr>Generated by <i>"				 + getServer().getURL());	    }	    reply.setContentType(org.w3c.www.mime.MimeType.TEXT_HTML);	}	return reply;    }    /**     * This resource is being unloaded.     * Tell the HttpManager to save any pending data to stable storage.     */    public synchronized void notifyUnload() {	if (manager != null) {	    manager.sync();	}	super.notifyUnload();    }    /**     * companion to initialize, called after the register     */    public void registerResource(FramedResource resource) {	super.registerOtherResource(resource);	this.props = getResource().getServer().getProperties();	synchronized(this.getClass()) {	    if (!inited) {		// Register the client side properties (if still needed):		httpd server = (httpd) getServer();		server.registerPropertySet(new ProxyProp("proxy", server));		inited = true;	    }	}	manager = org.w3c.www.protocol.http.HttpManager.getManager(props);    }    public void initialize(Object values[]) {	super.initialize(values) ;    }}

⌨️ 快捷键说明

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