github.com/polarismesh/polaris@v1.17.8/apiserver/eurekaserver/tool.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package eurekaserver
    19  
    20  import (
    21  	"encoding/base64"
    22  	"fmt"
    23  	"strconv"
    24  	"strings"
    25  
    26  	"github.com/emicklei/go-restful/v3"
    27  )
    28  
    29  // hashKey in map
    30  func hasKey(values map[string]bool, key string) bool {
    31  	_, ok := values[key]
    32  	return ok
    33  }
    34  
    35  // ObjectToString interface type to string
    36  func ObjectToString(value interface{}) string {
    37  	switch m := value.(type) {
    38  	case string:
    39  		return m
    40  	case int:
    41  		return strconv.Itoa(m)
    42  	default:
    43  		return fmt.Sprintf("%v", value)
    44  	}
    45  }
    46  
    47  func getParamFromEurekaRequestHeader(req *restful.Request, headerName string) string {
    48  	headerValue := req.HeaderParameter(headerName)
    49  	if len(headerValue) > 0 {
    50  		return headerValue
    51  	}
    52  	headerValue = req.HeaderParameter(strings.ToLower(headerName))
    53  	return headerValue
    54  }
    55  
    56  func getAuthFromEurekaRequestHeader(req *restful.Request) (string, error) {
    57  	token := ""
    58  	basicInfo := strings.TrimPrefix(req.Request.Header.Get("Authorization"), "Basic ")
    59  	if len(basicInfo) != 0 {
    60  		ret, err := base64.StdEncoding.DecodeString(basicInfo)
    61  		if err != nil {
    62  			return "", err
    63  		}
    64  		info := string(ret)
    65  		token = strings.Split(info, ":")[1]
    66  	}
    67  	return token, nil
    68  }
    69  
    70  func writeHeader(httpStatus int, rsp *restful.Response) {
    71  	rsp.AddHeader(restful.HEADER_ContentType, restful.MIME_XML)
    72  	rsp.WriteHeader(httpStatus)
    73  }
    74  
    75  func formatWriteName(appId string) string {
    76  	return strings.ToLower(appId)
    77  }
    78  
    79  func formatReadName(appId string) string {
    80  	return strings.ToUpper(appId)
    81  }
    82  
    83  func readAppIdFromRequest(req *restful.Request) string {
    84  	appId := req.PathParameter(ParamAppId)
    85  	return formatReadName(appId)
    86  }
    87  
    88  func readNamespaceFromRequest(req *restful.Request, defaultValue string) string {
    89  	namespace := req.HeaderParameter(HeaderNamespace)
    90  	if len(namespace) == 0 {
    91  		namespace = defaultValue
    92  	}
    93  	return namespace
    94  }