tomcat 7 源码分析-11 tomcat对http协议的实现

Implementation of InputBuffer which provides HTTP request header parsing as well as transfer decoding

 

socket能获得客户端发来的http协议,tomcat需要对http协议(传输的是byte流)进行解析,例如获得http的method,protocol,URI等信息.

既然是对byte流进行处理,tomcat封装了InternalInputBuffer。

public class InternalInputBuffer extends AbstractInputBuffer

 核心函数为

public boolean parseRequestLine(boolean useAvailableDataOnly)

public boolean parseHeader()

有多个header,循环处理

    public boolean parseHeaders()
        throws IOException {

        while (parseHeader()) {
        }

        parsingHeader = false;
        end = pos;
        return true;
    }

 一个简单例子:

改写分析9中的例子

	protected boolean processSocket(Socket socket) throws IOException {
		
		InputStream inputsteam = socket.getInputStream();		
		Request request = new Request();
		InternalInputBuffer inputBuffer = new InternalInputBuffer(request);
		inputBuffer.setInputStream(inputsteam);
		
        request.setInputBuffer(inputBuffer);
        
        inputBuffer.parseRequestLine(false);
        
        System.out.println("@@@protocol="+request.protocol().toString());
        System.out.println("@@@method="+request.method().toString());
        /*
		BufferedReader in = new BufferedReader(new InputStreamReader(
				inputsteam));
		String inputLine;
		while ((inputLine = in.readLine()) != null) {	
		    System.out.println(inputLine);
		}
		
		*/
		return true;
	}

 在浏览器输入:localhost:8080/,测试结果

你可能感兴趣的:(tomcat,socket,浏览器)