-
Notifications
You must be signed in to change notification settings - Fork 0
/
gofetch.go
192 lines (170 loc) · 4.35 KB
/
gofetch.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
package main
import (
"golang.org/x/net/html"
"github.com/gorilla/websocket"
"github.com/gorilla/mux"
"fmt"
"encoding/json"
"net/http"
"net/url"
"strings"
"sync"
)
type QueryURL struct {
Url string
}
type HttpResponse struct {
Url string
StatusCode int
}
type ConcurrentMap struct {
v map[string]bool
sync.RWMutex
}
func (c *ConcurrentMap) HasProcessed(key string) bool {
c.RLock()
defer c.RUnlock()
_, ok := c.v[key]
return ok
}
func (c *ConcurrentMap) Process(key string) {
c.Lock()
defer c.Unlock()
c.v[key] = true
}
func getHypertextReference(tag html.Token) (href string) {
for _, attr := range tag.Attr {
if attr.Key == "href" {
href = attr.Val
break
}
}
return href
}
func extractLinksFromPage(address string, c chan<- string) {
info, err := url.Parse(address)
if err != nil {
panic(err)
}
response, err := http.Get(address)
if err != nil {
fmt.Printf("An error occurred while issuing a HTTP GET request to %s\n", address)
return
}
i := 0
defer response.Body.Close()
tokenizer := html.NewTokenizer(response.Body)
for {
tokenType := tokenizer.Next()
if tokenType == html.ErrorToken {
return
}
token := tokenizer.Token()
switch tokenType {
case html.StartTagToken:
if token.Data == "html" {
i += 1
}
if token.Data == "a" {
href := getHypertextReference(token)
location, err := url.Parse(href)
if err != nil {
// FIXME: That's wrong, handle error
panic(err)
}
location.RawQuery = url.QueryEscape(location.RawQuery)
c <- info.ResolveReference(location).String()
}
case html.EndTagToken:
if token.Data == "html" {
i -= 1
if i == 0 {
close(c)
}
}
}
}
}
func checkLink(href string, responses chan<- HttpResponse, c chan<- string, crawler ConcurrentMap) {
if (strings.HasPrefix(href, "mailto")) {
return
}
if (strings.HasPrefix(href, "http://www.fondation-entreprise-ricard.com")) {
fmt.Printf("VALUE STORED FOR KEY %s: %t\n\n", href, crawler.v[href])
if (!crawler.HasProcessed(href)) {
crawler.Process(href)
go extractLinksFromPage(href, c)
return
} else {
fmt.Printf("We've already checked URL %s", href)
}
} else {
response, err := http.Get(href)
// FIXME: This retry is awful, we might want to
// send that to a retry channel or something
if err != nil {
response, err := http.Get(href)
if err != nil {
responses <- HttpResponse{href, 999}
return
}
defer response.Body.Close()
responses <- HttpResponse{href, response.StatusCode}
}
defer response.Body.Close()
responses <- HttpResponse{href, response.StatusCode}
}
}
func WebSocketHandler(rw http.ResponseWriter, request *http.Request) {
ws, err := websocket.Upgrade(rw, request, nil, 1024, 1024)
if err != nil {
return
}
for {
messageType, p, err := ws.ReadMessage()
if err != nil {
return
} else {
var urlData QueryURL
err := json.Unmarshal(p, &urlData)
if err != nil {
panic(err)
}
hrefs := make(chan string)
httpResponses := make(chan HttpResponse)
go extractLinksFromPage(urlData.Url, hrefs)
numberOfLinks := 0
c := ConcurrentMap{v: make(map[string]bool)}
for href := range hrefs {
numberOfLinks += 1
go checkLink(href, httpResponses, hrefs, c)
}
for i := 0; i < numberOfLinks; i += 1 {
response := <-httpResponses
responseJSON, err := json.Marshal(response)
if err != nil {
panic(err)
}
ws.WriteMessage(messageType, responseJSON)
}
}
}
}
// Parameters are simply the request and the response writer
func RootHandler(rw http.ResponseWriter, request *http.Request) {
fmt.Println("GET /")
fmt.Println("Processing by root handler")
http.ServeFile(rw, request, "public/gofetch.html")
}
func QueryHandler(rw http.ResponseWriter, request *http.Request) {
// Process the POST request
}
func main() {
r := mux.NewRouter()
s := r.Schemes("http").Host("localhost").Subrouter()
s.HandleFunc("/", RootHandler)
s.HandleFunc("/websocket", WebSocketHandler)
http.Handle("/", s)
fmt.Println("Up and listening on port 12345")
http.ListenAndServe(":12345", nil)
}