(一)Http请求、Http响应、 Socket

第一章:一个简单的Web服务器

    本章说明java web服务器是如何工作的。Web服务器也成为超文本传输协议(HTTP)服务器,因为它使用HTTP来跟客户端进行通信的,这通常是个web浏览器。

一个基于java的web服务器使用两个重要的类:java.net.Socket和java.net.ServerSocket,并通过HTTP消息进行通信。因此这章就自然是从HTTP和这两个类的讨论开始的。接下去,解释这章附带的一个简单的web服务器。

 

1.1超文本传输协议(HTTP)

    HTTP是一种协议,允许web服务器和浏览器通过互联网进行来发送和接受数据。它是一种请求和响应协议。客户端请求一个文件而服务器响应请求。HTTP使用可靠的TCP连接--TCP默认使用80端口。第一个HTTP版是HTTP/0.9,然后被HTTP/1.0所替代。正在取代HTTP/1.0的是当前版本HTTP/1.1,它定义于征求意见文档(RFC) 2616,可以从http://www.w3.org/Protocols/HTTP/1.1/rfc2616.pdf下载。

    注意:本节涵盖的HTTP 1.1只是简略的帮助你理解web服务器应用发送的消息。假如你对更多详细信息感兴趣,请阅读RFC 2616。

    在HTTP中,始终都是客户端通过建立连接和发送一个HTTP请求从而开启一个事务。web服务器不需要联系客户端或者对客户端做一个回调连接。无论是客户端或者服务器都可以提前终止连接。举例来说,当你正在使用一个web浏览器的时候,可以通过点击浏览器上的停止按钮来停止一个文件的下载进程,从而有效的关闭与web服务器的HTTP连接。

 

1.2HTTP请求                                     

    一个HTTP请求包括三个组成部分

·  方法 — 统一资源标识符(URI) — 协议/版本

·  请求的头部

·  主体内容

 

    下面是一个HTTP请求的例子:

POST /examples/default.jsp HTTP/1.1
Accept: text/plain; text/html
Accept-Language: en-gb
Connection: Keep-Alive
Host: localhost
User-Agent: Mozilla/4.0 (compatible; MSIE 4.01; Windows 98)
Content-Length: 33
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate

lastName=Franks&firstName=Michael

 

    方法—统一资源标识符(URI)—协议/版本出现在请求的第一行。

POST /examples/default.jsp HTTP/1.1

    这里POST是请求方法,/examples/default.jsp是URI,而HTTP/1.1是协议/版本部分。

 

每个HTTP请求可以使用HTTP标准里边提到的多种方法之一。HTTP 1.1支持7种类型的请求:GET, POST,HEAD, OPTIONS, PUT, DELETE和TRACE。

GET和POST在互联网应用里边最普遍使用的。

URI完全指明了一个互联网资源。URI通常是相对服务器的根目录解释的。因此,始终一斜线/开头。统一资源定位器(URL)其实是一种URI(查看http://www.ietf.org/rfc/rfc2396.txt)来的。该协议版本代表了正在使用的HTTP协议的版本。

请求的头部包含了关于客户端环境和请求的主体内容的有用信息。例如它可能包括浏览器设置的语言,主体内容的长度等等。每个头部通过一个回车换行符(CRLF)来分隔的。

对于HTTP请求格式来说,头部和主体内容之间有一个回车换行符(CRLF)是相当重要的。CRLF告诉HTTP服务器主体内容是在什么地方开始的。在一些互联网编程书籍中,CRLF还被认为是HTTP请求的第四部分。

 

在前面一个HTTP请求中,主体内容只不过是下面一行:

lastName=Franks&firstName=Michael

    实体内容在一个典型的HTTP请求中可以很容易的变得更长。

 

1.3 HTTP Response

 

与HTTP Request类似,HTTP Response也由三部分组成:

    协议-状态-描述

Response 头

Response 体

 

举例如下: 

HTTP/1.1 200 OK  
Server: Microsoft-IIS/4.0  
Date: Mon, 5 Jan 2004 13:13:33 GMT  Content-Type: text/html  
Last-Modified: Mon, 5 Jan 2004 13:13:12 GMT  Content-Length: 112    
<html>
	<head>  
		<title>HTTP Response Example</title>
	</head>
<body>  Welcome to Brainy Software  </body>
</html> 

   

注意响应实体(entity)与响应头(header)之间有一个空白行(CRLF)。

 

1.4   Socket 类

套接字socket代表客户端与服务器连接,你可以通过他与服务器建立连接,可以指定host和port,Java中用Socket类来建立,有多个构造函数。

可以通过ServerSocket建立http服务器或者ftp服务器。

socket 通信的实例代码如下:

Socket socket = new Socket("127.0.0.1", "8080"); 	//建立连接
  OutputStream os = socket.getOutputStream(); 	//获取输出流
  boolean autoflush = true; 
  PrintWriter out = new PrintWriter( 
		socket.getOutputStream(), autoflush); 	//设置自动flush
  BufferedReader in = new BufferedReader( 
		new InputStreamReader( socket.getInputstream() )); 
 
  // send an HTTP request to the web server 
  out.println("GET /index.jsp HTTP/1.1"); 		//拼装HTTP请求信息
  out.println("Host: localhost:8080"); 
 
  out.println("Connection: Close"); 
  out.println(); 
 
  // read the response 
  boolean loop = true; 
  StringBuffer sb = new StringBuffer(8096); 
  while (loop) { 
    if ( in.ready() ) { 
      int i=0;
	   while (i!=-1) { 
        i = in.read(); 
        sb.append((char) i); 
      } 
      loop = false; 
    } 
    Thread.currentThread().sleep(50); 	//由于是阻塞写入,暂停50ms,保证可以写入。
  } 
 
  // display the response to the out console 
  System.out.println(sb.toString()); 
  socket.close();

   

1.5   ServerSocket 类

Socket类表示一个客户端socket,相应的ServerSocket类表示了一个服务器端应用。服务器端socket需要等待来自客户端的连接请求。一旦ServerSocket接收到来自客户端的连接请求,它会实例化一个Socket类的对象来处理与客户端的通信。

 

1.6  应用举例

该程序包括三个部分,HttpServer、Request和Response。该程序只能发送静态资源,如HTML文件,图片文件,但不会发送响应头信息。

代码文件如下:

 

 服务器:

socket = serverSocket.accept();   

input = socket.getInputStream();   

output = socket.getOutputStream(); 

服务器Request将inputStream请求解析为字符串,从中拿到URI

服务器Response拼接服务器路径和URI,得到文件路径,判断是否存在,存在则输出到outputStream请求中,不存在则输出错误字符串到outputstream中

package ex01.pyrmont; 
 
import java.net.Socket; 
import java.net.ServerSocket; 
import java.net.InetAddress; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.io.IOException; 
import java.io.File; 
 
public class HttpServer { 
 
    /** WEB_ROOT is the directory where our HTML and other files reside. 
    *  For this package, WEB_ROOT is the "webroot" directory under the 
    *  working directory. 
    *  The working directory is the location in the file system 
    *  from where the java command was invoked. 
    */ 
    public static final String WEB_ROOT = 
    System.getProperty("user.dir") + File.separator  + "webroot"; 

    // shutdown command 
    private static final String SHUTDOWN_COMMAND = "/SHUTDOWN"; 

    // the shutdown command received 
    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 (IOException e) { 
        e.printStackTrace(); 
        System.exit(1); 
      } 
      // Loop waiting for a request 
      while (!shutdown) { 
        Socket socket = null; 
        InputStream input = null; 
        OutputStream output = null; 
     
        try { 
          socket = serverSocket.accept(); 
          input = socket.getInputStream(); 
          output = socket.getOutputStream(); 
		  
          // create Request object and parse 
          Request request = new Request(input); 
          request.parse(); 
     
          // create Response object 
          Response response = new Response(output); 
          response.setRequest(request); 
          response.sendStaticResource(); 
     
          // Close the socket 
          socket.close();  
          //check if the previous URI is a shutdown command 
          shutdown = request.getUri().equals(SHUTDOWN_COMMAND); 
        } 
        catch (Exception e) { 
          e.printStackTrace (); 
          continue; 
        } 
      } 
    }   
} 

 

package ex01.pyrmont; 
 
import java.io.InputStream; 
import java.io.IOException; 
 
public class Request { 
   private InputStream input; 
   private String uri; 
 
   public Request(InputStream input) { 
     this.input = input; 
   } 
 
    public void parse() { 
       // Read a set of characters from the socket 
       StringBuffer request = new StringBuffer(2048); 
       int i; 
       byte[] buffer = new byte[2048]; 
       try { 
         i = input.read(buffer); 
       } 
       catch (IOException e) { 
         e.printStackTrace(); 
         i = -1; 
       } 
       for (int j=0; j<i; j++) { 
         request.append((char) buffer[j]); 
       } 
       System.out.print(request.toString()); 
       uri = parseUri(request.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; 
   } 
}

 

package ex01.pyrmont; 
 
import java.io.OutputStream; import java.io.IOException; 
import java.io.FileInputStream; 
import java.io.File; 
 
/* 
   HTTP Response = Status-Line 
     *(( general-header | response-header | entity-header ) CRLF) 
     CRLF 
     [ message-body ] 
     Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF 
*/ 
 
public class Response { 
 
   private static final int BUFFER_SIZE = 1024; 
   Request request; 
   OutputStream output; 
 
   public Response(OutputStream output) { 
     this.output = output; 
   } 
 
   public void setRequest(Request request) { 
     this.request = request; 
   } 
 
   public void sendStaticResource() throws IOException { 
     byte[] bytes = new byte[BUFFER_SIZE]; 
     FileInputStream fis = null; 
     try { 
       File file = new File(HttpServer.WEB_ROOT, request.getUri()); 
       if (file.exists()) { 
			 fis = new FileInputStream(file); 
			 int ch = fis.read(bytes, 0, BUFFER_SIZE); 
			 while (ch!=-1) { 
			   output.write(bytes, 0, ch); 
			   ch = fis.read(bytes, 0, BUFFER_SIZE); 
			 } 
       } 
       else { 
			// file not found 
			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>"; 
			output.write(errorMessage.getBytes()); 
       } 
     } 
     catch (Exception e) { 
       // thrown if cannot instantiate a File object 
       System.out.println(e.toString() ); 
     } 
     finally { 
       if (fis!=null) 
         fis.close(); 
     } 
   } 
} 

 ...

 Servlet容器处理Servlet请求的简单流程图

 

 

猜你喜欢

转载自uule.iteye.com/blog/2188813
今日推荐