2012-01-22 15:28:32 -07:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2011-01-26 17:32:41 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2011-01-27 06:19:37 -07:00
|
|
|
"fmt"
|
2011-01-26 17:32:41 -07:00
|
|
|
"io"
|
|
|
|
"log"
|
2011-01-27 06:19:37 -07:00
|
|
|
"net"
|
2011-11-08 16:43:02 -07:00
|
|
|
"net/http"
|
2011-01-26 17:32:41 -07:00
|
|
|
"os"
|
|
|
|
"strings"
|
2012-11-19 13:36:15 -07:00
|
|
|
"time"
|
2011-01-26 17:32:41 -07:00
|
|
|
)
|
|
|
|
|
2011-01-27 06:19:37 -07:00
|
|
|
var (
|
|
|
|
post = flag.String("post", "", "urlencoded form data to POST")
|
2011-01-28 07:42:51 -07:00
|
|
|
addr = flag.Bool("addr", false, "find open address and print to stdout")
|
2012-11-19 13:36:15 -07:00
|
|
|
wait = flag.Duration("wait_for_port", 0, "if non-zero, the amount of time to wait for the address to become available")
|
2011-01-27 06:19:37 -07:00
|
|
|
)
|
2011-01-26 17:32:41 -07:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
2011-01-28 07:42:51 -07:00
|
|
|
if *addr {
|
2011-01-27 06:19:37 -07:00
|
|
|
l, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
2011-02-01 13:47:35 -07:00
|
|
|
log.Fatal(err)
|
2011-01-27 06:19:37 -07:00
|
|
|
}
|
|
|
|
defer l.Close()
|
2011-01-28 07:42:51 -07:00
|
|
|
fmt.Print(l.Addr())
|
2011-01-27 06:19:37 -07:00
|
|
|
return
|
|
|
|
}
|
2011-01-26 17:32:41 -07:00
|
|
|
url := flag.Arg(0)
|
|
|
|
if url == "" {
|
2011-02-01 13:47:35 -07:00
|
|
|
log.Fatal("no url supplied")
|
2011-01-26 17:32:41 -07:00
|
|
|
}
|
|
|
|
var r *http.Response
|
2011-11-01 20:06:05 -06:00
|
|
|
var err error
|
2012-11-19 13:36:15 -07:00
|
|
|
loopUntil := time.Now().Add(*wait)
|
|
|
|
for {
|
|
|
|
if *post != "" {
|
|
|
|
b := strings.NewReader(*post)
|
|
|
|
r, err = http.Post(url, "application/x-www-form-urlencoded", b)
|
|
|
|
} else {
|
|
|
|
r, err = http.Get(url)
|
|
|
|
}
|
|
|
|
if err == nil || *wait == 0 || time.Now().After(loopUntil) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2011-01-26 17:32:41 -07:00
|
|
|
}
|
|
|
|
if err != nil {
|
2011-02-01 13:47:35 -07:00
|
|
|
log.Fatal(err)
|
2011-01-26 17:32:41 -07:00
|
|
|
}
|
|
|
|
defer r.Body.Close()
|
|
|
|
_, err = io.Copy(os.Stdout, r.Body)
|
|
|
|
if err != nil {
|
2011-02-01 13:47:35 -07:00
|
|
|
log.Fatal(err)
|
2011-01-26 17:32:41 -07:00
|
|
|
}
|
|
|
|
}
|