Permalink
Browse files

simple_http: add support for filename suggestion from Content-Disposi…

…tion

will be used by GMB::DropURI
lots of common code added to the 3 files, will need to reorganize that
  • Loading branch information...
squentin committed Jun 22, 2014
1 parent 9b72091 commit cba20c80d65958116b9d794b9fcaafb8284a6d73
Showing with 51 additions and 9 deletions.
  1. +17 −3 simple_http.pm
  2. +17 −3 simple_http_AE.pm
  3. +17 −3 simple_http_wget.pm
View
@@ -31,7 +31,7 @@ sub get_with_cb
my ($callback,$url,$post)=@params{qw/cb url post/};
if (my $cached= $params{cache} && GMB::Cache::get($url))
{ warn "cached result\n" if $::debug;
Glib::Timeout->add(10,sub { $callback->( ${$cached->{data}}, type=>$cached->{type}, ); 0});
Glib::Timeout->add(10,sub { $callback->( ${$cached->{data}}, type=>$cached->{type}, filename=>$cached->{filename}, ); 0});
return $self;
}
warn "simple_http : fetching $url\n" if $::debug;
@@ -162,6 +162,20 @@ sub receiving_cb
my %headers;
$headers{lc $1}=$2 while $headers=~m/([^:]*): (.*?)$EOL/og;
my $filename;
if ($headers{'content-disposition'} && $headers{'content-disposition'}=~m#^\s*\w+\s*;\s*filename(\*)?=(.*)$#mgi)
{ $filename=$2; my $rfc5987=$1;
#decode filename, not perfectly, but good enough (http://greenbytes.de/tech/tc2231/ is a good reference)
$filename=~s#\\(.)#"\x00".ord($1)."\x00"#ge;
my $enc='iso-8859-1';
if ($rfc5987 && $filename=~s#^([A-Za-z0-9_-]+)'\w*'##) {$enc=$1; $filename=::decode_url($filename)} #RFC5987
else
{ if ($filename=~s/^"(.*)"$/$1/) { $filename=~s#\x00(\d+)\x00#chr($1)#ge; $filename=~s#\\(.)#"\x00".ord($1)."\x00"#ge; }
elsif ($filename=~m#[^A-Za-z0-9_.\x00-]#) {$filename=''}
}
$filename=~s#\x00(\d+)\x00#chr($1)#ge;
$filename= eval {Encode::decode($enc,$filename)};
}
if (my $enc=$headers{'content-encoding'})
{ if ($enc eq 'gzip' && $gzip_ok)
{ my $gzipped= $response;
@@ -177,9 +191,9 @@ sub receiving_cb
{ #warn "ok $url\n$callback\n";
my $type=$headers{'content-type'};
if ($self->{params}{cache} && defined $response)
{ GMB::Cache::add($url,{data=>\$response,type=>$type,size=>length($response)});
{ GMB::Cache::add($url,{data=>\$response,type=>$type,size=>length($response),filename=>$filename});
}
$callback->($response, type=>$type, url=>$self->{params}{url});
$callback->($response, type=>$type, url=>$self->{params}{url}, filename=>$filename);
}
elsif ($result=~m#^HTTP/1\.\d+ 30[123]# && $headers{location}) #redirection
{ my $url=$headers{location};
View
@@ -24,7 +24,7 @@ sub get_with_cb
delete $params{cache} unless $UseCache;
if (my $cached= $params{cache} && GMB::Cache::get($url))
{ warn "cached result\n" if $::debug;
Glib::Timeout->add(10,sub { $callback->( ${$cached->{data}}, type=>$cached->{type}, ); 0});
Glib::Timeout->add(10,sub { $callback->( ${$cached->{data}}, type=>$cached->{type}, filename=>$cached->{filename}, ); 0});
return $self;
}
warn "simple_http_AE : fetching $url\n" if $::debug;
@@ -57,6 +57,20 @@ sub finished
my $callback= $self->{params}{cb};
delete $_[0]{request};
#warn "$_=>$headers->{$_}\n" for sort keys %$headers;
my $filename;
if ($headers->{'content-disposition'} && $headers->{'content-disposition'}=~m#^\s*\w+\s*;\s*filename(\*)?=(.*)$#mgi)
{ $filename=$2; my $rfc5987=$1;
#decode filename, not perfectly, but good enough (http://greenbytes.de/tech/tc2231/ is a good reference)
$filename=~s#\\(.)#"\x00".ord($1)."\x00"#ge;
my $enc='iso-8859-1';
if ($rfc5987 && $filename=~s#^([A-Za-z0-9_-]+)'\w*'##) {$enc=$1; $filename=::decode_url($filename)} #RFC5987
else
{ if ($filename=~s/^"(.*)"$/$1/) { $filename=~s#\x00(\d+)\x00#chr($1)#ge; $filename=~s#\\(.)#"\x00".ord($1)."\x00"#ge; }
elsif ($filename=~m#[^A-Za-z0-9_.\x00-]#) {$filename=''}
}
$filename=~s#\x00(\d+)\x00#chr($1)#ge;
$filename= eval {Encode::decode($enc,$filename)};
}
if (my $enc=$headers->{'content-encoding'})
{ if ($enc eq 'gzip' && $gzip_ok)
{ my $gzipped= $response;
@@ -71,9 +85,9 @@ sub finished
if ($headers->{Reason} eq 'OK') # and $headers->{Status} == 200 ?
{ my $type= $headers->{'content-type'};
if ($self->{params}{cache} && defined $response)
{ GMB::Cache::add($url,{data=>\$response,type=>$type,size=>length($response)});
{ GMB::Cache::add($url,{data=>\$response,type=>$type,size=>length($response),filename=>$filename});
}
$callback->($response,type=>$type,url=>$self->{params}{url});
$callback->($response,type=>$type,url=>$self->{params}{url},filename=>$filename);
}
else
{ my $error= $headers->{Status}.' '.$headers->{Reason};
View
@@ -26,7 +26,7 @@ sub get_with_cb
delete $params{cache} unless $UseCache;
if (my $cached= $params{cache} && GMB::Cache::get($url))
{ warn "cached result\n" if $::debug;
Glib::Timeout->add(10,sub { $callback->( ${$cached->{data}}, type=>$cached->{type}, ); 0});
Glib::Timeout->add(10,sub { $callback->( ${$cached->{data}}, type=>$cached->{type}, filename=>$cached->{filename}, ); 0});
return $self;
}
@@ -90,6 +90,20 @@ sub receiving_cb
$result=$1 while $self->{ebuffer}=~m#^ (HTTP/1\.\d+.*)$#mg; ##
#warn $self->{ebuffer};
my $filename;
while ($self->{ebuffer}=~m#^ Content-Disposition:\s*\w+\s*;\s*filename(\*)?=(.*)$#mgi)
{ $filename=$2; my $rfc5987=$1;
#decode filename, not perfectly, but good enough (http://greenbytes.de/tech/tc2231/ is a good reference)
$filename=~s#\\(.)#"\x00".ord($1)."\x00"#ge;
my $enc='iso-8859-1';
if ($rfc5987 && $filename=~s#^([A-Za-z0-9_-]+)'\w*'##) {$enc=$1; $filename=::decode_url($filename)} #RFC5987
else
{ if ($filename=~s/^"(.*)"$/$1/) { $filename=~s#\x00(\d+)\x00#chr($1)#ge; $filename=~s#\\(.)#"\x00".ord($1)."\x00"#ge; }
elsif ($filename=~m#[^A-Za-z0-9_.\x00-]#) {$filename=''}
}
$filename=~s#\x00(\d+)\x00#chr($1)#ge;
$filename= eval {Encode::decode($enc,$filename)};
}
my ($enc)= $self->{ebuffer}=~m#^ Content-Encoding:\s*(.*)#mg;
if ($enc)
{ if ($enc eq 'gzip' && $gzip_ok)
@@ -106,9 +120,9 @@ sub receiving_cb
if ($result=~m#^HTTP/1\.\d+ 200 OK#)
{ my $response=\$self->{content};
if ($self->{params}{cache} && defined $$response)
{ GMB::Cache::add($url,{data=>$response,type=>$type,size=>length($$response)});
{ GMB::Cache::add($url,{data=>$response,type=>$type,size=>length($$response),filename=>$filename});
}
$callback->($$response,type=>$type,url=>$self->{params}{url});
$callback->($$response,type=>$type,url=>$self->{params}{url},filename=>$filename);
}
else
{ warn "Error fetching $url : $result\n";

0 comments on commit cba20c8

Please sign in to comment.