Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit cfa904a

Browse files
authored
Use dedicated headers/trailers types (#127)
Signed-off-by: Takeshi Yoneda <[email protected]>
1 parent df9a0b0 commit cfa904a

File tree

12 files changed

+180
-168
lines changed

12 files changed

+180
-168
lines changed

e2e/e2e_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ func getEnvoyConfigurationPath(t *testing.T, name string, ps envoyPorts) (string
161161
func helloworld(t *testing.T, ps envoyPorts, stdErr *bytes.Buffer) {
162162
out := stdErr.String()
163163
fmt.Println(out)
164-
assert.Contains(t, out, "wasm log helloworld: proxy_on_vm_start from Go!")
165-
assert.Contains(t, out, "wasm log helloworld: It's")
164+
assert.Contains(t, out, "helloworld: proxy_on_vm_start from Go!")
165+
assert.Contains(t, out, "helloworld: It's")
166166
}
167167

168168
func httpAuthRandom(t *testing.T, ps envoyPorts, stdErr *bytes.Buffer) {

examples/access_logger/main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func TestHelloWorld_OnTick(t *testing.T) {
1717
host := proxytest.NewHostEmulator(opt)
1818
defer host.Done() // release the host emulation lock so that other test cases can insert their own host emulation
1919

20-
host.CallOnLogForAccessLogger([][2]string{{":path", "/this/is/path"}}, nil) // call OnLog
20+
host.CallOnLogForAccessLogger(types.Headers{{":path", "/this/is/path"}}, nil) // call OnLog
2121

2222
logs := host.GetLogs(types.LogLevelInfo)
2323
require.Greater(t, len(logs), 0)

examples/dispatch_call_on_tick/main.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package main
1616

1717
import (
1818
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
19+
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
1920
)
2021

2122
const tickMilliseconds uint32 = 100
@@ -44,8 +45,10 @@ func (ctx *rootContext) OnVMStart(vmConfigurationSize int) bool {
4445
}
4546

4647
func (ctx *rootContext) OnTick() {
47-
hs := [][2]string{{":method", "GET"}, {":authority", "some_authority"}, {":path", "/path/to/service"}, {"accept", "*/*"}}
48-
if _, err := proxywasm.DispatchHttpCall("web_service", hs, "", [][2]string{},
48+
hs := types.Headers{
49+
{":method", "GET"}, {":authority", "some_authority"}, {":path", "/path/to/service"}, {"accept", "*/*"},
50+
}
51+
if _, err := proxywasm.DispatchHttpCall("web_service", hs, "", nil,
4952
5000, callback); err != nil {
5053
proxywasm.LogCriticalf("dispatch httpcall failed: %v", err)
5154
}

examples/http_auth_random/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (ctx *httpAuthRandom) OnHttpRequestHeaders(numHeaders int, endOfStream bool
4848
proxywasm.LogInfof("request header: %s: %s", h[0], h[1])
4949
}
5050

51-
if _, err := proxywasm.DispatchHttpCall(clusterName, hs, "", [][2]string{},
51+
if _, err := proxywasm.DispatchHttpCall(clusterName, hs, "", nil,
5252
50000, httpCallResponseCallback); err != nil {
5353
proxywasm.LogCriticalf("dipatch httpcall failed: %v", err)
5454
return types.ActionContinue
@@ -92,7 +92,7 @@ func httpCallResponseCallback(numHeaders, bodySize, numTrailers int) {
9292

9393
msg := "access forbidden"
9494
proxywasm.LogInfo(msg)
95-
proxywasm.SendHttpResponse(403, [][2]string{
95+
proxywasm.SendHttpResponse(403, types.Headers{
9696
{"powered-by", "proxy-wasm-go-sdk!!"},
9797
}, msg)
9898
}

examples/http_auth_random/main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func TestHttpAuthRandom_OnHttpRequestHeaders(t *testing.T) {
1717
defer host.Done()
1818

1919
contextID := host.HttpFilterInitContext()
20-
host.HttpFilterPutRequestHeaders(contextID, [][2]string{{"key", "value"}}) // OnHttpRequestHeaders called
20+
host.HttpFilterPutRequestHeaders(contextID, types.Headers{{"key", "value"}}) // OnHttpRequestHeaders called
2121

2222
attrs := host.GetCalloutAttributesFromContext(contextID)
2323
require.Equal(t, len(attrs), 1) // verify DispatchHttpCall is called

examples/http_headers/main_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func TestHttpHeaders_OnHttpRequestHeaders(t *testing.T) {
1818
defer host.Done()
1919
id := host.HttpFilterInitContext()
2020

21-
hs := [][2]string{{"key1", "value1"}, {"key2", "value2"}}
21+
hs := types.Headers{{"key1", "value1"}, {"key2", "value2"}}
2222
host.HttpFilterPutRequestHeaders(id, hs) // call OnHttpRequestHeaders
2323

2424
host.HttpFilterCompleteHttpStream(id)
@@ -38,7 +38,7 @@ func TestHttpHeaders_OnHttpResponseHeaders(t *testing.T) {
3838
defer host.Done()
3939
id := host.HttpFilterInitContext()
4040

41-
hs := [][2]string{{"key1", "value1"}, {"key2", "value2"}}
41+
hs := types.Headers{{"key1", "value1"}, {"key2", "value2"}}
4242
host.HttpFilterPutResponseHeaders(id, hs) // call OnHttpRequestHeaders
4343
host.HttpFilterCompleteHttpStream(id) // call OnHttpStreamDone
4444

proxytest/http.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ type (
2626
httpStreams map[uint32]*httpStreamState
2727
}
2828
httpStreamState struct {
29-
requestHeaders, responseHeaders,
30-
requestTrailers, responseTrailers [][2]string
31-
requestBody, responseBody []byte
29+
requestHeaders, responseHeaders types.Headers
30+
requestTrailers, responseTrailers types.Trailers
31+
requestBody, responseBody []byte
3232

3333
action types.Action
3434
sentLocalResponse *LocalHttpResponse
@@ -37,7 +37,7 @@ type (
3737
StatusCode uint32
3838
StatusCodeDetail string
3939
Data []byte
40-
Headers [][2]string
40+
Headers types.Headers
4141
GRPCStatus int32
4242
}
4343
)
@@ -309,12 +309,12 @@ func (h *httpHostEmulator) HttpFilterInitContext() (contextID uint32) {
309309
}
310310

311311
// impl HostEmulator
312-
func (h *httpHostEmulator) HttpFilterPutRequestHeaders(contextID uint32, headers [][2]string) {
312+
func (h *httpHostEmulator) HttpFilterPutRequestHeaders(contextID uint32, headers types.Headers) {
313313
h.HttpFilterPutRequestHeadersEndOfStream(contextID, headers, false)
314314
}
315315

316316
// impl HostEmulator
317-
func (h *httpHostEmulator) HttpFilterGetRequestHeaders(contextID uint32) (headers [][2]string) {
317+
func (h *httpHostEmulator) HttpFilterGetRequestHeaders(contextID uint32) (headers types.Headers) {
318318
cs, ok := h.httpStreams[contextID]
319319
if !ok {
320320
log.Fatalf("invalid context id: %d", contextID)
@@ -324,7 +324,7 @@ func (h *httpHostEmulator) HttpFilterGetRequestHeaders(contextID uint32) (header
324324
}
325325

326326
// impl HostEmulator
327-
func (h *httpHostEmulator) HttpFilterGetResponseHeaders(contextID uint32) (headers [][2]string) {
327+
func (h *httpHostEmulator) HttpFilterGetResponseHeaders(contextID uint32) (headers types.Headers) {
328328
cs, ok := h.httpStreams[contextID]
329329
if !ok {
330330
log.Fatalf("invalid context id: %d", contextID)
@@ -334,7 +334,7 @@ func (h *httpHostEmulator) HttpFilterGetResponseHeaders(contextID uint32) (heade
334334
}
335335

336336
// impl HostEmulator
337-
func (h *httpHostEmulator) HttpFilterPutRequestHeadersEndOfStream(contextID uint32, headers [][2]string, endOfStream bool) {
337+
func (h *httpHostEmulator) HttpFilterPutRequestHeadersEndOfStream(contextID uint32, headers types.Headers, endOfStream bool) {
338338
cs, ok := h.httpStreams[contextID]
339339
if !ok {
340340
log.Fatalf("invalid context id: %d", contextID)
@@ -346,12 +346,12 @@ func (h *httpHostEmulator) HttpFilterPutRequestHeadersEndOfStream(contextID uint
346346
}
347347

348348
// impl HostEmulator
349-
func (h *httpHostEmulator) HttpFilterPutResponseHeaders(contextID uint32, headers [][2]string) {
349+
func (h *httpHostEmulator) HttpFilterPutResponseHeaders(contextID uint32, headers types.Headers) {
350350
h.HttpFilterPutResponseHeadersEndOfStream(contextID, headers, false)
351351
}
352352

353353
// impl HostEmulator
354-
func (h *httpHostEmulator) HttpFilterPutResponseHeadersEndOfStream(contextID uint32, headers [][2]string, endOfStream bool) {
354+
func (h *httpHostEmulator) HttpFilterPutResponseHeadersEndOfStream(contextID uint32, headers types.Headers, endOfStream bool) {
355355
cs, ok := h.httpStreams[contextID]
356356
if !ok {
357357
log.Fatalf("invalid context id: %d", contextID)
@@ -364,25 +364,25 @@ func (h *httpHostEmulator) HttpFilterPutResponseHeadersEndOfStream(contextID uin
364364
}
365365

366366
// impl HostEmulator
367-
func (h *httpHostEmulator) HttpFilterPutRequestTrailers(contextID uint32, headers [][2]string) {
367+
func (h *httpHostEmulator) HttpFilterPutRequestTrailers(contextID uint32, trailers types.Trailers) {
368368
cs, ok := h.httpStreams[contextID]
369369
if !ok {
370370
log.Fatalf("invalid context id: %d", contextID)
371371
}
372372

373-
cs.requestTrailers = headers
374-
cs.action = proxywasm.ProxyOnRequestTrailers(contextID, len(headers))
373+
cs.requestTrailers = trailers
374+
cs.action = proxywasm.ProxyOnRequestTrailers(contextID, len(trailers))
375375
}
376376

377377
// impl HostEmulator
378-
func (h *httpHostEmulator) HttpFilterPutResponseTrailers(contextID uint32, headers [][2]string) {
378+
func (h *httpHostEmulator) HttpFilterPutResponseTrailers(contextID uint32, trailers types.Trailers) {
379379
cs, ok := h.httpStreams[contextID]
380380
if !ok {
381381
log.Fatalf("invalid context id: %d", contextID)
382382
}
383383

384-
cs.responseTrailers = headers
385-
cs.action = proxywasm.ProxyOnResponseTrailers(contextID, len(headers))
384+
cs.responseTrailers = trailers
385+
cs.action = proxywasm.ProxyOnResponseTrailers(contextID, len(trailers))
386386
}
387387

388388
// impl HostEmulator
@@ -464,7 +464,7 @@ func (h *httpHostEmulator) HttpFilterGetSentLocalResponse(contextID uint32) *Loc
464464
}
465465

466466
// impl HostEmulator
467-
func (h *httpHostEmulator) CallOnLogForAccessLogger(requestHeaders, responseHeaders [][2]string) {
467+
func (h *httpHostEmulator) CallOnLogForAccessLogger(requestHeaders, responseHeaders types.Headers) {
468468
h.httpStreams[RootContextID] = &httpStreamState{
469469
requestHeaders: requestHeaders,
470470
responseHeaders: responseHeaders,

proxytest/proxytest.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type HostEmulator interface {
3232
FinishVM()
3333

3434
GetCalloutAttributesFromContext(contextID uint32) []HttpCalloutAttribute
35-
PutCalloutResponse(contextID uint32, headers, trailers [][2]string, body []byte)
35+
PutCalloutResponse(contextID uint32, headers types.Headers, trailers types.Trailers, body []byte)
3636

3737
GetLogs(level types.LogLevel) []string
3838
GetTickPeriod() uint32
@@ -49,14 +49,14 @@ type HostEmulator interface {
4949

5050
// http
5151
HttpFilterInitContext() (contextID uint32)
52-
HttpFilterPutRequestHeaders(contextID uint32, headers [][2]string)
53-
HttpFilterGetRequestHeaders(contextID uint32) (headers [][2]string)
54-
HttpFilterPutRequestHeadersEndOfStream(contextID uint32, headers [][2]string, endOfStream bool)
55-
HttpFilterPutResponseHeaders(contextID uint32, headers [][2]string)
56-
HttpFilterGetResponseHeaders(contextID uint32) (headers [][2]string)
57-
HttpFilterPutResponseHeadersEndOfStream(contextID uint32, headers [][2]string, endOfStream bool)
58-
HttpFilterPutRequestTrailers(contextID uint32, headers [][2]string)
59-
HttpFilterPutResponseTrailers(contextID uint32, headers [][2]string)
52+
HttpFilterPutRequestHeaders(contextID uint32, headers types.Headers)
53+
HttpFilterGetRequestHeaders(contextID uint32) (headers types.Headers)
54+
HttpFilterPutRequestHeadersEndOfStream(contextID uint32, headers types.Headers, endOfStream bool)
55+
HttpFilterPutResponseHeaders(contextID uint32, headers types.Headers)
56+
HttpFilterGetResponseHeaders(contextID uint32) (headers types.Headers)
57+
HttpFilterPutResponseHeadersEndOfStream(contextID uint32, headers types.Headers, endOfStream bool)
58+
HttpFilterPutRequestTrailers(contextID uint32, trailers types.Trailers)
59+
HttpFilterPutResponseTrailers(contextID uint32, trailers types.Trailers)
6060
HttpFilterPutRequestBody(contextID uint32, body []byte)
6161
HttpFilterPutRequestBodyEndOfStream(contextID uint32, body []byte, endOfStream bool)
6262
HttpFilterGetRequestBody(contextID uint32) []byte
@@ -66,7 +66,7 @@ type HostEmulator interface {
6666
HttpFilterCompleteHttpStream(contextID uint32)
6767
HttpFilterGetCurrentStreamAction(contextID uint32) types.Action
6868
HttpFilterGetSentLocalResponse(contextID uint32) *LocalHttpResponse
69-
CallOnLogForAccessLogger(requestHeaders, responseHeaders [][2]string)
69+
CallOnLogForAccessLogger(requestHeaders, responseHeaders types.Headers)
7070
}
7171

7272
const (

proxytest/root.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ type (
3838
httpContextIDToCalloutInfos map[uint32][]HttpCalloutAttribute // key: contextID
3939
httpCalloutIDToContextID map[uint32]uint32 // key: calloutID
4040
httpCalloutResponse map[uint32]struct { // key: calloutID
41-
headers, trailers [][2]string
42-
body []byte
41+
headers types.Headers
42+
trailers types.Trailers
43+
body []byte
4344
}
4445

4546
pluginConfiguration, vmConfiguration []byte
@@ -48,10 +49,11 @@ type (
4849
}
4950

5051
HttpCalloutAttribute struct {
51-
CalloutID uint32
52-
Upstream string
53-
Headers, Trailers [][2]string
54-
Body []byte
52+
CalloutID uint32
53+
Upstream string
54+
Headers types.Headers
55+
Trailers types.Trailers
56+
Body []byte
5557
}
5658
)
5759

@@ -71,8 +73,9 @@ func newRootHostEmulator(pluginConfiguration, vmConfiguration []byte) *rootHostE
7173
httpContextIDToCalloutInfos: map[uint32][]HttpCalloutAttribute{},
7274
httpCalloutIDToContextID: map[uint32]uint32{},
7375
httpCalloutResponse: map[uint32]struct {
74-
headers, trailers [][2]string
75-
body []byte
76+
headers types.Headers
77+
trailers types.Trailers
78+
body []byte
7679
}{},
7780

7881
pluginConfiguration: pluginConfiguration,

proxywasm/hostcall.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func GetVMConfiguration(size int) ([]byte, error) {
3131
return ret, types.StatusToError(st)
3232
}
3333

34-
func SendHttpResponse(statusCode uint32, headers [][2]string, body string) types.Status {
34+
func SendHttpResponse(statusCode uint32, headers types.Headers, body string) types.Status {
3535
shs := SerializeMap(headers)
3636
hp := &shs[0]
3737
hl := len(shs)
@@ -45,7 +45,7 @@ func SetTickPeriodMilliSeconds(millSec uint32) error {
4545
}
4646

4747
func DispatchHttpCall(upstream string,
48-
headers [][2]string, body string, trailers [][2]string,
48+
headers types.Headers, body string, trailers types.Trailers,
4949
timeoutMillisecond uint32, callBack HttpCalloutCallBack) (calloutID uint32, err error) {
5050
shs := SerializeMap(headers)
5151
hp := &shs[0]
@@ -66,7 +66,7 @@ func DispatchHttpCall(upstream string,
6666
}
6767
}
6868

69-
func GetHttpCallResponseHeaders() ([][2]string, error) {
69+
func GetHttpCallResponseHeaders() (types.Headers, error) {
7070
ret, st := getMap(types.MapTypeHttpCallResponseHeaders)
7171
return ret, types.StatusToError(st)
7272
}
@@ -76,7 +76,7 @@ func GetHttpCallResponseBody(start, maxSize int) ([]byte, error) {
7676
return ret, types.StatusToError(st)
7777
}
7878

79-
func GetHttpCallResponseTrailers() ([][2]string, error) {
79+
func GetHttpCallResponseTrailers() (types.Trailers, error) {
8080
ret, st := getMap(types.MapTypeHttpCallResponseTrailers)
8181
return ret, types.StatusToError(st)
8282
}
@@ -91,12 +91,12 @@ func GetUpstreamData(start, maxSize int) ([]byte, error) {
9191
return ret, types.StatusToError(st)
9292
}
9393

94-
func GetHttpRequestHeaders() ([][2]string, error) {
94+
func GetHttpRequestHeaders() (types.Headers, error) {
9595
ret, st := getMap(types.MapTypeHttpRequestHeaders)
9696
return ret, types.StatusToError(st)
9797
}
9898

99-
func SetHttpRequestHeaders(headers [][2]string) error {
99+
func SetHttpRequestHeaders(headers types.Headers) error {
100100
return types.StatusToError(setMap(types.MapTypeHttpRequestHeaders, headers))
101101
}
102102

@@ -131,13 +131,13 @@ func SetHttpRequestBody(body []byte) error {
131131
return types.StatusToError(st)
132132
}
133133

134-
func GetHttpRequestTrailers() ([][2]string, error) {
134+
func GetHttpRequestTrailers() (types.Trailers, error) {
135135
ret, st := getMap(types.MapTypeHttpRequestTrailers)
136136
return ret, types.StatusToError(st)
137137
}
138138

139-
func SetHttpRequestTrailers(headers [][2]string) error {
140-
return types.StatusToError(setMap(types.MapTypeHttpRequestTrailers, headers))
139+
func SetHttpRequestTrailers(trailers types.Trailers) error {
140+
return types.StatusToError(setMap(types.MapTypeHttpRequestTrailers, trailers))
141141
}
142142

143143
func GetHttpRequestTrailer(key string) (string, error) {
@@ -161,12 +161,12 @@ func ResumeHttpRequest() error {
161161
return types.StatusToError(rawhostcall.ProxyContinueStream(types.StreamTypeRequest))
162162
}
163163

164-
func GetHttpResponseHeaders() ([][2]string, error) {
164+
func GetHttpResponseHeaders() (types.Headers, error) {
165165
ret, st := getMap(types.MapTypeHttpResponseHeaders)
166166
return ret, types.StatusToError(st)
167167
}
168168

169-
func SetHttpResponseHeaders(headers [][2]string) error {
169+
func SetHttpResponseHeaders(headers types.Headers) error {
170170
return types.StatusToError(setMap(types.MapTypeHttpResponseHeaders, headers))
171171
}
172172

@@ -201,13 +201,13 @@ func SetHttpResponseBody(body []byte) error {
201201
return types.StatusToError(st)
202202
}
203203

204-
func GetHttpResponseTrailers() ([][2]string, error) {
204+
func GetHttpResponseTrailers() (types.Trailers, error) {
205205
ret, st := getMap(types.MapTypeHttpResponseTrailers)
206206
return ret, types.StatusToError(st)
207207
}
208208

209-
func SetHttpResponseTrailers(headers [][2]string) error {
210-
return types.StatusToError(setMap(types.MapTypeHttpResponseTrailers, headers))
209+
func SetHttpResponseTrailers(trailers types.Trailers) error {
210+
return types.StatusToError(setMap(types.MapTypeHttpResponseTrailers, trailers))
211211
}
212212

213213
func GetHttpResponseTrailer(key string) (string, error) {

0 commit comments

Comments
 (0)