Indeed, :content_cb seems the way to go...
After having a look at the docs, I understand it like this: :content:cb takes the $response->content out of the loop and pushes everything received to the defined sub. Ok so far. But does this mean the response object becomes incomplete?
I just want to peek at the content and in case of non-text/* data quit the request. But as far as I understand it, content won't end up in $response->content when I use :content_cb - right?
BTW: any hints what a good regex to find 'binary values' would be?
Thanks to all!
| [reply] |
But as far as I understand it, content won't end up in $response->content when I use :content_cb - right?
When I've used this I just accumulated the content myself. The response object still contains everything else, status, headers etc. If it is important for you to have a complete response object, you can always set the content back into it using $resp->content( $content );.
Any hints what a good regex to find 'binary values' would be?
Tough one for the general case. Binary data, by definition can not only contain any byte value, but also may not contain any given subset of byte values. Things are further complicated it your site delivers unicode rather than just ascii (or whatever that darn iso number is), but you know your server and the types of data it delivers, which should help you decide.
Assuming your text is ascii, you might try looking for just null (ord 0) bytes, most text would not contain those, but some small header samples of binary files might not also. So something like $data =~ tr[\x00-\x08\x0f-\x1f][]c;, ie. ascii 0 thru 31 minus HT, LF, VT, FF and CR ought to detect something non-text in most cases.
If you are delivering unicode, then you will need a different heuristic. For example, there is a module somewhere on CPAN that attempts to detect filetype from header information: like .exe's having MX as the first two bytes; and .zips have PK; and GIFs, GIF98 etc. Reading the first block and passing it to one of those routines, or the unix utility that does the same thing might allow a higher degree of success.
In the end, you know (or can find out) what kind of textual data your server can deliver. If, for example, you only deliver html, then just looking for the <HEAD> tag might suffice.
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |
You could write the beginning to a file and use the -B function.
| [reply] |