diff options
author | Valery Piashchynski <[email protected]> | 2021-06-03 16:59:10 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2021-06-03 16:59:10 +0300 |
commit | 063703e96e5f7cee59139e98d446d5577fb5c224 (patch) | |
tree | e669d08cbbafaa5d809bfb870e874d3de4683dad | |
parent | 62bbde7936109d18bf1f727974719804dad4c105 (diff) |
- Add internal_error_code option
- Update tests
Signed-off-by: Valery Piashchynski <[email protected]>
-rw-r--r-- | pkg/worker_handler/handler.go | 28 | ||||
-rw-r--r-- | plugins/http/config/http.go | 7 | ||||
-rw-r--r-- | plugins/http/plugin.go | 2 | ||||
-rw-r--r-- | tests/plugins/http/configs/.rr-issue659.yaml | 1 | ||||
-rw-r--r-- | tests/plugins/http/handler_test.go | 56 | ||||
-rw-r--r-- | tests/plugins/http/http_plugin_test.go | 2 | ||||
-rw-r--r-- | tests/plugins/http/uploads_test.go | 8 |
7 files changed, 58 insertions, 46 deletions
diff --git a/pkg/worker_handler/handler.go b/pkg/worker_handler/handler.go index 672c5838..0ff23d9d 100644 --- a/pkg/worker_handler/handler.go +++ b/pkg/worker_handler/handler.go @@ -57,25 +57,27 @@ func (e *ResponseEvent) Elapsed() time.Duration { // Handler serves http connections to underlying PHP application using PSR-7 protocol. Context will include request headers, // parsed files and query, payload will include parsed form dataTree (if any). type Handler struct { - maxRequestSize uint64 - uploads config.Uploads - trusted config.Cidrs - log logger.Logger - pool pool.Pool - mul sync.Mutex - lsn events.Listener + maxRequestSize uint64 + uploads config.Uploads + trusted config.Cidrs + log logger.Logger + pool pool.Pool + mul sync.Mutex + lsn events.Listener + internalHTTPCode uint64 } // NewHandler return handle interface implementation -func NewHandler(maxReqSize uint64, uploads config.Uploads, trusted config.Cidrs, pool pool.Pool) (*Handler, error) { +func NewHandler(maxReqSize uint64, internalHTTPCode uint64, uploads config.Uploads, trusted config.Cidrs, pool pool.Pool) (*Handler, error) { if pool == nil { return nil, errors.E(errors.Str("pool should be initialized")) } return &Handler{ - maxRequestSize: maxReqSize * MB, - uploads: uploads, - pool: pool, - trusted: trusted, + maxRequestSize: maxReqSize * MB, + uploads: uploads, + pool: pool, + trusted: trusted, + internalHTTPCode: internalHTTPCode, }, nil } @@ -177,7 +179,7 @@ func (h *Handler) handleError(w http.ResponseWriter, r *http.Request, start time errors.Is(errors.Decode, err) || errors.Is(errors.Network, err) { // write an internal server error - w.WriteHeader(http.StatusInternalServerError) + w.WriteHeader(int(h.internalHTTPCode)) h.sendEvent(ErrorEvent{Request: r, Error: errors.E(op, err), start: start, elapsed: time.Since(start)}) } } diff --git a/plugins/http/config/http.go b/plugins/http/config/http.go index a1c2afa6..f06adc49 100644 --- a/plugins/http/config/http.go +++ b/plugins/http/config/http.go @@ -15,6 +15,9 @@ type HTTP struct { // Host and port to handle as http server. Address string + // InternalErrorCode used to override default 500 (InternalServerError) http code + InternalErrorCode uint64 `mapstructure:"internal_error_code"` + // SSLConfig defines https server options. SSLConfig *SSL `mapstructure:"ssl"` @@ -80,6 +83,10 @@ func (c *HTTP) InitDefaults() error { } } + if c.InternalErrorCode == 0 { + c.InternalErrorCode = 500 + } + if c.HTTP2Config == nil { c.HTTP2Config = &HTTP2{} } diff --git a/plugins/http/plugin.go b/plugins/http/plugin.go index 770ca8ca..1952679a 100644 --- a/plugins/http/plugin.go +++ b/plugins/http/plugin.go @@ -149,6 +149,7 @@ func (p *Plugin) serve(errCh chan error) { p.handler, err = handler.NewHandler( p.cfg.MaxRequestSize, + p.cfg.InternalErrorCode, *p.cfg.Uploads, p.cfg.Cidrs, p.pool, @@ -323,6 +324,7 @@ func (p *Plugin) Reset() error { p.handler, err = handler.NewHandler( p.cfg.MaxRequestSize, + p.cfg.InternalErrorCode, *p.cfg.Uploads, p.cfg.Cidrs, p.pool, diff --git a/tests/plugins/http/configs/.rr-issue659.yaml b/tests/plugins/http/configs/.rr-issue659.yaml index 6e0584a5..bf192fab 100644 --- a/tests/plugins/http/configs/.rr-issue659.yaml +++ b/tests/plugins/http/configs/.rr-issue659.yaml @@ -9,6 +9,7 @@ server: http: address: 127.0.0.1:32552 max_request_size: 1024 + internal_error_code: 444 middleware: [ ] uploads: forbid: [ ".php", ".exe", ".bat" ] diff --git a/tests/plugins/http/handler_test.go b/tests/plugins/http/handler_test.go index 1b7f5bac..40e3a720 100644 --- a/tests/plugins/http/handler_test.go +++ b/tests/plugins/http/handler_test.go @@ -35,7 +35,7 @@ func TestHandler_Echo(t *testing.T) { t.Fatal(err) } - h, err := handler.NewHandler(1024, config.Uploads{ + h, err := handler.NewHandler(1024, 500, config.Uploads{ Dir: os.TempDir(), Forbid: []string{}, }, nil, p) @@ -66,7 +66,7 @@ func TestHandler_Echo(t *testing.T) { } func Test_HandlerErrors(t *testing.T) { - _, err := handler.NewHandler(1024, config.Uploads{ + _, err := handler.NewHandler(1024, 500, config.Uploads{ Dir: os.TempDir(), Forbid: []string{}, }, nil, nil) @@ -89,7 +89,7 @@ func TestHandler_Headers(t *testing.T) { p.Destroy(context.Background()) }() - h, err := handler.NewHandler(1024, config.Uploads{ + h, err := handler.NewHandler(1024, 500, config.Uploads{ Dir: os.TempDir(), Forbid: []string{}, }, nil, p) @@ -150,7 +150,7 @@ func TestHandler_Empty_User_Agent(t *testing.T) { p.Destroy(context.Background()) }() - h, err := handler.NewHandler(1024, config.Uploads{ + h, err := handler.NewHandler(1024, 500, config.Uploads{ Dir: os.TempDir(), Forbid: []string{}, }, nil, p) @@ -210,7 +210,7 @@ func TestHandler_User_Agent(t *testing.T) { p.Destroy(context.Background()) }() - h, err := handler.NewHandler(1024, config.Uploads{ + h, err := handler.NewHandler(1024, 500, config.Uploads{ Dir: os.TempDir(), Forbid: []string{}, }, nil, p) @@ -270,7 +270,7 @@ func TestHandler_Cookies(t *testing.T) { p.Destroy(context.Background()) }() - h, err := handler.NewHandler(1024, config.Uploads{ + h, err := handler.NewHandler(1024, 500, config.Uploads{ Dir: os.TempDir(), Forbid: []string{}, }, nil, p) @@ -335,7 +335,7 @@ func TestHandler_JsonPayload_POST(t *testing.T) { p.Destroy(context.Background()) }() - h, err := handler.NewHandler(1024, config.Uploads{ + h, err := handler.NewHandler(1024, 500, config.Uploads{ Dir: os.TempDir(), Forbid: []string{}, }, nil, p) @@ -399,7 +399,7 @@ func TestHandler_JsonPayload_PUT(t *testing.T) { p.Destroy(context.Background()) }() - h, err := handler.NewHandler(1024, config.Uploads{ + h, err := handler.NewHandler(1024, 500, config.Uploads{ Dir: os.TempDir(), Forbid: []string{}, }, nil, p) @@ -459,7 +459,7 @@ func TestHandler_JsonPayload_PATCH(t *testing.T) { p.Destroy(context.Background()) }() - h, err := handler.NewHandler(1024, config.Uploads{ + h, err := handler.NewHandler(1024, 500, config.Uploads{ Dir: os.TempDir(), Forbid: []string{}, }, nil, p) @@ -519,7 +519,7 @@ func TestHandler_FormData_POST(t *testing.T) { p.Destroy(context.Background()) }() - h, err := handler.NewHandler(1024, config.Uploads{ + h, err := handler.NewHandler(1024, 500, config.Uploads{ Dir: os.TempDir(), Forbid: []string{}, }, nil, p) @@ -592,7 +592,7 @@ func TestHandler_FormData_POST_Overwrite(t *testing.T) { p.Destroy(context.Background()) }() - h, err := handler.NewHandler(1024, config.Uploads{ + h, err := handler.NewHandler(1024, 500, config.Uploads{ Dir: os.TempDir(), Forbid: []string{}, }, nil, p) @@ -665,7 +665,7 @@ func TestHandler_FormData_POST_Form_UrlEncoded_Charset(t *testing.T) { p.Destroy(context.Background()) }() - h, err := handler.NewHandler(1024, config.Uploads{ + h, err := handler.NewHandler(1024, 500, config.Uploads{ Dir: os.TempDir(), Forbid: []string{}, }, nil, p) @@ -737,7 +737,7 @@ func TestHandler_FormData_PUT(t *testing.T) { p.Destroy(context.Background()) }() - h, err := handler.NewHandler(1024, config.Uploads{ + h, err := handler.NewHandler(1024, 500, config.Uploads{ Dir: os.TempDir(), Forbid: []string{}, }, nil, p) @@ -809,7 +809,7 @@ func TestHandler_FormData_PATCH(t *testing.T) { p.Destroy(context.Background()) }() - h, err := handler.NewHandler(1024, config.Uploads{ + h, err := handler.NewHandler(1024, 500, config.Uploads{ Dir: os.TempDir(), Forbid: []string{}, }, nil, p) @@ -881,7 +881,7 @@ func TestHandler_Multipart_POST(t *testing.T) { p.Destroy(context.Background()) }() - h, err := handler.NewHandler(1024, config.Uploads{ + h, err := handler.NewHandler(1024, 500, config.Uploads{ Dir: os.TempDir(), Forbid: []string{}, }, nil, p) @@ -995,7 +995,7 @@ func TestHandler_Multipart_PUT(t *testing.T) { p.Destroy(context.Background()) }() - h, err := handler.NewHandler(1024, config.Uploads{ + h, err := handler.NewHandler(1024, 500, config.Uploads{ Dir: os.TempDir(), Forbid: []string{}, }, nil, p) @@ -1109,7 +1109,7 @@ func TestHandler_Multipart_PATCH(t *testing.T) { p.Destroy(context.Background()) }() - h, err := handler.NewHandler(1024, config.Uploads{ + h, err := handler.NewHandler(1024, 500, config.Uploads{ Dir: os.TempDir(), Forbid: []string{}, }, nil, p) @@ -1225,7 +1225,7 @@ func TestHandler_Error(t *testing.T) { p.Destroy(context.Background()) }() - h, err := handler.NewHandler(1024, config.Uploads{ + h, err := handler.NewHandler(1024, 500, config.Uploads{ Dir: os.TempDir(), Forbid: []string{}, }, nil, p) @@ -1271,7 +1271,7 @@ func TestHandler_Error2(t *testing.T) { p.Destroy(context.Background()) }() - h, err := handler.NewHandler(1024, config.Uploads{ + h, err := handler.NewHandler(1024, 500, config.Uploads{ Dir: os.TempDir(), Forbid: []string{}, }, nil, p) @@ -1317,7 +1317,7 @@ func TestHandler_Error3(t *testing.T) { p.Destroy(context.Background()) }() - h, err := handler.NewHandler(1, config.Uploads{ + h, err := handler.NewHandler(1, 500, config.Uploads{ Dir: os.TempDir(), Forbid: []string{}, }, nil, p) @@ -1376,7 +1376,7 @@ func TestHandler_ResponseDuration(t *testing.T) { p.Destroy(context.Background()) }() - h, err := handler.NewHandler(1024, config.Uploads{ + h, err := handler.NewHandler(1024, 500, config.Uploads{ Dir: os.TempDir(), Forbid: []string{}, }, nil, p) @@ -1437,7 +1437,7 @@ func TestHandler_ResponseDurationDelayed(t *testing.T) { p.Destroy(context.Background()) }() - h, err := handler.NewHandler(1024, config.Uploads{ + h, err := handler.NewHandler(1024, 500, config.Uploads{ Dir: os.TempDir(), Forbid: []string{}, }, nil, p) @@ -1497,7 +1497,7 @@ func TestHandler_ErrorDuration(t *testing.T) { p.Destroy(context.Background()) }() - h, err := handler.NewHandler(1024, config.Uploads{ + h, err := handler.NewHandler(1024, 500, config.Uploads{ Dir: os.TempDir(), Forbid: []string{}, }, nil, p) @@ -1572,7 +1572,7 @@ func TestHandler_IP(t *testing.T) { p.Destroy(context.Background()) }() - h, err := handler.NewHandler(1024, config.Uploads{ + h, err := handler.NewHandler(1024, 500, config.Uploads{ Dir: os.TempDir(), Forbid: []string{}, }, cidrs, p) @@ -1633,7 +1633,7 @@ func TestHandler_XRealIP(t *testing.T) { p.Destroy(context.Background()) }() - h, err := handler.NewHandler(1024, config.Uploads{ + h, err := handler.NewHandler(1024, 500, config.Uploads{ Dir: os.TempDir(), Forbid: []string{}, }, cidrs, p) @@ -1699,7 +1699,7 @@ func TestHandler_XForwardedFor(t *testing.T) { p.Destroy(context.Background()) }() - h, err := handler.NewHandler(1024, config.Uploads{ + h, err := handler.NewHandler(1024, 500, config.Uploads{ Dir: os.TempDir(), Forbid: []string{}, }, cidrs, p) @@ -1764,7 +1764,7 @@ func TestHandler_XForwardedFor_NotTrustedRemoteIp(t *testing.T) { p.Destroy(context.Background()) }() - h, err := handler.NewHandler(1024, config.Uploads{ + h, err := handler.NewHandler(1024, 500, config.Uploads{ Dir: os.TempDir(), Forbid: []string{}, }, cidrs, p) @@ -1812,7 +1812,7 @@ func BenchmarkHandler_Listen_Echo(b *testing.B) { p.Destroy(context.Background()) }() - h, err := handler.NewHandler(1024, config.Uploads{ + h, err := handler.NewHandler(1024, 500, config.Uploads{ Dir: os.TempDir(), Forbid: []string{}, }, nil, p) diff --git a/tests/plugins/http/http_plugin_test.go b/tests/plugins/http/http_plugin_test.go index f8044c97..a1fc94ec 100644 --- a/tests/plugins/http/http_plugin_test.go +++ b/tests/plugins/http/http_plugin_test.go @@ -2208,7 +2208,7 @@ func echoIssue659(t *testing.T) { b, err := ioutil.ReadAll(r.Body) assert.NoError(t, err) assert.Empty(t, b) - assert.Equal(t, 500, r.StatusCode) + assert.Equal(t, 444, r.StatusCode) err = r.Body.Close() assert.NoError(t, err) diff --git a/tests/plugins/http/uploads_test.go b/tests/plugins/http/uploads_test.go index 903a930a..df696668 100644 --- a/tests/plugins/http/uploads_test.go +++ b/tests/plugins/http/uploads_test.go @@ -40,7 +40,7 @@ func TestHandler_Upload_File(t *testing.T) { t.Fatal(err) } - h, err := handler.NewHandler(1024, config.Uploads{ + h, err := handler.NewHandler(1024, 500, config.Uploads{ Dir: os.TempDir(), Forbid: []string{}, }, nil, pool) @@ -123,7 +123,7 @@ func TestHandler_Upload_NestedFile(t *testing.T) { t.Fatal(err) } - h, err := handler.NewHandler(1024, config.Uploads{ + h, err := handler.NewHandler(1024, 500, config.Uploads{ Dir: os.TempDir(), Forbid: []string{}, }, nil, pool) @@ -206,7 +206,7 @@ func TestHandler_Upload_File_NoTmpDir(t *testing.T) { t.Fatal(err) } - h, err := handler.NewHandler(1024, config.Uploads{ + h, err := handler.NewHandler(1024, 500, config.Uploads{ Dir: "-------", Forbid: []string{}, }, nil, pool) @@ -289,7 +289,7 @@ func TestHandler_Upload_File_Forbids(t *testing.T) { t.Fatal(err) } - h, err := handler.NewHandler(1024, config.Uploads{ + h, err := handler.NewHandler(1024, 500, config.Uploads{ Dir: os.TempDir(), Forbid: []string{".go"}, }, nil, pool) |