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 nik600 <ni...@gmail.com> on 2012/08/18 10:51:59 UTC
best way to return the content of a file
Dear all
what is the best way to return the content of a file (the content
could be also binary).
I'm writing a custom module that sends some headers to the client and
then need to print out the content of a file.
Is better:
- to read the file and then send is using ap_rprintf
- to use apr_bucket_*
- to simply redirect the request changing the r->uri and then return DECLINED.
I was oriented to the 3rd option but isn't working.
This is my code:
***************************************************************************************************
/*
* some check
*/
if(condition){
r->uri = "/data/_01d7aea2d28036e9b1a6aac465d65787";
return DECLINED;
}
***************************************************************************************************
The strange thing is that i get a 404 error and in error.log i get:
[Sat Aug 18 10:49:10 2012] [error] [client 127.0.0.1] File does not
exist: /var/www/data/_01d7aea2d28036e9b1a6aac465d65787
but the path /var/www/data/_01d7aea2d28036e9b1a6aac465d65787 is valid
and the file exists!
Sould i do something else after changing the uri value?
Thanks to all in advance
--
/*************/
nik600
http://www.kumbe.it
Re: best way to return the content of a file
Posted by Joe Lewis <jl...@silverhawk.net>.
Yes. Every virtual host should be able to have a separate configuration,
hence different configs. The server_rec structure points to the virtual
host. There are ways to get the global settings from the global server_rec,
do some searches.on the list archives and you will find that if its what
you needed.
Joe
--
Http://www.silverhawk.net/
On Aug 19, 2012 3:49 AM, "nik600" <ni...@gmail.com> wrote:
> On Sat, Aug 18, 2012 at 7:18 PM, nik600 <ni...@gmail.com> wrote:
> > On Sat, Aug 18, 2012 at 6:32 PM, Eric Covener <co...@gmail.com> wrote:
> >>> i'm enabling my module with a Location directive:
> >>>
> >>> <Location /data>
> >>> SetHandler kcache
> >>> </Location>
> >>>
> >>> and i want to control the execution of the module with an initial
> check:
> >>>
> >>> if (!r->handler || strcmp(r->handler, "kcache")) return (DECLINED);
> >>>
> >>> but r->handler is null if the hook is called in ap_hook_translate_name
> >>> state, so how can i check this condition?
> >>
> >> You can't check that condition early. You should implement a
> >> directive to enable your module in these early phases.
> >
> > i've done that, and i've set the configuration per-directory, but it
> > seems that is have every time the same value, even if called from
> > different locations:
> >
> > static const command_rec kcache_directives[] =
> > {
> > AP_INIT_TAKE1("KcacheEnabled", kcache_set_enabled, NULL,
> > ACCESS_CONF, "KcacheEnabled must have Off or On value"),
> > AP_INIT_TAKE1("KcachePath", kcache_set_path, NULL, ACCESS_CONF,
> > "KcachePath must contain the path"),
> > { NULL }
> > };
> >
> >
> > static void register_hooks(apr_pool_t* pool)
> > {
> >
> > /*
> > * imposto i valori di default
> > */
> > config.enabled=FALSE;
> > config.path="/var/www";
> >
> > static const char *succ[] = {"mod_proxy.c","mod_alias.c", NULL};
> > ap_hook_translate_name(kcache_handler, NULL, succ, APR_HOOK_MIDDLE);
> > }
> > module AP_MODULE_DECLARE_DATA kcache_module = {
> > STANDARD20_MODULE_STUFF,
> > NULL,
> > NULL,
> > NULL,
> > NULL,
> > kcache_directives,
> > register_hooks
> > };
> >
> > then i have:
> > in the global config
> > <Directory />
> > KcacheEnabled Off
> > </Directory>
> > and in a specific vhost:
> > <Directory "/var/www/kcache/trunk/data">
> > KcacheEnabled On
> > KcachePath /var/www/data
> > </Directory>
> >
> > finally i've added in my handler:
> >
> > static int kcache_handler(request_rec* r)
> > {
> > apr_status_t s;
> >
> > ap_log_rerror(APLOG_MARK, APLOG_DEBUG,s,r,"KcacheEnabled =
> %i",config.enabled);
> > ap_log_rerror(APLOG_MARK, APLOG_DEBUG,s,r,"KcachePath =
> %s",config.path);
> > if(!config.enabled){
> > ap_log_rerror(APLOG_MARK, APLOG_NOTICE,s,r,"KcacheEnabled =
> Off,
> > exiting from kcache");
> > return DECLINED;
> >
> > }
> > ...
> > ...
> >
> > but looking at logs it seems that KcacheEnabled is always On.
> >
> > Is my approach correct?
> >
> > Thanks
> >
> >
> >
> >
> > --
> > /*************/
> > nik600
> > http://www.kumbe.it
>
> Ok, i was missing the create_dir_conf and merge_dir_conf handlers.
>
> One more thing (i hope the last :-) ) when the documentation talk
> about server configuration handler/merge it is intended by vhost?
>
> Thanks
> --
> /*************/
> nik600
> http://www.kumbe.it
>
Re: best way to return the content of a file
Posted by nik600 <ni...@gmail.com>.
On Sat, Aug 18, 2012 at 7:18 PM, nik600 <ni...@gmail.com> wrote:
> On Sat, Aug 18, 2012 at 6:32 PM, Eric Covener <co...@gmail.com> wrote:
>>> i'm enabling my module with a Location directive:
>>>
>>> <Location /data>
>>> SetHandler kcache
>>> </Location>
>>>
>>> and i want to control the execution of the module with an initial check:
>>>
>>> if (!r->handler || strcmp(r->handler, "kcache")) return (DECLINED);
>>>
>>> but r->handler is null if the hook is called in ap_hook_translate_name
>>> state, so how can i check this condition?
>>
>> You can't check that condition early. You should implement a
>> directive to enable your module in these early phases.
>
> i've done that, and i've set the configuration per-directory, but it
> seems that is have every time the same value, even if called from
> different locations:
>
> static const command_rec kcache_directives[] =
> {
> AP_INIT_TAKE1("KcacheEnabled", kcache_set_enabled, NULL,
> ACCESS_CONF, "KcacheEnabled must have Off or On value"),
> AP_INIT_TAKE1("KcachePath", kcache_set_path, NULL, ACCESS_CONF,
> "KcachePath must contain the path"),
> { NULL }
> };
>
>
> static void register_hooks(apr_pool_t* pool)
> {
>
> /*
> * imposto i valori di default
> */
> config.enabled=FALSE;
> config.path="/var/www";
>
> static const char *succ[] = {"mod_proxy.c","mod_alias.c", NULL};
> ap_hook_translate_name(kcache_handler, NULL, succ, APR_HOOK_MIDDLE);
> }
> module AP_MODULE_DECLARE_DATA kcache_module = {
> STANDARD20_MODULE_STUFF,
> NULL,
> NULL,
> NULL,
> NULL,
> kcache_directives,
> register_hooks
> };
>
> then i have:
> in the global config
> <Directory />
> KcacheEnabled Off
> </Directory>
> and in a specific vhost:
> <Directory "/var/www/kcache/trunk/data">
> KcacheEnabled On
> KcachePath /var/www/data
> </Directory>
>
> finally i've added in my handler:
>
> static int kcache_handler(request_rec* r)
> {
> apr_status_t s;
>
> ap_log_rerror(APLOG_MARK, APLOG_DEBUG,s,r,"KcacheEnabled = %i",config.enabled);
> ap_log_rerror(APLOG_MARK, APLOG_DEBUG,s,r,"KcachePath = %s",config.path);
> if(!config.enabled){
> ap_log_rerror(APLOG_MARK, APLOG_NOTICE,s,r,"KcacheEnabled = Off,
> exiting from kcache");
> return DECLINED;
>
> }
> ...
> ...
>
> but looking at logs it seems that KcacheEnabled is always On.
>
> Is my approach correct?
>
> Thanks
>
>
>
>
> --
> /*************/
> nik600
> http://www.kumbe.it
Ok, i was missing the create_dir_conf and merge_dir_conf handlers.
One more thing (i hope the last :-) ) when the documentation talk
about server configuration handler/merge it is intended by vhost?
Thanks
--
/*************/
nik600
http://www.kumbe.it
Re: best way to return the content of a file
Posted by nik600 <ni...@gmail.com>.
On Sat, Aug 18, 2012 at 6:32 PM, Eric Covener <co...@gmail.com> wrote:
>> i'm enabling my module with a Location directive:
>>
>> <Location /data>
>> SetHandler kcache
>> </Location>
>>
>> and i want to control the execution of the module with an initial check:
>>
>> if (!r->handler || strcmp(r->handler, "kcache")) return (DECLINED);
>>
>> but r->handler is null if the hook is called in ap_hook_translate_name
>> state, so how can i check this condition?
>
> You can't check that condition early. You should implement a
> directive to enable your module in these early phases.
i've done that, and i've set the configuration per-directory, but it
seems that is have every time the same value, even if called from
different locations:
static const command_rec kcache_directives[] =
{
AP_INIT_TAKE1("KcacheEnabled", kcache_set_enabled, NULL,
ACCESS_CONF, "KcacheEnabled must have Off or On value"),
AP_INIT_TAKE1("KcachePath", kcache_set_path, NULL, ACCESS_CONF,
"KcachePath must contain the path"),
{ NULL }
};
static void register_hooks(apr_pool_t* pool)
{
/*
* imposto i valori di default
*/
config.enabled=FALSE;
config.path="/var/www";
static const char *succ[] = {"mod_proxy.c","mod_alias.c", NULL};
ap_hook_translate_name(kcache_handler, NULL, succ, APR_HOOK_MIDDLE);
}
module AP_MODULE_DECLARE_DATA kcache_module = {
STANDARD20_MODULE_STUFF,
NULL,
NULL,
NULL,
NULL,
kcache_directives,
register_hooks
};
then i have:
in the global config
<Directory />
KcacheEnabled Off
</Directory>
and in a specific vhost:
<Directory "/var/www/kcache/trunk/data">
KcacheEnabled On
KcachePath /var/www/data
</Directory>
finally i've added in my handler:
static int kcache_handler(request_rec* r)
{
apr_status_t s;
ap_log_rerror(APLOG_MARK, APLOG_DEBUG,s,r,"KcacheEnabled = %i",config.enabled);
ap_log_rerror(APLOG_MARK, APLOG_DEBUG,s,r,"KcachePath = %s",config.path);
if(!config.enabled){
ap_log_rerror(APLOG_MARK, APLOG_NOTICE,s,r,"KcacheEnabled = Off,
exiting from kcache");
return DECLINED;
}
...
...
but looking at logs it seems that KcacheEnabled is always On.
Is my approach correct?
Thanks
--
/*************/
nik600
http://www.kumbe.it
Re: best way to return the content of a file
Posted by Eric Covener <co...@gmail.com>.
> i'm enabling my module with a Location directive:
>
> <Location /data>
> SetHandler kcache
> </Location>
>
> and i want to control the execution of the module with an initial check:
>
> if (!r->handler || strcmp(r->handler, "kcache")) return (DECLINED);
>
> but r->handler is null if the hook is called in ap_hook_translate_name
> state, so how can i check this condition?
You can't check that condition early. You should implement a
directive to enable your module in these early phases.
Re: best way to return the content of a file
Posted by nik600 <ni...@gmail.com>.
On Sat, Aug 18, 2012 at 3:27 PM, Nick Kew <ni...@apache.org> wrote:
>
> That has nothing to do with it!
>
> Several people have tried to help you, but you need to learn a bit before
> you're capable of being helped. If you aren't going to learn the basics of
> the request processing cycle (which can also be found in the docs at
> apache.org, where they were recently updated), stick to something
> simple like CGI or PHP.
>
> --
> Nick Kew
Thanks for your patience, i've read the page at
http://httpd.apache.org/docs/2.2/developer/
and
http://httpd.apache.org/docs/trunk/developer/modguide.html
now is more clear to me the meaning of hooks and priority and i've
been able to change the uri value and see correct effects.
BUT, now i've got one more question :-)
why if i call the handler from a translate_name hook the
ap_hook_translate_name(kcache_handler, NULL, succ, APR_HOOK_MIDDLE);
the r->handler value is NULL?
is because the handler is evaluated only when there is an ap_hook_handler?
i'm enabling my module with a Location directive:
<Location /data>
SetHandler kcache
</Location>
and i want to control the execution of the module with an initial check:
if (!r->handler || strcmp(r->handler, "kcache")) return (DECLINED);
but r->handler is null if the hook is called in ap_hook_translate_name
state, so how can i check this condition?
Thanks
--
/*************/
nik600
http://www.kumbe.it
Re: best way to return the content of a file
Posted by Nick Kew <ni...@apache.org>.
On 18 Aug 2012, at 13:17, nik600 wrote:
> Thanks for pointing me to the right direction.
>
> I've already tried to change order of execution i'v also tried to use
> APR_HOOK_REALLY_FIRST or APR_HOOK_FIRST or APR_HOOK_MIDDLE but the
> result is the same.
That has nothing to do with it!
Several people have tried to help you, but you need to learn a bit before
you're capable of being helped. If you aren't going to learn the basics of
the request processing cycle (which can also be found in the docs at
apache.org, where they were recently updated), stick to something
simple like CGI or PHP.
--
Nick Kew
Re: best way to return the content of a file
Posted by 松本 亮介 <ma...@gmail.com>.
Should hook phase be ap_hook_translatename or ap_hook_fixup ?
----
MATSUMOTO Ryosuke < matsu1229 at gmail.com >
http://blog.matsumoto-r.jp/
On 2012/08/18, at 21:17, nik600 <ni...@gmail.com> wrote:
> Thanks for pointing me to the right direction.
>
> I've already tried to change order of execution i'v also tried to use
> APR_HOOK_REALLY_FIRST or APR_HOOK_FIRST or APR_HOOK_MIDDLE but the
> result is the same.
>
> this is my test code:
>
> ******************************
> static int kcache_handler(request_rec* r)
> {
>
> if (!r->handler || strcmp(r->handler, "kcache"))
> return DECLINED;
>
> if (r->method_number != M_GET)
> return HTTP_METHOD_NOT_ALLOWED;
>
> apr_status_t s;
>
> r->uri = "/data/_01d7aea2d28036e9b1a6aac465d65787";
> r->unparsed_uri="/data/_01d7aea2d28036e9b1a6aac465d65787";
> r->filename="/var/www/data/_01d7aea2d28036e9b1a6aac465d65787";
>
> return DECLINED;
>
> }
>
> static void register_hooks(apr_pool_t* pool)
> {
>
> static const char *succ[] = {"mod_proxy.c","mod_alias.c", NULL};
> ap_hook_handler(kcache_handler, NULL, succ, APR_HOOK_REALLY_FIRST);
> }
> module AP_MODULE_DECLARE_DATA kcache_module = {
> STANDARD20_MODULE_STUFF,
> NULL,
> NULL,
> NULL,
> NULL,
> NULL,
> register_hooks
> };
> ******************************
>
> Any idea?
>
> Thanks
>
>
> On Sat, Aug 18, 2012 at 12:33 PM, Nick Kew <ni...@apache.org> wrote:
>>
>> On 18 Aug 2012, at 09:51, nik600 wrote:
>>
>>> Is better:
>>>
>>> - to read the file and then send is using ap_rprintf
>>
>> Bad.
>>
>>> - to use apr_bucket_*
>>
>> Acceptable.
>>
>>> - to simply redirect the request changing the r->uri and then return DECLINED.
>>
>> Yep.
>>
>>> The strange thing is that i get a 404 error and in error.log i get:
>>
>> That's because you set r-->uri too late, after it's already been mapped to the
>> filesystem.
>>
>> Others have already told you to hook in earlier in the request processing cycle.
>> I guess it's time to point you at http://www.apachetutor.org/
>> and the new programmer docs at httpd.apache.org.
>>
>> --
>> Nick Kew
>
>
>
> --
> /*************/
> nik600
> http://www.kumbe.it
Re: best way to return the content of a file
Posted by nik600 <ni...@gmail.com>.
Thanks for pointing me to the right direction.
I've already tried to change order of execution i'v also tried to use
APR_HOOK_REALLY_FIRST or APR_HOOK_FIRST or APR_HOOK_MIDDLE but the
result is the same.
this is my test code:
******************************
static int kcache_handler(request_rec* r)
{
if (!r->handler || strcmp(r->handler, "kcache"))
return DECLINED;
if (r->method_number != M_GET)
return HTTP_METHOD_NOT_ALLOWED;
apr_status_t s;
r->uri = "/data/_01d7aea2d28036e9b1a6aac465d65787";
r->unparsed_uri="/data/_01d7aea2d28036e9b1a6aac465d65787";
r->filename="/var/www/data/_01d7aea2d28036e9b1a6aac465d65787";
return DECLINED;
}
static void register_hooks(apr_pool_t* pool)
{
static const char *succ[] = {"mod_proxy.c","mod_alias.c", NULL};
ap_hook_handler(kcache_handler, NULL, succ, APR_HOOK_REALLY_FIRST);
}
module AP_MODULE_DECLARE_DATA kcache_module = {
STANDARD20_MODULE_STUFF,
NULL,
NULL,
NULL,
NULL,
NULL,
register_hooks
};
******************************
Any idea?
Thanks
On Sat, Aug 18, 2012 at 12:33 PM, Nick Kew <ni...@apache.org> wrote:
>
> On 18 Aug 2012, at 09:51, nik600 wrote:
>
>> Is better:
>>
>> - to read the file and then send is using ap_rprintf
>
> Bad.
>
>> - to use apr_bucket_*
>
> Acceptable.
>
>> - to simply redirect the request changing the r->uri and then return DECLINED.
>
> Yep.
>
>> The strange thing is that i get a 404 error and in error.log i get:
>
> That's because you set r-->uri too late, after it's already been mapped to the
> filesystem.
>
> Others have already told you to hook in earlier in the request processing cycle.
> I guess it's time to point you at http://www.apachetutor.org/
> and the new programmer docs at httpd.apache.org.
>
> --
> Nick Kew
--
/*************/
nik600
http://www.kumbe.it
Re: best way to return the content of a file
Posted by Nick Kew <ni...@apache.org>.
On 18 Aug 2012, at 09:51, nik600 wrote:
> Is better:
>
> - to read the file and then send is using ap_rprintf
Bad.
> - to use apr_bucket_*
Acceptable.
> - to simply redirect the request changing the r->uri and then return DECLINED.
Yep.
> The strange thing is that i get a 404 error and in error.log i get:
That's because you set r-->uri too late, after it's already been mapped to the
filesystem.
Others have already told you to hook in earlier in the request processing cycle.
I guess it's time to point you at http://www.apachetutor.org/
and the new programmer docs at httpd.apache.org.
--
Nick Kew