forked from zaddok/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlookup_url_appengine.go
45 lines (36 loc) · 1.14 KB
/
lookup_url_appengine.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
// +build appengine
package moodle
import (
"context"
"errors"
"google.golang.org/appengine/urlfetch"
"io/ioutil"
"strings"
)
type GoogleLookupUrl struct {
Context context.Context
}
func (d *GoogleLookupUrl) GetUrl(url string) (string, int, string, error) {
client := urlfetch.Client(d.Context)
response, err1 := client.Get(url)
if err1 != nil {
return "", 0, "", err1
}
contentType := response.Header.Get("Content-Type")
if response.StatusCode == 200 &&
!strings.HasPrefix(contentType, "application/xml") &&
!strings.HasPrefix(contentType, "application/json") &&
!strings.HasPrefix(contentType, "application/rss+xml") &&
!strings.HasPrefix(contentType, "application/atom+xml") &&
!strings.HasPrefix(contentType, "text/html") &&
!strings.HasPrefix(contentType, "text/json") &&
!strings.HasPrefix(contentType, "text/plain") &&
!strings.HasPrefix(contentType, "text/xml") {
return "", 0, contentType, errors.New("Ignored non-text response: " + contentType)
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return "", 0, "", err
}
return strings.TrimSpace(string(body)), response.StatusCode, contentType, nil
}