func()

in pkg/filter/http/remote/call.go [177:250]


func (f *Filter) resolve(ctx *contexthttp.HttpContext) error {
	// method must be post
	req := ctx.Request
	if req.Method != http.MethodPost {
		return errors.New("http request method must be post when trying to auto resolve")
	}
	// header must has x-dubbo-http1.1-dubbo-version to declare using auto resolve rule
	version := req.Header.Get(constant.DubboHttpDubboVersion)
	if version == "" {
		return errors.New("http request must has x-dubbo-http1.1-dubbo-version header when trying to auto resolve")
	}

	// http://host/{application}/{service}/{method} or https://host/{application}/{service}/{method}
	rawPath := req.URL.Path
	rawPath = strings.Trim(rawPath, "/")
	splits := strings.Split(rawPath, "/")
	if len(splits) != 3 {
		return errors.New("http request path must meet {application}/{service}/{method} format when trying to auto resolve")
	}

	integrationRequest := apiConf.IntegrationRequest{}
	resolveProtocol := req.Header.Get(constant.DubboServiceProtocol)
	if resolveProtocol == string(apiConf.HTTPRequest) {
		integrationRequest.RequestType = apiConf.HTTPRequest
	} else if resolveProtocol == string(apiConf.DubboRequest) {
		integrationRequest.RequestType = apiConf.DubboRequest
	} else if resolveProtocol == "triple" {
		integrationRequest.RequestType = "triple"
	} else {
		return errors.New("http request has unknown protocol in x-dubbo-service-protocol when trying to auto resolve")
	}

	dubboBackendConfig := apiConf.DubboBackendConfig{}
	dubboBackendConfig.Version = req.Header.Get(constant.DubboServiceVersion)
	dubboBackendConfig.Group = req.Header.Get(constant.DubboGroup)
	integrationRequest.DubboBackendConfig = dubboBackendConfig

	defaultMappingParams := []apiConf.MappingParam{
		{
			Name:  "requestBody.values",
			MapTo: "opt.values",
		}, {
			Name:  "requestBody.types",
			MapTo: "opt.types",
		}, {
			Name:  "uri.application",
			MapTo: "opt.application",
		}, {
			Name:  "uri.interface",
			MapTo: "opt.interface",
		}, {
			Name:  "uri.method",
			MapTo: "opt.method",
		},
	}
	integrationRequest.MappingParams = defaultMappingParams

	method := apiConf.Method{
		Enable:   true,
		Mock:     false,
		HTTPVerb: http.MethodPost,
	}
	method.IntegrationRequest = integrationRequest

	inboundRequest := apiConf.InboundRequest{}
	inboundRequest.RequestType = apiConf.HTTPRequest
	method.InboundRequest = inboundRequest

	api := router.API{}
	api.URLPattern = "/:application/:interface/:method"
	api.Method = method
	ctx.API(api)
	return nil
}