github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/pkg/blobserver/gethandler/get.go (about) 1 /* 2 Copyright 2011 Google Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // Package gethandler implements the HTTP handler for fetching blobs. 18 package gethandler 19 20 import ( 21 "fmt" 22 "io" 23 "net/http" 24 "os" 25 "regexp" 26 "strings" 27 "time" 28 29 "camlistore.org/pkg/blob" 30 "camlistore.org/pkg/httputil" 31 "camlistore.org/pkg/types" 32 ) 33 34 var kGetPattern = regexp.MustCompile(`/camli/` + blob.Pattern + `$`) 35 36 // Handler is the HTTP handler for serving GET requests of blobs. 37 type Handler struct { 38 Fetcher blob.Fetcher 39 } 40 41 // CreateGetHandler returns an http Handler for serving blobs from fetcher. 42 func CreateGetHandler(fetcher blob.Fetcher) http.Handler { 43 return &Handler{Fetcher: fetcher} 44 } 45 46 func (h *Handler) ServeHTTP(conn http.ResponseWriter, req *http.Request) { 47 if req.URL.Path == "/camli/sha1-deadbeef00000000000000000000000000000000" { 48 // Test handler. 49 simulatePrematurelyClosedConnection(conn, req) 50 return 51 } 52 53 blobRef := blobFromURLPath(req.URL.Path) 54 if !blobRef.Valid() { 55 http.Error(conn, "Malformed GET URL.", 400) 56 return 57 } 58 59 ServeBlobRef(conn, req, blobRef, h.Fetcher) 60 } 61 62 // ServeBlobRef serves a blob. 63 func ServeBlobRef(rw http.ResponseWriter, req *http.Request, blobRef blob.Ref, fetcher blob.Fetcher) { 64 rc, size, err := fetcher.Fetch(blobRef) 65 switch err { 66 case nil: 67 break 68 case os.ErrNotExist: 69 rw.WriteHeader(http.StatusNotFound) 70 fmt.Fprintf(rw, "Blob %q not found", blobRef) 71 return 72 default: 73 httputil.ServeError(rw, req, err) 74 return 75 } 76 defer rc.Close() 77 rw.Header().Set("Content-Type", "application/octet-stream") 78 79 var content io.ReadSeeker = types.NewFakeSeeker(rc, int64(size)) 80 rangeHeader := req.Header.Get("Range") != "" 81 const small = 32 << 10 82 var b *blob.Blob 83 if rangeHeader || size < small { 84 // Slurp to memory, so we can actually seek on it (for Range support), 85 // or if we're going to be showing it in the browser (below). 86 b, err = blob.FromReader(blobRef, rc, size) 87 if err != nil { 88 httputil.ServeError(rw, req, err) 89 return 90 } 91 content = b.Open() 92 } 93 if !rangeHeader && size < small { 94 // If it's small and all UTF-8, assume it's text and 95 // just render it in the browser. This is more for 96 // demos/debuggability than anything else. It isn't 97 // part of the spec. 98 if b.IsUTF8() { 99 rw.Header().Set("Content-Type", "text/plain; charset=utf-8") 100 } 101 } 102 http.ServeContent(rw, req, "", dummyModTime, content) 103 } 104 105 // dummyModTime is an arbitrary point in time that we send as fake modtimes for blobs. 106 // Because blobs are content-addressable, they can never change, so it's better to send 107 // *some* modtime and let clients do "If-Modified-Since" requests for it. 108 // This time is the first commit of the Camlistore project. 109 var dummyModTime = time.Unix(1276213335, 0) 110 111 func blobFromURLPath(path string) blob.Ref { 112 matches := kGetPattern.FindStringSubmatch(path) 113 if len(matches) != 3 { 114 return blob.Ref{} 115 } 116 return blob.ParseOrZero(strings.TrimPrefix(matches[0], "/camli/")) 117 } 118 119 // For client testing. 120 func simulatePrematurelyClosedConnection(conn http.ResponseWriter, req *http.Request) { 121 flusher, ok := conn.(http.Flusher) 122 if !ok { 123 return 124 } 125 hj, ok := conn.(http.Hijacker) 126 if !ok { 127 return 128 } 129 for n := 1; n <= 100; n++ { 130 fmt.Fprintf(conn, "line %d\n", n) 131 flusher.Flush() 132 } 133 wrc, _, _ := hj.Hijack() 134 wrc.Close() // without sending final chunk; should be an error for the client 135 }