当前位置: 首页>>代码示例>>Golang>>正文


Golang http.Head函数代码示例

本文整理汇总了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)
				}
			}
		}
	}
}
开发者ID:subalakr,项目名称:greenboard,代码行数:57,代码来源:purger.go

示例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
}
开发者ID:NeuralProsthesisLab,项目名称:unlock,代码行数:48,代码来源:install.go

示例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
}
开发者ID:Cardinala,项目名称:hk,代码行数:7,代码来源:gen.go

示例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
}
开发者ID:jiakuan,项目名称:livedev,代码行数:30,代码来源:server.go

示例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
}
开发者ID:rmorriso,项目名称:gomon,代码行数:10,代码来源:httpservice.go

示例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)
	}
}
开发者ID:mubitosh,项目名称:isup,代码行数:27,代码来源:isup.go

示例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
}
开发者ID:andreis,项目名称:rocker,代码行数:37,代码来源:rocker.go

示例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)
	}
}
开发者ID:camlistore,项目名称:camlistore,代码行数:29,代码来源:cloudlaunch.go

示例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()
}
开发者ID:nathj07,项目名称:talks,代码行数:27,代码来源:sigint.go

示例10: GetCode

func GetCode(url string) {
	res, err := http.Head(url)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(res.StatusCode)
}
开发者ID:tianyunchong,项目名称:golang,代码行数:7,代码来源:url.go

示例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
}
开发者ID:vaderyang,项目名称:gogstash,代码行数:27,代码来源:inputhttp.go

示例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
}
开发者ID:shenshouer,项目名称:downloader,代码行数:32,代码来源:httpDownloadtest.go

示例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}
	}
}
开发者ID:EricMountain-1A,项目名称:openshift-origin,代码行数:35,代码来源:client.go

示例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
}
开发者ID:liquidmetal,项目名称:broccoli-munch,代码行数:28,代码来源:rss.go

示例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
}
开发者ID:jagregory,项目名称:conditionalhttp,代码行数:25,代码来源:conditionalhttp.go


注:本文中的net/http.Head函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。