/*
	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
}
Exemple #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
}
Exemple #3
0
func ClientNeeded(options *common.CurlOptions) bool {
	if options.Http2Flag || options.Insecure || options.Proxy != "" || options.User != "" || len(options.Cookie) > 0 {
		return true
	}
	if len(options.AWSV2) > 0 {
		return true
	}
	if options.OnlyHasContentTypeHeader() {
		method := options.Method()
		if method != "GET" && method != "POST" {
			return true
		}
		return false
	}
	return true
}
func main() {
	var globalOptions GlobalOptions
	var curlOptions common.CurlOptions
	curlOptions.Init()

	parser := flags.NewParser(&globalOptions, flags.Default)
	curlCommand, err := parser.AddCommand("curl",
		"Generate code from curl options",
		"This command has almost same options of curl and generate code",
		&curlOptions)
	urls, err := parser.Parse()
	if err != nil {
		os.Exit(1)
	}
	if len(urls) > 1 {
		fmt.Println("It accept only one url. Remained urls are ignored.")
	}
	if parser.Active == curlCommand {
		// --url option has higher priority than params.
		if curlOptions.Url == "" {
			if len(urls) > 0 {
				curlOptions.Url = urls[0]
			} else {
				log.Fatalln("Both --url option and url parameters are missing")
			}
		}
		sourceCode, langName, templateName, option := generator.GenerateCode(globalOptions.Target, &curlOptions)
		if templateName != "" {
			if globalOptions.Debug {
				st := reflect.TypeOf(option)
				v := reflect.ValueOf(option)
				fmt.Fprintf(os.Stderr, "Debug: template name=%s_%s\n", langName, templateName)
				fmt.Fprintf(os.Stderr, "Debug: template context=%s\n", st.Name())
				num := st.NumField()
				for i := 0; i < num; i++ {
					fmt.Fprintf(os.Stderr, "    %s: %s\n", st.Field(i).Name, v.Field(i).String())
				}
			}
			fmt.Println(sourceCode)
		} else {
			PrintLangHelp(globalOptions.Target)
			os.Exit(1)
		}
	}
}
Exemple #5
0
/*
	Dispatcher function of curl command
	This is an exported function and called from httpgen.
*/
func ProcessCurlCommand(options *common.CurlOptions) (string, interface{}) {

	generator := NewGoGenerator(options)

	if ClientNeeded(options) {
		return processCurlFullFeatureRequest(generator)
	}

	method := options.Method()
	onlyHasContentTypeHeader := options.OnlyHasContentTypeHeader()

	if method == "POST" && onlyHasContentTypeHeader {
		if options.ProcessedData.HasData() {
			if options.Get {
				return processCurlPostDataWithUrl(generator)
			} else if len(options.ProcessedData) == 1 && options.ProcessedData[0].UseExternalFile() {
				return processCurlPostSingleFile(generator)
			} else {
				return processCurlPostData(generator)
			}
		} else if options.ProcessedData.HasForm() {
			return processCurlPostData(generator)
		} else {
			return processCurlSimple(generator)
		}
	}

	if method == "GET" {
		if len(options.ProcessedData) > 0 {
			return processCurlGetDataWithUrl(generator)
		} else {
			return processCurlSimple(generator)
		}
	}

	if !options.ProcessedData.HasData() && !options.ProcessedData.HasForm() {
		return processCurlSimple(generator)
	}

	return "", nil
}
func GenerateCode(target, options string) (string, string) {
	var globalOptions GlobalOptions
	var curlOptions common.CurlOptions
	curlOptions.Init()

	parser := flags.NewParser(&globalOptions, flags.Default)
	curlCommand, err := parser.AddCommand("curl",
		"Generate code from curl options",
		"This command has almost same options of curl and generate code",
		&curlOptions)

	if !strings.HasPrefix(options, "curl ") {
		options = "curl " + options
	}
	args := shell.Parse(options)

	urls, err := parser.ParseArgs(args)
	if err != nil {
		console.Log(err)
		return "", err.Error()
	}
	if len(urls) > 1 {
		return "", "It accept only one url. Remained urls are ignored:" + strings.Join(urls, ", ")
	}
	if parser.Active == curlCommand {
		// --url option has higher priority than params.
		if curlOptions.Url == "" {
			if len(urls) > 0 {
				curlOptions.Url = urls[0]
			} else {
				console.Error("Both --url option and url parameters are missing")
				return "", "Both --url option and url parameters are missing"
			}
		}
		sourceCode, _, _, _ := generator.GenerateCode(target, &curlOptions)
		return html.EscapeString(sourceCode), ""
	}
	return "", ""
}