-
Notifications
You must be signed in to change notification settings - Fork 5
/
context.go
389 lines (300 loc) · 8.18 KB
/
context.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package gig
import (
"crypto/md5"
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"io/ioutil"
"mime"
"net"
"net/url"
"os"
"path"
"path/filepath"
"sort"
"strings"
"sync"
)
type (
// Context represents the context of the current request. It holds connection
// reference, path, path parameters, data and registered handler.
// DO NOT retain Context instance, as it will be reused by other connections.
Context interface {
// Response returns `*Response`.
Response() *Response
// IP returns the client's network address.
IP() string
// Certificate returns client's leaf certificate or nil if none provided
Certificate() *x509.Certificate
// CertHash returns a hash of client's leaf certificate or empty string is none
CertHash() string
// URL returns the URL for the context.
URL() *url.URL
// Path returns the registered path for the handler.
Path() string
// QueryString returns unescaped URL query string or error if the raw query
// could not be unescaped. Use Context#URL().RawQuery to get raw query string.
QueryString() (string, error)
// RequestURI is the unmodified URL string as sent by the client
// to a server. Usually the URL() or Path() should be used instead.
RequestURI() string
// Param returns path parameter by name.
Param(name string) string
// Get retrieves data from the context.
Get(key string) interface{}
// Set saves data in the context.
Set(key string, val interface{})
// Render renders a template with data and sends a text/gemini response with status
// code Success. Renderer must be registered using `Gig.Renderer`.
Render(name string, data interface{}) error
// Gemini sends a text/gemini response with status code Success.
Gemini(text string, args ...interface{}) error
// GeminiBlob sends a text/gemini blob response with status code Success.
GeminiBlob(b []byte) error
// Text sends a text/plain response with status code Success.
Text(format string, values ...interface{}) error
// Blob sends a blob response with status code Success and content type.
Blob(contentType string, b []byte) error
// Stream sends a streaming response with status code Success and content type.
Stream(contentType string, r io.Reader) error
// File sends a response with the content of the file.
File(file string) error
// NoContent sends a response with no body, and a status code and meta field.
// Use for any non-2x status codes
NoContent(code Status, meta string, values ...interface{}) error
// Error invokes the registered error handler. Generally used by middleware.
Error(err error)
// Handler returns the matched handler by router.
Handler() HandlerFunc
// Gig returns the `Gig` instance.
Gig() *Gig
}
context struct {
conn tlsconn
TLS *tls.ConnectionState
u *url.URL
response *Response
path string
requestURI string
pnames []string
pvalues []string
handler HandlerFunc
store storeMap
gig *Gig
lock sync.RWMutex
}
)
const (
indexPage = "index.gmi"
)
func (c *context) Response() *Response {
return c.response
}
func (c *context) IP() string {
ra, _, _ := net.SplitHostPort(c.conn.RemoteAddr().String())
return ra
}
func (c *context) Certificate() *x509.Certificate {
if c.TLS == nil || len(c.TLS.PeerCertificates) == 0 {
return nil
}
return c.TLS.PeerCertificates[0]
}
func (c *context) CertHash() string {
cert := c.Certificate()
if cert == nil {
return ""
}
return fmt.Sprintf("%x", md5.Sum(cert.Raw))
}
func (c *context) URL() *url.URL {
return c.u
}
func (c *context) Path() string {
return c.path
}
func (c *context) RequestURI() string {
return c.requestURI
}
func (c *context) Param(name string) string {
for i, n := range c.pnames {
if i < len(c.pvalues) {
if n == name {
return c.pvalues[i]
}
}
}
return ""
}
func (c *context) QueryString() (string, error) {
return url.QueryUnescape(c.u.RawQuery)
}
func (c *context) Get(key string) interface{} {
c.lock.RLock()
defer c.lock.RUnlock()
return c.store[key]
}
func (c *context) Set(key string, val interface{}) {
c.lock.Lock()
defer c.lock.Unlock()
if c.store == nil {
c.store = make(storeMap)
}
c.store[key] = val
}
func (c *context) Render(name string, data interface{}) (err error) {
if c.gig.Renderer == nil {
return ErrRendererNotRegistered
}
if err = c.response.WriteHeader(StatusSuccess, MIMETextGemini); err != nil {
return
}
return c.gig.Renderer.Render(c.response, name, data, c)
}
func (c *context) Gemini(format string, values ...interface{}) error {
return c.GeminiBlob([]byte(fmt.Sprintf(format, values...)))
}
func (c *context) GeminiBlob(b []byte) (err error) {
return c.Blob(MIMETextGemini, b)
}
func (c *context) Text(format string, values ...interface{}) (err error) {
return c.Blob(MIMETextPlain, []byte(fmt.Sprintf(format, values...)))
}
func (c *context) Blob(contentType string, b []byte) (err error) {
err = c.response.WriteHeader(StatusSuccess, contentType)
if err != nil {
return
}
_, err = c.response.Write(b)
return
}
func (c *context) Stream(contentType string, r io.Reader) (err error) {
err = c.response.WriteHeader(StatusSuccess, contentType)
if err != nil {
return
}
_, err = io.Copy(c.response, r)
return
}
func (c *context) File(file string) (err error) {
if containsDotDot(file) {
c.Error(ErrBadRequest)
return
}
s, err := os.Stat(file)
if err != nil {
c.Error(ErrNotFound)
return
}
if uint64(s.Mode().Perm())&0444 != 0444 {
c.Error(ErrGone)
return
}
if s.IsDir() {
files, err := ioutil.ReadDir(file)
if err != nil {
c.Error(ErrTemporaryFailure)
return err
}
for _, f := range files {
if f.Name() == indexPage {
return c.File(path.Join(file, indexPage))
}
}
err = c.response.WriteHeader(StatusSuccess, "text/gemini")
if err != nil {
return err
}
_, _ = c.response.Write([]byte(fmt.Sprintf("# Listing %s\n\n", c.u.Path)))
sort.Slice(files, func(i, j int) bool { return files[i].Name() < files[j].Name() })
for _, file := range files {
if strings.HasPrefix(file.Name(), ".") {
continue
}
if uint64(file.Mode().Perm())&0444 != 0444 {
continue
}
_, _ = c.response.Write([]byte(fmt.Sprintf("=> %s %s [ %v ]\n", filepath.Clean(path.Join(c.u.Path, file.Name())), file.Name(), bytefmt(file.Size()))))
}
return nil
}
ext := filepath.Ext(file)
var mimeType string
if ext == ".gmi" {
mimeType = "text/gemini"
} else {
mimeType = mime.TypeByExtension(ext)
if mimeType == "" {
mimeType = "octet/stream"
}
}
f, err := os.OpenFile(file, os.O_RDONLY, 0600)
if err != nil {
c.Error(ErrTemporaryFailure)
return
}
defer f.Close()
err = c.response.WriteHeader(StatusSuccess, mimeType)
if err != nil {
return
}
_, err = io.Copy(c.response, f)
if err != nil {
// .. remote closed the connection, nothing we can do besides log
// or io error, but status is already sent, everything is broken!
c.Error(ErrTemporaryFailure)
}
return
}
func containsDotDot(v string) bool {
if !strings.Contains(v, "..") {
return false
}
for _, ent := range strings.FieldsFunc(v, isSlashRune) {
if ent == ".." {
return true
}
}
return false
}
func isSlashRune(r rune) bool { return r == '/' || r == '\\' }
func (c *context) NoContent(code Status, meta string, values ...interface{}) error {
return c.response.WriteHeader(code, fmt.Sprintf(meta, values...))
}
func (c *context) Error(err error) {
c.gig.GeminiErrorHandler(err, c)
}
func (c *context) Gig() *Gig {
return c.gig
}
func (c *context) Handler() HandlerFunc {
return c.handler
}
func (c *context) reset(conn tlsconn, u *url.URL, requestURI string, tls *tls.ConnectionState) {
c.conn = conn
c.TLS = tls
c.u = u
c.requestURI = requestURI
c.response.reset(conn)
c.handler = NotFoundHandler
c.store = nil
c.path = ""
c.pnames = nil
// NOTE: Don't reset because it has to have length c.gig.maxParam at all times
for i := 0; i < *c.gig.maxParam; i++ {
c.pvalues[i] = ""
}
}
func bytefmt(b int64) string {
const unit = 1000
if b < unit {
return fmt.Sprintf("%dB", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f%cB", float64(b)/float64(div), "kMGTPE"[exp])
}