Skip to content
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

Serve: send back 431 error instead of panicking when request header too big #27

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion fsthttp/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ package fsthttp

import (
"context"
"errors"
"fmt"
"sync"

"github.com/fastly/compute-sdk-go/internal/abi/fastly"
)

var (
Expand All @@ -23,11 +26,19 @@ func Serve(h Handler) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

headerTooLong := false

serveOnce.Do(func() {
var err error
clientRequest, err = newClientRequest()
if err != nil {
panic(fmt.Errorf("create client Request: %w", err))
status, ok := fastly.IsFastlyError(errors.Unwrap(err))
if ok && status == fastly.FastlyStatusBufLen {
headerTooLong = true
}
if !headerTooLong {
panic(fmt.Errorf("create client Request: %w", err))
}
}
clientResponseWriter, err = newResponseWriter()
if err != nil {
Expand All @@ -36,6 +47,11 @@ func Serve(h Handler) {
})

defer clientResponseWriter.Close()
if headerTooLong {
clientResponseWriter.WriteHeader(StatusRequestHeaderFieldsTooLarge)
clientResponseWriter.Write([]byte("Request Header Fields Too Large\n"))
return
}
h.ServeHTTP(ctx, clientResponseWriter, clientRequest)
}

Expand Down