-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttp.go
More file actions
156 lines (111 loc) · 2.8 KB
/
http.go
File metadata and controls
156 lines (111 loc) · 2.8 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package reader
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"path/filepath"
"time"
"github.com/whosonfirst/go-ioutil"
)
// HTTPReader is a struct that implements the `Reader` interface for reading documents from an HTTP(S) resource.
type HTTPReader struct {
Reader
url *url.URL
throttle <-chan time.Time
user_agent string
}
func init() {
ctx := context.Background()
schemes := []string{
"http",
"https",
}
for _, s := range schemes {
err := RegisterReader(ctx, s, NewHTTPReader)
if err != nil {
panic(err)
}
}
}
// NewStdinReader returns a new `Reader` instance for reading documents from an HTTP(s) resource,
// configured by 'uri' in the form of:
//
// http(s)://{HOST}?{PARAMS}
//
// Where {PARAMS} can be:
// * user-agent - An optional user agent string to include with requests.
func NewHTTPReader(ctx context.Context, uri string) (Reader, error) {
u, err := url.Parse(uri)
if err != nil {
return nil, err
}
rate := time.Second / 3
throttle := time.Tick(rate)
r := HTTPReader{
throttle: throttle,
url: u,
}
q := u.Query()
ua := q.Get("user-agent")
if ua != "" {
r.user_agent = ua
}
return &r, nil
}
// Exists returns a boolean value indicating whether 'path' already exists.
func (r *HTTPReader) Exists(ctx context.Context, uri string) (bool, error) {
<-r.throttle
u, _ := url.Parse(r.url.String())
u.Path = filepath.Join(u.Path, uri)
url := u.String()
req, err := http.NewRequest(http.MethodHead, url, nil)
if err != nil {
return false, fmt.Errorf("Failed to create new request, %w", err)
}
if r.user_agent != "" {
req.Header.Set("User-Agent", r.user_agent)
}
cl := &http.Client{}
rsp, err := cl.Do(req)
if err != nil {
return false, err
}
defer rsp.Body.Close()
if rsp.StatusCode != http.StatusOK {
return false, nil
}
return true, nil
}
// Read will open a `io.ReadSeekCloser` for the resource located at 'uri'.
func (r *HTTPReader) Read(ctx context.Context, uri string) (io.ReadSeekCloser, error) {
<-r.throttle
u, _ := url.Parse(r.url.String())
u.Path = filepath.Join(u.Path, uri)
url := u.String()
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, fmt.Errorf("Failed to create new request, %w", err)
}
if r.user_agent != "" {
req.Header.Set("User-Agent", r.user_agent)
}
cl := &http.Client{}
rsp, err := cl.Do(req)
if err != nil {
return nil, fmt.Errorf("Failed to execute request, %w", err)
}
if rsp.StatusCode != 200 {
return nil, fmt.Errorf("Unexpected status code: %s", rsp.Status)
}
fh, err := ioutil.NewReadSeekCloser(rsp.Body)
if err != nil {
return nil, fmt.Errorf("Failed to create new ReadSeekCloser, %w", err)
}
return fh, nil
}
// ReaderURI returns 'uri'.
func (r *HTTPReader) ReaderURI(ctx context.Context, uri string) string {
return uri
}