本文整理汇总了Golang中net/http.Head函数的典型用法代码示例。如果您正苦于以下问题:Golang Head函数的具体用法?Golang Head怎么用?Golang Head使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Head函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: jobPurger
func jobPurger(couchbaseAddress string) {
client, _ := couchbase.Connect(couchbaseAddress)
pool, _ := client.GetPool("default")
params := map[string]interface{}{
"reduce": false,
"include_docs": true,
"stale": "false",
}
bucket := "server"
mc, err := memcached.Connect("tcp", "127.0.0.1:11210")
_, err = mc.Auth(bucket, "")
if err != nil {
log.Fatal(err)
}
// query bucket
b, err := pool.GetBucket(bucket)
if err != nil {
log.Fatalf("Error reading bucket: %v", err)
}
vr, err := b.View(bucket, "data_by_build", params)
if err != nil {
log.Println(err)
}
for _, row := range vr.Rows {
vals := row.Value.([]interface{})
jobUrl := vals[5].(string)
resp, _ := http.Head(jobUrl)
if resp == nil {
continue
}
if resp.StatusCode == 404 {
// make sure 404 is not because jenkins is down
parsedUrl, _ := url.Parse(jobUrl)
resp, _ = http.Head(parsedUrl.Scheme + "://" + parsedUrl.Host)
if resp != nil && resp.StatusCode != 200 {
log.Println("Jenkins down! skipping: " + jobUrl)
continue
}
log.Println("Purging: " + jobUrl)
id := row.ID
// TODO: using dirty workaround until keys are in correct vbuckets
_, err = mc.Get(0, id)
if err == nil {
_, err = mc.Del(0, id)
if err != nil {
log.Fatal(err)
}
}
}
}
}
示例2: getOnlineFile
func getOnlineFile(fileUrl string, fileName string) string {
finalPath := filepath.Join(getWorkingDirectoryAbsolutePath(), fileName)
// HEAD request to see if file exist on server
resp, err := http.Head(fileUrl)
if err != nil {
log.Fatalln(err)
}
defer resp.Body.Close()
// If whole file does not exist
if resp.StatusCode == 404 {
resp, err = http.Head(fileUrl + `.part0`)
if err != nil {
log.Fatalln(err)
}
defer resp.Body.Close()
// If small parts do not exist
if resp.StatusCode == 404 {
log.Fatalln(`File`, fileUrl, `not found`)
// Else, it must be big file
} else {
// Download parts
for i := 0; resp.StatusCode != 404; i++ {
pathToWrite := filepath.Join(getWorkingDirectoryAbsolutePath(), fileName+`.part`+strconv.Itoa(i))
downloadIfBadFile(fileUrl+`.part`+strconv.Itoa(i), pathToWrite, fileName+`.part`+strconv.Itoa(i))
resp, err = http.Head(fileUrl + `.part` + strconv.Itoa(i+1))
if err != nil {
log.Fatalln(`Error getting part`, strconv.Itoa(i)+`:`, err)
}
defer resp.Body.Close()
}
// Reconstruct
chunker.Reconstruct(finalPath)
}
// Else, download like normal
} else {
pathToWrite := finalPath
downloadIfBadFile(fileUrl, pathToWrite, fileName)
}
return finalPath
}
示例3: patchExists
func patchExists(a, b release) (bool, error) {
res, err := http.Head(s3PatchURL + patchFilename(a, b))
if err != nil {
return false, err
}
return res.StatusCode == 200, nil
}
示例4: wait
func (srv *Server) wait(timeout <-chan time.Time) error {
var (
lastError error
)
url := fmt.Sprintf("http://%s/", srv.addr)
for {
select {
case <-timeout:
return E_TIMEOUT
default:
if srv.state != nil {
return srv.state
}
response, err := http.Head(url)
if err == nil {
if response != nil && response.Body != nil {
response.Body.Close()
}
return nil
}
lastError = err
time.Sleep(100 * time.Millisecond)
}
}
return lastError
}
示例5: Poll
// Poll executes an HTTP HEAD request for the resource url
// and returns health status and a detail string;
// TODO: if HTTP status is 403 or 404 returns an os.Error (NewError)
func (svc *HTTPService) Poll() (monitor.Health, string, error) {
resp, err := http.Head(svc.URL)
if err != nil {
return monitor.Critical, err.Error(), nil
}
return monitor.Ok, resp.Status, nil
}
示例6: checkStatus
func checkStatus(urlName string) {
url, found := res[urlName]
if !found {
fmt.Println("\nUnknown URL name:", urlName)
printUrlNames()
fmt.Println()
return
}
// Just want the response header. Don't want the body content.
// Use HEAD method instead of GET.
resp, err := http.Head(url)
if err != nil {
fmt.Println("Error:", err)
return
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusOK:
fmt.Println(urlName, ":", url, "is up.")
case http.StatusServiceUnavailable:
fmt.Println(urlName, ":", url, "is down.")
default:
fmt.Println(urlName, ":", url, " return statusCode ", resp.StatusCode)
}
}
示例7: Download
// Download fetches an image from the Internet.
// Defaults:
// - dest os.Getwd()
func Download(URL string, dest string) error {
_, err := url.Parse(URL)
if err != nil {
return fmt.Errorf("not a valid URL: %+v", err)
}
res, err := http.Head(URL)
if err != nil {
return fmt.Errorf("url unavailable: %s", URL)
}
if res.StatusCode != http.StatusOK {
return fmt.Errorf("non-200 code from url: %s", URL)
}
res, err = http.Get(URL)
if err != nil {
return fmt.Errorf("couldn't download the file: %+v", err)
}
defer res.Body.Close()
if dest == "" {
tok := strings.Split(URL, "/")
dest = tok[len(tok)-1]
}
if _, err := os.Stat(dest); !os.IsNotExist(err) {
return fmt.Errorf("file already exists: %s", dest)
}
w, err := os.Create(dest)
if err != nil {
return fmt.Errorf("couldn't create file: %s", dest)
}
defer w.Close()
_, err = io.Copy(w, res.Body)
return err
}
示例8: restartLoop
func (c *Config) restartLoop() {
if c.RestartPolicy == RestartNever {
return
}
url := "https://storage.googleapis.com/" + c.BinaryBucket + "/" + c.binaryObject()
var lastEtag string
for {
res, err := http.Head(url + "?" + fmt.Sprint(time.Now().Unix()))
if err != nil {
log.Printf("Warning: %v", err)
time.Sleep(15 * time.Second)
continue
}
etag := res.Header.Get("Etag")
if etag == "" {
log.Printf("Warning, no ETag in response: %v", res)
time.Sleep(15 * time.Second)
continue
}
if lastEtag != "" && etag != lastEtag {
log.Printf("Binary updated; restarting.")
// TODO: more graceful restart, letting systemd own the network connections.
// Then we can finish up requests here.
os.Exit(0)
}
lastEtag = etag
time.Sleep(15 * time.Second)
}
}
示例9: useData
// useData will read until the chan is closed but it will read items concurrently and the
// work is controlled with a loop acting as a pool and a WaitGroup to ensure it all finishes before we return.
func useData() {
defer close(done)
var wg sync.WaitGroup
concurrencyRate := 10 // in the wild you'd use a config variable for this
for i := 0; i <= concurrencyRate; i++ {
fmt.Println("Worker ", i)
wg.Add(1)
go func() {
defer wg.Done()
for p := range providerChan {
func() {
fmt.Printf("Read from chan: %q, %q\n", p.name, p.url)
resp, err := http.Head(p.url)
if err != nil {
fmt.Printf("Error making head request for %q: %v\n", p.url, err)
return
}
defer resp.Body.Close()
fmt.Printf("Processing Data: %q\t%v\n", p.name, resp)
}()
}
}()
}
wg.Wait()
}
示例10: GetCode
func GetCode(url string) {
res, err := http.Head(url)
if err != nil {
fmt.Println(err)
}
fmt.Println(res.StatusCode)
}
示例11: SendRequest
func (self *InputConfig) SendRequest() (data string, err error) {
var (
res *http.Response
raw []byte
)
switch self.Method {
case "HEAD":
res, err = http.Head(self.Url)
case "GET":
res, err = http.Get(self.Url)
default:
err = errors.New("Unknown method")
}
if err != nil {
return
}
defer res.Body.Close()
if raw, err = ioutil.ReadAll(res.Body); err != nil {
return
}
data = string(raw)
data = strings.TrimSpace(data)
return
}
示例12: download_file_info
// 获取下载文件信息
func download_file_info(url string) (download_file *DownloadFile, err error) {
head_resp, err := http.Head(url)
if err != nil {
return nil, err
}
download_file = &DownloadFile{}
download_file.OriginUrl = url
if ranges := head_resp.Header.Get("Accept-Ranges"); len(strings.Trim(ranges, " ")) > 0 {
log.Println("==>", ranges)
download_file.AcceptRanges = true
}
download_file.ContentLength = float64(head_resp.ContentLength)
sections := strings.Split(url, "/")
download_file.FileName = sections[len(sections)-1]
if download_file.ContentLength > TB {
download_file.Sizer = fmt.Sprintf("%.2f%s", download_file.ContentLength/TB, "TB")
} else if download_file.ContentLength > GB {
download_file.Sizer = fmt.Sprintf("%.2f%s", download_file.ContentLength/GB, "GB")
} else if download_file.ContentLength > MB {
download_file.Sizer = fmt.Sprintf("%.2f%s", download_file.ContentLength/MB, "MB")
} else if download_file.ContentLength > KB {
download_file.Sizer = fmt.Sprintf("%.2f%s", download_file.ContentLength/KB, "KB")
} else {
download_file.Sizer = fmt.Sprintf("%.2f%s", download_file.ContentLength, "B")
}
return
}
示例13: BlobLength
func (r *clientImpl) BlobLength(name string, dgst digest.Digest) (int, error) {
blobURL, err := r.ub.BuildBlobURL(name, dgst)
if err != nil {
return -1, err
}
response, err := http.Head(blobURL)
if err != nil {
return -1, err
}
defer response.Body.Close()
// TODO(bbland): handle other status codes, like 5xx errors
switch {
case response.StatusCode == http.StatusOK:
lengthHeader := response.Header.Get("Content-Length")
length, err := strconv.ParseInt(lengthHeader, 10, 64)
if err != nil {
return -1, err
}
return int(length), nil
case response.StatusCode == http.StatusNotFound:
return -1, nil
case response.StatusCode >= 400 && response.StatusCode < 500:
var errs v2.Errors
decoder := json.NewDecoder(response.Body)
err = decoder.Decode(&errs)
if err != nil {
return -1, err
}
return -1, &errs
default:
return -1, &UnexpectedHTTPStatusError{Status: response.Status}
}
}
示例14: hasNewStories
func (rss *SourceRss) hasNewStories() (has bool) {
response, err := http.Head(rss.url)
if err != nil {
fmt.Printf("There was an error running the HEAD command on url:\n%s", rss.url)
fmt.Printf("Assuming new stories were available\n")
return true
}
// Try and figure out the mtime
mtime := response.Header.Get("Last-Modified")
var parsed_time int64
if len(mtime) > 0 {
parsed_time, err = parse_time(mtime)
if err != nil {
fmt.Printf("There was an error parsing the timestamp: %s\n", mtime)
}
fmt.Printf("Last crawled = %d\n", rss.lastCrawled)
fmt.Printf("Updated = %d (%s)\n", parsed_time, mtime)
} else {
fmt.Printf("There was omething wrong with Last-Modified")
}
// If there are changes, yes
if rss.lastCrawled < parsed_time {
return true
}
return false
}
示例15: hasChangedSince
func hasChangedSince(url string, lastModified time.Time) (bool, time.Time, error) {
res, err := http.Head(url)
if err != nil {
return false, time.Time{}, fmt.Errorf("HEAD request failed (%s)", err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
return false, time.Time{}, fmt.Errorf("HEAD request response %d", res.StatusCode)
}
header := res.Header.Get("Last-Modified")
t, err := time.Parse(time.RFC1123, header)
if err != nil {
return false, time.Time{}, fmt.Errorf("Failed to parse Last-Modified header (%s)", err)
}
if t.After(lastModified) {
return true, t, nil
}
return false, t, nil
}