Skip to content

Commit

Permalink
[patch] remove check when response is already written, prevent writin…
Browse files Browse the repository at this point in the history
…g any further.

[-] the blocking behaviour was breaking large responses like files and such
  • Loading branch information
bnkamalesh committed Aug 6, 2021
1 parent db2032b commit 930cb6d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
30 changes: 29 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@ package main

import (
"errors"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"

"github.com/bnkamalesh/webgo/v5"
"github.com/bnkamalesh/webgo/v5/middleware/accesslog"
"github.com/bnkamalesh/webgo/v5/middleware/cors"
)

var (
lastModified = time.Now().Format(http.TimeFormat)
)

func helloWorld(w http.ResponseWriter, r *http.Request) {
// WebGo context
wctx := webgo.Context(r)
Expand Down Expand Up @@ -70,6 +77,16 @@ func errLogger(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
}
}

// StaticFiles is used to serve static files
func StaticFiles(rw http.ResponseWriter, r *http.Request) {
wctx := webgo.Context(r)
// '..' is replaced to prevent directory traversal which could go out of static directory
path := strings.ReplaceAll(wctx.Params()["w"], "..", "-")

rw.Header().Set("Last-Modified", lastModified)
http.ServeFile(rw, r, fmt.Sprintf("./cmd/static/%s", path))
}

func getRoutes() []*webgo.Route {
return []*webgo.Route{
{
Expand Down Expand Up @@ -115,13 +132,24 @@ func getRoutes() []*webgo.Route {
Handlers: []http.HandlerFunc{originalResponseWriter},
TrailingSlash: true,
},
{
Name: "static",
Method: http.MethodGet,
Pattern: "/static/:w*",
Handlers: []http.HandlerFunc{StaticFiles},
TrailingSlash: true,
},
}
}

func main() {
port := strings.TrimSpace(os.Getenv("HTTP_PORT"))
if port == "" {
port = "8080"
}
cfg := &webgo.Config{
Host: "",
Port: "8080",
Port: port,
ReadTimeout: 15 * time.Second,
WriteTimeout: 60 * time.Second,
}
Expand Down
Binary file added cmd/static/images/bird.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 1 addition & 7 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type customResponseWriter struct {
// WriteHeader is the interface implementation to get HTTP response code and add
// it to the custom response writer
func (crw *customResponseWriter) WriteHeader(code int) {
if crw.written || crw.headerWritten {
if crw.headerWritten {
return
}

Expand All @@ -70,13 +70,7 @@ func (crw *customResponseWriter) WriteHeader(code int) {
// Write is the interface implementation to respond to the HTTP request,
// but check if a response was already sent.
func (crw *customResponseWriter) Write(body []byte) (int, error) {
if crw.written {
LOGHANDLER.Warn(errMultiWrite)
return 0, nil
}

crw.WriteHeader(crw.statusCode)

crw.written = true
return crw.ResponseWriter.Write(body)
}
Expand Down

0 comments on commit 930cb6d

Please sign in to comment.