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

📄 httprequest.java

📁 轻量级Http代理服务器
💻 JAVA
字号:
/*
 * jRevProxy, an opensource Java reverse proxy server
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
  
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
  
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
 *
 * $Id: HTTPRequest.java,v 2.11 2003/03/28 16:01:04 fnoe Exp $ 
 */

package cx.noe.jrevproxy;

import java.util.Hashtable;
import java.net.URL;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

import java.io.*;

/**
 * HTTPRequest
 * 
 * @author <a href="mailto:frederik@noe.cx"> Frederik Noe </a>
 * @version <tt>$Revision: 2.11 $</tt>
 */
public class HTTPRequest extends HTTPBase{
     
    /**
     * Checks if the request complies with RFC 2068
     * 
     * @param request   the HTTPRequest string
     * @return true if request is a valid HTTP request; false otherwise
     **/
    public static boolean isHTTPRequest(String request) {
                       
        if(request.indexOf("GET") == -1 && request.indexOf("POST") == -1)
            return false;
        
        if(request.indexOf("HTTP/1.0") == -1){
            if(request.indexOf("HTTP/1.1") != -1) {
                // HTTP 1.1 requires the use of a Host key
                if(request.indexOf("Host: ") == -1)
                    return false;
            }
            else
                return false;
        }
        return true;
    }
    
    /**
     * Retrieves the hostname out of the HTTP request
     *
     * @param request   the HTTPRequest string
     * @return the hostname
     **/
    public static String getHostname(String request) {
        return getValue(request,"Host");
    }
    
    /**
     * Replaces the hostname in the HTTP request
     *
     * @param request   the HTTPRequest string
     * @param hostname  the hostname to be placed in the request
     **/
    public static String setHostname(String request, String hostname) {
        int hostStart = request.indexOf("Host: ");
        int hostEnd = request.indexOf(CRLF,hostStart);
        
        return request.substring(0,hostStart + 6) + hostname + request.substring(hostEnd);
    }
    
    /**
     * Retrieves the referer value in the HTTP request
     *
     * @param request   the HTTPRequest string
     **/
    public static String getReferer(String request) throws HTTPFormatException{
        int index = 0, offset = 0;
        
        if((index = request.indexOf("Referer: ")) == -1)
            return null;
        offset = 9;
        
        //our fullpath starts immediately after the method
        int fullpathStart = index + offset;
        
        // we search the end of the path
        int fullpathEnd = -1;
        
        if((fullpathEnd = request.indexOf(" ",fullpathStart)) == -1)
            throw new HTTPFormatException("Invalid HTTPRequest: no end of referer key");
        
        // we take the path out of the request string
        String tmp = request.substring(fullpathStart,fullpathEnd);
        
        //if the first character is not a '/' then we have a bad request
        if(tmp == null || tmp.length() == 0 )
            throw new HTTPFormatException("Invalid HTTPRequest: start virtual directory incorrect");
        
        return tmp;
    }
    
    /**
     * Retrieves the requested file out of the HTTP request
     *
     * @param request   the HTTPRequest string
     **/
    public static String getVirtualDir(String request) throws HTTPFormatException{
        int index = -1;
        int offset = 0;
       
        if((index = request.indexOf("GET")) != -1)
            offset = 4;
        else if((index = request.indexOf("POST")) != -1)
            offset = 5;
        else
           throw new HTTPFormatException("Invalid HTTPRequest: no request type info found");
        //our fullpath starts immediately after the method
        int fullpathStart = index + offset;
        
        // we search the end of the path
        int fullpathEnd = -1;
        
        if((fullpathEnd = request.indexOf(" ",fullpathStart)) == -1)
            throw new HTTPFormatException("Invalid HTTPRequest: no end of virtual directory");
        
        // we take the path out of the request string
        String tmp = request.substring(fullpathStart,fullpathEnd);
        
        //if the first character is not a '/' then we have a bad request
        if(tmp == null || tmp.length() == 0 || tmp.charAt(0) != '/')
            throw new HTTPFormatException("Invalid HTTPRequest: start virtual directory incorrect");
        
        int vdirEnd = -1;
        
        // let's find out where the next '/' is located
        if((vdirEnd = tmp.indexOf('/',1)) == -1) {
            // we do not have a second '/'
            // let's check if we do not have a extension in the request
            // if yes, then we have a request for a file
            int extStart = -1;
            if((extStart = tmp.indexOf('.',1)) == -1) {
                // no dot inthere -> vdir
                return tmp;
            }
            else
                return "/";
        }
        else
            // we remove the prefix '/'
            return tmp.substring(0,vdirEnd);
    }
    
    /**
     * Replaces the virtualdirectory in the HTTP request
     *
     * @param request   the HTTPRequest string
     * @param vdir  the virtual directory to be placed in the request
     * @return the modified HTTPRequest string with the new virtual directory
     **/
    public static String setVirtualDir(String request, String newVdir) throws HTTPFormatException {
        int index = -1;
        int offset = 0;
        
        if((index = request.indexOf("GET")) != -1)
            offset = 4;
        else if((index = request.indexOf("POST")) != -1)
            offset = 5;
        else
            throw new HTTPFormatException("Invalid HTTPRequest: no request type info found");
        
        // our virtual directory starts right behind the GET or POST method
        int vdirStart = index + offset;
        int pathEnd = request.indexOf(' ',vdirStart);
        //
        int vdirEnd = -1;
        if(((vdirEnd = request.indexOf('/', vdirStart+1)) == -1) ||
        ((vdirEnd != -1) && (vdirEnd > pathEnd))) {
            //the virtual directory does not end on a '/' or the '/' is not in the virtual directory
            
            int dotStart = -1;
            //check if we have '.' in our path -> file
            if(((dotStart = request.indexOf('.', vdirStart+1)) == -1) ||
            ((dotStart != -1) && (dotStart > pathEnd)))
                vdirEnd = pathEnd;
            else
                // we point to the beginning of the filename
                vdirEnd = vdirStart +1;
        }
        // we have a starting /
        else {
            // if we have a default virtual directory '/' then we do not need to place anything
            if(newVdir.length() == 1 && newVdir.charAt(0) == '/' && vdirEnd < pathEnd)
                newVdir = "";
        }
        return request.substring(0,vdirStart) + newVdir + request.substring(vdirEnd);
    }
    
    /**
     * Removes junk preceding the GET/POST keyword in HTTP request
     *
     * @param request the HTTPRequest string
     * @return the trimmed request string
     **/
    public static String trim(String request) throws HTTPFormatException{
        int index = -1;
              
        if((index = request.indexOf("GET")) != -1 || (index = request.indexOf("POST")) != -1) {
            // we have a GET or POST request
            // let's strip off what we do not need
            return request.substring(index);
        }
        throw new HTTPFormatException("Invalid HTTPRequest: no request type info found");
    }
}

⌨️ 快捷键说明

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