-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
183 lines (149 loc) · 3.5 KB
/
main.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
package main
import (
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
"git.mills.io/prologic/go-gopher"
"github.com/gorilla/feeds"
)
func main() {
listenAndServe("0.0.0.0:9000", "cosmic.voyage:70")
}
const MAX_RSS_FEED_ITEMS = 10
func newFeedItem(title string, url string) (item *feeds.Item, err error) {
item = new(feeds.Item)
item.Title = title
item.Link = &feeds.Link{}
item.Created = time.Now()
body, err := renderPage(url)
if err != nil {
return
}
body = strings.Replace(body, "\n", "<br>", -1)
item.Content = body
return
}
func renderPage(url string) (body string, err error) {
res, err := gopher.Get(
fmt.Sprintf(
"gopher://%s",
url,
),
)
if err != nil {
return
}
if res.Body == nil {
err = errors.New("no response body")
return
}
bytes, err := io.ReadAll(res.Body)
if err != nil {
return
}
body = string(bytes)
return
}
func renderRss(w http.ResponseWriter, hostport string, d gopher.Directory) error {
now := time.Now()
feed := &feeds.Feed{
Title: hostport,
Created: now,
Link: &feeds.Link{},
}
rss_item_count := 0
for _, item := range d.Items {
if item.Type == gopher.INFO && item.Selector == "TITLE" {
feed.Title = item.Description
continue
}
if item.Type == gopher.INFO {
continue
}
if rss_item_count >= MAX_RSS_FEED_ITEMS {
break
}
if strings.HasPrefix(item.Selector, "URL:") {
url := item.Selector[4:]
fmt.Printf("url type one %s\n", url)
} else {
var hostport string
if item.Port == 70 {
hostport = item.Host
} else {
hostport = fmt.Sprintf("%s:%d", item.Host, item.Port)
}
path := url.PathEscape(item.Selector)
path = strings.Replace(path, "%2F", "/", -1)
url := fmt.Sprintf("%s/%s%s", hostport, string(byte(item.Type)), path)
new_item, err := newFeedItem(item.Description, url)
if err != nil {
fmt.Printf("error... %s", err)
continue
}
feed.Items = append(feed.Items, new_item)
rss_item_count += 1
}
}
rss, err := feed.ToRss()
if err != nil {
fmt.Printf("error making feed into rss %s", err)
w.WriteHeader(http.StatusInternalServerError)
return nil
}
w.Write([]byte(rss))
return nil
}
func gopherHandler(uri string) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
path := strings.TrimPrefix(req.URL.Path, "/")
parts := strings.Split(path, "/")
hostport := parts[0]
if len(hostport) == 0 {
http.Redirect(w, req, "/"+uri, http.StatusFound)
return
}
var query_string string
if req.URL.RawQuery != "" {
query_string = fmt.Sprintf("?%s", url.QueryEscape(req.URL.RawQuery))
}
uri, err := url.QueryUnescape(strings.Join(parts[1:], "/"))
if err != nil {
io.WriteString(w, fmt.Sprintf("Error: %s", err))
w.WriteHeader(http.StatusBadRequest)
return
}
res, err := gopher.Get(
fmt.Sprintf(
"gopher://%s/%s%s",
hostport,
uri,
query_string,
),
)
if err != nil {
io.WriteString(w, fmt.Sprintf("Error: %s", err))
w.WriteHeader(http.StatusUnprocessableEntity)
return
}
if res.Body != nil {
io.WriteString(w, "This is not a directory, cannot be turned into an rss feed!")
w.WriteHeader(http.StatusUnprocessableEntity)
return
} else {
if err := renderRss(w, hostport, res.Dir); err != nil {
io.WriteString(w, fmt.Sprintf("Error: %s", err))
w.WriteHeader(http.StatusInternalServerError)
return
}
}
}
}
func listenAndServe(bind, uri string) error {
http.HandleFunc("/", gopherHandler(uri))
return http.ListenAndServe(bind, nil)
}