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

📄 httpbase.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: HTTPBase.java,v 1.8 2003/04/23 19:43:49 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.*;

/**
 * HTTPBase
 *
 * @author <a href="mailto:frederik@noe.cx"> Frederik Noe </a>
 * @version <tt>$Revision: 1.8 $</tt>
 */

public class HTTPBase {
    
    public static final int NOT_AVAILABLE = 0;
    public static final int TEXT_HTML = 1;
    public static final int IMAGE_PNG = 2;
    public static final int IMAGE_GIF = 3;
    public static final int APPLICATION_JAVASCRIPT = 4;
    
    static final int CRLF_LENGTH = 2;
    static final String CRLF = "\r\n";
    
    static final String[] contentTypes = {
        "text/html",
        "image/png",
        "image/gif",
        "application/x-javascript"
    };
    
    
    /**
     *  Updates HTTP header values or adds the key/value pair if the header is missing at all
     *  @param response the HTTP message
     *  @param key the HTTP header key
     *  @param value the HTTP header value
     *  @return the updated HTTP response
     **/
    static String setValue(String msg, String key, String value) {
        
         int patternFlags = Pattern.CASE_INSENSITIVE;
        
        //find the header indicated by the key
        String ptr = "("+key + ": )(.+)(\\r\\n)";
        StringBuffer tmp = new StringBuffer();
        
        Pattern pattrn = Pattern.compile(ptr, patternFlags);
        Matcher matcher = pattrn.matcher(msg);

        // do the necessary replacements
        if (matcher.find()) {
            
            String firstgrp = matcher.group(1);
            String secondgrp = matcher.group(2);
            String thirdgrp = matcher.group(3);
            
            matcher.appendReplacement( tmp, "$1"+value+"$3");
            matcher.appendTail(tmp);
            return tmp.toString();
        }
        // we did not find a match, so let's add a complete new key/value pair
        // first find the end of the header section
        int start;
        if((start = msg.indexOf(CRLF + CRLF)) == -1)
            return msg;
        
        // let's create the key/value string and add it into the header section of the response
        String newheader = key+ ": "+value;
        tmp = new StringBuffer(msg.substring(0,start));
        tmp.append(CRLF);
        tmp.append(newheader);
        tmp.append(msg.substring(start));
        
        return tmp.toString();
    }
    
    /**
     *  Retrieves a value for a header key in the specified HTTP message
     *  @param request the HTTP message
     **/
    static String getValue(String msg, String key) {
        //find the header indicated by the key
        // we take care of lower case and upper case keys by lowering them all
        String ptr = key.toLowerCase() + ": (.+)\\r\\n";
        String tmp = null;
        
        Pattern pattrn = Pattern.compile(ptr);
        Matcher matcher = pattrn.matcher(msg.toLowerCase());
        
        // isolate the value group and return it
        if (matcher.find()) {
            tmp = matcher.group(1);
            return tmp;
        }
        return null;
    }
}

⌨️ 快捷键说明

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