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

📄 request.java

📁 纯java服务器
💻 JAVA
字号:
// $Id: Request.java,v 1.11 2001/01/23 18:43:06 nconway Exp $
package tornado;
import java.net.*;
import java.io.*;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.NoSuchElementException;

public class Request {
    private final static int BUFFER_SIZE = 4096;
    private final static int HEADER_SIZE = 8;
    private final static int HEADER_LENGTH = 20;

    private final Socket socket;
    private final InputStreamReader input;
    private final HashMap headers = new HashMap(HEADER_SIZE);

    private float protocolVersion;
    private String requestURI;
    private String requestType;
    private String rawRequest;

    public Request(Socket client) throws IOException {
        socket = client;
        BufferedInputStream in =
                new BufferedInputStream(socket.getInputStream(), BUFFER_SIZE);
        input = new InputStreamReader(in, "ASCII");

        readHeaders();
    }

    private void readHeaders() throws IOException {
        // read the request line
        processRequest(readLine(input, true));

        // read the rest of the headers
        while (true) {
            String header = readLine(input, false);
            if (header.equals("")) {
                break;
            } else {
                processHeader(header);
            }
        }
    }

    private String readLine(Reader r, boolean multiLine) throws IOException {
        StringBuffer buffer = new StringBuffer(HEADER_LENGTH);
        while (true) {
            char c = (char)r.read();

            if (c == '\r') {
                continue;
            } else if (c == '\n') {
                if (buffer.length() == 0 && multiLine) {
                    continue;
                } else {
                    return buffer.toString();
                }
            } else if (c == -1) {
                return buffer.toString();
            } else {
                buffer.append(c);
            }
        }
    }

    private void processRequest(String request) {
        rawRequest = request;

        StringTokenizer st = new StringTokenizer(request);
        formatType(st);
        formatURI(st);
        formatProtocolVersion(st);
    }

    private void formatType(StringTokenizer st) {
        requestType = st.nextToken().toUpperCase();
    }

    private void formatURI(StringTokenizer st) {
        String uri = null;
        try {
            uri = URLDecoder.decode(st.nextToken());
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (! uri.startsWith("http://")) {
            uri = "http://localhost" + uri;
        }
        requestURI = uri;
    }

    private void formatProtocolVersion(StringTokenizer st) {
        /* Be careful: we need to handle legacy clients who do
         * not send an HTTP version.*/
        String str;
        try {
            str = st.nextToken();
        } catch (NoSuchElementException e) {
            str = "";
        }

        if (str.endsWith("1.1")) {
            protocolVersion = (float)1.1;
        } else if (str.endsWith("1.0")) {
            protocolVersion = (float)1.0;
        } else {
            protocolVersion = (float)0.9; //default to 0.9
        }
    }

    private void processHeader(String header) {
        int split = header.indexOf(':', 3);
        if (split == -1)
            return; // ignore malformed headers

        String key = header.substring(0, split);
        String val = header.substring(split);
        headers.put(key, val);
    }

    public String getLogName() {
        return null;
    }

    public String getHostAddress() {
        return socket.getInetAddress().getHostAddress();
    }

    public int getPort() {
        return socket.getPort();
    }

    public float getProtocolVersion() {
        return protocolVersion;
    }

    public String getURI() {
        return requestURI;
    }

    public String getType() {
        return requestType;
    }

    // will never include CRLF
    public String getRawRequest() {
        return rawRequest;
    }

    public String getHeader(String key) {
        return (String)headers.get(key);
    }

    public Socket getSocket() {
        return socket;
    }

}

⌨️ 快捷键说明

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