You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modules-dev@httpd.apache.org by Mark Harrison <mh...@pixar.com> on 2010/04/29 05:56:44 UTC

simple mapping module example?

I would like to do a simple mapper:

    - given some inputs, calculate a file path
    - let apache serve out that file for me

Is there a simple example of how to do this?  mod_rewrite
is pretty heavy-duty, and I'd like to get a simpler
model to follow.


Here's what I've got so far, am I on the right track?

Many TIA!
Mark

--------------------------------------------------------------------------------

static int my_mapper(request_rec *r)
{
     mypath = lookup(...); <-- e.g. "/usr/mydir/myfile.mp4"

     ap_set_content_type(r, "video/mp4");
     apr_table_add(r->headers_out, "Content-disposition",
                                   "inline; filename=acad.mp4");


     ap_???(r, mypath); <-- tell apache we want to serve this file
                            do this by setting something in r?

     return ???;      <-- tell apache to continue to the next step
                          and serve out the file
}


static void register_mymapper(apr_pool_t *p)
{
     ap_hook_translate_name(my_mapper,NULL,NULL,APR_HOOK_MIDDLE);
}

module AP_MODULE_DECLARE_DATA mymapper_module = {
     STANDARD20_MODULE_STUFF,
     NULL,
     NULL,
     NULL,
     NULL,
     NULL,
     register_mymapper
};


-- 
Mark Harrison
Pixar Animation Studios

Re: simple mapping module example?

Posted by Mark Harrison <mh...@pixar.com>.
On 4/29/10 12:54 AM, Nick Kew wrote:
>
> On 29 Apr 2010, at 04:56, Mark Harrison wrote:
>
>> I would like to do a simple mapper:
>>
>>    - given some inputs, calculate a file path
>>    - let apache serve out that file for me
>>
>> Is there a simple example of how to do this?  mod_rewrite
>> is pretty heavy-duty, and I'd like to get a simpler
>> model to follow.
>
> Maybe mod_rewrite will do all you need anyway!

It would, but I need to execute some internal logic to
fully resolve.

> The simplest example to look at in the apache code would be
> mod_alias.  Otherwise, the end of Chapter 6 of the modules
> book[1] leads you through developing a mapper module.

Ah, the miracle of ebooks!  Yes, Chapter 6 seems to be just
what I need.

So, I think that I would:

- register a hook handler
- in the hook handler:
   - set an X-header
   - calculate a new file path
   - set r->filename to my file path
   - call ap_internal_redirect(const char *new_uri, request_rec *r)

Is that the right track to be going down?  This file path will be
on an external (NFS) storage device and not under the apache directory,
will that matter?

Thanks!
Mark

Re: simple mapping module example?

Posted by Nick Kew <ni...@apache.org>.
On 29 Apr 2010, at 04:56, Mark Harrison wrote:

> I would like to do a simple mapper:
> 
>   - given some inputs, calculate a file path
>   - let apache serve out that file for me
> 
> Is there a simple example of how to do this?  mod_rewrite
> is pretty heavy-duty, and I'd like to get a simpler
> model to follow.

Maybe mod_rewrite will do all you need anyway!

The simplest example to look at in the apache code would be
mod_alias.  Otherwise, the end of Chapter 6 of the modules
book[1] leads you through developing a mapper module.

[1] http://www.apachetutor.org/

-- 
Nick Kew

Re: simple mapping module example?

Posted by Mark Harrison <mh...@pixar.com>.
On 4/29/10 12:09 AM, Jerome Renard wrote:
> I would have said mod_rewrite is a perfect example of what you can do.

mod_rewrite is a great piece of work, but I was hoping
for something a little bit more hello-world-ish.

My desired logic is:

     - based on the input, calculate a file path
     - ask apache to serve that file

The files being served are on NFS, and not part
of the apache htdocs tree, so I can't do a simple
url rewrite.


Thanks!
Mark

Re: simple mapping module example?

Posted by Jerome Renard <je...@gmail.com>.
Hello,

On Thu, Apr 29, 2010 at 5:56 AM, Mark Harrison <mh...@pixar.com> wrote:
> I would like to do a simple mapper:
>
>   - given some inputs, calculate a file path
>   - let apache serve out that file for me
>
> Is there a simple example of how to do this?  mod_rewrite
> is pretty heavy-duty, and I'd like to get a simpler
> model to follow.

I would have said mod_rewrite is a perfect example of what you can do.

If you look for something smaller, maybe you will find mod_user_dir
easier to read and understand :

- http://svn.apache.org/repos/asf/httpd/httpd/trunk/modules/mappers/mod_userdir.c

>
>
> Here's what I've got so far, am I on the right track?

Could you please post the entire source code so we can help you ?

>
> Many TIA!
> Mark
>
> --------------------------------------------------------------------------------
>
> static int my_mapper(request_rec *r)
> {
>    mypath = lookup(...); <-- e.g. "/usr/mydir/myfile.mp4"

One of http://www.temme.net/sander/api/httpd/http__request_8h.html maybe ?

>
>    ap_set_content_type(r, "video/mp4");
>    apr_table_add(r->headers_out, "Content-disposition",
>                                  "inline; filename=acad.mp4");
>
>
>    ap_???(r, mypath); <-- tell apache we want to serve this file
>                           do this by setting something in r?
>
>    return ???;      <-- tell apache to continue to the next step
>                         and serve out the file

most likely "return OK;" (or return DECLINED; on failure)

> }
>
>
> static void register_mymapper(apr_pool_t *p)
> {
>    ap_hook_translate_name(my_mapper,NULL,NULL,APR_HOOK_MIDDLE);
> }
>
> module AP_MODULE_DECLARE_DATA mymapper_module = {
>    STANDARD20_MODULE_STUFF,
>    NULL,
>    NULL,
>    NULL,
>    NULL,
>    NULL,
>    register_mymapper
> };
>
>
> --
> Mark Harrison
> Pixar Animation Studios
>