github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/ais/s3redirect.go (about)

     1  // Package ais provides core functionality for the AIStore object storage.
     2  /*
     3   * Copyright (c) 2021-2024, NVIDIA CORPORATION. All rights reserved.
     4   */
     5  package ais
     6  
     7  import (
     8  	"fmt"
     9  	"net/http"
    10  	"strings"
    11  
    12  	"github.com/NVIDIA/aistore/ais/s3"
    13  	"github.com/NVIDIA/aistore/api/apc"
    14  	"github.com/NVIDIA/aistore/cmn"
    15  	"github.com/NVIDIA/aistore/cmn/cos"
    16  	"github.com/NVIDIA/aistore/cmn/feat"
    17  	"github.com/NVIDIA/aistore/core/meta"
    18  )
    19  
    20  // s3Redirect performs reverse proxy call or HTTP-redirects to a designated node
    21  // in a cluster based on feature flag. See also: docs/s3compat.md
    22  func (p *proxy) s3Redirect(w http.ResponseWriter, r *http.Request, si *meta.Snode, redirectURL, bucket string) {
    23  	if cmn.GCO.Get().Features.IsSet(feat.S3ReverseProxy) {
    24  		p.reverseNodeRequest(w, r, si)
    25  	} else {
    26  		h := w.Header()
    27  		h.Set(cos.HdrLocation, redirectURL)
    28  		h.Set(cos.HdrContentType, "text/xml; charset=utf-8")
    29  		h.Set(cos.HdrServer, s3.AISServer)
    30  		w.WriteHeader(http.StatusTemporaryRedirect)
    31  		ep := extractEndpoint(redirectURL)
    32  		body := "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
    33  			"<Error><Code>TemporaryRedirect</Code><Message>Redirect</Message>" +
    34  			"<Endpoint>" + ep + "</Endpoint>" +
    35  			"<Bucket>" + bucket + "</Bucket></Error>"
    36  		fmt.Fprint(w, body)
    37  	}
    38  }
    39  
    40  // extractEndpoint extracts an S3 endpoint from the full URL path.
    41  // Endpoint is a host name with port and root URL path (if exists).
    42  // E.g. for AIS `http://localhost:8080/s3/bck1/obj1` the endpoint
    43  // would be `localhost:8080/s3`
    44  func extractEndpoint(path string) string {
    45  	ep := path
    46  	if idx := strings.Index(ep, "/"+apc.S3); idx > 0 {
    47  		ep = ep[:idx+3]
    48  	}
    49  	ep = strings.TrimPrefix(ep, "http://")
    50  	ep = strings.TrimPrefix(ep, "https://")
    51  	return ep
    52  }