1
|
|
|
// Copyright 2018 Fedir RYKHTIK. All rights reserved. |
2
|
|
|
// Use of this source code is governed by the GNU GPL 3.0 |
3
|
|
|
// license that can be found in the LICENSE file. |
4
|
|
|
|
5
|
|
|
// Package httpcache provides an interface to HTTP request static cache, |
6
|
|
|
// |
7
|
|
|
// Usage example : |
8
|
|
|
// func main() { |
9
|
|
|
// url := "https://api.github.com/repos/astaxie/beego/contributors" |
10
|
|
|
// body := httpcache.MakeCachedHTTPRequest(url) |
11
|
|
|
// jsonResp, linkHeader, _ := httpcache.ReadResp(body) |
12
|
|
|
// fmt.Printf("%s\n%s", jsonResp, linkHeader) |
13
|
|
|
// } |
14
|
|
|
// |
15
|
|
|
package httpcache |
16
|
|
|
|
17
|
|
|
import ( |
18
|
|
|
"bufio" |
19
|
|
|
"bytes" |
20
|
|
|
"crypto/sha256" |
21
|
|
|
"encoding/hex" |
22
|
|
|
"fmt" |
23
|
|
|
"io/ioutil" |
24
|
|
|
"log" |
25
|
|
|
"net/http" |
26
|
|
|
"net/http/httputil" |
27
|
|
|
"os" |
28
|
|
|
"time" |
29
|
|
|
) |
30
|
|
|
|
31
|
|
|
var cacheTTL = 3600 |
32
|
|
|
|
33
|
|
|
var httpClient = &http.Client{Timeout: 10 * time.Second} |
34
|
|
|
|
35
|
|
|
const dumpBody = true |
36
|
|
|
|
37
|
|
|
// GetFilename gets encoded filename for cache usage |
38
|
|
|
func GetFilename(url string) string { |
39
|
|
|
encoder := sha256.New() |
40
|
|
|
encoder.Write([]byte(url)) |
41
|
|
|
return hex.EncodeToString(encoder.Sum(nil)) |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// MakeHTTPRequest makes a non-cacheable request to the external URL |
45
|
|
|
func MakeHTTPRequest(url string) ([]byte, int, error) { |
46
|
|
|
req, err := http.NewRequest("GET", url, nil) |
47
|
|
|
if err != nil { |
48
|
|
|
log.Fatal("Cannont prepare the HTTP request", err) |
49
|
|
|
} |
50
|
|
|
if os.Getenv("GH_USR") != "" && os.Getenv("GH_PASS") != "" { |
51
|
|
|
req.SetBasicAuth(os.Getenv("GH_USR"), os.Getenv("GH_PASS")) |
52
|
|
|
} |
53
|
|
|
resp, err := httpClient.Do(req) |
54
|
|
|
if err != nil { |
55
|
|
|
log.Fatal("Cannot process the HTTP request", err) |
56
|
|
|
} |
57
|
|
|
defer resp.Body.Close() |
58
|
|
|
body, err := httputil.DumpResponse(resp, dumpBody) |
59
|
|
|
if err != nil { |
60
|
|
|
log.Fatal("Cannont dump the body of HTTP response", err) |
61
|
|
|
} |
62
|
|
|
return body, resp.StatusCode, err |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
func saveRespToFile(file string, resp []byte) { |
66
|
|
|
err := ioutil.WriteFile(file, resp, 0644) |
67
|
|
|
if err != nil { |
68
|
|
|
panic(err) |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
func loadRespFromFile(file string) []byte { |
73
|
|
|
resp, err := ioutil.ReadFile(file) |
74
|
|
|
if err != nil { |
75
|
|
|
panic(err) |
76
|
|
|
} |
77
|
|
|
return resp |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// ReadResp : reads response from the cached HTTP query. |
81
|
|
|
func ReadResp(fullResp []byte) ([]byte, string, error) { |
82
|
|
|
r := bufio.NewReader(bytes.NewReader(fullResp)) |
83
|
|
|
resp, err := http.ReadResponse(r, nil) |
84
|
|
|
if err != nil { |
85
|
|
|
log.Printf("%v\n%s", err, fullResp) |
86
|
|
|
} |
87
|
|
|
body, err := ioutil.ReadAll(resp.Body) |
88
|
|
|
if err != nil { |
89
|
|
|
log.Printf("%v\n%s", err, resp.Body) |
90
|
|
|
} |
91
|
|
|
linkHeader := resp.Header.Get("Link") |
92
|
|
|
return body, linkHeader, err |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// MakeCachedHTTPRequest makes a cacheable request to the external URL |
96
|
|
|
// If the request was already made once, it will be not done again, |
97
|
|
|
// but read from the file in temporary folder. |
98
|
|
|
// Currently is was tested only for GET queries |
99
|
|
|
func MakeCachedHTTPRequest(url string, tmpFolder string, debug bool) []byte { |
100
|
|
|
var fullResp []byte |
101
|
|
|
filename := GetFilename(url) |
102
|
|
|
filepath := filename |
103
|
|
|
if tmpFolder != "" { |
104
|
|
|
filepath = tmpFolder + "/" + filename |
105
|
|
|
} |
106
|
|
|
if _, err := os.Stat(filepath); os.IsNotExist(err) { |
107
|
|
|
if debug == true { |
108
|
|
|
fmt.Println("HTTP query: " + url) |
109
|
|
|
} |
110
|
|
|
resp, statusCode, err := MakeHTTPRequest(url) |
111
|
|
|
if err != nil { |
112
|
|
|
panic(err) |
113
|
|
|
} |
114
|
|
|
if statusCode == 403 { |
115
|
|
|
log.Fatalf("Looks like the rate limit is exceeded, please try again in 60 minutes. Or make a pull request with authentification feature.") |
116
|
|
|
} else if statusCode == 202 { |
117
|
|
|
log.Printf("Server need some time to prepare request. Trying again.") |
118
|
|
|
time.Sleep(2 * time.Second) |
119
|
|
|
return MakeCachedHTTPRequest(url, tmpFolder, debug) |
120
|
|
|
} else if statusCode != 200 { |
121
|
|
|
log.Fatalf("The status code of URL %s is not OK : %d", url, statusCode) |
122
|
|
|
} |
123
|
|
|
saveRespToFile(filepath, resp) |
124
|
|
|
fullResp = loadRespFromFile(filepath) |
125
|
|
|
} else { |
126
|
|
|
if debug == true { |
127
|
|
|
fmt.Println("Loaded results directly from: " + filepath) |
128
|
|
|
} |
129
|
|
|
fullResp = loadRespFromFile(filepath) |
130
|
|
|
} |
131
|
|
|
return fullResp |
132
|
|
|
} |
133
|
|
|
|