LWPng patch to fix binmode problems on Windows NT

Blair Zajac (bzajac@geostaff.com)
Wed, 14 Apr 1999 12:20:02 -0700


This is a multi-part message in MIME format.
--------------5C589D785E1842675CD2452F
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Hello Gisle,

Thank you for putting out the currentl release of LWPng so quickly
with my patch.  I appreciate it.

I'm including a patch below for LWP::Conn::FILE that fixes a problem
when LWPng is used on Windows systems and either binary or text files
are being read from local disk.  The current code would never read
the file in binary mode and hence could not read binary data containing
control-Z's.  The patch below uses the guess_media_type subroutine
to switch the reading of the file into binary using binmode unless
the file is of a type text.  The code may need to be adopted more
as other 

Could you please push out a new LWPng release quickly with this?  My
team is releasing a product using LWPng and applying patches to other
modules is not ideal.

Thanks,
Blair
--------------5C589D785E1842675CD2452F
Content-Type: text/plain; charset=us-ascii;
 name="LWPng-patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="LWPng-patch"

diff -rc ../LWPng-alpha-0.24/lib/LWP/Conn/FILE.pm ./lib/LWP/Conn/FILE.pm
*** ../LWPng-alpha-0.24/lib/LWP/Conn/FILE.pm	Mon Apr 12 04:29:36 1999
--- ./lib/LWP/Conn/FILE.pm	Tue Apr 13 10:07:17 1999
***************
*** 84,89 ****
--- 84,99 ----
      local(*FILE);
      if (sysopen(FILE, $path, 0)) {
  	my $res = $req->new_response(200, "OK");
+ 
+ 	# Guess the content type and try to determine if the file
+ 	# should be read in binary or text mode.  Go into binary
+ 	# mode unless the content type is some form of text.
+ 	$res->header("Content-Location" => "file:$path"); # XXX absolutize
+ 	my $content_type = guess_media_type($path, $res);
+ 	unless ($content_type =~ /^text/i) {
+ 	    binmode(FILE);
+ 	}
+ 
  	my $now = time;
  
  	$res->date($now);
***************
*** 95,101 ****
  	my $uname = ($has_getpwuid ? getpwuid($uid) : undef) || $uid;
  	my $gname = ($has_getgrgid ? getgrgid($gid) : undef) || $gid;
  
! 	# far more than you ever wanted to know
  	$res->header("INode" => sprintf("[%04x]:%d", $dev, $ino)) if $ino;
  	$res->header("Owner" => $uname);
  	$res->header("Group" => $gname);
--- 105,111 ----
  	my $uname = ($has_getpwuid ? getpwuid($uid) : undef) || $uid;
  	my $gname = ($has_getgrgid ? getgrgid($gid) : undef) || $gid;
  
! 	# Far more than you ever wanted to know.
  	$res->header("INode" => sprintf("[%04x]:%d", $dev, $ino)) if $ino;
  	$res->header("Owner" => $uname);
  	$res->header("Group" => $gname);
***************
*** 105,114 ****
  	$res->header("Last-Accessed" => time2str($atime));
  	$res->header("Status-Modified" => time2str($ctime));
  
! 	$res->header("Content-Location" => "file:$path"); # XXX absolutize
! 	guess_media_type($path, $res);
! 
! 	# We use the same algoritm as Apache to generate an etag.
  	my $etag = sprintf qq("%x-%x-%x"), $ino, $filesize, $mtime;
  	$etag = "W/$etag" if $now - $mtime < 2;
  	$res->header("ETag" => $etag);
--- 115,121 ----
  	$res->header("Last-Accessed" => time2str($atime));
  	$res->header("Status-Modified" => time2str($ctime));
  
! 	# We use the same algorithm as Apache to generate an etag.
  	my $etag = sprintf qq("%x-%x-%x"), $ino, $filesize, $mtime;
  	$etag = "W/$etag" if $now - $mtime < 2;
  	$res->header("ETag" => $etag);

--------------5C589D785E1842675CD2452F
Content-Type: text/x-vcard; charset=us-ascii;
 name="bzajac.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for Blair Zajac
Content-Disposition: attachment;
 filename="bzajac.vcf"

begin:vcard 
n:Zajac;Blair
tel;fax:310-827-8177
tel;work:310-827-3700 x1703
x-mozilla-html:FALSE
url:www.geocities.com
org:GeoCities
adr:;;4499 Glencoe Avenue;Marina del Rey;CA;90292;
version:2.1
email;internet:bzajac@geostaff.com
title:IT Analyst
x-mozilla-cpt:;-3232
fn:Dr. Blair Zajac
end:vcard

--------------5C589D785E1842675CD2452F--