1
0
mirror of https://github.com/rancher/norman.git synced 2025-06-21 13:07:10 +00:00
norman/urlbuilder/url.go

261 lines
7.2 KiB
Go
Raw Normal View History

2017-11-11 04:44:02 +00:00
package urlbuilder
import (
"bytes"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/rancher/norman/name"
"github.com/rancher/norman/types"
)
const (
2017-11-21 20:46:30 +00:00
DefaultOverrideURLHeader = "X-API-request-url"
ForwardedHostHeader = "X-Forwarded-Host"
ForwardedProtoHeader = "X-Forwarded-Proto"
ForwardedPortHeader = "X-Forwarded-Port"
2017-11-11 04:44:02 +00:00
)
func New(r *http.Request, version types.APIVersion, schemas *types.Schemas) (types.URLBuilder, error) {
2017-11-21 20:46:30 +00:00
requestURL := parseRequestURL(r)
responseURLBase, err := parseResponseURLBase(requestURL, r)
2017-11-11 04:44:02 +00:00
if err != nil {
return nil, err
}
builder := &urlBuilder{
schemas: schemas,
2017-11-21 20:46:30 +00:00
requestURL: requestURL,
responseURLBase: responseURLBase,
2017-11-11 04:44:02 +00:00
apiVersion: version,
query: r.URL.Query(),
}
return builder, nil
}
type urlBuilder struct {
schemas *types.Schemas
2017-11-21 20:46:30 +00:00
requestURL string
responseURLBase string
2017-11-11 04:44:02 +00:00
apiVersion types.APIVersion
subContext string
query url.Values
}
func (u *urlBuilder) SetSubContext(subContext string) {
u.subContext = subContext
}
2017-11-21 20:46:30 +00:00
func (u *urlBuilder) SchemaLink(schema *types.Schema) string {
return u.constructBasicURL(schema.Version, "schemas", schema.ID)
}
2017-12-05 16:21:12 +00:00
func (u *urlBuilder) Link(linkName string, resource *types.RawResource) string {
if resource.ID == "" || linkName == "" {
return ""
}
return u.constructBasicURL(resource.Schema.Version, resource.Schema.PluralName, resource.ID, strings.ToLower(linkName))
}
2017-11-11 04:44:02 +00:00
func (u *urlBuilder) ResourceLink(resource *types.RawResource) string {
if resource.ID == "" {
return ""
}
2017-11-21 20:46:30 +00:00
return u.constructBasicURL(resource.Schema.Version, resource.Schema.PluralName, resource.ID)
}
func (u *urlBuilder) Marker(marker string) string {
newValues := url.Values{}
for k, v := range u.query {
newValues[k] = v
}
newValues.Set("marker", marker)
return u.requestURL + "?" + newValues.Encode()
2017-11-11 04:44:02 +00:00
}
func (u *urlBuilder) ReverseSort(order types.SortOrder) string {
newValues := url.Values{}
for k, v := range u.query {
newValues[k] = v
}
newValues.Del("order")
newValues.Del("marker")
if order == types.ASC {
newValues.Add("order", string(types.DESC))
} else {
newValues.Add("order", string(types.ASC))
}
2017-11-21 20:46:30 +00:00
return u.requestURL + "?" + newValues.Encode()
2017-11-11 04:44:02 +00:00
}
func (u *urlBuilder) Current() string {
2017-11-21 20:46:30 +00:00
return u.requestURL
2017-11-11 04:44:02 +00:00
}
func (u *urlBuilder) RelativeToRoot(path string) string {
2017-11-21 20:46:30 +00:00
return u.responseURLBase + path
2017-11-11 04:44:02 +00:00
}
2017-11-21 20:46:30 +00:00
func (u *urlBuilder) Sort(field string) string {
newValues := url.Values{}
for k, v := range u.query {
newValues[k] = v
}
newValues.Del("order")
newValues.Del("marker")
newValues.Set("sort", field)
return u.requestURL + "?" + newValues.Encode()
}
func (u *urlBuilder) Collection(schema *types.Schema, versionOverride *types.APIVersion) string {
2017-11-11 04:44:02 +00:00
plural := u.getPluralName(schema)
2017-11-21 20:46:30 +00:00
if versionOverride == nil {
return u.constructBasicURL(schema.Version, plural)
}
return u.constructBasicURL(*versionOverride, plural)
}
func (u *urlBuilder) SubContextCollection(subContext *types.Schema, contextName string, schema *types.Schema) string {
return u.constructBasicURL(schema.Version, subContext.SubContext, contextName, u.getPluralName(schema))
2017-11-11 04:44:02 +00:00
}
2017-11-21 20:46:30 +00:00
func (u *urlBuilder) Version(version types.APIVersion) string {
return u.constructBasicURL(version)
2017-11-11 04:44:02 +00:00
}
2017-12-13 15:53:28 +00:00
func (u *urlBuilder) FilterLink(schema *types.Schema, fieldName string, value string) string {
return u.constructBasicURL(schema.Version, schema.PluralName) + "?" +
url.QueryEscape(fieldName) + "=" + url.QueryEscape(value)
}
2017-11-21 20:46:30 +00:00
func (u *urlBuilder) constructBasicURL(version types.APIVersion, parts ...string) string {
2017-11-11 04:44:02 +00:00
buffer := bytes.Buffer{}
2017-11-21 20:46:30 +00:00
buffer.WriteString(u.responseURLBase)
2017-11-11 04:44:02 +00:00
if version.Path == "" {
buffer.WriteString(u.apiVersion.Path)
} else {
buffer.WriteString(version.Path)
}
buffer.WriteString(u.subContext)
for _, part := range parts {
if part == "" {
return ""
}
buffer.WriteString("/")
buffer.WriteString(part)
}
return buffer.String()
}
func (u *urlBuilder) getPluralName(schema *types.Schema) string {
if schema.PluralName == "" {
return strings.ToLower(name.GuessPluralName(schema.ID))
}
return strings.ToLower(schema.PluralName)
}
// Constructs the request URL based off of standard headers in the request, falling back to the HttpServletRequest.getRequestURL()
// if the headers aren't available. Here is the ordered list of how we'll attempt to construct the URL:
// - x-api-request-url
// - x-forwarded-proto://x-forwarded-host:x-forwarded-port/HttpServletRequest.getRequestURI()
// - x-forwarded-proto://x-forwarded-host/HttpServletRequest.getRequestURI()
// - x-forwarded-proto://host:x-forwarded-port/HttpServletRequest.getRequestURI()
// - x-forwarded-proto://host/HttpServletRequest.getRequestURI() request.getRequestURL()
//
// Additional notes:
// - With x-api-request-url, the query string is passed, it will be dropped to match the other formats.
// - If the x-forwarded-host/host header has a port and x-forwarded-port has been passed, x-forwarded-port will be used.
2017-11-21 20:46:30 +00:00
func parseRequestURL(r *http.Request) string {
2017-11-11 04:44:02 +00:00
// Get url from custom x-api-request-url header
2017-11-21 20:46:30 +00:00
requestURL := getOverrideHeader(r, DefaultOverrideURLHeader, "")
if requestURL != "" {
return strings.SplitN(requestURL, "?", 2)[0]
2017-11-11 04:44:02 +00:00
}
// Get url from standard headers
2017-11-21 20:46:30 +00:00
requestURL = getURLFromStandardHeaders(r)
if requestURL != "" {
return requestURL
2017-11-11 04:44:02 +00:00
}
// Use incoming url
return fmt.Sprintf("http://%s%s", r.Host, r.URL.Path)
}
2017-11-21 20:46:30 +00:00
func getURLFromStandardHeaders(r *http.Request) string {
xForwardedProto := getOverrideHeader(r, ForwardedProtoHeader, "")
2017-11-11 04:44:02 +00:00
if xForwardedProto == "" {
return ""
}
2017-11-21 20:46:30 +00:00
host := getOverrideHeader(r, ForwardedHostHeader, "")
2017-11-11 04:44:02 +00:00
if host == "" {
host = r.Host
}
if host == "" {
return ""
}
2017-11-21 20:46:30 +00:00
port := getOverrideHeader(r, ForwardedPortHeader, "")
2017-11-11 04:44:02 +00:00
if port == "443" || port == "80" {
port = "" // Don't include default ports in url
}
if port != "" && strings.Contains(host, ":") {
// Have to strip the port that is in the host. Handle IPv6, which has this format: [::1]:8080
if (strings.HasPrefix(host, "[") && strings.Contains(host, "]:")) || !strings.HasPrefix(host, "[") {
host = host[0:strings.LastIndex(host, ":")]
}
}
if port != "" {
port = ":" + port
}
return fmt.Sprintf("%s://%s%s%s", xForwardedProto, host, port, r.URL.Path)
}
func getOverrideHeader(r *http.Request, header string, defaultValue string) string {
// Need to handle comma separated hosts in X-Forwarded-For
value := r.Header.Get(header)
if value != "" {
return strings.TrimSpace(strings.Split(value, ",")[0])
}
return defaultValue
}
2017-11-21 20:46:30 +00:00
func parseResponseURLBase(requestURL string, r *http.Request) (string, error) {
2017-11-11 04:44:02 +00:00
path := r.URL.Path
2017-11-21 20:46:30 +00:00
index := strings.LastIndex(requestURL, path)
2017-11-11 04:44:02 +00:00
if index == -1 {
2017-11-21 20:46:30 +00:00
// Fallback, if we can't find path in requestURL, then we just assume the base is the root of the web request
u, err := url.Parse(requestURL)
2017-11-11 04:44:02 +00:00
if err != nil {
return "", err
}
buffer := bytes.Buffer{}
buffer.WriteString(u.Scheme)
buffer.WriteString("://")
buffer.WriteString(u.Host)
return buffer.String(), nil
}
2017-11-21 20:46:30 +00:00
return requestURL[0:index], nil
2017-11-11 04:44:02 +00:00
}
2017-12-16 02:24:04 +00:00
func (u *urlBuilder) Action(action string, resource *types.RawResource) string {
return u.constructBasicURL(resource.Schema.Version, resource.Schema.PluralName, resource.ID) + "?action=" + url.QueryEscape(action)
}