codemanki / cloudscraper

--DEPRECATED -- 🛑 🛑 Node.js library to bypass cloudflare's anti-ddos page
MIT License
600 stars 139 forks source link

How to cancel a request? #262

Closed waltaki closed 4 years ago

waltaki commented 5 years ago

For example, I need to download a file. In the beginning I will get the headers, check them, for example for the file extension, and if this extension does not suit me, I go to stop receiving the file, do abort.

How do I imagine it:

let get = this.scraper.get(url)

get.onResponse(({ caseless }) => {
  let fileName = caseless.get("content-disposition").match(/filename\*?=['"]?(?:UTF-\d['"]*)?([^;\r\n"']*)['"]?;?/i)[1]
    if(fileName != 'jar') {
        get.abort()
    }
});

get.onResponseEnd(({ caseless, body }) => {
    let fileName = caseless.get("content-disposition").match(/filename\*?=['"]?(?:UTF-\d['"]*)?([^;\r\n"']*)['"]?;?/i)[1]
    if(fileName != 'jar') {
        get.abort()
    } else {
        this.storage.saveFile(body)
    }
});

Also, implementation would be very good as Interceptor, that is, to prevent packets from being received until I continue.

How to implement this?

ghost commented 4 years ago

Hi @waltaki,

Yes, this a bug.

It's not currently possible to abort requests that were redirected due to challenge solving. The number of challenges to solve can be configured by using the challengesToSolve option (defaults to 3). The request can be aborted during normal redirects or if challenges aren't encountered.

const cloudscraper = require('cloudscraper');
const request = cloudscraper.get('https://example.com')

request.abort()

Workaround:

PR's welcome.

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.