Skip to content

Commit

Permalink
Merge pull request #1925 from rajatjindal/golang-outbound-headers2
Browse files Browse the repository at this point in the history
fix http.Headers to outboundHeaders
  • Loading branch information
rajatjindal authored Oct 26, 2023
2 parents c5c1e1e + c139cb4 commit da166ac
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions sdk/go/http/outbound_internals.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,23 +111,21 @@ func toResponse(res *C.wasi_outbound_http_response_t) (*http.Response, error) {

func toOutboundHeaders(hm http.Header) C.wasi_outbound_http_headers_t {
var reqHeaders C.wasi_outbound_http_headers_t

headersLen := len(hm)

if headersLen > 0 {
reqHeaders.len = C.ulong(headersLen)
var x C.wasi_outbound_http_string_t
headersPtr := C.malloc(C.size_t(headersLen) * C.size_t(unsafe.Sizeof(x)))
ptr := (*[1 << 30]C.wasi_outbound_http_tuple2_string_string_t)(unsafe.Pointer(&headersPtr))[:headersLen:headersLen]
var x C.wasi_outbound_http_tuple2_string_string_t
reqHeaders.ptr = (*C.wasi_outbound_http_tuple2_string_string_t)(C.malloc(C.size_t(headersLen) * C.size_t(unsafe.Sizeof(x))))
headers := unsafe.Slice(reqHeaders.ptr, headersLen)

idx := 0
for k, v := range hm {
ptr[idx].f0 = C.wasi_outbound_http_string_t{ptr: C.CString(k), len: C.size_t(len(k))}
ptr[idx].f1 = C.wasi_outbound_http_string_t{ptr: C.CString(v[0]), len: C.size_t(len(v[0]))}
headers[idx] = newOutboundHeader(k, v[0])
idx++
}
reqHeaders.ptr = &ptr[0]
}

return reqHeaders
}

Expand Down Expand Up @@ -188,3 +186,11 @@ func toErr(code C.uint8_t, url string) error {
return nil
}
}

// newOutboundHeader creates a new outboundHeader with the given key/value.
func newOutboundHeader(k, v string) C.wasi_outbound_http_tuple2_string_string_t {
return C.wasi_outbound_http_tuple2_string_string_t{
f0: C.wasi_outbound_http_string_t{ptr: C.CString(k), len: C.size_t(len(k))},
f1: C.wasi_outbound_http_string_t{ptr: C.CString(v), len: C.size_t(len(v))},
}
}

0 comments on commit da166ac

Please sign in to comment.