]> Cypherpunks repositories - gostls13.git/commitdiff
http: add Head function for making HTTP HEAD requests
authorAndrew Gerrand <adg@golang.org>
Mon, 7 Jun 2010 23:28:40 +0000 (01:28 +0200)
committerAndrew Gerrand <adg@golang.org>
Mon, 7 Jun 2010 23:28:40 +0000 (01:28 +0200)
R=rsc
CC=golang-dev
https://golang.org/cl/1581041

src/pkg/http/client.go
src/pkg/http/client_test.go

index fe61d2073326c784e714705133879a3d1d800add..54487dac2f9c60ce8fdc29e34c754a9285c528f6 100644 (file)
@@ -130,7 +130,6 @@ func Get(url string) (r *Response, finalURL string, err os.Error) {
        return
 }
 
-
 // Post issues a POST to the specified URL.
 //
 // Caller should close r.Body when done reading it.
@@ -154,6 +153,19 @@ func Post(url string, bodyType string, body io.Reader) (r *Response, err os.Erro
        return send(&req)
 }
 
+// Head issues a HEAD to the specified URL.
+func Head(url string) (r *Response, err os.Error) {
+       var req Request
+       req.Method = "HEAD"
+       if req.URL, err = ParseURL(url); err != nil {
+               return
+       }
+       if r, err = send(&req); err != nil {
+               return
+       }
+       return
+}
+
 type nopCloser struct {
        io.Reader
 }
index 6787825d81f001c1e78b0cde65bad6a17784a2a6..a916b12e240fbc6c762058dcd51946e512c36e9a 100644 (file)
@@ -28,3 +28,13 @@ func TestClient(t *testing.T) {
                t.Errorf("Incorrect page body (did not begin with User-agent): %q", s)
        }
 }
+
+func TestClientHead(t *testing.T) {
+       r, err := Head("http://www.google.com/robots.txt")
+       if err != nil {
+               t.Error(err)
+       }
+       if _, ok := r.Header["Last-Modified"]; !ok {
+               t.Error("Last-Modified header not found.")
+       }
+}