func buildEdgeRemapLine()

in lib/go-atscfg/remapdotconfig.go [653:846]


func buildEdgeRemapLine(
	atsMajorVersion uint,
	server *Server,
	pData map[string]string,
	text string,
	ds DeliveryService,
	mapFrom string,
	mapTo string,
	remapConfigParams []tc.ParameterV5,
	cacheGroups map[tc.CacheGroupName]tc.CacheGroupNullableV5,
	nameTopologies map[TopologyName]tc.TopologyV5,
	configDir string,
	opts *RemapDotConfigOpts,
) (RemapLines, []string, error) {
	warnings := []string{}
	remapLines := RemapLines{}

	// ds = 'remap' in perl
	mapFrom = strings.Replace(mapFrom, `__http__`, server.HostName, -1)

	isLastCache, err := serverIsLastCacheForDS(server, &ds, nameTopologies, cacheGroups)
	if err != nil {
		return remapLines, warnings, errors.New("determining if cache is the last tier: " + err.Error())
	}

	// if this remap is going to a parent, use http not https.
	// cache-to-cache communication inside the CDN is always http (though that's likely to change in the future)
	if !isLastCache {
		mapTo = strings.Replace(mapTo, `https://`, `http://`, -1)
	}

	// template fill for moustache
	//remapTags := NewRemapTags()
	remapTags := RemapTags{}
	remapTags.Source = mapFrom
	remapTags.Destination = mapTo
	strategy := strategyDirective(getStrategyName(ds.XMLID), configDir, opts)
	if strategy != "" {
		remapTags.Strategy = strategy
	}

	if _, hasDSCPRemap := pData["dscp_remap"]; hasDSCPRemap {
		remapTags.Dscp = `@plugin=dscp_remap.so @pparam=` + strconv.Itoa(ds.DSCP)
	} else {
		remapTags.Dscp = `@plugin=header_rewrite.so @pparam=dscp/set_dscp_` + strconv.Itoa(ds.DSCP) + ".config"
	}

	if *ds.Topology != "" {
		topoTxt, err := makeDSTopologyHeaderRewriteTxt(ds, tc.CacheGroupName(server.CacheGroup), nameTopologies[TopologyName(*ds.Topology)], cacheGroups)
		if err != nil {
			return remapLines, warnings, err
		}
		remapTags.HeaderRewrite = topoTxt
	} else if (ds.EdgeHeaderRewrite != nil && *ds.EdgeHeaderRewrite != "") || (ds.ServiceCategory != nil && *ds.ServiceCategory != "") || (ds.MaxOriginConnections != nil && *ds.MaxOriginConnections != 0) {
		remapTags.HeaderRewrite = `@plugin=header_rewrite.so @pparam=` + edgeHeaderRewriteConfigFileName(ds.XMLID)
	}

	dsConfigParamsMap, selfHeal := classifyConfigParams(remapConfigParams)

	if ds.SigningAlgorithm != nil && *ds.SigningAlgorithm != "" {
		if *ds.SigningAlgorithm == tc.SigningAlgorithmURLSig {
			remapTags.Signing = `@plugin=url_sig.so @pparam=url_sig_` + ds.XMLID + ".config" +
				paramsStringFor(dsConfigParamsMap["url_sig.pparam"], &warnings)
		} else if *ds.SigningAlgorithm == tc.SigningAlgorithmURISigning {
			remapTags.Signing = `@plugin=uri_signing.so @pparam=uri_signing_` + ds.XMLID + ".config" +
				paramsStringFor(dsConfigParamsMap["uri_signing.pparam"], &warnings)
		}
	}

	// Raw remap text, this allows the directive hacks
	rawRemapText := ""
	if ds.RemapText != nil {
		rawRemapText = *ds.RemapText
	}

	// Form the cachekey args string, qstring ignore, then
	// remap.config then cachekey.config
	cachekeyTxt := ""
	cachekeyArgs := ""

	if ds.QStringIgnore != nil {
		if *ds.QStringIgnore == tc.QueryStringIgnoreDropAtEdge {
			remapTags.DropQstring = `@plugin=regex_remap.so @pparam=` + RemapConfigDropQstringConfigFile
		} else if *ds.QStringIgnore == tc.QueryStringIgnoreIgnoreInCacheKeyAndPassUp {
			cachekeyArgs = getQStringIgnoreRemap(atsMajorVersion)
		}
	}

	if len(dsConfigParamsMap) > 0 {
		cachekeyArgs += cachekeyArgsFor(dsConfigParamsMap, &warnings)
	}

	if cachekeyArgs != "" {
		cachekeyTxt = `@plugin=cachekey.so` + cachekeyArgs
	}

	// Hack for moving the cachekey directive into the raw remap text
	if strings.Contains(rawRemapText, RemapConfigCachekeyDirective) {
		rawRemapText = strings.Replace(rawRemapText, RemapConfigCachekeyDirective, cachekeyTxt, 1)
	} else if cachekeyTxt != "" {
		remapTags.Cachekey = cachekeyTxt
	}

	regexRemapTxt := ""

	// Note: should use full path here?
	if ds.RegexRemap != nil && *ds.RegexRemap != "" {
		regexRemapTxt = `@plugin=regex_remap.so @pparam=regex_remap_` + ds.XMLID + ".config"
	}

	// Hack for moving the regex_remap directive into the raw remap text
	if strings.Contains(rawRemapText, RemapConfigRegexRemapDirective) {
		rawRemapText = strings.Replace(rawRemapText, RemapConfigRegexRemapDirective, regexRemapTxt, 1)
	} else if regexRemapTxt != "" {
		remapTags.RegexRemap = regexRemapTxt
	}

	rangeReqTxt := ""
	if ds.RangeRequestHandling != nil {
		crr := false

		if *ds.RangeRequestHandling == tc.RangeRequestHandlingBackgroundFetch {
			rangeReqTxt = `@plugin=background_fetch.so @pparam=--config=bg_fetch.config` +
				paramsStringFor(dsConfigParamsMap["background_fetch.pparam"], &warnings)
		} else if *ds.RangeRequestHandling == tc.RangeRequestHandlingSlice && ds.RangeSliceBlockSize != nil {

			rangeReqTxt = `@plugin=slice.so @pparam=--blockbytes=` + strconv.Itoa(*ds.RangeSliceBlockSize) +
				paramsStringFor(dsConfigParamsMap["slice.pparam"], &warnings)
			rangeReqTxt += ` `
			crr = true
		} else if *ds.RangeRequestHandling == tc.RangeRequestHandlingCacheRangeRequest {
			crr = true
		}

		if crr {
			rangeReqTxt += `@plugin=cache_range_requests.so ` + paramsStringFor(dsConfigParamsMap["cache_range_requests.pparam"], &warnings)
			if *ds.RangeRequestHandling == tc.RangeRequestHandlingSlice && !strings.Contains(rangeReqTxt, "--consider-ims") && selfHeal {
				rangeReqTxt += ` @pparam=--consider-ims`
			}
		}
	}

	// Hack for moving the range directive into the raw remap text
	if strings.Contains(rawRemapText, RemapConfigRangeDirective) {
		rawRemapText = strings.Replace(rawRemapText, RemapConfigRangeDirective, rangeReqTxt, 1)
	} else if rangeReqTxt != "" {
		remapTags.RangeRequests = rangeReqTxt
	}

	if rawRemapText != "" {
		remapTags.RawText = rawRemapText
	}

	if ds.FQPacingRate != nil && *ds.FQPacingRate > 0 {
		remapTags.Pacing = `@plugin=fq_pacing.so @pparam=--rate=` + strconv.Itoa(*ds.FQPacingRate)
	}

	// Check for ds parameter template override
	var template *mustache.Template
	if params, ok := dsConfigParamsMap[RemapConfigTemplateFirst]; ok {
		if 0 < len(params) {
			templateString := params[0].Value
			template, err = RemapLineTemplates.parse(templateString)
			if err != nil {
				warnings = append(warnings, "Error decoding override "+RemapConfigTemplateFirst+" "+templateString+" for DS "+ds.XMLID+", falling back!")
			}
		}
	}

	// fall back to default remap config line template
	if template == nil {
		template, err = RemapLineTemplates.parse(DefaultFirstRemapConfigTemplateString)
		if err != nil {
			return remapLines, warnings, errors.New("unable to load default template string: " + DefaultFirstRemapConfigTemplateString + " " + err.Error())
		}
	}

	// Expand the moustache
	var linebytes bytes.Buffer
	err = template.FRender(&linebytes, remapTags)
	if err != nil {
		return remapLines, warnings, errors.New("Error filling edge remap template: " + err.Error())
	}

	//remapLines.Text = removeDeleteMes(linebytes.String())
	remapLines.Text = linebytes.String()

	// Any raw pre or post pend lines?
	if isLastCache {
		remapLines.Pre, remapLines.Post = lastPrePostRemapLinesFor(dsConfigParamsMap, ds.XMLID)
	}

	return remapLines, warnings, nil
}