55
66由 Java Socket API 实现的简单 HTTP Client 与 Http Server
77
8- ### TODO
8+ ## TODO
99
10- - [ ] 语言
11- - [ ] 程序运行方式
10+ - [x ] 语言
11+ - [x ] 程序运行方式
1212- [ ] 功能点
1313 - [ ] 说明、运行截图
1414- [ ] 关键数据结构、类说明
1515 - [ ] 名字与对应功能
1616
17- ## 1. 概览
17+ # 1. 概览
1818
19- ### 1.1 环境
19+ ## 1.1 环境
2020
2121本项目基于 JDK 17.0 编写,由 Maven 构造。
2222
23- ### 1.2 依赖
23+ ## 1.2 依赖
2424
2525| Scope | GroupId | ArtifactId | Version |
2626| ----- | ------------------- | ---------------------- | ---------- |
2727| Build | ` org.projectlombok ` | ` lombok ` | ` 1.18.22 ` |
2828| Build | ` org.json ` | ` json ` | ` 20220320 ` |
2929| Test | ` org.junit.jupiter ` | ` junit-jupiter-engine ` | ` 5.8.2 ` |
3030
31- ### 1.3 运行方式
31+ ## 1.3 运行方式
3232
33- 最终产品为jar文件,通过 ` java -jar [NAME].jar ` 运行。
33+ 最终产品为 jar 文件,通过 ` java -jar [NAME].jar ` 运行。
34+
35+ # 2. 使用
36+
37+ ## 2.1 外部文件
3438
35- ## 2. 使用说明
36- ### 2.1 外部文件
3739[ Data] ( ./Data/ ) 及其子目录会在 HttpClient 或 HttpServer 启动时被自动创建于 jar 文件所在目录,结构如下:
3840
3941```
40- Data
42+ Data
4143├── Client
4244│ └── Cache // Client的缓存目录
43- └── Server
45+ └── Server
4446 ├── Cache // Server的缓存目录(未使用)
4547 └── Static // Server的静态文件存放目录
4648```
4749
48- ### 2.2 HttpServer
50+ ## 2.2 HttpServer
4951
50- #### 2.2.1 执行指令
52+ ## 2.2.1 执行指令
5153
52- ` java -jar /path/to/HttpServer.jar [OPTIONS]... `
54+ ` java -jar /path/to/HttpServer.jar [OPTIONS]... `
5355
54- #### 2.2.2 具体语法
56+ ## 2.2.2 具体语法
5557
5658```
5759SYNOPSIS
5860 ~ [-p <PORT>] [--keep-alive]
5961 [-t <TIMEOUT>]
60-
62+
6163OPTIONS
6264 -p <PORT> Set up the server with the specified port number.
6365 The default value is 8080
64-
66+
6567 --keep-alive Enable keep-alive.
66-
68+
6769 -t <TIMEOUT> Socket timeout.
6870 The default value is 10000
6971```
7072
71- #### 2.2.3 启动信息
73+ ## 2.2.3 启动信息
7274
7375若运行正常,则会显示启动信息,包含:
76+
7477- Server 运行日志
7578- 预设 URL Mapping 信息
7679- 静态文件读取目录
@@ -90,50 +93,51 @@ SERVER: Reading static files from: [file:///.../Data/Server/Static]
9093SERVER: The server is now running
9194```
9295
96+ ## 2.3 HttpClient
9397
94- ### 2.3 HttpClient
95-
96- #### 2.3.1 执行指令
98+ ## 2.3.1 执行指令
9799
98- ` java -jar /path/to/HttpClient.jar <URL> [OPTIONS]... `
100+ ` java -jar /path/to/HttpClient.jar <URL> [OPTIONS]... `
99101
100- #### 2.3.2 具体语法
102+ ## 2.3.2 具体语法
101103
102104```
103105SYNOPSIS
104106 ~ <URL>
105107 [-m <METHOD>] [--keep-alive] [-b <text>]
106108 [-h <headers>...]
107-
109+
108110URL
109111 Using the generic URI syntax of:
110112 http://<HOSTNAME>[:PORT][/PATH][?QUERY]
111113
112114 The default value of the port number is 80.
113115 Only support HTTP protocol (not HTTPS).
114-
116+
115117OPTIONS
116118 -m <METHOD> Send with the specified web method.
117119 Only supports GET and POST.
118120 The default value is GET.
119-
121+
120122 --keep-alive Enable keep-alive.ß
121-
123+
122124 -b <text> Plain text body.
123-
125+
124126 -h <header>... Send with the specified headers.
125127 Syntax: <key>:<value>
126128 e.g.: User-Agent:AbaAba/0.1
127129```
128130
129- #### 2.3.3 执行信息
131+ ## 2.3.3 执行信息
132+
133+ 若执行正常,则会显示
130134
131- 若执行正常,则会显示
132135- Client 执行日志
133136- HTTP Request Message 原信息
134137- HTTP Response Message 原信息
135138
136139其中原信息包含:
140+
137141- Startline
138142- Headers
139143- Body
@@ -146,15 +150,15 @@ OPTIONS
146150/* 直接显示 Body 内容 */
147151
148152CLIENT: Client has connect to the host
149- SOCKET[localhost127.0.0.1:8080]: Message sent 0.097656 KB
153+ SOCKET[localhost127.0.0.1:8080]: Message sent 0.097656 KB
150154
151155>> ==================== HTTP Request Message ==================== <<
152156>> GET /test HTTP/1.1
153157>> Accept: */*
154158>> User-Agent: Wget/1.21.3
155159>> Host: localhost
156160>> Accept-Encoding: gzip
157- >>
161+ >>
158162CLIENT: Status code received: 200
159163CLIENT: Handle returned directly...
160164CLIENT: Request complete
@@ -166,11 +170,10 @@ CLIENT: Request complete
166170<< content-encoding: gzip
167171<< connection: keep-alive
168172<< content-type: text/plain; charset=UTF-8
169- <<
173+ <<
170174<< You got the place!!!
171175```
172176
173-
174177```
175178/* 显示 Body 存储路径 */
176179
@@ -181,7 +184,7 @@ CLIENT: Request complete
181184>> If-Modified-Since: Sun, 22 May 2022 11:13:51 GMT
182185>> Host: jyywiki.cn
183186>> Accept-Encoding: gzip
184- >>
187+ >>
185188CLIENT: Status code received: 200
186189CLIENT: Handle returned directly...
187190CLIENT: Request complete
@@ -194,7 +197,28 @@ CLIENT: Request complete
194197<< content-type: text/html; charset=utf-8
195198<< connection: keep-alive
196199<< Content-Length: 16876
197- <<
198- << Body saved at:
200+ <<
201+ << Body saved at:
199202<< file:///.../Data/Client/Cache/jyywiki.cn/OS/2022/index/cache.html
200- ```
203+ ```
204+
205+ # 3. 功能
206+
207+ ## 3.1 HttpServer
208+
209+ HttpServer 采用 Java NIO.2 模型进行开发,使用了 ` java.nio.channels.AsynchronousServerSocketChannel ` 、` java.nio.channels.AsynchronousSocketChannel ` 等类。
210+
211+ ### 3.1.1 接收
212+
213+ - 支持 ` GET ` 和 ` POST ` 请求
214+
215+ <img src =" ./docs/image/feature_get.png " alt =" GET " width =" 250 " >
216+ <img src =" ./docs/image/feature_post.png " alt =" POST " width =" 250 " >
217+
218+ 相关测试: [ LoginSystemTests] ( ./src/test/java/loginsystemtests/LoginSystemTests.java )
219+
220+ - 支持以下状态码:
221+ - 200 OK
222+ -
223+
224+ ## 3.2 HttpClient
0 commit comments