Skip to content

Commit 7d19609

Browse files
authored
Merge pull request #26 from fastly/fgsch/rework-limits
Rework limits
2 parents 0a324dd + 8472634 commit 7d19609

File tree

7 files changed

+102
-72
lines changed

7 files changed

+102
-72
lines changed

_examples/limits/main.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2023 Fastly, Inc.
2+
3+
package main
4+
5+
import (
6+
"context"
7+
"fmt"
8+
9+
"github.com/fastly/compute-sdk-go/fsthttp"
10+
)
11+
12+
func main() {
13+
// Increase URL length limit to 16K
14+
fsthttp.RequestLimits.SetMaxURLLen(16 * 1024)
15+
16+
fsthttp.ServeFunc(func(ctx context.Context, w fsthttp.ResponseWriter, r *fsthttp.Request) {
17+
fmt.Fprintf(w, "The length of the URL is %d\n", len(r.URL.String()))
18+
})
19+
}

fsthttp/limits.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,49 @@
11
package fsthttp
22

3-
import "github.com/fastly/compute-sdk-go/internal/abi/fastly"
4-
53
// Limits handles HTTP limits
6-
var Limits = limits{}
7-
8-
type limits struct {
4+
type Limits struct {
5+
maxHeaderNameLen int
6+
maxHeaderValueLen int
7+
maxMethodLen int
8+
maxURLLen int
99
}
1010

1111
// MaxHeaderNameLen gets the header name limit
12-
func (limits) MaxHeaderNameLen() int {
13-
return fastly.MaxHeaderNameLen
12+
func (limits *Limits) MaxHeaderNameLen() int {
13+
return limits.maxHeaderNameLen
1414
}
1515

1616
// SetMaxHeaderNameLen sets the header name limit
17-
func (limits) SetMaxHeaderNameLen(len int) {
18-
fastly.MaxHeaderNameLen = len
17+
func (limits *Limits) SetMaxHeaderNameLen(len int) {
18+
limits.maxHeaderNameLen = len
1919
}
2020

2121
// MaxHeaderValueLen gets the header value limit
22-
func (limits) MaxHeaderValueLen() int {
23-
return fastly.MaxHeaderValueLen
22+
func (limits *Limits) MaxHeaderValueLen() int {
23+
return limits.maxHeaderValueLen
2424
}
2525

2626
// SetMaxHeaderValueLen sets the header value limit
27-
func (limits) SetMaxHeaderValueLen(len int) {
28-
fastly.MaxHeaderValueLen = len
27+
func (limits *Limits) SetMaxHeaderValueLen(len int) {
28+
limits.maxHeaderValueLen = len
2929
}
3030

3131
// MaxMethodLen gets the request method limit
32-
func (limits) MaxMethodLen() int {
33-
return fastly.MaxMethodLen
32+
func (limits *Limits) MaxMethodLen() int {
33+
return limits.maxMethodLen
3434
}
3535

3636
// SetMaxMethodLen sets the request method limit
37-
func (limits) SetMaxMethodLen(len int) {
38-
fastly.MaxMethodLen = len
37+
func (limits *Limits) SetMaxMethodLen(len int) {
38+
limits.maxMethodLen = len
3939
}
4040

4141
// MaxURLLen gets the request URL limit
42-
func (limits) MaxURLLen() int {
43-
return fastly.MaxURLLen
42+
func (limits *Limits) MaxURLLen() int {
43+
return limits.maxURLLen
4444
}
4545

4646
// SetMaxURLLen sets the request URL limit
47-
func (limits) SetMaxURLLen(len int) {
48-
fastly.MaxURLLen = len
47+
func (limits *Limits) SetMaxURLLen(len int) {
48+
limits.maxURLLen = len
4949
}

fsthttp/request.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ import (
1313
"github.com/fastly/compute-sdk-go/internal/abi/fastly"
1414
)
1515

16+
// RequestLimits are the limits for the components of an HTTP request.
17+
var RequestLimits = Limits{
18+
maxHeaderNameLen: fastly.DefaultMaxHeaderNameLen,
19+
maxHeaderValueLen: fastly.DefaultMaxHeaderValueLen,
20+
maxMethodLen: fastly.DefaultMaxMethodLen,
21+
maxURLLen: fastly.DefaultMaxURLLen,
22+
}
23+
1624
// Request represents an HTTP request received by this server from a requesting
1725
// client, or to be sent from this server during this execution. Some fields
1826
// only have meaning in one context or the other.
@@ -116,12 +124,12 @@ func newClientRequest() (*Request, error) {
116124
return nil, fmt.Errorf("get client request and body: %w", err)
117125
}
118126

119-
method, err := abiReq.GetMethod()
127+
method, err := abiReq.GetMethod(RequestLimits.maxMethodLen)
120128
if err != nil {
121129
return nil, fmt.Errorf("get method: %w", err)
122130
}
123131

124-
uri, err := abiReq.GetURI()
132+
uri, err := abiReq.GetURI(RequestLimits.maxURLLen)
125133
if err != nil {
126134
return nil, fmt.Errorf("get URI: %w", err)
127135
}
@@ -137,10 +145,10 @@ func newClientRequest() (*Request, error) {
137145
}
138146

139147
header := NewHeader()
140-
keys := abiReq.GetHeaderNames()
148+
keys := abiReq.GetHeaderNames(RequestLimits.maxHeaderNameLen)
141149
for keys.Next() {
142150
k := string(keys.Bytes())
143-
vals := abiReq.GetHeaderValues(k)
151+
vals := abiReq.GetHeaderValues(k, RequestLimits.maxHeaderValueLen)
144152
for vals.Next() {
145153
v := string(vals.Bytes())
146154
header.Add(k, v)

fsthttp/response.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ import (
1010
"github.com/fastly/compute-sdk-go/internal/abi/fastly"
1111
)
1212

13+
// ResponseLimits are the limits for the components of an HTTP response.
14+
var ResponseLimits = Limits{
15+
maxHeaderNameLen: fastly.DefaultMaxHeaderNameLen,
16+
maxHeaderValueLen: fastly.DefaultMaxHeaderValueLen,
17+
}
18+
1319
// Response to an outgoing HTTP request made by this server.
1420
type Response struct {
1521
// Request associated with the response.
@@ -40,10 +46,10 @@ func newResponse(req *Request, backend string, abiResp *fastly.HTTPResponse, abi
4046
}
4147

4248
header := NewHeader()
43-
keys := abiResp.GetHeaderNames()
49+
keys := abiResp.GetHeaderNames(ResponseLimits.maxHeaderNameLen)
4450
for keys.Next() {
4551
k := string(keys.Bytes())
46-
vals := abiResp.GetHeaderValues(k)
52+
vals := abiResp.GetHeaderValues(k, ResponseLimits.maxHeaderValueLen)
4753
for vals.Next() {
4854
v := string(vals.Bytes())
4955
header.Add(k, v)

internal/abi/fastly/hostcalls_guest.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ func fastlyHTTPReqHeaderNamesGet(
668668

669669
// GetHeaderNames returns an iterator that yields the names of each header of
670670
// the request.
671-
func (r *HTTPRequest) GetHeaderNames() *Values {
671+
func (r *HTTPRequest) GetHeaderNames(maxHeaderNameLen int) *Values {
672672
adapter := func(
673673
buf *prim.Char8,
674674
bufLen prim.Usize,
@@ -686,7 +686,7 @@ func (r *HTTPRequest) GetHeaderNames() *Values {
686686
)
687687
}
688688

689-
return newValues(adapter, MaxHeaderNameLen)
689+
return newValues(adapter, maxHeaderNameLen)
690690
}
691691

692692
// witx:
@@ -713,7 +713,7 @@ func fastlyHTTPReqOriginalHeaderNamesGet(
713713

714714
// GetOriginalHeaderNames returns an iterator that yields the names of each
715715
// header of the singleton downstream request.
716-
func GetOriginalHeaderNames() *Values {
716+
func GetOriginalHeaderNames(maxHeaderNameLen int) *Values {
717717
adapter := func(
718718
buf *prim.Char8,
719719
bufLen prim.Usize,
@@ -730,7 +730,7 @@ func GetOriginalHeaderNames() *Values {
730730
)
731731
}
732732

733-
return newValues(adapter, MaxHeaderNameLen)
733+
return newValues(adapter, maxHeaderNameLen)
734734
}
735735

736736
// witx:
@@ -784,8 +784,8 @@ func fastlyHTTPReqHeaderValueGet(
784784

785785
// GetHeaderValue returns the first header value of the given header name on the
786786
// request, if any.
787-
func (r *HTTPRequest) GetHeaderValue(name string) (string, error) {
788-
buf := prim.NewWriteBuffer(MaxHeaderValueLen)
787+
func (r *HTTPRequest) GetHeaderValue(name string, maxHeaderValueLen int) (string, error) {
788+
buf := prim.NewWriteBuffer(maxHeaderValueLen)
789789
if err := fastlyHTTPReqHeaderValueGet(
790790
r.h,
791791
prim.NewReadBufferFromString(name).ArrayU8(),
@@ -827,7 +827,7 @@ func fastlyHTTPReqHeaderValuesGet(
827827

828828
// GetHeaderValues returns an iterator that yields the values for the named
829829
// header that are of the request.
830-
func (r *HTTPRequest) GetHeaderValues(name string) *Values {
830+
func (r *HTTPRequest) GetHeaderValues(name string, maxHeaderValueLen int) *Values {
831831
adapter := func(
832832
buf *prim.Char8,
833833
bufLen prim.Usize,
@@ -846,7 +846,7 @@ func (r *HTTPRequest) GetHeaderValues(name string) *Values {
846846
)
847847
}
848848

849-
return newValues(adapter, MaxHeaderValueLen)
849+
return newValues(adapter, maxHeaderValueLen)
850850
}
851851

852852
// witx:
@@ -983,8 +983,8 @@ func fastlyHTTPReqMethodGet(
983983
) FastlyStatus
984984

985985
// GetMethod returns the HTTP method of the request.
986-
func (r *HTTPRequest) GetMethod() (string, error) {
987-
buf := prim.NewWriteBuffer(MaxMethodLen)
986+
func (r *HTTPRequest) GetMethod(maxMethodLen int) (string, error) {
987+
buf := prim.NewWriteBuffer(maxMethodLen)
988988
if err := fastlyHTTPReqMethodGet(
989989
r.h,
990990
buf.Char8Pointer(),
@@ -1042,8 +1042,8 @@ func fastlyHTTPReqURIGet(
10421042
) FastlyStatus
10431043

10441044
// GetURI returns the fully qualified URI of the request.
1045-
func (r *HTTPRequest) GetURI() (string, error) {
1046-
buf := prim.NewWriteBuffer(MaxURLLen)
1045+
func (r *HTTPRequest) GetURI(maxURLLen int) (string, error) {
1046+
buf := prim.NewWriteBuffer(maxURLLen)
10471047
if err := fastlyHTTPReqURIGet(
10481048
r.h,
10491049
buf.Char8Pointer(),
@@ -1522,7 +1522,7 @@ func fastlyHTTPRespHeaderNamesGet(
15221522

15231523
// GetHeaderNames returns an iterator that yields the names of each header of
15241524
// the response.
1525-
func (r *HTTPResponse) GetHeaderNames() *Values {
1525+
func (r *HTTPResponse) GetHeaderNames(maxHeaderNameLen int) *Values {
15261526
adapter := func(
15271527
buf *prim.Char8,
15281528
bufLen prim.Usize,
@@ -1540,7 +1540,7 @@ func (r *HTTPResponse) GetHeaderNames() *Values {
15401540
)
15411541
}
15421542

1543-
return newValues(adapter, MaxHeaderNameLen)
1543+
return newValues(adapter, maxHeaderNameLen)
15441544
}
15451545

15461546
// witx:
@@ -1567,8 +1567,8 @@ func fastlyHTTPRespHeaderValueGet(
15671567

15681568
// GetHeaderValue returns the first header value of the given header name on the
15691569
// response, if any.
1570-
func (r *HTTPResponse) GetHeaderValue(name string) (string, error) {
1571-
buf := prim.NewWriteBuffer(MaxHeaderValueLen)
1570+
func (r *HTTPResponse) GetHeaderValue(name string, maxHeaderValueLen int) (string, error) {
1571+
buf := prim.NewWriteBuffer(maxHeaderValueLen)
15721572
if err := fastlyHTTPRespHeaderValueGet(
15731573
r.h,
15741574
prim.NewReadBufferFromString(name).ArrayU8(),
@@ -1610,7 +1610,7 @@ func fastlyHTTPRespHeaderValuesGet(
16101610

16111611
// GetHeaderValues returns an iterator that yields the values for the named
16121612
// header that are of the response.
1613-
func (r *HTTPResponse) GetHeaderValues(name string) *Values {
1613+
func (r *HTTPResponse) GetHeaderValues(name string, maxHeaderValueLen int) *Values {
16141614
adapter := func(
16151615
buf *prim.Char8,
16161616
bufLen prim.Usize,
@@ -1629,7 +1629,7 @@ func (r *HTTPResponse) GetHeaderValues(name string) *Values {
16291629
)
16301630
}
16311631

1632-
return newValues(adapter, MaxHeaderValueLen)
1632+
return newValues(adapter, maxHeaderValueLen)
16331633
}
16341634

16351635
// witx:
@@ -2268,7 +2268,7 @@ func (s *Secret) Plaintext() ([]byte, error) {
22682268
// Most secrets will fit into the initial secret buffer size, so
22692269
// we'll start with that. If it doesn't fit, we'll know the exact
22702270
// size of the buffer to try again.
2271-
buf := prim.NewWriteBuffer(InitialSecretLen)
2271+
buf := prim.NewWriteBuffer(initialSecretLen)
22722272

22732273
status := fastlySecretPlaintext(
22742274
s.h,

internal/abi/fastly/hostcalls_noguest.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func NewHTTPRequest() (*HTTPRequest, error) {
7979
return nil, fmt.Errorf("not implemented")
8080
}
8181

82-
func (r *HTTPRequest) GetHeaderNames() *Values {
82+
func (r *HTTPRequest) GetHeaderNames(maxHeaderNameLen int) *Values {
8383
return nil
8484
}
8585

@@ -91,11 +91,11 @@ func GetOriginalHeaderCount() (int, error) {
9191
return 0, fmt.Errorf("not implemented")
9292
}
9393

94-
func (r *HTTPRequest) GetHeaderValue(name string) (string, error) {
94+
func (r *HTTPRequest) GetHeaderValue(name string, maxHeaderValueLen int) (string, error) {
9595
return "", fmt.Errorf("not implemented")
9696
}
9797

98-
func (r *HTTPRequest) GetHeaderValues(name string) *Values {
98+
func (r *HTTPRequest) GetHeaderValues(name string, maxHeaderValueLen int) *Values {
9999
return nil
100100
}
101101

@@ -115,15 +115,15 @@ func (r *HTTPRequest) RemoveHeader(name string) error {
115115
return fmt.Errorf("not implemented")
116116
}
117117

118-
func (r *HTTPRequest) GetMethod() (string, error) {
118+
func (r *HTTPRequest) GetMethod(maxMethodLen int) (string, error) {
119119
return "", fmt.Errorf("not implemented")
120120
}
121121

122122
func (r *HTTPRequest) SetMethod(method string) error {
123123
return fmt.Errorf("not implemented")
124124
}
125125

126-
func (r *HTTPRequest) GetURI() (string, error) {
126+
func (r *HTTPRequest) GetURI(maxURLLen int) (string, error) {
127127
return "", fmt.Errorf("not implemented")
128128
}
129129

@@ -179,15 +179,15 @@ func NewHTTPResponse() (*HTTPResponse, error) {
179179
return nil, fmt.Errorf("not implemented")
180180
}
181181

182-
func (r *HTTPResponse) GetHeaderNames() *Values {
182+
func (r *HTTPResponse) GetHeaderNames(maxHeaderNameLen int) *Values {
183183
return nil
184184
}
185185

186-
func (r *HTTPResponse) GetHeaderValue(name string) (string, error) {
186+
func (r *HTTPResponse) GetHeaderValue(name string, maxHeaderValueLen int) (string, error) {
187187
return "", fmt.Errorf("not implemented")
188188
}
189189

190-
func (r *HTTPResponse) GetHeaderValues(name string) *Values {
190+
func (r *HTTPResponse) GetHeaderValues(name string, maxHeaderValueLen int) *Values {
191191
return nil
192192
}
193193

0 commit comments

Comments
 (0)