/*
	Dispatcher function of curl command
	This is an exported function and called from httpgen.
*/
func ProcessCurlCommand(options *common.CurlOptions) (string, interface{}) {
	generator := NewXHRGenerator(options)

	for i, data := range options.ProcessedData {
		fileName := data.FileName()
		if fileName != "" {
			isText := data.Type == common.DataAsciiType
			file := &ExternalFile{
				Data:     &data,
				FileName: fileName,
				TextType: isText,
			}
			generator.ExternalFiles[i] = file
		}
	}

	if len(generator.ExternalFiles) == 1 {
		for _, externalFile := range generator.ExternalFiles {
			externalFile.VariableName = "file"
		}
	} else {
		for i, externalFile := range generator.ExternalFiles {
			externalFile.VariableName = fmt.Sprintf("file_%d", i+1)
		}
	}

	generator.processedHeaders = options.GroupedHeaders()

	var templateName string
	switch len(generator.ExternalFiles) {
	case 0:
		templateName = "simple"
	case 1:
		templateName = "external_file"
	default:
		templateName = "external_files"
	}

	if generator.Options.User != "" {
		generator.specialHeaders = append(generator.specialHeaders, []string{"Authorization", fmt.Sprintf(`"Basic " + btoa("%s")`, generator.Options.User)})
	}

	if options.ProcessedData.HasData() {
		if options.Get {
			generator.SetDataForUrl()
		} else {
			generator.Options.InsertContentTypeHeader("application/x-www-form-urlencoded")
			generator.SetDataForBody()
		}
	} else if options.ProcessedData.HasForm() {
		generator.SetFormForBody()
	} else if options.Method() == "GET" && len(generator.processedHeaders) == 0 && len(generator.specialHeaders) == 0 {
	}

	return templateName, *generator
}
Example #2
0
/*
	Dispatcher function of curl command
	This is an exported function and called from httpgen.
*/
func ProcessCurlCommand(options *common.CurlOptions) (string, interface{}) {
	generator := NewNodeJsGenerator(options)
	if options.Http2Flag {
		generator.Modules["http2"] = true
		generator.ClientModule = "http2"
	} else if strings.HasPrefix(options.Url, "https") {
		generator.Modules["https"] = true
		generator.ClientModule = "https"
	} else {
		generator.Modules["http"] = true
		generator.ClientModule = "http"
		options.Insecure = false
	}

	for _, data := range options.ProcessedData {
		fileName := data.FileName()
		if fileName != "" {
			isText := data.Type == common.DataAsciiType
			generator.ExternalFiles = append(generator.ExternalFiles, ExternalFile{FileName: fileName, TextType: isText})
		}
	}

	generator.processedHeaders = options.GroupedHeaders()

	var templateName string
	switch len(generator.ExternalFiles) {
	case 0:
		templateName = "full"
	case 1:
		templateName = "external_file"
	default:
		templateName = "external_files"
	}

	if generator.Options.User != "" {
		generator.specialHeaders = append(generator.specialHeaders, fmt.Sprintf("\"Authorization\": \"Basic \" + new Buffer(\"%s\").toString(\"base64\")", generator.Options.User))
	}

	if options.ProcessedData.HasData() {
		if options.Get {
			generator.SetDataForUrl()
		} else {
			generator.Options.InsertContentTypeHeader("application/x-www-form-urlencoded")
			generator.SetDataForBody()
		}
	} else if options.ProcessedData.HasForm() {
		generator.SetFormForBody()
	} else if options.Method() == "GET" && len(generator.processedHeaders) == 0 && len(generator.specialHeaders) == 0 {
		if templateName == "full" && !options.Insecure {
			templateName = "simple_get"
		}
	}

	return templateName, *generator
}