// serveBlobRef sends 'blobref' to 'conn' as directed by the Range header in 'req' func serveBlobRef(conn http.ResponseWriter, req *http.Request, blobRef *blobref.BlobRef, fetcher blobref.StreamingFetcher) { if w, ok := fetcher.(blobserver.ContextWrapper); ok { fetcher = w.WrapContext(req) } file, size, err := fetcher.FetchStreaming(blobRef) switch err { case nil: break case os.ErrNotExist: conn.WriteHeader(http.StatusNotFound) fmt.Fprintf(conn, "Blob %q not found", blobRef) return default: httputil.ServerError(conn, req, err) return } defer file.Close() seeker, isSeeker := file.(io.Seeker) reqRange := httprange.FromRequest(req) if reqRange.SkipBytes() != 0 && isSeeker { // TODO: set the Range-specific response headers too, // acknowledging that we honored the content range // request. _, err = seeker.Seek(reqRange.SkipBytes(), 0) if err != nil { httputil.ServerError(conn, req, err) return } } var input io.Reader = file if reqRange.LimitBytes() != -1 { input = io.LimitReader(file, reqRange.LimitBytes()) } remainBytes := size - reqRange.SkipBytes() if reqRange.LimitBytes() != -1 && reqRange.LimitBytes() < remainBytes { remainBytes = reqRange.LimitBytes() } conn.Header().Set("Content-Type", "application/octet-stream") if reqRange.IsWholeFile() { conn.Header().Set("Content-Length", strconv.FormatInt(size, 10)) // 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, input) if err != nil { httputil.ServerError(conn, req, err) return } if utf8.Valid(buf.Bytes()) { conn.Header().Set("Content-Type", "text/plain; charset=utf-8") } input = &buf } } if !reqRange.IsWholeFile() { conn.Header().Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", reqRange.SkipBytes(), reqRange.SkipBytes()+remainBytes, size)) conn.WriteHeader(http.StatusPartialContent) } bytesCopied, err := io.Copy(conn, input) // If there's an error at this point, it's too late to tell the client, // as they've already been receiving bytes. But they should be smart enough // to verify the digest doesn't match. But we close the (chunked) response anyway, // to further signal errors. killConnection := func() { if hj, ok := conn.(http.Hijacker); ok { log.Printf("Force-closing TCP connection to signal error sending %q", blobRef) if closer, _, err := hj.Hijack(); err != nil { closer.Close() } } } if err != nil { fmt.Fprintf(os.Stderr, "Error sending file: %v, err=%v\n", blobRef, err) killConnection() return } if bytesCopied != remainBytes { fmt.Fprintf(os.Stderr, "Error sending file: %v, copied=%d, not %d\n", blobRef, bytesCopied, remainBytes) killConnection() return } }
// serveBlobRef sends 'blobref' to 'conn' as directed by the Range header in 'req' func serveBlobRef(conn http.ResponseWriter, req *http.Request, blobRef *blobref.BlobRef, fetcher blobref.StreamingFetcher) { if w, ok := fetcher.(blobserver.ContextWrapper); ok { fetcher = w.WrapContext(req) } file, size, err := fetcher.FetchStreaming(blobRef) switch err { case nil: break case os.ErrNotExist: conn.WriteHeader(http.StatusNotFound) fmt.Fprintf(conn, "Blob %q not found", blobRef) return default: httputil.ServerError(conn, req, err) return } defer file.Close() seeker, isSeeker := file.(io.Seeker) reqRange := httprange.FromRequest(req) if reqRange.SkipBytes() != 0 && isSeeker { // TODO: set the Range-specific response headers too, // acknowledging that we honored the content range // request. _, err = seeker.Seek(reqRange.SkipBytes(), 0) if err != nil { httputil.ServerError(conn, req, err) return } } var input io.Reader = file if reqRange.LimitBytes() != -1 { input = io.LimitReader(file, reqRange.LimitBytes()) } remainBytes := size - reqRange.SkipBytes() if reqRange.LimitBytes() != -1 && reqRange.LimitBytes() < remainBytes { remainBytes = reqRange.LimitBytes() } // Assume this generic content type by default. For better // demos we'll try to sniff and guess the "right" MIME type in // certain cases (no Range requests, etc) but this isn't part // of the Camli spec at all. We just do it to ease demos. contentType := "application/octet-stream" if reqRange.IsWholeFile() { const peekSize = 1024 bufReader := bufio.NewReaderSize(input, peekSize) header, _ := bufReader.Peek(peekSize) if len(header) >= 8 { switch { case utf8.Valid(header): contentType = "text/plain; charset=utf-8" case bytes.HasPrefix(header, []byte{0xff, 0xd8, 0xff, 0xe2}): contentType = "image/jpeg" case bytes.HasPrefix(header, []byte{0x89, 0x50, 0x4e, 0x47, 0xd, 0xa, 0x1a, 0xa}): contentType = "image/png" } } input = bufReader conn.Header().Set("Content-Length", strconv.FormatInt(size, 10)) } conn.Header().Set("Content-Type", contentType) if !reqRange.IsWholeFile() { conn.Header().Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", reqRange.SkipBytes(), reqRange.SkipBytes()+remainBytes, size)) conn.WriteHeader(http.StatusPartialContent) } bytesCopied, err := io.Copy(conn, input) // If there's an error at this point, it's too late to tell the client, // as they've already been receiving bytes. But they should be smart enough // to verify the digest doesn't match. But we close the (chunked) response anyway, // to further signal errors. killConnection := func() { if hj, ok := conn.(http.Hijacker); ok { log.Printf("Force-closing TCP connection to signal error sending %q", blobRef) if closer, _, err := hj.Hijack(); err != nil { closer.Close() } } } if err != nil { fmt.Fprintf(os.Stderr, "Error sending file: %v, err=%v\n", blobRef, err) killConnection() return } if bytesCopied != remainBytes { fmt.Fprintf(os.Stderr, "Error sending file: %v, copied=%d, not %d\n", blobRef, bytesCopied, remainBytes) killConnection() return } }