Created
January 23, 2015 02:13
-
-
Save ajstarks/0d4e78268b5097f42e3f to your computer and use it in GitHub Desktop.
gurl - get URL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// gurl - get the contents of a url to stdout, -v show response on stderr | |
package main | |
import ( | |
"errors" | |
"flag" | |
"fmt" | |
"io" | |
"net/http" | |
"os" | |
"time" | |
) | |
var verbose bool | |
var username, password string | |
func init() { | |
flag.BoolVar(&verbose, "v", false, "verbose") | |
flag.StringVar(&username, "username", "", "username") | |
flag.StringVar(&password, "password", "", "password") | |
flag.Parse() | |
} | |
func dumpresp(resp *http.Response) { | |
for k, v := range resp.Header { | |
fmt.Fprintf(os.Stderr, "%s: %v\n", k, v) | |
} | |
} | |
func requestOut(uri string, w io.Writer, r *http.Response, t time.Time) { | |
io.Copy(w, r.Body) | |
if verbose { | |
fmt.Fprintf(os.Stderr, "\n%s %s %v\nRequest Time: %.3f\n", | |
uri, r.Proto, r.Status, float64(time.Since(t))/1e9) | |
dumpresp(r) | |
} | |
} | |
func authrequest(c *http.Client, uri, user, pass string) (*http.Response, error) { | |
if len(user) == 0 { | |
return nil, errors.New("specify username") | |
} | |
authreq, authnerr := http.NewRequest("GET", uri, nil) | |
if authnerr != nil { | |
return nil, authnerr | |
} | |
authreq.SetBasicAuth(user, pass) | |
authresp, autherr := c.Do(authreq) | |
if autherr != nil { | |
return nil, autherr | |
} | |
return authresp, autherr | |
} | |
func main() { | |
var t0 time.Time | |
var client = &http.Client{} | |
for _, uri := range flag.Args() { | |
t0 = time.Now() | |
r, err := client.Get(uri) | |
if err != nil { | |
fmt.Fprintf(os.Stderr, "%v\n", err) | |
continue | |
} | |
switch r.StatusCode { | |
case http.StatusOK: | |
requestOut(uri, os.Stdout, r, t0) | |
case http.StatusUnauthorized, http.StatusProxyAuthRequired: | |
authresp, autherr := authrequest(client, uri, username, password) | |
if autherr != nil { | |
fmt.Fprintf(os.Stderr, "bad authenticated request: %v\n", autherr) | |
} else { | |
requestOut(uri, os.Stdout, authresp, t0) | |
} | |
default: | |
fmt.Fprintf(os.Stderr, "GET %s, status: %d)\n", uri, r.StatusCode) | |
} | |
r.Body.Close() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment