深入剖析tomcat(一)--一個簡單的web服務器


一個簡單的web服務器

本系列博客為《深入Tomcat》一書的讀書筆記,我也是從這本書開始接觸Tomcat

  • http協議
  • socket
  • ServerSocket類
  • 應用程序

http協議

http使用的是可靠的Tcp連接,默認使用Tcp80端口,總是由客戶建立連接並發送http請求來初始化一個事物的。

http請求
一個http請求包含以下三個部分:

  • 請求方法——統一資源標識符(URI)——協議/版本
  • 請求頭
  • 實體

在請求頭和請求實體正文之間有一個空行,該空行只有CRLF符(回車換行符),這個空行對http請求格式非常重要

http響應
與http請求類似,http響應也包括三部分:


  • 協議(如htttp/1.1)——狀態碼——描述
  • 響應頭
  • 響應實體段

同樣,響應頭和響應實體正文之間由只包含CRLF的一個空行分隔。

socket類

創建一個簡單的socket例子用於與本地http服務器通信,發送http請求,接受服務器的響應信息:

 package firstSection;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class SocketTest {
    public static void main(String [] args){
        try {
            Socket socket=new Socket("127.0.0.1", 8080);
            OutputStream os=socket.getOutputStream();
            PrintWriter out=new PrintWriter(os, true);
            BufferedReader reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));

            //send a http request
            out.println("GET /index.jsp HTTP/1.1");
            out.println("Host: localhost:8080");
            out.println("Connection:close");
            out.println();

            //read the response
            StringBuffer s=new StringBuffer(8096);
            boolean loop=true;
            while(loop){
                if(reader.ready()){
                    int i=0;
                    while(i!=-1){
                        i=reader.read();
                        s.append((char)i);
                    }
                    loop=false;
                }
                Thread.currentThread().sleep(50);
            }
            System.out.print(s.toString());
            socket.close();

        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

注意防火牆可能會是程序失敗

ServerSocket類

構造函數:

new ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException//backlog表示服務器子啊拒絕接受傳入的請求之前,傳入請求的最大隊列長度,backlog 參數必須是大於 0 的正值。如果傳遞的值等於或小於 0,則使用默認值

應用程序

該程序僅僅發送位於指定目錄的的靜態資源請求,如html文件和圖片文件,他可以將接收的http請求字節流打印到控制台,但是並不能發送任何頭信息到瀏覽器,如日期和cookies等

HttpServer類:

package firstSection;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

public class HttpServer {

    /** * web_root is the directory where our html and other files reside. * for this package web_root is the "web_root"directory under the working directory * the working directory is the location in the file system from where the java command was invoke */

    public static final String WEB_ROOT=System.getProperty("user.dir")+File.separator+"webroot";

    public static final String SHUTDOWN_COMMAND="/SHUTDOWN";

    private boolean shutdown=false;

    public static void main(String []args){
        HttpServer server=new HttpServer();
        server.await();
    }
    public void await(){
        ServerSocket serverSocket=null;
        int port=8080;
        try {
            serverSocket=new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1"));
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        while(!shutdown){
            Socket socket=null;
            InputStream in=null;
            OutputStream out =null;
            try {
                socket=serverSocket.accept();
                in=socket.getInputStream();
                out=socket.getOutputStream();

                //parse request 
                Request request=new Request(in);
                request.parse();

                //create response obj
                Response response=new Response(out);
                response.setrequest(request);
                response.sendStaticResourse();

                //close the soclet
                socket.close();

                shutdown=request.geturi().equals(SHUTDOWN_COMMAND);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                continue;
            }

        }

    }
}

Request類:

package firstSection;

import java.io.IOException;
import java.io.InputStream;

public class Request {
    private InputStream in;
    private String uri;
    public Request(InputStream in){
        this.in=in;
    }
    public void parse(){
        StringBuffer req=new StringBuffer(2048);
        int i;
        byte[] buffer=new byte[2048];
        try {
            i=in.read(buffer);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            i=-1;
        }
        for(int j=0;j<i;j++){
            req.append((char)buffer[j]);
        }

        System.out.print(req.toString());
        uri=parseUri(req.toString());
    }
    private String parseUri(String requestString){
        int index1,index2;
        index1=requestString.indexOf(' ');
        if(index1!=-1){
            index2=requestString.indexOf(' ', index1+1);
            if(index2>index1){
                return requestString.substring(index1+1, index2);
            }
        }
        return null;
    }
    public String geturi(){
        return uri;

    }
}

Response類:

package firstSection;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Response {
    private Request request; 
    private OutputStream out=null;
    public static final int BUFFER_SIZE=1024;
    public Response(OutputStream out){
        this.out=out;
    }
    public void setrequest(Request request){
        this.request=request;
    }
    public void sendStaticResourse()throws IOException{
        FileInputStream fis=null;
        byte []bytes=new byte[BUFFER_SIZE];
        try {
            File file=new File(HttpServer.WEB_ROOT,request.geturi()); 
            if(file.exists()){
                fis=new FileInputStream(file);
                int re=fis.read(bytes, 0, BUFFER_SIZE);
                while(re!=-1){
                    out.write(bytes, 0, re);
                    re=fis.read(bytes, 0, BUFFER_SIZE);
                }   
            }
            else {
                String errorMessage="HTTP/1.1 404 File Not Found\r\n"+
                        "Content-type: text/html\r\n"+
                        "Content_Length:23\r\n"+
                        "\r\n"+
                        "<h1> file not found</h1>";
                out.write(errorMessage.getBytes());
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally {
            if(fis!=null){
                fis.close();
            }
        }

    }
}

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com