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



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
@@ -334,6 +334,7 @@
hdr_error = -1,
hdr_end = 0,
hdr_unknown = 1,
+ hdr_content_disposition,
hdr_content_length,
hdr_content_range,
hdr_last_modified,
@@ -347,6 +348,7 @@
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" },
@@ -549,6 +551,30 @@
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
@@ -991,6 +1017,10 @@
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
_______________________________________________
freebsd-current@xxxxxxxxxxx mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-current
To unsubscribe, send any mail to "freebsd-current-unsubscribe@xxxxxxxxxxx"



Relevant Pages

  • Re: fetch extension - use local filename from content-disposition header
    ... suppose any security discussion is good. ... Any filename in the current directory can be wiped out ... >> if you fetch or wget and a URL redirects to another URL which leads to ... FreeBSD keeps the filename derived from the user-given URL, but wget ...
    (freebsd-current)
  • Re: fetch extension - use local filename from content-disposition header
    ... Any filename in the current directory can be wiped ... > out if you fetch or wget and a URL redirects to another URL which ... No. Fetch uses the original filename as specified on the command ...
    (freebsd-current)
  • CUPS installation, ghostscript patch?
    ... I'm installing CUPS and I get the question below. ... I honestly don't know what filename to write. ... => Attempting to fetch from http://www.interq.or.jp/mars/cherry/mac/. ... => MD5 Checksum OK for ghostscript/Font.tar.bz2. ...
    (freebsd-questions)
  • Re: fetch extension - use local filename from content-dispositionheader (new diff)
    ... Previously giving this non-argument -O flag would use the ... Now this flag takes an expected filename as an argument. ... if we make a mistake in the URL or if the server changes the mapping. ... +If the server sent a Content-Disposition header, the +.Fa content_disposition +field will contain the suggested local filename. ...
    (freebsd-current)
  • Re: Testing a file-type input element
    ... the filename selected is, or whether one was selected at all. ... may arise when the user inadvertently submits the form without selecting all ... I can test for an empty file form element name at the server side script, ... As far as client-side security goes: ...
    (comp.infosystems.www.authoring.html)