fasthttp icon indicating copy to clipboard operation
fasthttp copied to clipboard

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

Open edwvee opened this issue 2 years ago • 6 comments

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
...

edwvee avatar Aug 05 '21 13:08 edwvee

You could try using https://pkg.go.dev/github.com/valyala/fasthttp?utm_source=godoc#Server.HeaderReceived Its ugly but you could potentially reject the request by setting MaxRequestBodySize to 1 for example.

I would also be open to a pull request that adds something to https://pkg.go.dev/github.com/valyala/fasthttp?utm_source=godoc#RequestConfig to reject the request at that point.

erikdubbelboer avatar Aug 05 '21 15:08 erikdubbelboer

But I have a client, not a server.

edwvee avatar Aug 05 '21 15:08 edwvee

Added a code using the net/http to achieve what I need as an example.

edwvee avatar Aug 05 '21 17:08 edwvee

@erikdubbelboer, such a feature will be useful! I guess I have a patch for this, to skip downloading in case mime type in headers not expected, but not inside the actual body, but need to check.

moredure avatar Aug 11 '21 16:08 moredure

If you have something that works and you can make a pull request that would be great!

erikdubbelboer avatar Aug 14 '21 08:08 erikdubbelboer

but it works like skip body if unwanted header met, and not allows to download body in chunks and check it for mime type

moredure avatar Aug 15 '21 13:08 moredure