-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.go
More file actions
86 lines (68 loc) · 1.77 KB
/
Copy pathrequest.go
File metadata and controls
86 lines (68 loc) · 1.77 KB
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
package libgemini
import (
"fmt"
"io"
"net/url"
"strings"
)
const (
geminiScheme = "gemini"
geminiPort = 1965
schemeDelim = "://"
)
// NewRequest will take a raw url and parse it into a url.URL version.
// It will take care of setting some fields, like the scheme and port, if they are
// blank.
func NewRequest(rawURL string) (Request, error) {
if !strings.Contains(rawURL, schemeDelim) {
rawURL = geminiScheme + schemeDelim + rawURL
}
uri, err := url.Parse(rawURL)
if err != nil {
return Request{}, fmt.Errorf("could not parse URL (%s): %w", rawURL, err)
}
if uri.Port() == "" && uri.Scheme == geminiScheme {
uri.Host = fmt.Sprintf("%s:%d", uri.Host, geminiPort)
}
req := Request{uri}
if err := req.Valid(); err != nil {
return req, err
}
return req, nil
}
// Request is a simple struct which wraps around a url.URL, providing a few methods around it.
type Request struct {
u *url.URL
}
func (r Request) String() string {
return r.u.String()
}
const maxRequestSize = 1024
func (r Request) Valid() error {
n := len(r.u.String())
if n > maxRequestSize {
return fmt.Errorf("max request size of %d bytes exceeded, have %d bytes", maxRequestSize, n)
}
if r.u.Hostname() == "" {
return fmt.Errorf("no hostname")
}
return nil
}
const (
CRLF = "\r\n"
)
// Write will take any io.Writer and write the request on it
// in the format of: <URL>\r\n
// returning errors if the bytes written did not match the expected amount.
func (r Request) Write(w io.Writer) error {
bytesToWrite := []byte(r.String() + CRLF)
n := len(bytesToWrite)
wrote, err := w.Write(bytesToWrite)
if err != nil {
return fmt.Errorf("error writing request: %w", err)
}
if wrote != n {
return fmt.Errorf("expected to write %d bytes, wrote %d bytes instead", n, wrote)
}
return nil
}