Skip to content

Commit d67ddaf

Browse files
committed
feat(middleware/return): render page by StatusPage midware
1 parent ea559ea commit d67ddaf

File tree

7 files changed

+27
-61
lines changed

7 files changed

+27
-61
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,10 @@ This means it is impossible to use legacy Go version to compile binaries for leg
4141
Use `$0` to represent the whole match in `match`.
4242
use `$1` - `$9` to represent sub matches in `match`.
4343
44-
--return <separator><match><separator><status-code>[<separator><fs-path>]
44+
--return <separator><match><separator><status-code>
4545
When request URL (in the form of "/request/path?param=value")
4646
is matched by `match`, return the status code `status-code`
4747
immediately and stop processing.
48-
Optional response page content is specified by `fs-path`.
4948
5049
--status-page <separator><status-code><separator><fs-path>
5150
When response status is `status-code`, respond with the file content from `fs-path`.
@@ -57,6 +56,7 @@ This means it is impossible to use legacy Go version to compile binaries for leg
5756
- `--redirect` executed if URL matched, and stop processing.
5857
- `--proxy` executed if URL matched, and stop processing.
5958
- `--return` executed if URL matched, and stop processing.
59+
- `--status-page` executed if status code matched, and stop processing.
6060

6161
## Examples
6262

README.zh-CN.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,9 @@ Extra HTTP File Server基于Go HTTP File Server,附带额外功能。
4141
使用`$0`表示`match`的完整匹配。
4242
使用`$1`-`$9`来表示`match`中的子匹配。
4343
44-
--return <分隔符><match><分隔符><status-code>[<分隔符><fs-path>]
44+
--return <分隔符><match><分隔符><status-code>
4545
当请求的URL(“/request/path?param=value”的形式)匹配正则表达式`match`时,
4646
立即返回状态码`status-code`并停止处理。
47-
可选的`fs-path`用于指定返回页面的内容。
4847
4948
--status-page <分隔符><status-code><分隔符><fs-path>
5049
当响应状态码为`status-code`时,用文件`fs-path`的内容来响应。
@@ -56,6 +55,7 @@ Extra HTTP File Server基于Go HTTP File Server,附带额外功能。
5655
- 如果URL匹配,执行`--redirect`并停止处理。
5756
- 如果URL匹配,执行`--proxy`并停止处理。
5857
- 如果URL匹配,执行`--return`并停止处理。
58+
- 如果状态码匹配,执行`--status-page`并停止处理。
5959

6060
## 举例
6161

src/middleware/main.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ var errInvalidParamValue = errors.New("invalid param value")
1212
var errParamCountNotMatch = errors.New("base-param count is not equal to param count")
1313

1414
func ParamToMiddlewares(baseParam *baseParam.Param, param *param.Param) (preMids, postMids []middleware.Middleware, errs []error) {
15+
var statusPageMids []middleware.Middleware
16+
17+
// status pages
18+
for i := range param.StatusPages {
19+
mid, err := getStatusPageMiddleware(param.StatusPages[i])
20+
errs = serverError.AppendError(errs, err)
21+
if mid != nil {
22+
statusPageMids = append(statusPageMids, mid)
23+
postMids = append(postMids, mid)
24+
}
25+
}
26+
1527
// rewrites
1628
for i := range param.Rewrites {
1729
mid, err := getRewriteMiddleware(param.Rewrites[i])
@@ -41,22 +53,13 @@ func ParamToMiddlewares(baseParam *baseParam.Param, param *param.Param) (preMids
4153

4254
// returns
4355
for i := range param.Returns {
44-
mid, err := getReturnStatusMiddleware(param.Returns[i])
56+
mid, err := getReturnStatusMiddleware(param.Returns[i], statusPageMids)
4557
errs = serverError.AppendError(errs, err)
4658
if mid != nil {
4759
preMids = append(preMids, mid)
4860
}
4961
}
5062

51-
// status pages
52-
for i := range param.StatusPages {
53-
mid, err := getStatusPageMiddleware(param.StatusPages[i])
54-
errs = serverError.AppendError(errs, err)
55-
if mid != nil {
56-
postMids = append(postMids, mid)
57-
}
58-
}
59-
6063
return
6164
}
6265

src/middleware/return.go

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
package middleware
22

33
import (
4-
"io"
5-
"mjpclab.dev/ehfs/src/util"
64
"mjpclab.dev/ghfs/src/middleware"
75
"net/http"
8-
"os"
96
"regexp"
107
"strconv"
118
)
129

13-
func getReturnStatusMiddleware(arg [3]string) (middleware.Middleware, error) {
10+
func getReturnStatusMiddleware(arg [2]string, statusPageMids []middleware.Middleware) (middleware.Middleware, error) {
1411
var err error
1512
var reMatch *regexp.Regexp
1613
var code int
17-
var filename string
1814

1915
reMatch, err = regexp.Compile(arg[0])
2016
if err != nil {
@@ -24,7 +20,6 @@ func getReturnStatusMiddleware(arg [3]string) (middleware.Middleware, error) {
2420
if err != nil {
2521
return nil, err
2622
}
27-
filename = arg[2]
2823

2924
return func(w http.ResponseWriter, r *http.Request, context *middleware.Context) (result middleware.ProcessResult) {
3025
requestURI := r.URL.RequestURI() // request uri without prefix path
@@ -33,23 +28,11 @@ func getReturnStatusMiddleware(arg [3]string) (middleware.Middleware, error) {
3328
}
3429

3530
result = middleware.Processed
36-
3731
w.WriteHeader(code)
38-
39-
if len(filename) > 0 {
40-
file, err := os.Open(filename)
41-
if err != nil {
42-
util.LogError(context.Logger, err)
43-
return
44-
}
45-
46-
_, err = io.Copy(w, file)
47-
if err != nil {
48-
util.LogError(context.Logger, err)
49-
}
50-
err = file.Close()
51-
if err != nil {
52-
util.LogError(context.Logger, err)
32+
context.Status = code
33+
for i := range statusPageMids {
34+
if statusPageMids[i](w, r, context) != middleware.GoNext {
35+
break
5336
}
5437
}
5538
return

src/middleware/return_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func TestReturnStatus(t *testing.T) {
11-
mid, err := getReturnStatusMiddleware([3]string{`/doc`, "404", "/tmp/asdf"})
11+
mid, err := getReturnStatusMiddleware([2]string{`/doc`, "404"}, nil)
1212
if err != nil {
1313
t.FailNow()
1414
}

src/param/cli.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,8 @@ func CmdResultsToParams(results []*goNixArgParser.ParseResult) (params []*Param,
5757
param.Proxies = baseParam.SplitAllKeyValue(proxies)
5858

5959
// returns
60-
strReturns, _ := result.GetStrings("returns")
61-
returns := baseParam.SplitAllKeyValues(strReturns)
62-
param.Returns = make([][3]string, len(returns))
63-
for i := range returns {
64-
copy(param.Returns[i][:], returns[i])
65-
}
60+
returns, _ := result.GetStrings("returns")
61+
param.Returns = baseParam.SplitAllKeyValue(returns)
6662

6763
// status pages
6864
statusPages, _ := result.GetStrings("statuspages")

src/param/main.go

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ type Param struct {
1212
Redirects [][3]string
1313
// value: [match, replace]
1414
Proxies [][2]string
15-
// value: [match, code, file?]
16-
Returns [][3]string
15+
// value: [match, code]
16+
Returns [][2]string
1717

1818
// value: [code, file]
1919
StatusPages [][2]string
@@ -38,20 +38,4 @@ func (param *Param) normalize() {
3838
redirects = append(redirects, param.Redirects[i])
3939
}
4040
param.Redirects = redirects
41-
42-
// returns
43-
returns := make([][3]string, 0, len(param.Returns))
44-
for i := range param.Returns {
45-
if len(param.Returns[i][0]) == 0 || len(param.Returns[i][1]) == 0 {
46-
continue
47-
}
48-
code, err := strconv.Atoi(param.Returns[i][1])
49-
if err != nil {
50-
continue
51-
}
52-
param.Returns[i][1] = strconv.Itoa(code)
53-
54-
returns = append(returns, param.Returns[i])
55-
}
56-
param.Returns = returns
5741
}

0 commit comments

Comments
 (0)