Skip to content

Commit c15a79e

Browse files
committed
[add] http method support
1 parent 163b173 commit c15a79e

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

main.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ var (
2020
timeout string
2121
interval string
2222
sigs chan os.Signal
23+
24+
httpMode bool
25+
httpHead bool
26+
httpPost bool
27+
httpUA string
2328
)
2429

2530
var rootCmd = cobra.Command{
@@ -82,11 +87,16 @@ var rootCmd = cobra.Command{
8287
return
8388
}
8489
}
85-
protocol, err := ping.NewProtocol(schema)
86-
if err != nil {
87-
fmt.Println(err)
88-
cmd.Usage()
89-
return
90+
var protocol ping.Protocol
91+
if httpMode {
92+
protocol = ping.HTTP
93+
} else {
94+
protocol, err = ping.NewProtocol(schema)
95+
if err != nil {
96+
fmt.Println(err)
97+
cmd.Usage()
98+
return
99+
}
90100
}
91101
target := ping.Target{
92102
Timeout: timeoutDuration,
@@ -99,8 +109,17 @@ var rootCmd = cobra.Command{
99109
var pinger ping.Pinger
100110
if schema == ping.TCP.String() {
101111
pinger = ping.NewTCPing()
102-
} else if schema == ping.HTTP.String() {
103-
pinger = ping.NewHTTPing()
112+
} else if schema == ping.HTTP.String() || schema == ping.HTTPS.String() {
113+
var httpMethod string
114+
switch {
115+
case httpHead:
116+
httpMethod = "HEAD"
117+
case httpPost:
118+
httpMethod = "POST"
119+
default:
120+
httpMethod = "GET"
121+
}
122+
pinger = ping.NewHTTPing(httpMethod)
104123
} else {
105124
fmt.Printf("schema: %s not support\n", schema)
106125
cmd.Usage()
@@ -124,6 +143,12 @@ func init() {
124143
rootCmd.Flags().IntVarP(&counter, "counter", "c", 4, "ping counter")
125144
rootCmd.Flags().StringVarP(&timeout, "timeout", "T", "1s", `connect timeout, units are "ns", "us" (or "µs"), "ms", "s", "m", "h"`)
126145
rootCmd.Flags().StringVarP(&interval, "interval", "I", "1s", `ping interval, units are "ns", "us" (or "µs"), "ms", "s", "m", "h"`)
146+
147+
rootCmd.Flags().BoolVarP(&httpMode, "http", "H", false, `Use "HTTP" mode. will ignore URI Schema, force to http`)
148+
rootCmd.Flags().BoolVar(&httpHead, "head", false, `Use POST instead of GET in http mode.`)
149+
rootCmd.Flags().BoolVar(&httpPost, "post", false, `Use HEAD instead of GET in http mode.`)
150+
rootCmd.Flags().StringVar(&httpUA, "user-agent", "tcping", `Use custom UA in http mode.`)
151+
127152
}
128153

129154
func main() {

ping/http.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ping
22

33
import (
4+
"bytes"
45
"fmt"
56
"io"
67
"io/ioutil"
@@ -19,10 +20,10 @@ type HTTPing struct {
1920
var _ Pinger = (*HTTPing)(nil)
2021

2122
// NewHTTPing return new HTTPing
22-
func NewHTTPing() *HTTPing {
23+
func NewHTTPing(method string) *HTTPing {
2324
return &HTTPing{
2425
done: make(chan struct{}),
25-
Method: "GET",
26+
Method: method,
2627
}
2728
}
2829

@@ -53,7 +54,7 @@ func (ping *HTTPing) Start() <-chan struct{} {
5354
} else {
5455
defer resp.Body.Close()
5556
length, _ := io.Copy(ioutil.Discard, resp.Body)
56-
fmt.Printf("Ping %s - HTTP is open - time=%s status=%d bytes=%d\n", ping.target, duration, resp.StatusCode, length)
57+
fmt.Printf("Ping %s - %s is open - time=%s method=%s status=%d bytes=%d\n", ping.target, ping.target.Protocol, duration, ping.Method, resp.StatusCode, length)
5758
if ping.result.MinDuration == 0 {
5859
ping.result.MinDuration = duration
5960
}
@@ -88,10 +89,16 @@ func (ping *HTTPing) Stop() {
8889

8990
func (ping HTTPing) ping() (time.Duration, *http.Response, error) {
9091
var resp *http.Response
91-
req, err := http.NewRequest(ping.Method, ping.target.String(), nil)
92+
var body io.Reader
93+
if ping.Method == "POST" {
94+
body = bytes.NewBufferString("{}")
95+
}
96+
req, err := http.NewRequest(ping.Method, ping.target.String(), body)
97+
req.Header.Set(http.CanonicalHeaderKey("User-Agent"), "tcping")
9298
if err != nil {
9399
return 0, nil, err
94100
}
101+
95102
duration, errIfce := timeIt(func() interface{} {
96103
client := http.Client{Timeout: ping.target.Timeout}
97104
resp, err = client.Do(req)

0 commit comments

Comments
 (0)