Java implementation HTTP Proxy (1)

Source: Internet
Author: User
Tags http request

The key of the Java implementation HTTP Proxy is to forward the received HTTP request to the hosted host, and then forward the host's return data to the client

Key points:
1. HTTP protocol

2. Java Blocking IO

3. A key point in using Java blocking IO to forward HTTP requests is that forwarders must be able to correctly parse the received HTTP request content and forward it correctly based on the content of the request.

Here is a request: between-----------------------------------------------------is the true request content, with/r/n as the line terminator
-----------------------------------------------------
get/test/test.jsp http/1.1
Accept:image/gif, Image/x-xbitmap, Image/jpeg, Image/pjpeg, Application/x-shockwave-flash, application/vnd.ms-excel , Application/vnd.ms-powerpoint, Application/msword, */*
Accept-language:zh-cn
Accept-encoding:gzip, deflate
user-agent:mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
host:localhost:7272
Connection:keep-alive

-----------------------------------------------------
When Java IO reads to the end of the request, because blocking IO causes the read to wait, this time, the forwarder, Must be able to parse the above content, according to Content-length decided to read where the end, if there is no content-length, then Content-length 0, that is not read.
to make it easier to operate on this request, we encapsulate a simple HttpRequest object:

* * $RCSfile: httprequest.java,v $$ * $Revision: 1.1 $ * $Date: 2007-4-15 $ * * Copyright (C) 2008 Skin, Inc.
 All rights reserved.
 * * This software is the proprietary information of Skin, Inc.
 * Use are subject to license terms.

* * Package com.skin.taurus.http;
Import Java.io.InputStream;
Import Java.io.OutputStream;

Import java.io.UnsupportedEncodingException;
 /** * <p>Title:HttpRequest</p> * <p>description: </p> * @author xuesong.net * @version 1.0
    * * Public class HttpRequest {private String method;
    Private String RequestUri;
    Private String Requesturl;
    Private String querystring;

    Private String Httpprotocol;
    Private String localaddr;

    private int serverport =-1;
    Private String remoteaddr;
    Private String remotehost;
    private int remoteport =-1;
    Private String characterencoding;
    Private Httpheader Httpheader;
    Private InputStream InputStream;

    Private OutputStream OutputStream; PUblic HttpRequest () {this.httpheader = new Httpheader ();
    Public String Getcontextpath () {return "/"; }/** * @param encoding */public void setcharacterencoding (String encoding) throws Unsupportedencoding
    Exception {this.characterencoding = encoding; }/** * @return String */public string getcharacterencoding () {return this.characterencod
    ing }/** * @return Httpheader */public void Sethttpheader (Httpheader httpheader) {this.httph
    Eader = Httpheader;
    /** * @return Httpheader * * Public httpheader Gethttpheader () {return this.httpheader;
        }/** * @param name * @param value */public void AddHeader (string name, String value) {
    This.httpHeader.addHeader (name, value); }/** * @param name * @param value */public void SetHeader (String name, StrinG value) {This.httpHeader.setHeader (name, value); }/** * @param name * @return-string/public string GetHeader (string name) {Retur
    n This.httpHeader.getHeader (name);
        }/** * @param name * @return-string * * Public string[] Getheadervalues (String name) {
    return this.httpHeader.getHeaderValues (name);
    }/** * @return-string */public String Gethttpprotocol () {return httpprotocol; }/** * @param httpprotocol */public void Sethttpprotocol (String httpprotocol) {this.http
    Protocol = Httpprotocol;
    }/** * * @return-string/public String GetMethod () {return method;
    /** * @param method */public void Setmethod (String method) {This.method = method; }/** * @return-string */public String getquerystring () {REturn querystring; }/** * * @param querystring/public void setquerystring (String querystring) {This
    . QueryString = querystring;
    }/** * * @return-string/public String Getrequesturi () {return requesturi; }/** * * @param requesturi/public void Setrequesturi (String requesturi) {THIS.R
    Equesturi = RequestUri;
    }/** * * @return-string/public String Getrequesturl () {return requesturl; }/** * * @param requesturl/public void Setrequesturl (String requesturl) {THIS.R
    Equesturl = Requesturl; }/** * * @return-string/public String getContentType () {return (string) THIS.G
    Etheader ("Content-type")); /** * * @return-int/public int getcontentlength () {String value = (string) (th Is.getHeader ("Content-length"));

        int contentlength =-1;
        if (value = = null) {return contentlength;
        try {contentlength = Integer.parseint (value);
    catch (NumberFormatException e) {} return contentlength;
    Public String getlocaladdr () {return localaddr;
    } public void Setlocaladdr (String localaddr) {this.localaddr = localaddr;
    public int Getserverport () {return serverport;
    The public void setserverport (int serverport) {this.serverport = ServerPort;
    Public String getremoteaddr () {return remoteaddr;
    } public void Setremoteaddr (String remoteaddr) {this.remoteaddr = remoteaddr;
    Public String Getremotehost () {return remotehost; } public void Setremotehost (String remotehost) {this.remotehost = RemoteHost;
    public int Getremoteport () {return remoteport;
    The public void setremoteport (int remoteport) {this.remoteport = RemotePort;
    /** * @return-inputstream * * Public inputstream getInputStream () {return inputstream; }/** * @param inputstream */public void Setinputstream (InputStream inputstream) {thi
    S.inputstream = InputStream; /** * @return-outputstream/public OutputStream Getoutputstream () {return outputstre
    Am }/** * @param outputstream */public void Setoutputstream (OutputStream outputstream) {thi
    S.outputstream = OutputStream;  /** * @see java.lang.object#tostring () */public String toString () {StringBuilder buffer
        = new StringBuilder (); Buffer.append (This.method). Append (""). Append (This.getrequesturl ()). Append (""). Append (This.gethttpprotocol ()). Append ("\ r \ n");
        Buffer.append (This.httpHeader.toString ());
        Buffer.append ("\ r \ n");
    return buffer.tostring (); }
}


Complete project source Download address: https://github.com/xuesong123

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.