hooyantsing's Blog

第93次课程_HTTP协议

字数统计: 663阅读时长: 3 min
2019/12/21

源辰76班

第93次课程

2019.12.21

内容

HTTP协议[廖彦]

1.[案例]精简版Tomcat

请求:客户端 -> 服务器

响应:服务器 -> 客户端

[重点]报文格式

server.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.yc.http;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
    
    //默认路径:127.0.0.1/Untitled-1.html
    public final static String WEBAPPS =  "G:/源码库/HTML/多图校园官网";
    public static void main(String[] args)  throws IOException {
       ServerSocket server = new  ServerSocket(80);
       System.out.println("服务器启动成功:80");
       
       Socket socket;
       while((socket=  server.accept())!=null) {
           InputStream in =  socket.getInputStream();
           OutputStream out =  socket.getOutputStream();
           byte[] buffer = new byte[1024];
           int count = in.read(buffer);
           //显示请求报文
           System.out.println(new  String(buffer,0,count));
           if(count > 0) {
               //获取请求报文
               String reqstring = new  String(buffer,0,count);
               
               String[] lines =  reqstring.split("\r\n");
               //获取资源路径
               String srcpath =  lines[0].split("\\s")[1];
               
               //响应头行
               out.write("HTTP/1.1 200  OK\n".getBytes());
               //响应头域
               String contextType =  getContextType(srcpath);
               out.write(("Context-Type: " +  contextType + "\n").getBytes());
               //空行 分隔 CRLF
               out.write("\n".getBytes());
               
               String filepath = WEBAPPS +  srcpath;
               
               //读取本地文件
               File file = new  File(filepath);
               if(file.exists()==false) {
                  file = new File(WEBAPPS +  "");
               }
                  InputStream inFile = new  FileInputStream(file);
                  byte[] bufferFile = new  byte[1024];
                  int countFile = -1;
                  //响应实体
                  while((countFile =  inFile.read(bufferFile)) != -1) {
                      out.write(bufferFile,0,countFile);
                  }
                  inFile.close();   
           }
           //正常响应必须关闭socket
           socket.close();
       }
       /**
        * 请求报文
        * 请求头行:    GET  /index.jsp?a=100&b=200&c=%E5%AE%9E%E4%BD%93  HTTP/1.1
        *     头行分成3段   方法名 空格 服务器资源的地址 空格 协议版本
        * 头域: 每行一个键值对,以冒号分隔           作用:设置一些请求信息的相关属性
        * CRLF:空行       作用:分割头域和实体
        * 实体:post 有实体内容      GET请求无实体
        *
        *
        * 响应报文
        * 响应头行:   HTTP/1.1 200 OK
        *     头行分成3段   协议版本 空格 响应码 空格 响应码描述
        * 头域: 每行一个键值对,以冒号分隔           作用:设置一些请求信息的相关属性
        * CRLF:空行       作用:分割头域和实体
        * 实体:一般都有实体,3xx响应码没有实体  post 有实体内容      GET请求无实体
        *
        * 响应码:
        * 1xxx 接受请求,继续处理
        * 2xxx 正常结果       
        * 3xxx 响应重定向     
        * 4xxx 客户端错误
        * 5xxx 服务器错误
        *  
        */
    }
    private static String  getContextType(String srcpath) {
       int beginIndex =  srcpath.lastIndexOf(".");
       String suffix =  srcpath.substring(beginIndex);
       switch(suffix) {
       case ".css":
           return "text/css";
       case ".js":
           return "application/javascript";
       case ".jpg":
           return "image/jpeg";
       case ".gif":
           return "image/gif";
       case ".png":
           return "image/png";
       }      
       return "text/html";
    }
}
CATALOG