-
Notifications
You must be signed in to change notification settings - Fork 0
/
params.go
187 lines (164 loc) · 4.57 KB
/
params.go
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"flag"
"fmt"
"io"
"net"
"net/url"
"os"
"strings"
)
/*
Application Work plan:
1. Take input from all sources
a. stdin
b. flag
c. CGI Envs
2. Parse URL
3. output field of request
*/
/******************************************************************************/
// MARK - Structures
type AppData struct {
Url *string
Param *string
Path *string
Part *string
Verbose *bool
Cgi *bool
}
/******************************************************************************/
// MARK - Functions
func GetEnv(name, backup string) string {
if value := os.Getenv(name); len(value)>0 {
return value
}
return backup
}
func readFromEnv() string {
//method := os.Getenv("REQUEST_METHOD") // GET
scheme := GetEnv("REQUEST_SCHEME", "http") // https
server := GetEnv("SERVER_NAME", "localhost") // thomascherry.name
port := GetEnv("SERVER_PORT", "80") // 443
script := GetEnv("SCRIPT_NAME", "") // /cgi-bin/all.cgi
query := GetEnv("QUERY_STRING", "") // a=b&1=2
full := fmt.Sprintf("%s://%s:%s%s?%s", scheme, server, port, script, query)
return full
}
func readFromStdIn() string {
ret := ""
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
everything, err := io.ReadAll(os.Stdin)
if err != nil {
panic(err)
}
ret = string(everything)
}
return ret
}
func removeRedundency(raw string) string {
//remove port when it can be assumed
if strings.Contains(raw, "http://") &&
(strings.Contains(raw, ":80/") || strings.Contains(raw, ":80?")) {
raw = strings.Replace(raw, ":80", "", 1)
} else if strings.Contains(raw, "https://") &&
(strings.Contains(raw, ":443/") || strings.Contains(raw, ":443?")) {
raw = strings.Replace(raw, ":443", "", 1)
}
//remove trailing question mark
l := len(raw) - 1
if raw[l] == '?' {
raw = raw[0:l]
}
return raw
}
func work(sess AppData, details string) string {
details = strings.Trim(details, " \n\t")
//s := "postgres://user:[email protected]:5432/path?k=v#f"
//s = "?k=v#f"
u, err := url.Parse(details)
if err != nil {
panic(err)
}
if *sess.Param != "" {
m, _ := url.ParseQuery(u.RawQuery)
list := m[*sess.Param]
if len(list)>0 {
return list[0]
}
return ""
}
switch *sess.Part {
case "scheme":
return u.Scheme
case "user":
return fmt.Sprintf("%+v", u.User)
case "username":
return u.User.Username()
case "password":
p, _ := u.User.Password()
return p
case "host-port":
return u.Host
case "host":
host, _, _ := net.SplitHostPort(u.Host)
return host
case "port":
_, port, _ := net.SplitHostPort(u.Host)
return port
case "path":
return u.Path
case "fragment":
return u.Fragment
case "query":
m, _ := url.ParseQuery(u.RawQuery)
return fmt.Sprintf("%s\t%+v", u.RawQuery, m)
case "full":
approx := fmt.Sprintf("%s://%s%s?%s", u.Scheme, u.Host, u.Path, u.RawQuery)
approx = removeRedundency(approx)
if approx[len(approx)-1] == '?' {
approx = approx[0:len(approx)-1]
}
return approx
}
return ""
}
/******************************************************************************/
// MARK - Command Line Functions
/** write results to the console, support a verbose mode */
func output(sess AppData, text, who string) {
if *sess.Verbose {
fmt.Printf("%s\t%s\n", text, who)
} else {
fmt.Printf("%s\n", text)
}
}
/**
Process the Command Line Flags into an AppData structure to be used as the app
session data
*/
func process_flags() AppData {
session := AppData{}
session.Url = flag.String("url", "", "Input URL to process")
session.Verbose = flag.Bool("verbose", false, "Verbose Mode")
session.Cgi = flag.Bool("cgi", false, "Verbose Mode")
session.Param = flag.String("param", "", "Output Query parameter")
session.Part = flag.String("part", "", "Output One section: scheme, user, username, password, host-port, host,port, path, fragment, query, full")
flag.Parse()
return session
}
func main() {
sess := process_flags()
if url := *sess.Url; 0<len(url) {
output(sess, work(sess, url), "flag")
}
if url := readFromStdIn(); 0<len(url) {
output(sess, work(sess, url), "stdin")
}
if *sess.Cgi {
if url := readFromEnv(); 0<len(url) {
output(sess, work(sess, url), "cgi")
}
}
}