【问题标题】:HTTP Request on wasm returns status 0 and 0 bytes on bodywasm 上的 HTTP 请求在正文上返回状态 0 和 0 字节
【发布时间】:2021-10-20 15:32:10
【问题描述】:

使用 Golang 的 WASM 上的 HTTP 请求正在工作或部分工作,由于某种原因,请求返回但状态码为 0 并且正文有 0 个字节,以下是有关测试和预期内容的更多详细信息。

你使用的是什么版本的 Go (go version)?

$ go version 
go1.17 darwin/amd64

这个问题会在最新版本中重现吗?

是的,我使用的是 1.17 的最新版本

您使用的是什么操作系统和处理器架构 (go env)?

$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/eduardo/Library/Caches/go-build"
GOENV="/Users/eduardo/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/eduardo/.go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/eduardo/.go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/eduardo/.asdf/installs/golang/1.17/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/eduardo/.asdf/installs/golang/1.17/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.17"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/eduardo/Projects/test-wasm/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/sp/kd1c03556vx3j0klnmw50tnc0000gn/T/go-build2171896039=/tmp/go-build -gno-record-gcc-switches -fno-common"

你做了什么?

使用以下行编译并运行以下代码作为 WASM 二进制文件并尝试在浏览器上运行,这里使用 Chrome 测试版本 Version 92.0.4515.131 (Official Build) (x86_64)

# Compile with the following line
GOARCH=wasm GOOS=js go build -o static/wasm-test.wasm

WASM 二进制代码

// +build js,wasm

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
)

func main() {
    client := &http.Client{}

    req, err := http.NewRequest("GET", "https://golang.org", nil)
    if err != nil {
        panic(err)
    }
    req.Header.Add("js.fetch:mode", "no-cors")

    resp, err := client.Do(req)
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()

    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        panic(err)
    }

    fmt.Printf("Code %d Response len %d\n", resp.StatusCode, len(body))
}

使用以下 HTML 在服务器上运行

<html>

<head>
    <meta charset="utf-8" />
    <script src="wasm_exec.js"></script>
    <script>
        const go = new Go();
        WebAssembly.instantiateStreaming(fetch("wasm-test.wasm"), go.importObject).then((result) => {
            go.run(result.instance);
        });
    </script>
</head>

<body></body>

</html>

我正在使用下面的 Golang 服务器来测试 web 服务器

package main

import (
    "flag"
    "fmt"
    "log"
    "net/http"
)

func main() {
    port := 8888
    directory := "static"
    flag.Parse()

    http.Handle("/", http.FileServer(http.Dir(directory)))

    log.Printf("Serving %s on HTTP port: %d\n", directory, port)
    log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
}

也使用来自 GOROOT 的wasm_exec.js libexec/misc/wasm/wasm_exec.js

您希望看到什么?

在浏览器控制台上期望看到Code 200 Response len 9953

你看到了什么?

Code 0 Response len 0

在 Chrome 上调用堆栈似乎没问题:

syscall/js.valueCall @ http://localhost:8888/wasm_exec.js:399
$syscall_js.valueCall @ http://localhost:8888/wasm-test.wasm:1
$syscall_js.Value.Call @ http://localhost:8888/wasm-test.wasm:1
$net_http.__Transport_.RoundTrip @ http://localhost:8888/wasm-test.wasm:1
$net_http.send @ http://localhost:8888/wasm-test.wasm:1
$net_http.__Client_.send @ http://localhost:8888/wasm-test.wasm:1
$net_http.__Client_.do @ http://localhost:8888/wasm-test.wasm:1
$main.main @ http://localhost:8888/wasm-test.wasm:1
$runtime.main @ http://localhost:8888/wasm-test.wasm:1
$wasm_pc_f_loop @ http://localhost:8888/wasm-test.wasm:1
$wasm_export_run @ http://localhost:8888/wasm-test.wasm:1
run @ http://localhost:8888/wasm_exec.js:570
(anonymous) @ http://localhost:8888/:9
(anonymous) @ http://localhost:8888/:8

【问题讨论】:

    标签: http go webassembly


    【解决方案1】:
    猜你喜欢
    • 2016-05-11
    • 2014-10-19
    • 1970-01-01
    • 1970-01-01
    • 2021-01-15
    • 2017-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多