-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
flathttp/middleware: add content length check with test
- Loading branch information
Showing
6 changed files
with
104 additions
and
7 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,32 @@ | ||
package flathttp | ||
|
||
import ( | ||
"net/http" | ||
"strconv" | ||
) | ||
|
||
type Middleware struct { | ||
MinContentLength int64 // inclusive | ||
MaxContentLength int64 // inclusive | ||
|
||
Accepts []string // content types that are accepted | ||
} | ||
|
||
func (m *Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
if r.ContentLength <= m.MinContentLength || r.ContentLength >= m.MaxContentLength { | ||
tmpl := `{"error": "content length is not acceptable", "given": [[GIVEN]], "min": [[MIN]], "max": [[MAX]]}` | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
w.Header().Set("X-Content-Type-Options", "nosniff") | ||
w.WriteHeader(http.StatusBadRequest) | ||
|
||
w.Write( | ||
T(tmpl, F{ | ||
"GIVEN": strconv.FormatInt(r.ContentLength, 10), | ||
"MIN": strconv.FormatInt(m.MinContentLength, 10), | ||
"MAX": strconv.FormatInt(m.MaxContentLength, 10), | ||
}), | ||
) | ||
return | ||
} | ||
} |
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,32 @@ | ||
package flathttp | ||
|
||
import ( | ||
"bytes" | ||
"github.com/stretchr/testify/require" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestContentLength(t *testing.T) { | ||
cases := []struct { | ||
given, min, max int64 | ||
err bool | ||
}{ | ||
{given: 10, min: 10, max: 100, err: true}, | ||
{given: 100, min: 10, max: 100, err: true}, | ||
{given: 11, min: 10, max: 100}, | ||
{given: 99, min: 10, max: 100}, | ||
} | ||
|
||
for _, test := range cases { | ||
m := &Middleware{MinContentLength: test.min, MaxContentLength: test.max} | ||
|
||
w := httptest.NewRecorder() | ||
r := httptest.NewRequest("GET", "/", bytes.NewReader(make([]byte, test.given))) | ||
m.ServeHTTP(w, r) | ||
|
||
check := (test.err && w.Code == http.StatusBadRequest) || (!test.err && w.Code == http.StatusOK) | ||
require.True(t, check) | ||
} | ||
} |
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
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,23 @@ | ||
package flathttp | ||
|
||
import ( | ||
"github.com/valyala/fasttemplate" | ||
"reflect" | ||
"unsafe" | ||
) | ||
|
||
type F map[string]interface{} | ||
|
||
func T(tmpl string, fields F) []byte { | ||
return B(fasttemplate.ExecuteString(tmpl, "[[", "]]", fields)) | ||
} | ||
|
||
func B(s string) []byte { | ||
sh := (*reflect.StringHeader)(unsafe.Pointer(&s)) | ||
bh := reflect.SliceHeader{ | ||
Data: sh.Data, | ||
Len: sh.Len, | ||
Cap: sh.Len, | ||
} | ||
return *(*[]byte)(unsafe.Pointer(&bh)) | ||
} |
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
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