-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemento.go
62 lines (49 loc) · 1.27 KB
/
memento.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
// Copyright 2021 Wayback Archiver. All rights reserved.
// Use of this source code is governed by the GNU GPL v3
// license that can be found in the LICENSE file.
package memento
import (
"context"
"fmt"
"net/http"
"net/url"
"time"
)
// Memento represents a Memento client.
type Memento struct {
Client *http.Client
}
func (m *Memento) Mementos(_ context.Context, input *url.URL) (string, error) {
if m.Client == nil {
m.Client = &http.Client{Timeout: 60 * time.Second}
}
dst, err := m.timegate(input.String())
if err != nil {
return "", err
}
return dst, nil
}
func (m *Memento) timegate(uri string) (string, error) {
if m.Client == nil {
return "", fmt.Errorf("Client must not nil")
}
m.Client.CheckRedirect = noRedirect
var endpoint = "http://timetravel.mementoweb.org/timegate/"
var url = endpoint + uri
resp, err := m.Client.Head(url)
if err != nil {
return "", fmt.Errorf("The requested URI: %s is not available in an archive.", url)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusFound {
return "", fmt.Errorf(resp.Status)
}
location := resp.Header.Get("Location")
if location == "" {
location = "No Found"
}
return location, nil
}
func noRedirect(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}