Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #5 from RazorGhost/master

write to file with option CURLOPT_WRITEDATA. accepts IO::Handle as arg
  • Loading branch information...
commit a9450d7908cde10fd80b0021b6d8fb20b51062cb 2 parents 2c7b388 + cb0d5c3
@azawawi authored
Showing with 16 additions and 7 deletions.
  1. +16 −7 lib/Net/Curl.pm6
View
23 lib/Net/Curl.pm6
@@ -240,23 +240,32 @@ my sub _curl_easy_setopt_cb(OpaquePointer, int, &cb (Pointer $ptr, int $size, in
# For Buf type you must decode it manualy. For example my $buf = Buf.new(); `perform` my Str $s = $buf.decode('utf-8')
# curl_easy_setopt( $curl, CURLOPT_WRITEDATA, Str $body is rw );
# curl_easy_setopt( $curl, CURLOPT_WRITEDATA, Buf $body is rw );
-# TODO add ref to function/IO ( $value )
+# curl_easy_setopt( $curl, CURLOPT_WRITEDATA, IO::Handle $body is rw ); # you must close your FH
+# TODO add ref to function ( $value )
multi sub curl_easy_setopt(OpaquePointer $point, CURLOPT_WRITEDATA, $value is rw) returns int is export {
- die "Invalid type to write data to. Use Str|Buf" if $value !~~ Str|Buf ;
-
- $value = Buf.new if $value ~~ Buf ;
- $value = '' if $value ~~ Str ;
+ die "Invalid type to write data to. Use Str|Buf|IO::Handle" if $value !~~ Str|Buf|IO::Handle ;
my Bool $is_str = $value ~~ Str ;
+ my Bool $is_file = $value ~~ IO::Handle;
+ my Bool $is_buf = $value ~~ Buf ;
+
+ $value = Buf.new if $is_buf;
+ $value = '' if $is_str;
sub callback( Pointer $ptr , int $size, int $nmemb, OpaquePointer $wtf --> int ) {
my $bytes = nativecast( CArray[int8], $ptr ) ;
my $buf = Buf.new( $bytes[0..($size*$nmemb-1)] );
- $buf = $buf.decode('latin1') if $is_str ;# TODO in the future process encodings ?
- $value ~= $buf ;
+ $buf = $buf.decode('latin1') if $is_str ;# TODO in the future process encodings ?
+
+ if $is_buf {
+ $value ~= $buf ;
+ }
+ elsif $is_file {
+ $value.write( $buf );
+ }
CATCH {
warn "Failed to process chunk";
Please sign in to comment.
Something went wrong with that request. Please try again.