mirror of
https://github.com/soypat/lneto.git
synced 2026-09-10 00:29:34 +00:00
remove backoff from form parsing
This commit is contained in:
@@ -115,7 +115,7 @@ func BenchmarkRequestParseForm(b *testing.B) {
|
|||||||
buf := make([]byte, 64)
|
buf := make([]byte, 64)
|
||||||
var mux MuxSlice
|
var mux MuxSlice
|
||||||
mux.Handle("POST /f", func(ex *Exchange) {
|
mux.Handle("POST /f", func(ex *Exchange) {
|
||||||
err := ex.RequestParseForm(&benchForm, buf, nopBackoff)
|
err := ex.RequestParseForm(&benchForm, buf)
|
||||||
if err != nil || benchForm.Len() != 2 {
|
if err != nil || benchForm.Len() != 2 {
|
||||||
panic("invalid result")
|
panic("invalid result")
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-11
@@ -423,8 +423,8 @@ func (exch *Exchange) RequestContentLength() (int64, error) {
|
|||||||
//
|
//
|
||||||
// A request with no Content-Length has no body, RFC 9112 6.3, and yields an
|
// A request with no Content-Length has no body, RFC 9112 6.3, and yields an
|
||||||
// empty form. Use [Exchange.RequestContentLength] to tell that apart from a body
|
// empty form. Use [Exchange.RequestContentLength] to tell that apart from a body
|
||||||
// that arrived empty. backoff paces reads that return no data, as in [Handle].
|
// that arrived empty.
|
||||||
func (exch *Exchange) RequestParseForm(dst *httpraw.Form, buf []byte, backoff lneto.BackoffStrategy) error {
|
func (exch *Exchange) RequestParseForm(dst *httpraw.Form, buf []byte) error {
|
||||||
if !httpraw.MediaTypeIs(exch.RequestContentType(), "application/x-www-form-urlencoded") {
|
if !httpraw.MediaTypeIs(exch.RequestContentType(), "application/x-www-form-urlencoded") {
|
||||||
return errNotFormEncoded
|
return errNotFormEncoded
|
||||||
} else if exch.RequestHeader("Transfer-Encoding") != nil {
|
} else if exch.RequestHeader("Transfer-Encoding") != nil {
|
||||||
@@ -440,18 +440,12 @@ func (exch *Exchange) RequestParseForm(dst *httpraw.Form, buf []byte, backoff ln
|
|||||||
return lneto.ErrBufferFull // Refuse before reading, caller may answer 413.
|
return lneto.ErrBufferFull // Refuse before reading, caller may answer 413.
|
||||||
}
|
}
|
||||||
buf = buf[:length]
|
buf = buf[:length]
|
||||||
var consecutiveBackoffs uint
|
|
||||||
for read := 0; read < len(buf); {
|
for read := 0; read < len(buf); {
|
||||||
n, err := exch.ReadBody(buf[read:])
|
n, err := exch.ReadBody(buf[read:])
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
} else if n == 0 {
|
|
||||||
backoff.Do(consecutiveBackoffs)
|
|
||||||
consecutiveBackoffs++
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
consecutiveBackoffs = 0
|
|
||||||
read += n
|
read += n
|
||||||
|
if n == 0 && err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
dst.Reset(buf)
|
dst.Reset(buf)
|
||||||
return dst.Parse()
|
return dst.Parse()
|
||||||
|
|||||||
@@ -720,7 +720,7 @@ func TestExchangeRequestParseForm(t *testing.T) {
|
|||||||
var sm MuxSlice
|
var sm MuxSlice
|
||||||
sm.Reset(1)
|
sm.Reset(1)
|
||||||
sm.Handle("/f", func(exch *Exchange) {
|
sm.Handle("/f", func(exch *Exchange) {
|
||||||
gotErr = exch.RequestParseForm(&form, make([]byte, bufSize), nopBackoff)
|
gotErr = exch.RequestParseForm(&form, make([]byte, bufSize))
|
||||||
})
|
})
|
||||||
serve(t, test.request, &sm)
|
serve(t, test.request, &sm)
|
||||||
if gotErr != test.wantErr {
|
if gotErr != test.wantErr {
|
||||||
@@ -743,7 +743,7 @@ func TestExchangeRequestParseFormSplit(t *testing.T) {
|
|||||||
var sm MuxSlice
|
var sm MuxSlice
|
||||||
sm.Reset(1)
|
sm.Reset(1)
|
||||||
sm.Handle("/f", func(exch *Exchange) {
|
sm.Handle("/f", func(exch *Exchange) {
|
||||||
gotErr = exch.RequestParseForm(&form, make([]byte, 64), nopBackoff)
|
gotErr = exch.RequestParseForm(&form, make([]byte, 64))
|
||||||
})
|
})
|
||||||
exch := newExchange(t, conn, ExchangeConfig{RawBuf: make([]byte, 2*1024), RequestBufferLim: 1024})
|
exch := newExchange(t, conn, ExchangeConfig{RawBuf: make([]byte, 2*1024), RequestBufferLim: 1024})
|
||||||
if err := Handle(exch, &sm, nopBackoff); err != nil {
|
if err := Handle(exch, &sm, nopBackoff); err != nil {
|
||||||
@@ -763,7 +763,7 @@ func TestExchangeRequestParseFormDecode(t *testing.T) {
|
|||||||
var sm MuxSlice
|
var sm MuxSlice
|
||||||
sm.Reset(1)
|
sm.Reset(1)
|
||||||
sm.Handle("/f", func(exch *Exchange) {
|
sm.Handle("/f", func(exch *Exchange) {
|
||||||
if err := exch.RequestParseForm(&form, make([]byte, 64), nopBackoff); err != nil {
|
if err := exch.RequestParseForm(&form, make([]byte, 64)); err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
} else if err = form.Decode(); err != nil {
|
} else if err = form.Decode(); err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
@@ -1015,7 +1015,9 @@ func TestHandleRequestTooLargeAnswers431(t *testing.T) {
|
|||||||
var request strings.Builder
|
var request strings.Builder
|
||||||
request.WriteString("GET /echo HTTP/1.1\r\nHost: lneto.test\r\n")
|
request.WriteString("GET /echo HTTP/1.1\r\nHost: lneto.test\r\n")
|
||||||
for i := range 512 {
|
for i := range 512 {
|
||||||
request.WriteString("H" + strconv.Itoa(i) + ":v\r\n")
|
request.WriteByte('H')
|
||||||
|
request.WriteString(strconv.Itoa(i))
|
||||||
|
request.WriteString(":v\r\n")
|
||||||
}
|
}
|
||||||
request.WriteString("\r\n")
|
request.WriteString("\r\n")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user