-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. add middleware docs 2. optimize code structure
- Loading branch information
DashJay
committed
Jun 27, 2020
1 parent
90c6380
commit 492cc28
Showing
7 changed files
with
170 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
## 中间件的编写 | ||
[EN](./middleware.md)[中文] | ||
|
||
中间件基于请求体(http.Request)和返回体(http.Response)来编写,由于两个结构都包含一些buf,例如请求和返回体的body. | ||
|
||
因此为了防止body被中间件读取后,客户端或者服务端无法收到真实的请求体和返回体,我们需要做一些操作。 | ||
|
||
示例的中间件stdout中这样写到: | ||
```gotemplate | ||
func handleRequest(over chan bool, req *http.Request) { | ||
content, err := httputil.DumpRequest(req, true) | ||
over <- true | ||
if err != nil { | ||
panic(err) | ||
} | ||
... | ||
``` | ||
`httputil.DumpRequest`会将请求body读出(如果存在的话),并且将body还给req这个请求体一份。如果我们自己写中间件也应该这样来写。 | ||
|
||
如果你不能很好的处理请求和返回中body,可能会造成严重的后果: | ||
- body为空,读时EOF error | ||
- body已经close,读时panic | ||
|
||
为了避免程序因为这些原因崩溃,建议使用如下方式来编写中间件。 | ||
|
||
``` | ||
content, err := httputil.DumpRequest(req, true) | ||
newReq, err := http.ReadRequest(bufio.NewReader(bytes.NewReader(content))) | ||
``` | ||
|
||
这样可以完全避免req请求体收到影响,能够正常发送到服务端以收到回复。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
[EN][中文](./middleware.CN.md) | ||
|
||
## write a middleware | ||
|
||
|
||
Middleware based on request body( http.Request )And return body( http.Response )Because both structures contain buf, such as the body of request and response body. | ||
|
||
Therefore, in order to prevent superlcx read body, so that the client or server can't receive the real request body or response body, we need to do some operations. | ||
|
||
The standard middleware `stdout` of the example write follows: | ||
```gotemplate | ||
func handleRequest(over chan bool, req *http.Request) { | ||
content, err := httputil.DumpRequest(req, true) | ||
over <- true | ||
if err != nil { | ||
panic(err) | ||
} | ||
... | ||
``` | ||
|
||
The request body(if exists) will be read out by `httputil.DumpRequest`, and a copy of the request body will be assign to origin req. If we write a middleware by ourselves, we should do the same. | ||
|
||
If you can't handle the body in the request and response well, you may have serious consequences: | ||
- Body is empty, which can cause EOF error during reading. | ||
- The body has been closed, panic when you read it. | ||
|
||
In order to prevent the program from crashing for these reasons, it is recommended to write the middleware in the following way. | ||
``` | ||
content, err := httputil.DumpRequest(req, true) | ||
newReq, err := http.ReadRequest(bufio.NewReader(bytes.NewReader(content))) | ||
``` | ||
|
||
In this way, it can completely avoid the influence of req request body and send it to the server to receive the reply. |