-
Notifications
You must be signed in to change notification settings - Fork 207
/
share.go
78 lines (68 loc) · 2 KB
/
share.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
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"crypto/sha256"
"encoding/base64"
"fmt"
"io"
"net/http"
)
const (
// This salt is not meant to be kept secret (it’s checked in after all). It’s
// a tiny bit of paranoia to avoid whatever problems a collision may cause.
salt = "Go playground salt\n"
maxSnippetSize = 64 * 1024
)
type snippet struct {
Body []byte `datastore:",noindex"` // golang.org/issues/23253
}
func (s *snippet) ID() string {
h := sha256.New()
io.WriteString(h, salt)
h.Write(s.Body)
sum := h.Sum(nil)
b := make([]byte, base64.URLEncoding.EncodedLen(len(sum)))
base64.URLEncoding.Encode(b, sum)
// Web sites don’t always linkify a trailing underscore, making it seem like
// the link is broken. If there is an underscore at the end of the substring,
// extend it until there is not.
hashLen := 11
for hashLen <= len(b) && b[hashLen-1] == '_' {
hashLen++
}
return string(b)[:hashLen]
}
func (s *server) handleShare(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
if r.Method == "OPTIONS" {
// This is likely a pre-flight CORS request.
return
}
if r.Method != "POST" {
http.Error(w, "Requires POST", http.StatusMethodNotAllowed)
return
}
var body bytes.Buffer
_, err := io.Copy(&body, io.LimitReader(r.Body, maxSnippetSize+1))
r.Body.Close()
if err != nil {
s.log.Errorf("reading Body: %v", err)
http.Error(w, "Server Error", http.StatusInternalServerError)
return
}
if body.Len() > maxSnippetSize {
http.Error(w, "Snippet is too large", http.StatusRequestEntityTooLarge)
return
}
snip := &snippet{Body: body.Bytes()}
id := snip.ID()
if err := s.db.PutSnippet(r.Context(), id, snip); err != nil {
s.log.Errorf("putting Snippet: %v", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
fmt.Fprint(w, id)
}