func NewURLBuilderFromRequest(r *http.Request, relative bool) *URLBuilder {
	var (
		scheme = "http"
		host   = r.Host
	)
	if r.TLS != nil {
		scheme = "https"
	} else if len(r.URL.Scheme) > 0 {
		scheme = r.URL.Scheme
	}
	if forwarded := r.Header.Get("Forwarded"); len(forwarded) > 0 {
		forwardedHeader, _, err := parseForwardedHeader(forwarded)
		if err == nil {
			if fproto := forwardedHeader["proto"]; len(fproto) > 0 {
				scheme = fproto
			}
			if fhost := forwardedHeader["host"]; len(fhost) > 0 {
				host = fhost
			}
		}
	} else {
		if forwardedProto := r.Header.Get("X-Forwarded-Proto"); len(forwardedProto) > 0 {
			scheme = forwardedProto
		}
		if forwardedHost := r.Header.Get("X-Forwarded-Host"); len(forwardedHost) > 0 {
			hosts := strings.SplitN(forwardedHost, ",", 2)
			host = strings.TrimSpace(hosts[0])
		}
	}
	basePath := routeDescriptorsMap[RouteNameBase].Path
	requestPath := r.URL.Path
	index := strings.Index(requestPath, basePath)
	u := &url.URL{
		Scheme: scheme,
		Host:   host,
	}
	if index > 0 {
		u.Path = requestPath[0 : index+1]
	}
	return NewURLBuilder(u, relative)
}
NewURLBuilderFromRequest uses information from an *http.Request to
construct the root url.
package v2
import (
	"fmt"
	"net/http"
	"net/url"
	"strings"
	"github.com/docker/distribution/reference"
	"github.com/gorilla/mux"
)
type URLBuilder struct {
	root     *url.URL  
	router   *mux.Router
	relative bool
}
func NewURLBuilder(root *url.URL, relative bool) *URLBuilder {
	return &URLBuilder{
		root:     root,
		router:   Router(),
		relative: relative,
	}
}
func NewURLBuilderFromString(root string, relative bool) (*URLBuilder, error) {
	u, err := url.Parse(root)
	if err != nil {
		return nil, err
	}
	return NewURLBuilder(u, relative), nil
}