示例#1
0
文件: option.go 项目: itchio/wharf
func defaultHTTPClient() *http.Client {
	client := timeout.NewClient(time.Second*time.Duration(30), time.Second*time.Duration(15))
	client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
		if len(via) >= 10 {
			return errors.New("stopped after 10 redirects")
		}

		// forward initial request headers
		// see https://github.com/itchio/itch/issues/965
		ireq := via[0]
		for key, values := range ireq.Header {
			for _, value := range values {
				req.Header.Add(key, value)
			}
		}

		return nil
	}
	return client
}
示例#2
0
func NewResumableUpload(uploadURL string, done chan bool, errs chan error, settings ResumableUploadSettings) (*ResumableUpload, error) {
	ru := &ResumableUpload{}
	ru.MaxChunkGroup = settings.MaxChunkGroup
	if ru.MaxChunkGroup == 0 {
		ru.MaxChunkGroup = 64
	}
	ru.uploadURL = uploadURL
	ru.id = seed
	seed++
	ru.consumer = settings.Consumer
	ru.httpClient = timeout.NewClient(resumableConnectTimeout, resumableIdleTimeout)

	pipeR, pipeW := io.Pipe()

	ru.pipeWriter = pipeW

	bufferSize := settings.BufferSize
	if bufferSize == 0 {
		bufferSize = 32 * 1024 * 1024
	}

	bufferedWriter := bufio.NewWriterSize(pipeW, bufferSize)
	ru.bufferedWriter = bufferedWriter

	onWrite := func(count int64) {
		// ru.Debugf("onwrite %d", count)
		ru.TotalBytes = count
		if ru.OnProgress != nil {
			ru.OnProgress()
		}
	}
	ru.writeCounter = counter.NewWriterCallback(onWrite, bufferedWriter)

	go ru.uploadChunks(pipeR, done, errs)

	return ru, nil
}