Re: fetch extension - use local filename from content-disposition header

From: Panagiotis Astithas <past_at_ebs.gr>
Date: Fri, 30 Dec 2005 12:29:47 +0200
Martin Cracauer wrote:
> I'm a bit rusty, so please point me to style mistakes in the appended
> diff. 
> 
> The following diff implements a "-O" option to fetch(1), which, when
> set, will make fetch use a local filename supplied by the server in a
> Content-Disposition header.
> 
> The most common case for this is when things are stored on a web
> server by users and there handled as "attachments".  The URL filename
> will say "http://foo.bar.com/attachment.php?attid=42" which is useless
> as a local filename.
> 
> However, popular web software like the vBulletion forum system and
> Bugzilla internally store the original filename (e.g. "mysystem.jpg"
> which it was when the uploader submitted it) and provide it to the
> client in a Content-Disposition header.
> 
> If you visit such an attachment in Mozilla, you will see that using
> the "save" function will default to the original filename.
> 
> This extension to fetch implements the same thing.
> 
> You can test it here:
> http://www.cons.org/tmp/content-disposition.cgi
> 
> Open in browser, say "save to disk", it will default to "foo.txt"
> instead of "content-disposition.cgi".  Same if you use the new fetch
> with -O.  Or test on any attachment on a modern version of vBulletin.
> 
> If you use Bugzilla somewhere, use this fetch to get an attachment
> with "-O" and you'll be thankful that it got the original filename,
> e.g. "reproduce-bug.query".
> 
> Martin
> 
> 

[...]

> Index: lib/libfetch/http.c
> ===================================================================
> RCS file: /home/CVS-FreeBSD/src/lib/libfetch/http.c,v
> retrieving revision 1.77
> diff -u -r1.77 http.c
> --- lib/libfetch/http.c	24 Aug 2005 12:28:05 -0000	1.77
> +++ lib/libfetch/http.c	30 Dec 2005 00:11:38 -0000
> _at__at_ -334,6 +334,7 _at__at_
>  	hdr_error = -1,
>  	hdr_end = 0,
>  	hdr_unknown = 1,
> +	hdr_content_disposition,
>  	hdr_content_length,
>  	hdr_content_range,
>  	hdr_last_modified,
> _at__at_ -347,6 +348,7 _at__at_
>  	hdr_t		 num;
>  	const char	*name;
>  } hdr_names[] = {
> +	{ hdr_content_disposition,	"Content-Disposition" },
>  	{ hdr_content_length,		"Content-Length" },
>  	{ hdr_content_range,		"Content-Range" },
>  	{ hdr_last_modified,		"Last-Modified" },
> _at__at_ -549,6 +551,30 _at__at_
>  	return (0);
>  }
>  
> +/*
> + * Parse a content-composition header

You probably meant content-disposition here.

> + */
> +static char *
> +_http_parse_content_disposition(const char *p)
> +{
> +	char *s, *s2;
> +	const char *looking_for = "filename=\"";
> +
> +	if ((s = strstr(p, looking_for))) {
> +		s = strdup(s + strlen(looking_for));
> +		if ((s2 = strchr(s, '"'))) {
> +			*s2 = '\0';
> +			return s;
> +		} else {
> +			free(s);
> +			return NULL;
> +		}
> +			
> +	} else {
> +		return NULL;
> +	}
> +}
> +
>  
>  /*****************************************************************************
>   * Helper functions for authorization
> _at__at_ -991,6 +1017,10 _at__at_
>  			case hdr_error:
>  				_http_seterr(HTTP_PROTOCOL_ERROR);
>  				goto ouch;
> +			case hdr_content_disposition:
> +				us->content_disposition = 
> +					_http_parse_content_disposition(p);
> +				break;
>  			case hdr_content_length:
>  				_http_parse_length(p, &clength);
>  				break;
> 
> 


FWIW, I find this very useful.

Cheers,

Panagiotis
Received on Fri Dec 30 2005 - 09:29:54 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:38:50 UTC