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
|
package gopher
import (
"bytes"
"errors"
"io"
"net/url"
"path"
"strings"
"tildegit.org/tjp/sliderule/internal/types"
)
const (
maxSelectorLen = 255
maxRequestLen = 4096
)
// ParseRequest parses a gopher protocol request into a sliderule.Request object.
func ParseRequest(rdr io.Reader) (*types.Request, error) {
selector, search, err := readFullRequest(rdr)
if err != nil {
return nil, err
}
if !strings.HasPrefix(selector, "/") {
selector = "/" + selector
}
return &types.Request{
URL: &url.URL{
Scheme: "gopher",
Path: path.Clean(selector),
OmitHost: true, //nolint:typecheck
// (for some reason typecheck on drone-ci doesn't realize OmitHost is a field in url.URL)
RawQuery: url.QueryEscape(search),
},
}, nil
}
func readFullRequest(rdr io.Reader) (string, string, error) {
buf := make([]byte, maxSelectorLen+len("\r\n"))
n, err := rdr.Read(buf)
if err != nil && !errors.Is(err, io.EOF) {
return "", "", err
}
buf = buf[:n]
if lineEnd := bytes.IndexByte(buf, '\n'); lineEnd >= 0 {
buf = buf[:lineEnd+1]
} else if tab := bytes.IndexByte(buf, '\t'); tab >= 0 {
// Full-text search transactions append TAB and a search string.
// RFC 1436 still caps the selector at 255 bytes, but gives no
// search-string limit, so keep a small implementation cap.
for {
if len(buf) >= maxRequestLen {
return "", "", errors.New("gopher request too long")
}
intake := make([]byte, maxRequestLen-len(buf))
n, err = rdr.Read(intake)
if err != nil && err != io.EOF {
return "", "", err
}
buf = append(buf, intake[:n]...)
if lineEnd := bytes.IndexByte(intake[:n], '\n'); lineEnd >= 0 {
buf = buf[:len(buf)-n+lineEnd+1]
break
}
if err == io.EOF {
break
}
}
} else if len(buf) > maxSelectorLen {
return "", "", errors.New("gopher selector too long")
}
selector, search, _ := bytes.Cut(buf, []byte{'\t'})
selector = bytes.TrimRight(selector, "\r\n")
search = bytes.TrimRight(search, "\r\n")
if len(selector) > maxSelectorLen {
return "", "", errors.New("gopher selector too long")
}
if bytes.ContainsAny(selector, "\x00\r\n") || bytes.ContainsAny(search, "\x00\t\r\n") {
return "", "", errors.New("malformed gopher request")
}
return string(selector), string(search), nil
}
|