-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
67 lines (62 loc) · 1.28 KB
/
main.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
package main
import (
"fmt"
"io/ioutil"
"net/url"
"os"
"strings"
"github.com/mholt/caddy/caddyhttp/fastcgi"
)
func main() {
err := get(os.Args[1])
if err != nil {
panic(err)
}
}
func get(addr string) error {
u, err := url.Parse(addr)
if err != nil {
return err
}
if u.Scheme != "fcgi" {
return fmt.Errorf("Bad scheme: %s", u.Scheme)
}
if u.Port() == "" {
return fmt.Errorf("Explicit port is mandatory")
}
client, err := fastcgi.Dial("tcp", u.Host)
if err != nil {
return err
}
defer client.Close()
p := strings.Trim(u.Path, "/")
fcgiParams := map[string]string{
"REQUEST_METHOD": "GET",
"SERVER_PROTOCOL": "HTTP/1.0",
"SCRIPT_FILENAME": p,
"SCRIPT_NAME": "/" + p,
"CONTENT_LENGTH": "0",
"SERVER_SOFTWARE": "go / fcgiping ",
"REMOTE_ADDR": "127.0.0.1",
}
fmt.Println("REQUEST ==========\n\nHeaders:")
for k, v := range fcgiParams {
fmt.Println("\t", k, ": ", v)
}
resp, err := client.Get(fcgiParams, nil, 0)
if err != nil {
return err
}
fmt.Println("\nRESPONSE ==========\n\nStatus: ", resp.StatusCode, resp.Status)
fmt.Println("Headers:")
for k, v := range resp.Header {
fmt.Println("\t", k, ": ", v)
}
fmt.Println()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
fmt.Println(string(body))
return nil
}