You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Martin Sperl <ap...@martin.sperl.org> on 2005/01/05 16:18:30 UTC

mod_rewrite and forced mime-type

Hi!

For a project of ours we wanted to use mod_rewrite/apache to deliver a 
huge number of file directly but with different mime-type than the one 
the mime-module provides.
This sounds simple to do with mod_rewrite:
RewriteRule ^/path/to/file/(.*)$ /fs/path/to/file/$1 [T=text/plain,L]

But we are required (on some occasions) to deliver a file with a 
different mime-type dynamically (sometimes even for one and the same 
file!) - e.g:
RewriteRule ^/path/to/file/([a-zA-Z0-9-]+/[a-zA-Z0-9_-]+)/(.*)$ 
/fs/path/to/file/$2 [T=$1,L]

And this sadly does not work...
For sanity/security-reasons we are using this "feature" only for 
internal subrequests (Local Redirects), so that our FastCGIs are not 
blocked until the final delivery of huge files to users on slow links - 
the FastCGIs are just "dispatching" the request... We also investigated 
using php for delivery, as this would be executed in the apache-context 
not blocking external resources, but then features like "Partial" 
GET-Requests and out of the box "valid" caching-headers are not so easy...

I had a look at the mod_rewrite sourcecode and found that such 
substitutions are not implemented in mod rewrite.
Now I have got a small patch, that fix this, but I am not 100% sure if 
this does not open a memoryleak, as this is the first time, that I have 
been digging into the apache/apr code itself...

Any comments and and possibly an inclusion into the apache source tree 
would be appreciated,

Martin
---------------------------------------------------------------------------------------------------------------------------- 

P.s: Here the patch:
diff -ur httpd-2.0.52.orig/modules/mappers/mod_rewrite.c 
httpd-2.0.52/modules/mappers/mod_rewrite.c
--- httpd-2.0.52.orig/modules/mappers/mod_rewrite.c     2004-08-27 
21:23:26.000000000 +0200
+++ httpd-2.0.52/modules/mappers/mod_rewrite.c  2005-01-05 
14:39:36.020479280 +0100
@@ -2109,6 +2109,7 @@
    if (strcmp(output, "-") == 0) {
        do_expand_env(r, p->env, briRR, briRC);
        do_expand_cookie(r, p->cookie, briRR, briRC);
+        do_expand_mimetype(r, p, briRR, briRC);
        if (p->forced_mimetype != NULL) {
            if (perdir == NULL) {
                /* In the per-server context we can force the MIME-type
@@ -2164,6 +2165,12 @@
    do_expand_cookie(r, p->cookie, briRR, briRC);

    /*
+     *  Also set mimetype
+     *  (`RewriteRule .. .. [T=<string>]').
+     */
+    do_expand_mimetype(r,p,briRR, briRC);
+
+    /*
     *  Now replace API's knowledge of the current URI:
     *  Replace r->filename with the new URI string and split out
     *  an on-the-fly generated QUERY_STRING part into r->args
@@ -2608,6 +2615,20 @@
    }
}

+static void do_expand_mimetype( request_rec *r, rewriterule_entry *p,
+                              backrefinfo *briRR, backrefinfo *briRC)
+{
+    int i;
+    char buf[MAX_STRING_LEN];
+
+    /* do extended substitution only if required */
+    if (p->forced_mimetype) {
+
+      do_expand(r, p->forced_mimetype, buf, sizeof(buf), briRR, briRC);
+      p->forced_mimetype=apr_pstrdup(r->pool, buf);
+    }
+}
+

/*
**
diff -ur httpd-2.0.52.orig/modules/mappers/mod_rewrite.h 
httpd-2.0.52/modules/mappers/mod_rewrite.h
--- httpd-2.0.52.orig/modules/mappers/mod_rewrite.h     2004-08-20 
23:17:41.000000000 +0200
+++ httpd-2.0.52/modules/mappers/mod_rewrite.h  2005-01-05 
14:31:46.433867296 +0100
@@ -364,6 +364,8 @@
                         backrefinfo *briRR, backrefinfo *briRC);
static void do_expand_cookie(request_rec *r, char *cookie[],
                         backrefinfo *briRR, backrefinfo *briRC);
+static void do_expand_mimetype( request_rec *r, rewriterule_entry *p,
+                               backrefinfo *briRR, backrefinfo *briRC);

    /* URI transformation function */
static void  splitout_queryargs(request_rec *r, int qsappend);



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: mod_rewrite and forced mime-type

Posted by André Malo <nd...@perlig.de>.
* Martin Sperl wrote:

> RewriteRule ^/path/to/file/([a-zA-Z0-9-]+/[a-zA-Z0-9_-]+)/(.*)$
> /fs/path/to/file/$2 [T=$1,L]
>
> And this sadly does not work...

> Any comments and and possibly an inclusion into the apache source tree
> would be appreciated,

The patch looks ok.
However, the problem was already solved in the 2.1 development tree, while 
mod_rewrite was more or less completely cleaned up. I think backporting is 
not worth the effort, because dynamic mime typing is a really rarely used 
feature and I'd guess you're able to maintain the patch between the 
versions ;-)

nd
-- 
"Solides und umfangreiches Buch"
                                          -- aus einer Rezension

<http://pub.perlig.de/books.html#apache2>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org