MCPcopy
hub / github.com/keploy/keploy / ExtractPort

Function ExtractPort

pkg/util.go:2705–2729  ·  view source on GitHub ↗

ExtractPort extracts the port from a given URL string, defaulting to 80 if no port is specified.

(rawURL string)

Source from the content-addressed store, hash-verified

2703
2704// ExtractPort extracts the port from a given URL string, defaulting to 80 if no port is specified.
2705func ExtractPort(rawURL string) (uint32, error) {
2706 parsedURL, err := url.Parse(rawURL)
2707 if err != nil {
2708 return 0, err
2709 }
2710
2711 host := parsedURL.Host
2712 if strings.Contains(host, ":") {
2713 // Split the host by ":" and return the port part
2714 parts := strings.Split(host, ":")
2715 port, err := strconv.ParseUint(parts[len(parts)-1], 10, 32)
2716 if err != nil {
2717 return 0, fmt.Errorf("invalid port in URL: %s", rawURL)
2718 }
2719 return uint32(port), nil
2720 }
2721
2722 // Default ports based on scheme
2723 switch parsedURL.Scheme {
2724 case "https":
2725 return 443, nil
2726 default:
2727 return 80, nil
2728 }
2729}
2730
2731func ExtractHostAndPort(curlCmd string) (string, string, error) {
2732 // Split the command string to find the URL

Callers 1

ValidateFlagsMethod · 0.92

Calls

no outgoing calls

Tested by

no test coverage detected