Re: is ap_hook_log_transaction the wright place where to write my stats code ?

2014-12-07 Thread nik600
Thanka for your reply!

Is not so simple...the custom logic is a mixture of regex, memcached get,
json, cookies and http headers so i had to write a custom module.

Btw, thanks for your help!
Il 07/dic/2014 10:25 "Sorin Manolache"  ha scritto:

> On 2014-12-07 01:36, nik600 wrote:
>
>> Dear all
>>
>> i've written a custom module to handle the cache of my CMS system.
>>
>> Basically this module works in ap_hook_translate_name and decides
>> (following some custom logic) if the request can be served or not from the
>> cache.
>>
>> If yes, the r->uri is changed to be served locally from my cache dir
>> if no, the r->filename is changed to be server from
>> a proxy:balancer://cluster config
>>
>> In the ap_hook_log_transaction i'd like to compute the time of content
>> generation using:
>>
>>  float request_duration_sec=(float)(apr_time_now() -
>>>
>> r->request_time)/100;
>>
>> Is this approach correct?
>>
>> Is there any other hook more appropriate to do that?
>>
>> Thanks all in advance.
>>
>
> Hello,
>
> I think your approach is correct. However, have a look at the LogFormat
> directive. The %D directive might do exactly what you want and implemented
> in log_transaction.
>
> Have a look at the RewriteCond and RewriteRule directives. The selective
> proxying could be implemented by these directives without writing any code.
>
> E.g.:
>
> 
> RewriteEngine On
> RewriteCond ...
> RewriteCond ...
> RewriteRule .*  http://proxy/path?args keepalive=on [P]
> 
>
> http://proxy/path>
> ...
> 
>
> I have not tried it with balancers but I think it works and the
> configuration is similar (balancer:// instead of http://).
>
> Sorin
>
>
>> Bye
>>
>>
>


is ap_hook_log_transaction the wright place where to write my stats code ?

2014-12-06 Thread nik600
Dear all

i've written a custom module to handle the cache of my CMS system.

Basically this module works in ap_hook_translate_name and decides
(following some custom logic) if the request can be served or not from the
cache.

If yes, the r->uri is changed to be served locally from my cache dir
if no, the r->filename is changed to be server from
a proxy:balancer://cluster config

In the ap_hook_log_transaction i'd like to compute the time of content
generation using:

>float request_duration_sec=(float)(apr_time_now() -
r->request_time)/100;

Is this approach correct?

Is there any other hook more appropriate to do that?

Thanks all in advance.

Bye

-- 
/*/
nik600
http://www.kumbe.it


Re: problem with shared memory and directives for httpd

2014-12-04 Thread nik600
yes, now it works, thanks again!

2014-12-04 23:49 GMT+01:00 nik600 :

> Thanks a lot!
>
> i've understand the logic... i'll try and let you know!
>
> 2014-12-04 23:43 GMT+01:00 Yann Ylavic :
>
>> On Thu, Dec 4, 2014 at 10:52 PM, Yann Ylavic 
>> wrote:
>> > Please see attached modifications (where only a server_config is used).
>>
>> Note that I did not even test compile the chages, just a POC...
>>
>
>
>
> --
> /*/
> nik600
> http://www.kumbe.it
>



-- 
/*/
nik600
http://www.kumbe.it


Re: problem with shared memory and directives for httpd

2014-12-04 Thread nik600
Thanks a lot!

i've understand the logic... i'll try and let you know!

2014-12-04 23:43 GMT+01:00 Yann Ylavic :

> On Thu, Dec 4, 2014 at 10:52 PM, Yann Ylavic  wrote:
> > Please see attached modifications (where only a server_config is used).
>
> Note that I did not even test compile the chages, just a POC...
>



-- 
/*/
nik600
http://www.kumbe.it


Re: problem with shared memory and directives for httpd

2014-12-04 Thread nik600
because i've tried with both or with only one.

IE, if i use:

module AP_MODULE_DECLARE_DATA kcache_module = {
STANDARD20_MODULE_STUFF,NULL , NULL, create_server_conf, merge_conf,
kcache_directives, register_hooks };

i have the same behaviour


2014-12-04 17:32 GMT+01:00 Eric Johanson :

> Okay but why are you managing the kcache_config structure with both the
> per-directory AND the server-level functions?  You should decide if your
> kcache_config is server-wide or if it is per-directory, and use only the
> appropriate functions.
> -Eric
>
>
> From: nik600 [mailto:nik...@gmail.com]
> Sent: Thursday, December 04, 2014 11:15 AM
> To: modules-dev@httpd.apache.org
> Subject: Re: problem with shared memory and directives for httpd
>
> Hi
>
> thanks for your reply.
>
> i've tried also with a merging function but i have the same behaviour.
>
> Attached the same example with also merge function.
>
> Maybe i'm doing something wrong in my merge function?
>
>
> 2014-12-04 16:33 GMT+01:00 Eric Johanson  er...@valmarc.com>>:
> You have no merge function defined for your server-level config structure
> (kcache_config).  I would definitely try implementing that function.
> Otherwise, when apache goes to combine two levels of the config hierarchy
> together, the resulting combination is likely to just be an empty version
> of kcache_config, which sounds like it could be causing your problem.
>
> -Eric
>
>
> From: nik600 [mailto:nik...@gmail.com<mailto:nik...@gmail.com>]
> Sent: Thursday, December 04, 2014 10:05 AM
> To: modules-dev@httpd.apache.org<mailto:modules-dev@httpd.apache.org>
> Subject: problem with shared memory and directives for httpd
>
> Dear all
>
> i'm experiencing a problem with shared memory and i'm not able to figure
> it out.
>
> i've got a segment of shared memory in my module config and seen that if
> set some settings for the module in my configuration this memory isn't
> available in the request process.
>
> i've also attached an example (very simplified and without any mutex, just
> to show the case).
>
> i've noticed that if i have in my server configuration:
>
>  
>   KcacheEnabled On
> 
>
> The memory segment is not availabe:
>
> >>[Thu Dec 04 15:26:15 2014] [crit] [client 127.0.0.1]
> kcache_return_result invalid  config->s
>
> But if i comment this directive:
>
>  
> #  KcacheEnabled On
> 
>
> The memory segment is available and gets updated:
>
> >>[Thu Dec 04 15:24:47 2014] [debug] src/mod_kcache.c(96): [client
> 127.0.0.1] config->s->counter=68
> >>[Thu Dec 04 15:24:48 2014] [debug] src/mod_kcache.c(96): [client
> 127.0.0.1] config->s->counter=69
> >>[Thu Dec 04 15:24:48 2014] [debug] src/mod_kcache.c(96): [client
> 127.0.0.1] config->s->counter=70
> ...
>
> i've attached the full example (100 lines of code)
>
> I'll appreciate any help, thank all in advance
>
> --
> /*/
> nik600
> http://www.kumbe.it
>
>
>
> --
> /*/
> nik600
> http://www.kumbe.it
>



-- 
/*/
nik600
http://www.kumbe.it


Re: problem with shared memory and directives for httpd

2014-12-04 Thread nik600
Hi

thanks for your reply.

i've tried also with a merging function but i have the same behaviour.

Attached the same example with also merge function.

Maybe i'm doing something wrong in my merge function?


2014-12-04 16:33 GMT+01:00 Eric Johanson :

> You have no merge function defined for your server-level config structure
> (kcache_config).  I would definitely try implementing that function.
> Otherwise, when apache goes to combine two levels of the config hierarchy
> together, the resulting combination is likely to just be an empty version
> of kcache_config, which sounds like it could be causing your problem.
>
> -Eric
>
>
> From: nik600 [mailto:nik...@gmail.com]
> Sent: Thursday, December 04, 2014 10:05 AM
> To: modules-dev@httpd.apache.org
> Subject: problem with shared memory and directives for httpd
>
> Dear all
>
> i'm experiencing a problem with shared memory and i'm not able to figure
> it out.
>
> i've got a segment of shared memory in my module config and seen that if
> set some settings for the module in my configuration this memory isn't
> available in the request process.
>
> i've also attached an example (very simplified and without any mutex, just
> to show the case).
>
> i've noticed that if i have in my server configuration:
>
>  
>   KcacheEnabled On
> 
>
> The memory segment is not availabe:
>
> >>[Thu Dec 04 15:26:15 2014] [crit] [client 127.0.0.1]
> kcache_return_result invalid  config->s
>
> But if i comment this directive:
>
>  
> #  KcacheEnabled On
> 
>
> The memory segment is available and gets updated:
>
> >>[Thu Dec 04 15:24:47 2014] [debug] src/mod_kcache.c(96): [client
> 127.0.0.1] config->s->counter=68
> >>[Thu Dec 04 15:24:48 2014] [debug] src/mod_kcache.c(96): [client
> 127.0.0.1] config->s->counter=69
> >>[Thu Dec 04 15:24:48 2014] [debug] src/mod_kcache.c(96): [client
> 127.0.0.1] config->s->counter=70
> ...
>
> i've attached the full example (100 lines of code)
>
> I'll appreciate any help, thank all in advance
>
> --
> /*/
> nik600
> http://www.kumbe.it
>



-- 
/*/
nik600
http://www.kumbe.it
#include 
#include 
#include 
#include 
#include 
#include 

typedef struct {
	apr_size_t counter; /* my counter */
} kcache_config_stat;

typedef struct {
	int enabled;
	apr_shm_t *counters_shm; /* the APR shared segment object*/
	kcache_config_stat *s;/*my stats*/
} kcache_config;

void* merge_conf(apr_pool_t* pool, void* BASE, void* ADD);
void *create_dir_conf(apr_pool_t *pool, char *context);
void *create_server_conf(apr_pool_t *p, server_rec *s);
static void register_hooks(apr_pool_t *pool);

const char *kcache_set_enabled(cmd_parms *cmd, void *cfg, const char *arg);

static const command_rec kcache_directives[] = {
		AP_INIT_TAKE1("KcacheEnabled",
		kcache_set_enabled, NULL, RSRC_CONF,
		"KcacheEnabled must have Off or On value"), { NULL } };

module AP_MODULE_DECLARE_DATA kcache_module = { STANDARD20_MODULE_STUFF, create_dir_conf,
		merge_conf, create_server_conf, merge_conf, kcache_directives, register_hooks };

void* merge_conf(apr_pool_t* pool, void* BASE, void* ADD) {
	kcache_config* base = (kcache_config *) BASE ; /* This is what was set in the parent context */
	kcache_config* add = (kcache_config *) ADD ;   /* This is what is set in the new context */
	kcache_config* conf = (kcache_config *) create_server_conf(pool, NULL); /* This will be the merged configuration */

/* Merge configurations */
conf->enabled = ( add->enabled == 0 ) ? base->enabled : add->enabled ;
conf->s=base->s;
conf->counters_shm=base->counters_shm;

return conf ;
}
void *create_dir_conf(apr_pool_t *pool, char *context) {
	kcache_config *cfg = apr_pcalloc(pool, sizeof(kcache_config));

	return cfg;
}
/*
 * creating the server cfg
 */
void *create_server_conf(apr_pool_t *pool, server_rec *s) {
	kcache_config *cfg = apr_pcalloc(pool, sizeof(kcache_config));

	if(s!=NULL){
		ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,"kcache:create_server_conf creo la configurazione");
	}

	return cfg;
}
;
const char *kcache_set_enabled(cmd_parms *cmd, void *cfg, const char *arg) {
	/*~~~*/
	kcache_config *conf = (kcache_config *) ap_get_module_config(
			cmd->server->module_config, &kcache_module);
	/*~~~*/

	if (conf) {
		if (!strcasecmp(arg, "on"))
			conf->enabled = 1;
		else
			conf->enabled = 0;

	}
	return NULL;
}

/*
 * init child
 */
static void kcache_child_init(apr_pool_t *p, server_rec *s) {

	ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,"kcache:kcache_child_init imposto l'indirizzo della shared m

problem with shared memory and directives for httpd

2014-12-04 Thread nik600
Dear all

i'm experiencing a problem with shared memory and i'm not able to figure it
out.

i've got a segment of shared memory in my module config and seen that if
set some settings for the module in my configuration this memory isn't
available in the request process.

i've also attached an example (very simplified and without any mutex, just
to show the case).

i've noticed that if i have in my server configuration:

 
  KcacheEnabled On


The memory segment is not availabe:

>>[Thu Dec 04 15:26:15 2014] [crit] [client 127.0.0.1] kcache_return_result
invalid  config->s

But if i comment this directive:

 
#  KcacheEnabled On


The memory segment is available and gets updated:

>>[Thu Dec 04 15:24:47 2014] [debug] src/mod_kcache.c(96): [client
127.0.0.1] config->s->counter=68
>>[Thu Dec 04 15:24:48 2014] [debug] src/mod_kcache.c(96): [client
127.0.0.1] config->s->counter=69
>>[Thu Dec 04 15:24:48 2014] [debug] src/mod_kcache.c(96): [client
127.0.0.1] config->s->counter=70
...

i've attached the full example (100 lines of code)

I'll appreciate any help, thank all in advance

-- 
/*/
nik600
http://www.kumbe.it
#include 
#include 
#include 
#include 
#include 
#include 

typedef struct {
	apr_size_t counter; /* my counter */
} kcache_config_stat;

typedef struct {
	int enabled;
	apr_shm_t *counters_shm; /* the APR shared segment object*/
	kcache_config_stat *s;/*my stats*/
} kcache_config;

void *create_server_conf(apr_pool_t *p, server_rec *s);
static void register_hooks(apr_pool_t *pool);

const char *kcache_set_enabled(cmd_parms *cmd, void *cfg, const char *arg);

static const command_rec kcache_directives[] = {
		AP_INIT_TAKE1("KcacheEnabled",
		kcache_set_enabled, NULL, RSRC_CONF,
		"KcacheEnabled must have Off or On value"), { NULL } };

module AP_MODULE_DECLARE_DATA kcache_module = { STANDARD20_MODULE_STUFF, NULL,
		NULL, create_server_conf, NULL, kcache_directives, register_hooks };

/*
 * creating the server cfg
 */
void *create_server_conf(apr_pool_t *pool, server_rec *s) {
	kcache_config *cfg = apr_pcalloc(pool, sizeof(kcache_config));

	ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,"kcache:create_server_conf creo la configurazione");

	return cfg;
}
;
const char *kcache_set_enabled(cmd_parms *cmd, void *cfg, const char *arg) {
	/*~~~*/
	kcache_config *conf = (kcache_config *) ap_get_module_config(
			cmd->server->module_config, &kcache_module);
	/*~~~*/

	if (conf) {
		if (!strcasecmp(arg, "on"))
			conf->enabled = 1;
		else
			conf->enabled = 0;

	}
	return NULL;
}

/*
 * init child
 */
static void kcache_child_init(apr_pool_t *p, server_rec *s) {

	ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,"kcache:kcache_child_init imposto l'indirizzo della shared memory");

	kcache_config *scfg =
			ap_get_module_config(s->module_config, &kcache_module);

	scfg->s = apr_shm_baseaddr_get(scfg->counters_shm);

}
/*
 * create the shm
 */
static int kcache_post_config(apr_pool_t *pconf, apr_pool_t *plog,
		apr_pool_t *ptemp, server_rec *s) {
	ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,"kcache:kcache_post_config creo la shared memory");

	kcache_config *scfg = ap_get_module_config(s->module_config, &kcache_module);

	apr_shm_create(&scfg->counters_shm, sizeof(*scfg->s), NULL, pconf);

	scfg->s = apr_shm_baseaddr_get(scfg->counters_shm);
	scfg->s->counter = 0;

	return DECLINED;
}

static int kcache_handler_translate_files(request_rec* r) {

	/*~~*/
	kcache_config *config = (kcache_config *) ap_get_module_config(
			r->server->module_config, &kcache_module);
	/*~~*/

	if (!config->s) {
		ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r,
"kcache_return_result invalid  config->s");
		return DECLINED;
	}

	config->s->counter++;
	ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
"config->s->counter=%"APR_SIZE_T_FMT,config->s->counter);

	return DECLINED;

}

static void register_hooks(apr_pool_t* pool) {

	static const char *succ[] = { "mod_proxy.c", "mod_alias.c",
			"mod_userdir.c", NULL };
	ap_hook_child_init(kcache_child_init, NULL, NULL, APR_HOOK_REALLY_FIRST);
	ap_hook_post_config(kcache_post_config, NULL, NULL, APR_HOOK_REALLY_FIRST);
	ap_hook_translate_name(kcache_handler_translate_files, NULL, succ,
			APR_HOOK_REALLY_FIRST);
}


Re: strange behaviour using mod_proxy when the r->filename partially overlaps the requested uri

2012-08-21 Thread nik600
2012/8/21 Sorin Manolache :
> On 2012-08-21 13:39, nik600 wrote:
>
> Replace with
>
> apr_pcalloc(r->pool, strlen(newurl) + 1);
>
> in order to allocate space for the closing '\0'.
>
>

Oh, i'm stupid...thanks

-- 
/*/
nik600
http://www.kumbe.it


strange behaviour using mod_proxy when the r->filename partially overlaps the requested uri

2012-08-21 Thread nik600
Dear all

i've got a strange behaviour in my custom module:

it seems that if the set filename overlaps the requested uri ths final
uri passed to mod_proxy is wrong, or has some added strange
characters.

some information:

* the module is activated only when the requested uri contains the
string "/files/anteprima"
* the module just redirect the request to mod_proxy changing the
request from "/files/anteprima" to "nocachedfiles/anteprima/"
* i know all is hardcoded here but is just to reproduce the problem

My module is configured in a vhost and related to a specific Location:


SetHandler kcache


if i make a request to this module, for example:

http://kcache.foo.com:19007/data/_www.foo.com/files/anteprima/whatyouwant

Looking at the code it should be redirected to:

proxy:http://www.f.com/nocachedfiles/anteprima/12345

but looking at apache logs i see:

[Tue Aug 21 13:27:01 2012] [debug] mod_proxy_http.c(56): proxy: HTTP:
canonicalising URL //www.f.com/nocachedfiles/anteprima/12345
dJ\x98\x01\x7f
[Tue Aug 21 13:27:01 2012] [debug] proxy_util.c(1515): [client
127.0.0.1] proxy: *: found forward proxy worker for
http://www.f.com/nocachedfiles/anteprima/12345 dJ\x98\x01\x7f
[Tue Aug 21 13:27:01 2012] [debug] mod_proxy_http.c(1973): proxy:
HTTP: serving URL http://www.f.com/nocachedfiles/anteprima/12345
dJ\x98\x01\x7f
[Tue Aug 21 13:27:01 2012] [debug] proxy_util.c(2067): proxy:
connecting http://www.f.com/nocachedfiles/anteprima/12345
dJ\x98\x01\x7f to www.f.com:80

You can see some "garbage" at the end of the string.

I've tried to investigate a lot on in, but i'm not able to figure it
out... notice that if you change the length of the newurl string, or
if you change it from:

"proxy:http://www.f.com/nocachedfiles/anteprima/12345";; 
to
"proxy:http://www.f.com/nocachedfiles2/anteprima/12345";;

The module tries correctly to load
http://www.f.com/nocachedfiles2/anteprima/12345 withoud any
garbage at the end.

It seems to be a combination of overlaps of the requested uri, the
destination file and their length... here there is the exact code to
reproduce it:

One last thing :
is not yet clear to me if is correct to put this kind of redirection
in  ap_hook_translate_name or if i have to use another type of hook,
by the way if you use a ap_hook_handler in the same conditions you get
an http contatenated at the end of your filename:

[Tue Aug 21 13:37:02 2012] [debug] proxy_util.c(1515): [client
127.0.0.1] proxy: *: found forward proxy worker for
http://www.f.com/nocachedfiles/anteprima/12345http

Any idea?

Thanks to all in advance

//
#include 
#include 
#include 

static void   register_hooks(apr_pool_t *pool);

module AP_MODULE_DECLARE_DATA kcache_module = {
STANDARD20_MODULE_STUFF,
NULL,
NULL,
NULL,
NULL,
NULL,
register_hooks
};

static int kcache_handler_translate(request_rec* r)
{
if (r->method_number != M_GET && r->method_number != M_POST){
return HTTP_METHOD_NOT_ALLOWED;
}

if(
strstr(r->unparsed_uri, "/files/anteprima")==NULL){
return DECLINED;
}   

char *newurl = 
"proxy:http://www.f.com/nocachedfiles/anteprima/12345";;  

r->filename = apr_pcalloc(r->pool,strlen(newurl));
strcpy(r->filename,newurl);
r->proxyreq = PROXYREQ_PROXY;
r->handler  = "proxy-server";

return DECLINED;

}

static void register_hooks(apr_pool_t* pool)
{


static const char *succ[] =
{"mod_proxy.c","mod_alias.c","mod_userdir.c", NULL};
ap_hook_translate_name(kcache_handler_translate, NULL, succ,
APR_HOOK_MIDDLE);
}

/***/


-- 
/*/
nik600
http://www.kumbe.it


Re: best way to return the content of a file

2012-08-19 Thread nik600
On Sat, Aug 18, 2012 at 7:18 PM, nik600  wrote:
> On Sat, Aug 18, 2012 at 6:32 PM, Eric Covener  wrote:
>>> i'm enabling my module with a Location directive:
>>>
>>> 
>>> SetHandler kcache
>>> 
>>>
>>> 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_reckcache_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
> 
>KcacheEnabled Off
> 
> and in a specific vhost:
> 
> KcacheEnabled On
> KcachePath /var/www/data
>  
>
> 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

2012-08-18 Thread nik600
On Sat, Aug 18, 2012 at 6:32 PM, Eric Covener  wrote:
>> i'm enabling my module with a Location directive:
>>
>> 
>> SetHandler kcache
>> 
>>
>> 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_reckcache_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

   KcacheEnabled Off

and in a specific vhost:

KcacheEnabled On
KcachePath /var/www/data
 

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

2012-08-18 Thread nik600
On Sat, Aug 18, 2012 at 3:27 PM, Nick Kew  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:


SetHandler kcache


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

2012-08-18 Thread nik600
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  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: forward request with proxy_http in custom module

2012-08-17 Thread nik600
Ok, it wasn't clear to me to return DECLINED to look for other
handlers, and also wasn't clear the possibility to specify an order.

Now it works, thanks.

On Fri, Aug 17, 2012 at 4:42 PM, Sorin Manolache  wrote:
> On 2012-08-17 16:25, nik600 wrote:
>>
>> Dear all
>>
>> i'm trying to code a custom module that will implement some logic this
>> is the concept of the module:
>>
>> *
>>
>> /*
>> * some stuff...
>> */
>> if(condition){
>>
>> /*return a custom result*/
>>
>> return OK;
>>
>> }else{
>> /*forward the request to another server*/
>>
>> r->filename = "proxy:http://www.google.it/";;
>> r->proxyreq = PROXYREQ_PROXY;
>> r->handler  = "proxy-server";
>>
>> return OK;
>> }
>> *
>>
>> But it seems that when i go into the else condition the proxy request
>> isn't handled.
>>
>> proxy and proxy_http are enabled and correctly working.
>>
>> Is this code correct to forward a request and make my module working
>> as a proxy_http ?
>
>
>
> Try
>
> if (condition) {
>  ...
>  return OK;
> } else {
>  return DECLINED;
> }
>
> and make sure your handler runs before mod_proxy's:
>
> static const char *succ[] = {"mod_proxy.c", NULL};
> ap_hook_handler(&your_handler, NULL, succ, APR_HOOK_MIDDLE);
>
> Then put a ProxyPass in your conf:
>
> 
>ProxyPass http://www.google.it/ keepalive=On
> 
>
> Also make sure you do not check on r->handler. Even if you set "SetHandler
> your_handler", ProxyPass will overwrite it with "proxy-server".
>
>
> Sorin
>
>>
>> Thanks in advance
>>
>



-- 
/*/
nik600
http://www.kumbe.it


forward request with proxy_http in custom module

2012-08-17 Thread nik600
Dear all

i'm trying to code a custom module that will implement some logic this
is the concept of the module:

*

/*
* some stuff...
*/
if(condition){

/*return a custom result*/

return OK;

}else{
/*forward the request to another server*/

r->filename = "proxy:http://www.google.it/";;
r->proxyreq = PROXYREQ_PROXY;
r->handler  = "proxy-server";   

return OK;
}
*

But it seems that when i go into the else condition the proxy request
isn't handled.

proxy and proxy_http are enabled and correctly working.

Is this code correct to forward a request and make my module working
as a proxy_http ?

Thanks in advance

-- 
/*/
nik600


Re: problem with ap_md5 in custom module

2012-08-16 Thread nik600
Great! this fix the problem

thanks for your help.

On Wed, Aug 15, 2012 at 10:47 PM, Ben Noordhuis  wrote:
> On Wed, Aug 15, 2012 at 5:13 PM, nik600  wrote:
>> Dear all
>>
>> i'm having a problem with ap_md5, i just want to write a custom module
>> that compute che md5 checksum of the requested url and give it back to
>> the user.
>>
>> This is my 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;
>>
>> char* kcache_md5;
>> kcache_md5 = (char *)ap_md5(r->pool,r->unparsed_uri);
>>
>> ap_set_content_type(r, "text/html;charset=ascii");
>> ap_rputs("", r);
>> ap_rputs("K-Hello World!", r);
>> ap_rprintf(r,"K-Hello
>> World!%s=%s", r->unparsed_uri,kcache_md5);
>> return OK;
>> }
>> *
>> *
>>
>> i've got a warning during compilation:
>>
>> src/mod_kcache.c:18:15: warning: cast to pointer from integer of
>> different size [-Wint-to-pointer-cast]
>>
>> Is quite strange to me that ap_md5 returns an int, as in the
>> documentation it is reported to return a char *
>>
>> http://ci.apache.org/projects/httpd/trunk/doxygen/group__APACHE__CORE__MD5.html
>>
>> By the way, if i try to run it i get a segfault, if i comment the line
>> that prints kcache_md5 with   ap_rprintf the module doesn't segfault.
>>
>> So, where i'm wrong?
>
> It seems you don't include util_md5.h so the compiler defaults the
> function prototype to `int ap_md5()`. Compile with -Wall -Wextra and
> you'll get warnings about things like that.



-- 
/*/
nik600
http://www.kumbe.it


problem with ap_md5 in custom module

2012-08-15 Thread nik600
Dear all

i'm having a problem with ap_md5, i just want to write a custom module
that compute che md5 checksum of the requested url and give it back to
the user.

This is my 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;

char* kcache_md5;
kcache_md5 = (char *)ap_md5(r->pool,r->unparsed_uri);

ap_set_content_type(r, "text/html;charset=ascii");
ap_rputs("", r);
ap_rputs("K-Hello World!", r);
ap_rprintf(r,"K-Hello
World!%s=%s", r->unparsed_uri,kcache_md5);
return OK;
}
*
*

i've got a warning during compilation:

src/mod_kcache.c:18:15: warning: cast to pointer from integer of
different size [-Wint-to-pointer-cast]

Is quite strange to me that ap_md5 returns an int, as in the
documentation it is reported to return a char *

http://ci.apache.org/projects/httpd/trunk/doxygen/group__APACHE__CORE__MD5.html

By the way, if i try to run it i get a segfault, if i comment the line
that prints kcache_md5 with   ap_rprintf the module doesn't segfault.

So, where i'm wrong?

Thanks to all in advance

-- 
/*/
nik600