GoPLS Viewer

Home|gopls/go/vcs/http.go
1// Copyright 2012 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package vcs
6
7import (
8    "fmt"
9    "io"
10    "io/ioutil"
11    "log"
12    "net/http"
13    "net/url"
14)
15
16// httpClient is the default HTTP client, but a variable so it can be
17// changed by tests, without modifying http.DefaultClient.
18var httpClient = http.DefaultClient
19
20// httpGET returns the data from an HTTP GET request for the given URL.
21func httpGET(url string) ([]byteerror) {
22    resperr := httpClient.Get(url)
23    if err != nil {
24        return nilerr
25    }
26    defer resp.Body.Close()
27    if resp.StatusCode != 200 {
28        return nilfmt.Errorf("%s: %s"urlresp.Status)
29    }
30    berr := ioutil.ReadAll(resp.Body)
31    if err != nil {
32        return nilfmt.Errorf("%s: %v"urlerr)
33    }
34    return bnil
35}
36
37// httpsOrHTTP returns the body of either the importPath's
38// https resource or, if unavailable, the http resource.
39func httpsOrHTTP(importPath string) (urlStr stringbody io.ReadClosererr error) {
40    fetch := func(scheme string) (urlStr stringres *http.Responseerr error) {
41        uerr := url.Parse(scheme + "://" + importPath)
42        if err != nil {
43            return ""nilerr
44        }
45        u.RawQuery = "go-get=1"
46        urlStr = u.String()
47        if Verbose {
48            log.Printf("Fetching %s"urlStr)
49        }
50        reserr = httpClient.Get(urlStr)
51        return
52    }
53    closeBody := func(res *http.Response) {
54        if res != nil {
55            res.Body.Close()
56        }
57    }
58    urlStrreserr := fetch("https")
59    if err != nil || res.StatusCode != 200 {
60        if Verbose {
61            if err != nil {
62                log.Printf("https fetch failed.")
63            } else {
64                log.Printf("ignoring https fetch with status code %d"res.StatusCode)
65            }
66        }
67        closeBody(res)
68        urlStrreserr = fetch("http")
69    }
70    if err != nil {
71        closeBody(res)
72        return ""nilerr
73    }
74    // Note: accepting a non-200 OK here, so people can serve a
75    // meta import in their http 404 page.
76    if Verbose {
77        log.Printf("Parsing meta tags from %s (status code %d)"urlStrres.StatusCode)
78    }
79    return urlStrres.Bodynil
80}
81
MembersX
httpsOrHTTP.body
httpsOrHTTP.BlockStmt.u
ioutil
log
httpsOrHTTP.importPath
httpsOrHTTP.BlockStmt.err
url
httpGET.b
httpGET.url
httpGET.resp
httpGET.err
http
httpGET
httpsOrHTTP.urlStr
httpsOrHTTP.err
httpsOrHTTP.res
httpClient
httpsOrHTTP
Members
X