This repository has been archived by the owner on Aug 11, 2023. It is now read-only.
/
qhttp.go
234 lines (200 loc) · 5.13 KB
/
qhttp.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* golang example using http.Client on multiple goroutines
License:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Fredrik Steen <stone4x4@gmail.com>
*/
package main
import (
"bufio"
"encoding/csv"
"flag"
"fmt"
"io"
"net/http"
"os"
"runtime"
"strings"
"time"
)
// Command line flags.
var (
inputFileName = flag.String("f", "", "read urls from file")
getHeaders = flag.String("H", "Server", "Which header(s) to show (Default Server)")
numCores = flag.Int("n", runtime.NumCPU(), "number of CPU cores to use")
verbose = flag.Bool("v", false, "verbose")
methodGet = flag.Bool("get", false, "Use GET instad of HEAD")
writeCsvFile = flag.String("w", "", "Write to csv file")
)
// struct to hold httpStatus and results from query
type result struct {
id int // Simple id
url string // Holds URL of query
httpStatus string // HTTP Status Code ex 200 OK or error mess
headers []string // Result of the headers we are interesed in
time time.Duration // The duration the check took
}
// Print usage information
func usage() {
fmt.Fprintln(os.Stderr, "usage: qhttp [flags] url [url...]")
flag.PrintDefaults()
os.Exit(2)
}
// Do the actual checking of the url
func geturl_head(num int, url string, c chan *result) {
t0 := time.Now()
var response *http.Response
var err error
// Depending on methodGet variable we GET or HEAD
if *methodGet {
response, err = http.Get(url)
} else {
response, err = http.Head(url)
}
t1 := time.Now()
time := t1.Sub(t0)
res := &result{}
res.id = num
res.url = url
res.time = time
if err != nil {
res.headers = nil
if *verbose {
res.httpStatus = err.Error()
} else {
res.httpStatus = "000 ERR"
}
c <- res
return
}
defer response.Body.Close()
//Get headers to get from flag
headers := strings.Split(*getHeaders, " ")
for _, h := range headers {
// Will be empty if no Server header is recieved
tmphead := response.Header.Get(h)
/* if tmphead == "" {
continue
}
*/
res.headers = append(res.headers, tmphead)
}
res.httpStatus = response.Status
c <- res
}
// readFile returns a string array from path read from start
// to eof, removing newlines and if error returns os.Error.
func readFile(path string) (lines []string, err error) {
file, err := os.Open(path)
if err != nil {
return
}
defer file.Close()
reader := bufio.NewReader(file)
for true {
line, errr := reader.ReadString('\n')
if errr == io.EOF {
break
}
// Skip empty lines
if line == "\n" {
continue
}
lines = append(lines, line[:len(line)-1])
}
if err == io.EOF {
err = nil
}
return
}
// Pretty stupid function that checks if
// url starts with http, if not appends http://
func fixurl(url string) (furl string) {
if url[:4] != "http" {
furl = "http://" + url
} else {
furl = url
}
return
}
// Returns a csv.Writer that we can use to write
// our lines to.
func NewCsv(filename string) (*csv.Writer, error) {
fd, err := os.Create(filename)
if err != nil {
return nil, err
}
csv := csv.NewWriter(fd)
return csv, nil
}
// Takes a *result struct and writes out lines to *csv.Writer
func writeCsvLine(w *csv.Writer, res *result) {
headers_joined := strings.Join(res.headers, ",")
// When we save to CSV duration is always in seconds
duration_seconds := fmt.Sprintf("%v", res.time.Seconds())
// We need a array of strings for the csv package.
record := []string{res.url, res.httpStatus, headers_joined, duration_seconds}
err := w.Write(record)
if err != nil {
fmt.Println("Problems writing to csv file")
}
w.Flush()
}
func main() {
// Handle command line args
flag.Usage = usage
flag.Parse()
runtime.GOMAXPROCS(*numCores)
var (
urls []string
err error
)
// if we got a file to read urls from
if *inputFileName != "" {
urls, err = readFile(*inputFileName)
if err != nil {
fmt.Printf("\nOpen Error => %s\n\n", err)
os.Exit(1)
}
} else {
// if we got args
if flag.NArg() > 0 {
urls = flag.Args()
} else {
usage()
}
}
// our channel which we use to communicate via
c := make(chan *result)
for i, _ := range urls {
furl := fixurl(urls[i])
urls[i] = furl
go geturl_head(i, urls[i], c)
}
if *writeCsvFile != "" {
csv, err := NewCsv(*writeCsvFile)
if err != nil {
fmt.Println(err)
}
totalurls := len(urls)
for i, _ := range urls {
fmt.Printf("query %d of %d done\r", i+1, totalurls)
res := <-c
writeCsvLine(csv, res)
}
fmt.Println("")
} else {
for i, _ := range urls {
res := <-c
fmt.Printf("[%d] %s : %s : %s time=%v\n", i, urls[res.id], res.httpStatus, res.headers, res.time)
}
}
}