Skip to content

Commit a6257ad

Browse files
author
zach
authored
feat: allow max HTTP response size to be configured in the manifest (#59)
See extism/extism#674
1 parent 91f663b commit a6257ad

File tree

2 files changed

+30
-23
lines changed

2 files changed

+30
-23
lines changed

extism.go

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,14 @@ type Plugin struct {
8585
Timeout time.Duration
8686
Config map[string]string
8787
// NOTE: maybe we can have some nice methods for getting/setting vars
88-
Var map[string][]byte
89-
AllowedHosts []string
90-
AllowedPaths map[string]string
91-
LastStatusCode int
92-
log func(LogLevel, string)
93-
logLevel LogLevel
94-
guestRuntime guestRuntime
88+
Var map[string][]byte
89+
AllowedHosts []string
90+
AllowedPaths map[string]string
91+
LastStatusCode int
92+
MaxHttpResponseBytes int64
93+
log func(LogLevel, string)
94+
logLevel LogLevel
95+
guestRuntime guestRuntime
9596
}
9697

9798
func logStd(level LogLevel, message string) {
@@ -220,7 +221,8 @@ func (u WasmUrl) ToWasmData(ctx context.Context) (WasmData, error) {
220221
type Manifest struct {
221222
Wasm []Wasm `json:"wasm"`
222223
Memory struct {
223-
MaxPages uint32 `json:"max_pages,omitempty"`
224+
MaxPages uint32 `json:"max_pages,omitempty"`
225+
MaxHttpResponseBytes uint64 `json:"max_http_response_bytes,omitempty"`
224226
} `json:"memory,omitempty"`
225227
Config map[string]string `json:"config,omitempty"`
226228
AllowedHosts []string `json:"allowed_hosts,omitempty"`
@@ -231,7 +233,8 @@ type Manifest struct {
231233
type concreteManifest struct {
232234
Wasm []concreteWasm `json:"wasm"`
233235
Memory struct {
234-
MaxPages uint32 `json:"max_pages,omitempty"`
236+
MaxPages uint32 `json:"max_pages,omitempty"`
237+
MaxHttpResponseBytes uint64 `json:"max_http_response_bytes,omitempty"`
235238
} `json:"memory,omitempty"`
236239
Config map[string]string `json:"config,omitempty"`
237240
AllowedHosts []string `json:"allowed_hosts,omitempty"`
@@ -415,20 +418,26 @@ func NewPlugin(
415418
}
416419

417420
i := 0
421+
httpMax := int64(1024 * 1024 * 50)
422+
if manifest.Memory.MaxHttpResponseBytes != 0 {
423+
httpMax = int64(manifest.Memory.MaxHttpResponseBytes)
424+
}
418425
for _, m := range modules {
419426
if m.Name() == "main" {
420427
p := &Plugin{
421-
Runtime: &c,
422-
Modules: modules,
423-
Main: m,
424-
Config: manifest.Config,
425-
Var: map[string][]byte{},
426-
AllowedHosts: manifest.AllowedHosts,
427-
AllowedPaths: manifest.AllowedPaths,
428-
LastStatusCode: 0,
429-
Timeout: time.Duration(manifest.Timeout) * time.Millisecond,
430-
log: logStd,
431-
logLevel: logLevel}
428+
Runtime: &c,
429+
Modules: modules,
430+
Main: m,
431+
Config: manifest.Config,
432+
Var: map[string][]byte{},
433+
AllowedHosts: manifest.AllowedHosts,
434+
AllowedPaths: manifest.AllowedPaths,
435+
LastStatusCode: 0,
436+
Timeout: time.Duration(manifest.Timeout) * time.Millisecond,
437+
MaxHttpResponseBytes: httpMax,
438+
log: logStd,
439+
logLevel: logLevel,
440+
}
432441

433442
p.guestRuntime = detectGuestRuntime(p)
434443
return p, nil

host.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -509,9 +509,7 @@ func httpRequest(ctx context.Context, m api.Module, requestOffset uint64, bodyOf
509509

510510
plugin.LastStatusCode = resp.StatusCode
511511

512-
// TODO: make this limit configurable
513-
// TODO: the rust implementation silently truncates the response body, should we keep the behavior here?
514-
limiter := http.MaxBytesReader(nil, resp.Body, 1024*1024*50)
512+
limiter := http.MaxBytesReader(nil, resp.Body, int64(plugin.MaxHttpResponseBytes))
515513
body, err := io.ReadAll(limiter)
516514
if err != nil {
517515
panic(err)

0 commit comments

Comments
 (0)