-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
84 lines (73 loc) · 1.91 KB
/
http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package bfsp
import (
"bytes"
"io"
"net/http"
"net/url"
"runtime"
"google.golang.org/protobuf/proto"
)
type httpClient struct {
token string
baseUrl string
https bool
}
func NewHTTPFileServerClient(token string, baseUrl string, https bool) (FileServerClient, error) {
return &httpClient{
token: token,
baseUrl: baseUrl,
https: https,
}, nil
}
func (cli *httpClient) setToken(token string) FileServerClient {
newCli := *cli
newCli.token = token
return &newCli
}
func (cli *httpClient) SendFileServerMessage(msg isFileServerMessage_Message, resp proto.Message) error {
msgBin, err := encodeFileServerMessage(msg, cli.token)
if err != nil {
return err
}
reader := bytes.NewReader(msgBin)
// the only two headers that the server cares about are Content-Type and Origin
headers := map[string][]string{
"Content-Type": {"application/octet-stream"},
"Origin": {"localhost:8080"},
}
if runtime.GOOS == "js" {
// this header isn't actually sent to the server, but it tells the browser to send the request with CORS
headers["js.fetch:mode"] = []string{"cors"}
}
scheme := "http"
if cli.https {
scheme = "https"
}
// TODO: can we use QUIC here, even in browsers? Otherwise, can we have an impl in quic and using HTTP, depending on the client
req := &http.Request{
Method: "POST",
URL: &url.URL{Scheme: scheme, Host: cli.baseUrl, Path: "/api"},
Header: map[string][]string{
"Content-Type": {"application/octet-stream"},
"Origin": {"localhost:8080"},
},
Body: io.NopCloser(reader),
}
respBin, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer respBin.Body.Close()
body, err := io.ReadAll(respBin.Body)
if err != nil {
return err
}
// the first 4 bytes are the length of the message in uint32_le, we'll ignore that for now
body = body[4:]
// i <3 interfaces
err = proto.Unmarshal(body, resp)
if err != nil {
return err
}
return nil
}