Skip to content

Commit b62155f

Browse files
committed
internal: switch to withAdaptiveBuffer()
1 parent fbba839 commit b62155f

File tree

2 files changed

+43
-76
lines changed

2 files changed

+43
-76
lines changed

fsthttp/request.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,12 @@ type Request struct {
117117

118118
sent bool // a request may only be sent once
119119

120-
abi struct {
121-
req *fastly.HTTPRequest
122-
body *fastly.HTTPBody
123-
}
120+
abi reqAbi
121+
}
122+
123+
type reqAbi struct {
124+
req *fastly.HTTPRequest
125+
body *fastly.HTTPBody
124126
}
125127

126128
// NewRequest constructs an outgoing request with the given HTTP method, URI,
@@ -247,6 +249,7 @@ func newClientRequest(abiReq *fastly.HTTPRequest, abiReqBody *fastly.HTTPBody) (
247249
ServerAddr: serverAddr.String(),
248250
TLSInfo: tlsInfo,
249251
RequestID: reqID,
252+
abi: reqAbi{req: abiReq, body: abiReqBody},
250253
}, nil
251254
}
252255

internal/abi/fastly/http_guest.go

Lines changed: 36 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,24 +1314,18 @@ func fastlyHTTPDownstreamH2Fingerprint(
13141314
) FastlyStatus
13151315

13161316
func (r *HTTPRequest) DownstreamH2Fingerprint() ([]byte, error) {
1317-
n := DefaultMediumBufLen
1318-
for {
1319-
buf := prim.NewWriteBuffer(n)
1320-
status := fastlyHTTPDownstreamH2Fingerprint(
1317+
value, err := withAdaptiveBuffer(DefaultSmallBufLen, func(buf *prim.WriteBuffer) FastlyStatus {
1318+
return fastlyHTTPDownstreamH2Fingerprint(
13211319
r.h,
13221320
prim.ToPointer(buf.Char8Pointer()),
13231321
buf.Cap(),
13241322
prim.ToPointer(buf.NPointer()),
13251323
)
1326-
if status == FastlyStatusBufLen && buf.NValue() > 0 {
1327-
n = int(buf.NValue())
1328-
continue
1329-
}
1330-
if err := status.toError(); err != nil {
1331-
return nil, err
1332-
}
1333-
return buf.AsBytes(), nil
1324+
})
1325+
if err != nil {
1326+
return nil, err
13341327
}
1328+
return value.AsBytes(), nil
13351329
}
13361330

13371331
// witx;
@@ -1354,24 +1348,18 @@ func fastlyHTTPDownstreamRequestID(
13541348
) FastlyStatus
13551349

13561350
func (r *HTTPRequest) DownstreamRequestID() (string, error) {
1357-
n := DefaultSmallBufLen
1358-
for {
1359-
buf := prim.NewWriteBuffer(n)
1360-
status := fastlyHTTPDownstreamRequestID(
1351+
value, err := withAdaptiveBuffer(DefaultSmallBufLen, func(buf *prim.WriteBuffer) FastlyStatus {
1352+
return fastlyHTTPDownstreamRequestID(
13611353
r.h,
13621354
prim.ToPointer(buf.Char8Pointer()),
13631355
buf.Cap(),
13641356
prim.ToPointer(buf.NPointer()),
13651357
)
1366-
if status == FastlyStatusBufLen && buf.NValue() > 0 {
1367-
n = int(buf.NValue())
1368-
continue
1369-
}
1370-
if err := status.toError(); err != nil {
1371-
return "", err
1372-
}
1373-
return buf.ToString(), nil
1358+
})
1359+
if err != nil {
1360+
return "", err
13741361
}
1362+
return value.ToString(), nil
13751363
}
13761364

13771365
// witx:
@@ -1394,24 +1382,18 @@ func fastlyHTTPDownstreamOHFingerprint(
13941382
) FastlyStatus
13951383

13961384
func (r *HTTPRequest) DownstreamOHFingerprint() ([]byte, error) {
1397-
n := DefaultSmallBufLen
1398-
for {
1399-
buf := prim.NewWriteBuffer(n)
1400-
status := fastlyHTTPDownstreamOHFingerprint(
1385+
value, err := withAdaptiveBuffer(DefaultSmallBufLen, func(buf *prim.WriteBuffer) FastlyStatus {
1386+
return fastlyHTTPDownstreamOHFingerprint(
14011387
r.h,
14021388
prim.ToPointer(buf.Char8Pointer()),
14031389
buf.Cap(),
14041390
prim.ToPointer(buf.NPointer()),
14051391
)
1406-
if status == FastlyStatusBufLen && buf.NValue() > 0 {
1407-
n = int(buf.NValue())
1408-
continue
1409-
}
1410-
if err := status.toError(); err != nil {
1411-
return nil, err
1412-
}
1413-
return buf.AsBytes(), nil
1392+
})
1393+
if err != nil {
1394+
return nil, err
14141395
}
1396+
return value.AsBytes(), nil
14151397
}
14161398

14171399
// witx:
@@ -1573,24 +1555,18 @@ func fastlyHTTPReqDownstreamTLSRawCertificate(
15731555
) FastlyStatus
15741556

15751557
func (r *HTTPRequest) DownstreamTLSRawCertificate() ([]byte, error) {
1576-
n := DefaultLargeBufLen // Longest (~132,000); typically < 2^14; RFC https://datatracker.ietf.org/doc/html/rfc8446#section-4.1.2
1577-
for {
1578-
buf := prim.NewWriteBuffer(n)
1579-
status := fastlyHTTPReqDownstreamTLSRawCertificate(
1558+
value, err := withAdaptiveBuffer(DefaultLargeBufLen, func(buf *prim.WriteBuffer) FastlyStatus {
1559+
return fastlyHTTPReqDownstreamTLSRawCertificate(
15801560
r.h,
15811561
prim.ToPointer(buf.Char8Pointer()),
15821562
buf.Cap(),
15831563
prim.ToPointer(buf.NPointer()),
15841564
)
1585-
if status == FastlyStatusBufLen && buf.NValue() > 0 {
1586-
n = int(buf.NValue())
1587-
continue
1588-
}
1589-
if err := status.toError(); err != nil {
1590-
return nil, err
1591-
}
1592-
return buf.AsBytes(), nil
1565+
})
1566+
if err != nil {
1567+
return nil, err
15931568
}
1569+
return value.AsBytes(), nil
15941570
}
15951571

15961572
// witx:
@@ -1671,24 +1647,18 @@ func fastlyHTTPReqDownstreamTLSJA4(
16711647
) FastlyStatus
16721648

16731649
func (r *HTTPRequest) DownstreamTLSJA4() ([]byte, error) {
1674-
n := DefaultSmallBufLen // JA4 hashes should be <64 bytes
1675-
for {
1676-
buf := prim.NewWriteBuffer(n)
1677-
status := fastlyHTTPReqDownstreamTLSJA4(
1650+
value, err := withAdaptiveBuffer(DefaultSmallBufLen, func(buf *prim.WriteBuffer) FastlyStatus {
1651+
return fastlyHTTPReqDownstreamTLSJA4(
16781652
r.h,
16791653
prim.ToPointer(buf.Char8Pointer()),
16801654
buf.Cap(),
16811655
prim.ToPointer(buf.NPointer()),
16821656
)
1683-
if status == FastlyStatusBufLen && buf.NValue() > 0 {
1684-
n = int(buf.NValue())
1685-
continue
1686-
}
1687-
if err := status.toError(); err != nil {
1688-
return nil, err
1689-
}
1690-
return buf.AsBytes(), nil
1657+
})
1658+
if err != nil {
1659+
return nil, err
16911660
}
1661+
return value.AsBytes(), nil
16921662
}
16931663

16941664
// witx:
@@ -1712,24 +1682,18 @@ func fastlyHTTPReqDownstreamComplianceRegion(
17121682

17131683
// DownstreamComplianceRegion returns the compliance region (US/EU/None) for the request.
17141684
func (r *HTTPRequest) DownstreamComplianceRegion() (string, error) {
1715-
n := 4
1716-
for {
1717-
buf := prim.NewWriteBuffer(n)
1718-
status := fastlyHTTPReqDownstreamComplianceRegion(
1685+
value, err := withAdaptiveBuffer(4, func(buf *prim.WriteBuffer) FastlyStatus {
1686+
return fastlyHTTPReqDownstreamComplianceRegion(
17191687
r.h,
17201688
prim.ToPointer(buf.Char8Pointer()),
17211689
buf.Cap(),
17221690
prim.ToPointer(buf.NPointer()),
17231691
)
1724-
if status == FastlyStatusBufLen && buf.NValue() > 0 {
1725-
n = int(buf.NValue())
1726-
continue
1727-
}
1728-
if err := status.toError(); err != nil {
1729-
return "", err
1730-
}
1731-
return buf.ToString(), nil
1692+
})
1693+
if err != nil {
1694+
return "", err
17321695
}
1696+
return value.ToString(), nil
17331697
}
17341698

17351699
// witx;

0 commit comments

Comments
 (0)