-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Error handling and fix potential security bug
- Loading branch information
Showing
2 changed files
with
66 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/valyala/fasthttp" | ||
"log" | ||
"strings" | ||
) | ||
|
||
func HandleNotAllowed(ctx *fasthttp.RequestCtx, message string) { | ||
status := fasthttp.StatusMethodNotAllowed | ||
ctx.Response.SetStatusCode(status) | ||
fmt.Fprintf(ctx, "%v %s\n", status, message) | ||
} | ||
|
||
func HandleGeneric(ctx *fasthttp.RequestCtx, status int, message string) { | ||
ctx.Response.SetStatusCode(status) | ||
fmt.Fprintf(ctx, "%v %s\n", status, message) | ||
} | ||
|
||
func HandleForbidden(ctx *fasthttp.RequestCtx) { | ||
ctx.Response.SetStatusCode(fasthttp.StatusForbidden) | ||
fmt.Fprint(ctx, "403 Forbidden\n") | ||
log.Printf( | ||
"- Returned 403 to %s - tried to connect with '%s' to '%s'", | ||
ctx.RemoteIP(), ctx.Request.Header.Peek("Auth"), ctx.Path()) | ||
} | ||
|
||
func HandleInternalServerError(ctx *fasthttp.RequestCtx, err error) { | ||
if strings.HasSuffix(err.Error(), "no such file or directory") { | ||
HandleGeneric(ctx, fasthttp.StatusNotFound, "Not Found") | ||
return | ||
} | ||
|
||
ctx.Response.SetStatusCode(fasthttp.StatusInternalServerError) | ||
fmt.Fprintf(ctx, "500 %v\n", err) | ||
log.Printf("- Returned 500 to %s with error %v", ctx.RemoteIP(), err) | ||
} |
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