This repository was archived by the owner on Nov 16, 2022. It is now read-only.
forked from googlecodelabs/tools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfetch.go
350 lines (322 loc) · 9 KB
/
fetch.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Copyright 2016 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"encoding/json"
"fmt"
"hash/crc64"
"io"
"io/ioutil"
"math"
"math/rand"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"time"
"github.com/CloudVLab/tools/claat/parser"
"github.com/CloudVLab/tools/claat/types"
)
const (
// supported codelab source types must be registered parsers
// TODO: define these in claat/parser/..., e.g. in parser/gdoc
srcInvalid srcType = ""
srcGoogleDoc srcType = "gdoc" // Google Docs doc
srcMarkdown srcType = "md" // Markdown text
// driveAPI is a base URL for Drive API
driveAPI = "https://www.googleapis.com/drive/v3"
)
// srcType is codelab source type
type srcType string
// resource is a codelab resource, loaded from local file
// or fetched from remote location.
type resource struct {
typ srcType // source type
body io.ReadCloser // resource body
mod time.Time // last update of content
}
// codelab wraps types.Codelab, while adding source type
// and modified timestamp fields.
type codelab struct {
*types.Codelab
typ srcType // source type
mod time.Time // last modified timestamp
}
// slurpCodelab retrieves and parses codelab source.
// It returns parsed codelab and its source type.
//
// The function will also fetch and parse fragments included
// with types.ImportNode.
func slurpCodelab(src string, parseFragments bool) (*codelab, error) {
res, err := fetch(src)
if err != nil {
return nil, err
}
defer res.body.Close()
clab, err := parser.Parse(string(res.typ), res.body, parseFragments)
if err != nil {
return nil, err
}
// fetch imports and parse them as fragments
var imports []*types.ImportNode
for _, st := range clab.Steps {
imports = append(imports, importNodes(st.Content.Nodes)...)
}
ch := make(chan error, len(imports))
defer close(ch)
for _, imp := range imports {
go func(n *types.ImportNode) {
frag, err := slurpFragment(n.URL)
if err != nil {
ch <- fmt.Errorf("%s: %v", n.URL, err)
return
}
n.Content.Nodes = frag
ch <- nil
}(imp)
}
for _ = range imports {
if err := <-ch; err != nil {
return nil, err
}
}
v := &codelab{
Codelab: clab,
typ: res.typ,
mod: res.mod,
}
return v, nil
}
func slurpFragment(url string) ([]types.Node, error) {
res, err := fetchRemote(url, true)
if err != nil {
return nil, err
}
defer res.body.Close()
return parser.ParseFragment(string(res.typ), res.body, true)
}
// fetch retrieves codelab doc either from local disk
// or a remote location.
// The caller is responsible for closing returned stream.
func fetch(name string) (*resource, error) {
fi, err := os.Stat(name)
if os.IsNotExist(err) {
return fetchRemote(name, false)
}
r, err := os.Open(name)
if err != nil {
return nil, err
}
return &resource{
body: r,
typ: srcMarkdown,
mod: fi.ModTime(),
}, nil
}
// fetchRemote retrieves resource r from the network.
//
// If urlStr is not a URL, i.e. does not have the host part, it is considered to be
// a Google Doc ID and fetched accordingly. Otherwise, a simple GET request
// is used to retrieve the contents.
//
// The caller is responsible for closing returned stream.
// If nometa is true, resource.mod may have zero value.
func fetchRemote(urlStr string, nometa bool) (*resource, error) {
u, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
if u.Host == "" || u.Host == "docs.google.com" {
return fetchDriveFile(urlStr, nometa)
}
return fetchRemoteFile(urlStr)
}
// fetchRemoteFile retrieves codelab resource from url.
// It is a special case of fetchRemote function.
func fetchRemoteFile(url string) (*resource, error) {
res, err := retryGet(nil, url, 3)
if err != nil {
return nil, err
}
t, err := http.ParseTime(res.Header.Get("last-modified"))
if err != nil {
t = time.Now()
}
return &resource{
body: res.Body,
mod: t,
typ: srcMarkdown,
}, nil
}
// fetchDriveFile uses Drive API to retrieve HTML representation of a Google Doc.
// See https://developers.google.com/drive/web/manage-downloads#downloading_google_documents
// for more details.
//
// If nometa is true, resource.mod will have zero value.
func fetchDriveFile(id string, nometa bool) (*resource, error) {
id = gdocID(id)
exportURL := gdocExportURL(id)
client, err := driveClient()
if err != nil {
return nil, err
}
if nometa {
res, err := retryGet(client, exportURL, 7)
if err != nil {
return nil, err
}
return &resource{body: res.Body, typ: srcGoogleDoc}, nil
}
q := url.Values{
"fields": {"id,mimeType,modifiedTime"},
"supportsTeamDrives": {"true"},
}
u := fmt.Sprintf("%s/files/%s?%s", driveAPI, id, q.Encode())
res, err := retryGet(client, u, 7)
if err != nil {
return nil, err
}
defer res.Body.Close()
meta := &struct {
ID string `json:"id"`
MimeType string `json:"mimeType"`
Modified time.Time `json:"modifiedTime"`
}{}
if err := json.NewDecoder(res.Body).Decode(meta); err != nil {
return nil, err
}
if meta.MimeType != "application/vnd.google-apps.document" {
return nil, fmt.Errorf("%s: invalid mime type: %s", id, meta.MimeType)
}
if res, err = retryGet(client, exportURL, 7); err != nil {
return nil, err
}
return &resource{
body: res.Body,
mod: meta.Modified,
typ: srcGoogleDoc,
}, nil
}
var crcTable = crc64.MakeTable(crc64.ECMA)
func slurpBytes(client *http.Client, codelabSrc, dir, imgURL string, n int) (string, error) {
// images can be local in Markdown cases or remote.
// Only proceed a simple copy on local reference.
var b []byte
var ext string
u, err := url.Parse(imgURL)
if err != nil {
return "", err
}
if u.Host == "" {
if imgURL, err = restrictPathToParent(imgURL, filepath.Dir(codelabSrc)); err != nil {
return "", err
}
b, err = ioutil.ReadFile(imgURL)
ext = filepath.Ext(imgURL)
} else {
b, err = slurpRemoteBytes(client, dir, imgURL, 5)
ext = ".png"
}
if err != nil {
return "", err
}
crc := crc64.Checksum(b, crcTable)
file := fmt.Sprintf("%x%s", crc, ext)
dst := filepath.Join(dir, file)
return file, ioutil.WriteFile(dst, b, 0644)
}
func slurpRemoteBytes(client *http.Client, dir, url string, n int) ([]byte, error) {
res, err := retryGet(client, url, n)
if err != nil {
return nil, err
}
defer res.Body.Close()
return ioutil.ReadAll(res.Body)
}
// retryGet tries to GET specified url up to n times.
// Default client will be used if not provided.
func retryGet(client *http.Client, url string, n int) (*http.Response, error) {
if client == nil {
client = http.DefaultClient
}
for i := 0; i <= n; i++ {
if i > 0 {
t := time.Duration((math.Pow(2, float64(i)) + rand.Float64()) * float64(time.Second))
time.Sleep(t)
}
res, err := client.Get(url)
// return early with a good response
// the rest is error handling
if err == nil && res.StatusCode == http.StatusOK {
return res, nil
}
// sometimes Drive API wouldn't even start a response,
// we get net/http: TLS handshake timeout instead:
// consider this a temporary failure and retry again
if err != nil {
continue
}
// otherwise, decode error response and check for "rate limit"
defer res.Body.Close()
var erres struct {
Error struct {
Errors []struct{ Reason string }
}
}
b, _ := ioutil.ReadAll(res.Body)
json.Unmarshal(b, &erres)
var rateLimit bool
for _, e := range erres.Error.Errors {
if e.Reason == "rateLimitExceeded" || e.Reason == "userRateLimitExceeded" {
rateLimit = true
break
}
}
// this is neither a rate limit error, nor a server error:
// retrying is useless
if !rateLimit && res.StatusCode < http.StatusInternalServerError {
return nil, fmt.Errorf("fetch %s: %s; %s", url, res.Status, b)
}
}
return nil, fmt.Errorf("%s: failed after %d retries", url, n)
}
func gdocID(url string) string {
const s = "/document/d/"
if i := strings.Index(url, s); i >= 0 {
url = url[i+len(s):]
}
if i := strings.IndexRune(url, '/'); i > 0 {
url = url[:i]
}
return url
}
func gdocExportURL(id string) string {
return fmt.Sprintf("%s/files/%s/export?mimeType=text/html", driveAPI, id)
}
// restrictPathToParent will ensure that assetPath is in parent.
// It will thus return an absolute path to the asset.
func restrictPathToParent(assetPath, parent string) (string, error) {
parent, err := filepath.Abs(parent)
if err != nil {
return "", err
}
if !filepath.IsAbs(assetPath) {
assetPath = filepath.Join(parent, assetPath)
}
if !strings.HasPrefix(assetPath, parent) {
return "", fmt.Errorf("%s isn't a subdirectory of %s", assetPath, parent)
}
return assetPath, nil
}