...

Source file src/github.com/docker/distribution/registry/handlers/context.go

Documentation: github.com/docker/distribution/registry/handlers

     1  package handlers
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  
     8  	"github.com/docker/distribution"
     9  	dcontext "github.com/docker/distribution/context"
    10  	"github.com/docker/distribution/registry/api/errcode"
    11  	v2 "github.com/docker/distribution/registry/api/v2"
    12  	"github.com/docker/distribution/registry/auth"
    13  	"github.com/opencontainers/go-digest"
    14  )
    15  
    16  // Context should contain the request specific context for use in across
    17  // handlers. Resources that don't need to be shared across handlers should not
    18  // be on this object.
    19  type Context struct {
    20  	// App points to the application structure that created this context.
    21  	*App
    22  	context.Context
    23  
    24  	// Repository is the repository for the current request. All requests
    25  	// should be scoped to a single repository. This field may be nil.
    26  	Repository distribution.Repository
    27  
    28  	// RepositoryRemover provides method to delete a repository
    29  	RepositoryRemover distribution.RepositoryRemover
    30  
    31  	// Errors is a collection of errors encountered during the request to be
    32  	// returned to the client API. If errors are added to the collection, the
    33  	// handler *must not* start the response via http.ResponseWriter.
    34  	Errors errcode.Errors
    35  
    36  	urlBuilder *v2.URLBuilder
    37  
    38  	// TODO(stevvooe): The goal is too completely factor this context and
    39  	// dispatching out of the web application. Ideally, we should lean on
    40  	// context.Context for injection of these resources.
    41  }
    42  
    43  // Value overrides context.Context.Value to ensure that calls are routed to
    44  // correct context.
    45  func (ctx *Context) Value(key interface{}) interface{} {
    46  	return ctx.Context.Value(key)
    47  }
    48  
    49  func getName(ctx context.Context) (name string) {
    50  	return dcontext.GetStringValue(ctx, "vars.name")
    51  }
    52  
    53  func getReference(ctx context.Context) (reference string) {
    54  	return dcontext.GetStringValue(ctx, "vars.reference")
    55  }
    56  
    57  var errDigestNotAvailable = fmt.Errorf("digest not available in context")
    58  
    59  func getDigest(ctx context.Context) (dgst digest.Digest, err error) {
    60  	dgstStr := dcontext.GetStringValue(ctx, "vars.digest")
    61  
    62  	if dgstStr == "" {
    63  		dcontext.GetLogger(ctx).Errorf("digest not available")
    64  		return "", errDigestNotAvailable
    65  	}
    66  
    67  	d, err := digest.Parse(dgstStr)
    68  	if err != nil {
    69  		dcontext.GetLogger(ctx).Errorf("error parsing digest=%q: %v", dgstStr, err)
    70  		return "", err
    71  	}
    72  
    73  	return d, nil
    74  }
    75  
    76  func getUploadUUID(ctx context.Context) (uuid string) {
    77  	return dcontext.GetStringValue(ctx, "vars.uuid")
    78  }
    79  
    80  // getUserName attempts to resolve a username from the context and request. If
    81  // a username cannot be resolved, the empty string is returned.
    82  func getUserName(ctx context.Context, r *http.Request) string {
    83  	username := dcontext.GetStringValue(ctx, auth.UserNameKey)
    84  
    85  	// Fallback to request user with basic auth
    86  	if username == "" {
    87  		var ok bool
    88  		uname, _, ok := basicAuth(r)
    89  		if ok {
    90  			username = uname
    91  		}
    92  	}
    93  
    94  	return username
    95  }
    96  

View as plain text