-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
89 lines (76 loc) · 2.54 KB
/
Copy pathmain.go
File metadata and controls
89 lines (76 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"bufio"
"fmt"
"net/url"
"os"
"strings"
)
// RemoveURLQueryParams removes the query parameters and fragment from a URL.
func RemoveURLQueryParams(urlStr string) string {
parsedURL, _ := url.Parse(urlStr) // ignore error
parsedURL.RawQuery = ""
parsedURL.Fragment = ""
return parsedURL.String()
}
// AppendPath appends a path to a URL, ensuring no double slashes.
func AppendPath(urlStr, path string) string {
// Ensure the path starts with a slash
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
// Remove query parameters and fragments from the URL
parsedURL := RemoveURLQueryParams(urlStr)
// Append the path to the URL, ensuring no double slashes
return strings.TrimRight(parsedURL, "/") + path
}
func showHelp() {
fmt.Print("\nUsage: cat <url> [<path>] | pathslapper <path> [<url>]\n\n")
fmt.Println("Example (append urls to a path): ")
fmt.Println(" cat urls.txt | pathslapper /api/v1/admin")
fmt.Println("")
fmt.Println("Example (append paths to a url): ")
fmt.Println(" cat paths.txt | pathslapper http://example.com")
}
// handleURLFile processes each URL from the input and appends the specified path
func handleURLFile(scanner *bufio.Scanner, path string) {
for scanner.Scan() {
parsedURL := RemoveURLQueryParams(scanner.Text()) // Using RemoveURLQueryParams function
newURL := AppendPath(parsedURL, path) // Using AppendPath function
escapedURL, _ := url.QueryUnescape(newURL) // Unescape the URL to handle special characters like %2
fmt.Println(escapedURL)
}
}
// handlePathFile processes each path and appends it to the given URL
func handlePathFile(scanner *bufio.Scanner, url string) {
for scanner.Scan() {
path := scanner.Text()
newURL := AppendPath(url, path) // Using AppendPath function
fmt.Println(newURL)
}
}
func main() {
if len(os.Args) != 2 {
showHelp()
return
}
// Read the first argument (path or URL)
input := os.Args[1]
// Create the scanner object outside the conditional blocks
scanner := bufio.NewScanner(os.Stdin)
// Check if input is a URL or path
if strings.HasPrefix(input, "http") {
// Case 1: A URL is provided, use pathslapper with URL
url := input
handlePathFile(scanner, url)
} else {
// Case 2: Path is provided, use it to append paths to URLs
if !strings.HasPrefix(input, "/") {
input = "/" + input
}
handleURLFile(scanner, input)
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "Error reading input:", err)
}
}