-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
97 lines (82 loc) · 2.38 KB
/
main.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
85
86
87
88
89
90
91
92
93
94
95
96
97
// Package fetchall is a drop-in replacement for appengine/urlfetch
// to make requests exceeding the 32MB response size limit
package fetchall // import "timm.io/fetchall"
import (
"fmt"
"net"
"net/http"
"appengine"
"appengine/socket"
)
func logDebugf(c appengine.Context, format string, args ...interface{}) {
if appengine.IsDevAppServer() {
c.Debugf(format, args...)
}
}
func Client(c appengine.Context) *http.Client {
var tr *http.Transport
if appengine.IsDevAppServer() {
// need to use `net` implementation with dev server, because of https://code.google.com/p/googleappengine/issues/detail?id=11076
logDebugf(c, "using `net` implementation")
tr = &http.Transport{
Dial: func(network, hostPort string) (net.Conn, error) {
host, port, err := net.SplitHostPort(hostPort)
if err != nil {
return nil, err
}
addrs, err := net.LookupIP(host)
if err != nil {
return nil, err
}
logDebugf(c, "found addrs: %v", addrs)
firstIP := addrs[0]
var conn net.Conn
if firstIP.To4() != nil {
logDebugf(c, "first ip is ip4 %s", firstIP)
conn, err = net.Dial(network, fmt.Sprintf("%s:%s", addrs[0], port))
} else {
// brackets for IPv6 addrs
logDebugf(c, "first ip is ip6 %s", firstIP)
conn, err = net.Dial(network, fmt.Sprintf("[%s]:%s", addrs[0], port))
}
if err != nil {
return nil, err
}
logDebugf(c, "dialed, returning conn")
return conn, nil
},
}
} else {
logDebugf(c, "using `appengine/socket` implementation")
tr = &http.Transport{
Dial: func(network, hostPort string) (net.Conn, error) {
host, port, err := net.SplitHostPort(hostPort)
if err != nil {
return nil, err
}
addrs, err := socket.LookupIP(c, host)
if err != nil {
return nil, err
}
logDebugf(c, "found addrs: %v", addrs)
firstIP := addrs[0]
var conn *socket.Conn
if firstIP.To4() != nil {
logDebugf(c, "first ip is ip4 %s", firstIP)
conn, err = socket.Dial(c, network, fmt.Sprintf("%s:%s", addrs[0], port))
} else {
// brackets for IPv6 addrs
logDebugf(c, "first ip is ip6 %s", firstIP)
conn, err = socket.Dial(c, network, fmt.Sprintf("[%s]:%s", addrs[0], port))
}
if err != nil {
return nil, err
}
logDebugf(c, "dialed, returning conn")
return conn, nil
},
}
}
client := &http.Client{Transport: tr}
return client
}