valyala/fasthttp

fasthttp client. Read body in chunks and stop downloading on condition.

Open

#1.065 aberto em 5 de ago. de 2021

Ver no GitHub
 (8 comments) (0 reactions) (0 assignees)Go (1.755 forks)batch import
help wanted

Métricas do repositório

Stars
 (21.741 stars)
Métricas de merge de PR
 (Mesclagem média 1d 11h) (34 fundiu PRs em 30d)

Description

For example, I want to stop downloading content if it has wrong mime type. How can I do so? The documentation shows only resp.Body() which gets whole response. Below is how I use standard http library:

resp, err := client.Do(req)
if err != nil {
	return nil, err
}
body := resp.Body
defer body.Close()

checkedMime := false
readBuffer := make([]byte, 1 << 12)
bodyData := make([]byte, 0, typicalSize)
for {
	n, err := body.Read(readBuffer)
	if err != nil && err != io.EOF {
		return nil, err
	}
	bodyData = append(bodyData, readBuffer[0:n]...)
	if !checkedMime && len(bodyData) > 512{			
		mimeType := http.DetectContentType(bodyData)
		if isArchive(mimeType){
			isArchive = true
			break
		}
		if mimeType  != "text/html"{
			isUselessContent = true
			break
		}
		checkedMime = true
	}
	if len(bodyData) > tooBigContentSize {
		tooBigContent = true
		break
	}
	if err == io.EOF {
		break
	}
}
// do something with bodyData, isArchive, isUselessContent, tooBigContent
...

Guia do colaborador