-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathclient_example_test.go
More file actions
115 lines (100 loc) · 2.89 KB
/
Copy pathclient_example_test.go
File metadata and controls
115 lines (100 loc) · 2.89 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package fasthttp_test
import (
"errors"
"fmt"
"io"
"log"
"net"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttputil"
)
func ExampleHostClient() {
// Prepare a client, which fetches webpages via HTTP proxy listening
// on the localhost:8080.
c := &fasthttp.HostClient{
Addr: "localhost:8080",
MaxResponseBodySize: 10 * 1024 * 1024, // Reject responses larger than 10 MiB.
}
// Fetch google page via local proxy.
statusCode, body, err := c.Get(nil, "http://google.com/foo/bar")
if err != nil {
log.Fatalf("Error when loading google page through local proxy: %v", err)
}
if statusCode != fasthttp.StatusOK {
log.Fatalf("Unexpected status code: %d. Expecting %d", statusCode, fasthttp.StatusOK)
}
useResponseBody(body)
// Fetch foobar page via local proxy. Reuse body buffer.
statusCode, body, err = c.Get(body, "http://foobar.com/google/com")
if err != nil {
log.Fatalf("Error when loading foobar page through local proxy: %v", err)
}
if statusCode != fasthttp.StatusOK {
log.Fatalf("Unexpected status code: %d. Expecting %d", statusCode, fasthttp.StatusOK)
}
useResponseBody(body)
}
func useResponseBody(body []byte) {
// Do something with body :)
}
func ExampleResponse_BodyStream() {
listener := fasthttputil.NewInmemoryListener()
server := &fasthttp.Server{
Handler: func(ctx *fasthttp.RequestCtx) {
ctx.SetBodyString("hello world")
},
}
go func() {
_ = server.Serve(listener)
}()
defer func() {
_ = server.Shutdown()
}()
readBodyPrefix := func(resp *fasthttp.Response, limit int) ([]byte, error) {
stream := resp.BodyStream()
// BodyStream is nil for responses without a body, such as HEAD, 204,
// and 304 responses.
if stream == nil {
return nil, nil
}
closer, ok := stream.(fasthttp.ReadCloserWithError)
if !ok {
_ = resp.CloseBodyStream()
return nil, fmt.Errorf("unexpected body stream type %T", stream)
}
// Read one extra byte so an exact-size body can be distinguished from
// a truncated body.
body, err := io.ReadAll(io.LimitReader(stream, int64(limit)+1))
if err != nil {
_ = closer.CloseWithError(err)
return body, err
}
if len(body) > limit {
body = body[:limit]
_ = closer.CloseWithError(fasthttp.ErrBodyTooLarge)
return body, fasthttp.ErrBodyTooLarge
}
return body, resp.CloseBodyStream()
}
client := &fasthttp.Client{
Dial: func(string) (net.Conn, error) {
return listener.Dial()
},
}
defer client.CloseIdleConnections()
req := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(req)
req.SetRequestURI("http://example.com/")
resp := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(resp)
resp.StreamBody = true
if err := client.Do(req, resp); err != nil {
fmt.Printf("request error: %v\n", err)
return
}
body, err := readBodyPrefix(resp, 5)
fmt.Printf("%s\n%t\n", body, errors.Is(err, fasthttp.ErrBodyTooLarge))
// Output:
// hello
// true
}