github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/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  	"bytes"
    22  	"fmt"
    23  	"io"
    24  	"net/http"
    25  	"os"
    26  	"regexp"
    27  	"strings"
    28  	"time"
    29  	"unicode/utf8"
    30  
    31  	"camlistore.org/pkg/blob"
    32  	"camlistore.org/pkg/httputil"
    33  )
    34  
    35  var kGetPattern = regexp.MustCompile(`/camli/` + blob.Pattern + `$`)
    36  
    37  // Handler is the HTTP handler for serving GET requests of blobs.
    38  type Handler struct {
    39  	Fetcher blob.StreamingFetcher
    40  }
    41  
    42  // CreateGetHandler returns an http Handler for serving blobs from fetcher.
    43  func CreateGetHandler(fetcher blob.StreamingFetcher) http.Handler {
    44  	return &Handler{Fetcher: fetcher}
    45  }
    46  
    47  func (h *Handler) ServeHTTP(conn http.ResponseWriter, req *http.Request) {
    48  	if req.URL.Path == "/camli/sha1-deadbeef00000000000000000000000000000000" {
    49  		// Test handler.
    50  		simulatePrematurelyClosedConnection(conn, req)
    51  		return
    52  	}
    53  
    54  	blobRef := blobFromURLPath(req.URL.Path)
    55  	if !blobRef.Valid() {
    56  		http.Error(conn, "Malformed GET URL.", 400)
    57  		return
    58  	}
    59  
    60  	ServeBlobRef(conn, req, blobRef, h.Fetcher)
    61  }
    62  
    63  // ServeBlobRef serves a blob.
    64  func ServeBlobRef(rw http.ResponseWriter, req *http.Request, blobRef blob.Ref, fetcher blob.StreamingFetcher) {
    65  	seekFetcher := blob.SeekerFromStreamingFetcher(fetcher)
    66  
    67  	file, size, err := seekFetcher.Fetch(blobRef)
    68  	switch err {
    69  	case nil:
    70  		break
    71  	case os.ErrNotExist:
    72  		rw.WriteHeader(http.StatusNotFound)
    73  		fmt.Fprintf(rw, "Blob %q not found", blobRef)
    74  		return
    75  	default:
    76  		httputil.ServeError(rw, req, err)
    77  		return
    78  	}
    79  	defer file.Close()
    80  	var content io.ReadSeeker = file
    81  
    82  	rw.Header().Set("Content-Type", "application/octet-stream")
    83  	if req.Header.Get("Range") == "" {
    84  		// If it's small and all UTF-8, assume it's text and
    85  		// just render it in the browser.  This is more for
    86  		// demos/debuggability than anything else.  It isn't
    87  		// part of the spec.
    88  		if size <= 32<<10 {
    89  			var buf bytes.Buffer
    90  			_, err := io.Copy(&buf, file)
    91  			if err != nil {
    92  				httputil.ServeError(rw, req, err)
    93  				return
    94  			}
    95  			if utf8.Valid(buf.Bytes()) {
    96  				rw.Header().Set("Content-Type", "text/plain; charset=utf-8")
    97  			}
    98  			content = bytes.NewReader(buf.Bytes())
    99  		}
   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  }