Repository navigation
feat(vhost): support custom error pages via server config #5610
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Tommickey2020gmail
wants to merge
1
commit into
fatedier:dev
Choose a base branch
from
Tommickey2020gmail:feat/custom-error-pages
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,106 @@ | ||
| package vhost | ||
|
|
||
| import ( | ||
| "io" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func resetErrorPageState(t *testing.T) { | ||
| t.Helper() | ||
| oldPath, oldPages := NotFoundPagePath, customErrorPages | ||
| NotFoundPagePath = "" | ||
| customErrorPages = make(map[int]CustomErrorPage) | ||
| t.Cleanup(func() { | ||
| NotFoundPagePath = oldPath | ||
| customErrorPages = oldPages | ||
| }) | ||
| } | ||
|
|
||
| func TestResolveNotFoundPage(t *testing.T) { | ||
| t.Run("default page", func(t *testing.T) { | ||
| resetErrorPageState(t) | ||
| p := resolveNotFoundPage() | ||
| require.Equal(t, NotFound, p.Body) | ||
| require.Equal(t, "text/html", p.contentType()) | ||
| }) | ||
|
|
||
| t.Run("custom error page for 404", func(t *testing.T) { | ||
| resetErrorPageState(t) | ||
| customErrorPages[http.StatusNotFound] = CustomErrorPage{ | ||
| ContentType: "text/plain", | ||
| Body: "custom not found", | ||
| Headers: map[string]string{"X-Custom": "yes"}, | ||
| } | ||
| p := resolveNotFoundPage() | ||
| require.Equal(t, "custom not found", p.Body) | ||
| require.Equal(t, "text/plain", p.contentType()) | ||
| require.Equal(t, "yes", p.Headers["X-Custom"]) | ||
| }) | ||
|
|
||
| t.Run("custom404Page file takes precedence", func(t *testing.T) { | ||
| resetErrorPageState(t) | ||
| customErrorPages[http.StatusNotFound] = CustomErrorPage{Body: "inline"} | ||
| file := filepath.Join(t.TempDir(), "404.html") | ||
| require.NoError(t, os.WriteFile(file, []byte("from file"), 0o644)) | ||
| NotFoundPagePath = file | ||
|
|
||
| p := resolveNotFoundPage() | ||
| require.Equal(t, "from file", p.Body) | ||
| }) | ||
|
|
||
| t.Run("missing custom404Page file falls back to inline page", func(t *testing.T) { | ||
| resetErrorPageState(t) | ||
| customErrorPages[http.StatusNotFound] = CustomErrorPage{Body: "inline"} | ||
| NotFoundPagePath = filepath.Join(t.TempDir(), "missing.html") | ||
|
|
||
| p := resolveNotFoundPage() | ||
| require.Equal(t, "inline", p.Body) | ||
| }) | ||
| } | ||
|
|
||
| func TestWriteCustomErrorPage(t *testing.T) { | ||
| t.Run("no custom page", func(t *testing.T) { | ||
| resetErrorPageState(t) | ||
| rw := httptest.NewRecorder() | ||
| require.False(t, writeCustomErrorPage(rw, http.StatusGatewayTimeout)) | ||
| require.Equal(t, http.StatusOK, rw.Code) | ||
| }) | ||
|
|
||
| t.Run("writes custom page", func(t *testing.T) { | ||
| resetErrorPageState(t) | ||
| customErrorPages[http.StatusGatewayTimeout] = CustomErrorPage{ | ||
| Body: "timeout page", | ||
| ContentType: "text/plain", | ||
| Headers: map[string]string{"Retry-After": "30"}, | ||
| } | ||
| rw := httptest.NewRecorder() | ||
| require.True(t, writeCustomErrorPage(rw, http.StatusGatewayTimeout)) | ||
| require.Equal(t, http.StatusGatewayTimeout, rw.Code) | ||
| require.Equal(t, "text/plain", rw.Header().Get("Content-Type")) | ||
| require.Equal(t, "30", rw.Header().Get("Retry-After")) | ||
| require.Equal(t, "timeout page", rw.Body.String()) | ||
| }) | ||
| } | ||
|
|
||
| func TestNotFoundResponseCustomPage(t *testing.T) { | ||
| resetErrorPageState(t) | ||
| customErrorPages[http.StatusNotFound] = CustomErrorPage{ | ||
| Body: "inline not found", | ||
| ContentType: "application/json", | ||
| } | ||
|
|
||
| res := NotFoundResponse() | ||
| defer res.Body.Close() | ||
| require.Equal(t, http.StatusNotFound, res.StatusCode) | ||
| require.Equal(t, "application/json", res.Header.Get("Content-Type")) | ||
| body, err := io.ReadAll(res.Body) | ||
| require.NoError(t, err) | ||
| require.Equal(t, "inline not found", string(body)) | ||
| require.Equal(t, int64(len("inline not found")), res.ContentLength) | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WARNING: Move the error-page examples below the remaining root settings
Uncommenting these examples in their current location leaves TOML inside
[customErrorPages.headers], so the followingudpPacketSize = 1500andnatholeAnalysisDataReserveHours = 168become response-header entries instead of server settings. Configuration loading then fails becauseCustomErrorPage.Headersis amap[string]stringbut those values are numbers, even with strict parsing disabled. Enabling only the 404 example also captures those settings inside its array element and fails default strict parsing. Move the entire example block after the remaining root/dotted settings, immediately before[[httpPlugins]], so enabling this documented option does not prevent frps from starting.