forked from perkeep/perkeep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.go
134 lines (116 loc) · 3.64 KB
/
get.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
/*
Copyright 2011 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gethandler
import (
"bytes"
"fmt"
"io"
"net/http"
"os"
"regexp"
"time"
"unicode/utf8"
"camlistore.org/pkg/blobref"
"camlistore.org/pkg/blobserver"
"camlistore.org/pkg/httputil"
)
var kGetPattern = regexp.MustCompile(`/camli/` + blobref.Pattern + `$`)
// Handler is the HTTP handler for serving GET requests of blobs.
type Handler struct {
Fetcher blobref.StreamingFetcher
}
func CreateGetHandler(fetcher blobref.StreamingFetcher) func(http.ResponseWriter, *http.Request) {
gh := &Handler{Fetcher: fetcher}
return func(conn http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/camli/sha1-deadbeef00000000000000000000000000000000" {
// Test handler.
simulatePrematurelyClosedConnection(conn, req)
return
}
gh.ServeHTTP(conn, req)
}
}
func (h *Handler) ServeHTTP(conn http.ResponseWriter, req *http.Request) {
blobRef := blobFromUrlPath(req.URL.Path)
if blobRef == nil {
http.Error(conn, "Malformed GET URL.", 400)
return
}
ServeBlobRef(conn, req, blobRef, h.Fetcher)
}
// ServeBlobRef serves a blob.
func ServeBlobRef(rw http.ResponseWriter, req *http.Request, blobRef *blobref.BlobRef, fetcher blobref.StreamingFetcher) {
if w, ok := fetcher.(blobserver.ContextWrapper); ok {
fetcher = w.WrapContext(req)
}
seekFetcher := blobref.SeekerFromStreamingFetcher(fetcher)
file, size, err := seekFetcher.Fetch(blobRef)
switch err {
case nil:
break
case os.ErrNotExist:
rw.WriteHeader(http.StatusNotFound)
fmt.Fprintf(rw, "Blob %q not found", blobRef)
return
default:
httputil.ServeError(rw, req, err)
return
}
defer file.Close()
var content io.ReadSeeker = file
rw.Header().Set("Content-Type", "application/octet-stream")
if req.Header.Get("Range") == "" {
// If it's small and all UTF-8, assume it's text and
// just render it in the browser. This is more for
// demos/debuggability than anything else. It isn't
// part of the spec.
if size <= 32<<10 {
var buf bytes.Buffer
_, err := io.Copy(&buf, file)
if err != nil {
httputil.ServeError(rw, req, err)
return
}
if utf8.Valid(buf.Bytes()) {
rw.Header().Set("Content-Type", "text/plain; charset=utf-8")
}
content = bytes.NewReader(buf.Bytes())
}
}
http.ServeContent(rw, req, "", dummyModTime, content)
}
// dummyModTime is an arbitrary point in time that we send as fake modtimes for blobs.
// Because blobs are content-addressable, they can never change, so it's better to send
// *some* modtime and let clients do "If-Modified-Since" requests for it.
// This time is the first commit of the Camlistore project.
var dummyModTime = time.Unix(1276213335, 0)
func blobFromUrlPath(path string) *blobref.BlobRef {
return blobref.FromPattern(kGetPattern, path)
}
// For client testing.
func simulatePrematurelyClosedConnection(conn http.ResponseWriter, req *http.Request) {
flusher, ok := conn.(http.Flusher)
if !ok {
return
}
hj, ok := conn.(http.Hijacker)
if !ok {
return
}
for n := 1; n <= 100; n++ {
fmt.Fprintf(conn, "line %d\n", n)
flusher.Flush()
}
wrc, _, _ := hj.Hijack()
wrc.Close() // without sending final chunk; should be an error for the client
}