Re: Intercepting HTTP 301/302 redirects

2012-03-01 Thread Joe Lewis

On 02/29/2012 07:46 PM, Swaminathan Bhaskar wrote:

Thanks for the quick response Joe. Just to make sure, here is what I did:

IfModule mod_myfilter.c
Location /
SetOutputFilter myfilter
/Location
/IfModule

and the code

#include stdio.h
#include httpd.h
#include http_protocol.h
#include http_config.h
#include util_filter.h

#define MY_FILTER_NAME myfilter

static int my_output_filter(ap_filter_t *f, apr_bucket_brigade *bb)
{
fprintf(stderr, mod_myfilter: status = %d, status-line = %s\n, 
f-r-status, f-r-status_line);


ap_pass_brigade(f-next, bb);

return APR_SUCCESS;
}

static void my_filter_hooks(apr_pool_t *pool)
{
ap_register_output_filter(MY_FILTER_NAME, my_output_filter, NULL, 
AP_FTYPE_RESOURCE);


fprintf(stderr, mod_myfilter: registered my_output_filter\n);
}

module AP_MODULE_DECLARE_DATA myfilter_module = {
STANDARD20_MODULE_STUFF,
NULL,
NULL,
NULL,
NULL,
NULL,
my_filter_hooks
};

I setup an intentional redirect for testing

Redirect 301 /red.htm http://localhost/green.htm

When I try http://localhost/, should I not see the output from myfilter ?


Maybe.  Remember, errors don't go through the same outputs as regular 
responses.  If you want to filter the results of anything outside of the 
standard 2xx HTTP responses, you have to insert an error filter as well, 
hence my reference to ap_hook_insert_error_filter().  As an example 
(borrowed from some of my source and modified for yours) :


static void insert_my_output_error_filter(request_rec *r) {
  ap_add_output_filter(MY_FILTER_NAME,NULL,r,r-connection);
}

static void my_filter_hooks(apr_pool_t *p) {
  ap_hook_insert_error_filter(insert_my_output_error_filter, NULL, 
NULL, APR_HOOK_LAST);
  
ap_register_output_filter(MY_FILTER_NAME,my_output_filter,NULL,AP_FTYPE_RESOURCE);

};

Again, output filters and errors do not coincide.  If you want to catch 
both, you have to hook both.  (Same thing for r-headers_out and 
r-err_headers_out - r-headers_out won't make it into r-err_headers_out).


Joe
--
http://www.silverhawk.net


Re: Intercepting HTTP 301/302 redirects

2012-03-01 Thread Swaminathan Bhaskar

Ahh - Finally, I was able to get it working. Thanks for the pointer.

Here is the code snippet:

#include stdio.h
#include httpd.h
#include http_protocol.h
#include http_config.h
#include http_log.h
#include util_filter.h

#define MY_FILTER_NAME myfilter

static void insert_myfilter(request_rec *req)
{
ap_add_output_filter(MY_FILTER_NAME, NULL, req, req-connection);

ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, req-server, mod_myfilter:
inserted myfilter);
}

static int my_output_filter(ap_filter_t *f, apr_bucket_brigade *bb)
{
if (f-r-status_line != NULL) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, f-r-server,
mod_myfilter: status = %d, status-line = %s, f-r-status,
f-r-status_line);
}
else {
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, f-r-server,
mod_myfilter: status = %d, f-r-status);
}

ap_pass_brigade(f-next, bb);

return APR_SUCCESS;
}

static void my_filter_hooks(apr_pool_t *pool)
{
ap_register_output_filter(MY_FILTER_NAME, my_output_filter, NULL,
AP_FTYPE_RESOURCE);

ap_hook_insert_filter(insert_myfilter, NULL, NULL, APR_HOOK_LAST);

ap_hook_insert_error_filter(insert_myfilter, NULL, NULL, APR_HOOK_LAST);

fprintf(stderr, mod_myfilter: registered my_output_filter\n);
}

module AP_MODULE_DECLARE_DATA myfilter_module = {
STANDARD20_MODULE_STUFF,
NULL,
NULL,
NULL,
NULL,
NULL,
my_filter_hooks
};

Rgds
Bhaskar
-- 
View this message in context: 
http://old.nabble.com/Intercepting-HTTP-301-302-redirects-tp33418215p33422513.html
Sent from the Apache HTTP Server - Module Writers mailing list archive at 
Nabble.com.



Re: Intercepting HTTP 301/302 redirects

2012-03-01 Thread Joe Lewis

Congrats!  Welcome to the world of filters!


On 03/01/2012 09:37 AM, Swaminathan Bhaskar wrote:

Ahh - Finally, I was able to get it working. Thanks for the pointer.

Here is the code snippet:

#includestdio.h
#includehttpd.h
#includehttp_protocol.h
#includehttp_config.h
#includehttp_log.h
#includeutil_filter.h

#define MY_FILTER_NAME myfilter

static void insert_myfilter(request_rec *req)
{
 ap_add_output_filter(MY_FILTER_NAME, NULL, req, req-connection);

 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, req-server, mod_myfilter:
inserted myfilter);
}

static int my_output_filter(ap_filter_t *f, apr_bucket_brigade *bb)
{
 if (f-r-status_line != NULL) {
 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, f-r-server,
mod_myfilter: status = %d, status-line = %s, f-r-status,
f-r-status_line);
 }
 else {
 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, f-r-server,
mod_myfilter: status = %d, f-r-status);
 }

 ap_pass_brigade(f-next, bb);

 return APR_SUCCESS;
}

static void my_filter_hooks(apr_pool_t *pool)
{
 ap_register_output_filter(MY_FILTER_NAME, my_output_filter, NULL,
AP_FTYPE_RESOURCE);

 ap_hook_insert_filter(insert_myfilter, NULL, NULL, APR_HOOK_LAST);

 ap_hook_insert_error_filter(insert_myfilter, NULL, NULL, APR_HOOK_LAST);

 fprintf(stderr, mod_myfilter: registered my_output_filter\n);
}

module AP_MODULE_DECLARE_DATA myfilter_module = {
 STANDARD20_MODULE_STUFF,
 NULL,
 NULL,
 NULL,
 NULL,
 NULL,
 my_filter_hooks
};

Rgds
Bhaskar


Re: thread ID

2012-03-01 Thread Ben Noordhuis
On Thu, Mar 1, 2012 at 17:29,  sorin.manola...@orange.com wrote:
 Hello,

 I would need a memory buffer associated per worker thread (in the worker
 MPM) or to each process (in the prefork MPM).

 In order to do that, I would need a map thread-buffer. So, I would
 need a sort of thread ID/key/handle that stays the same during the
 lifetime of the thread and no two threads in the same process can have
 the same ID/key/handle.

 What is the most portable way to get this thread ID?

 I thought of r-connection-id. It works but it is not very portable as
 it is not guaranteed that two connections created by the same thread
 will have the same id. They do for now.

 If r-connection-sbh was not opaque it would be great, because
 sbh-thread_num would be exactly what I need.

 I could also use pthread_self. It works too but, in general, it is not
 guaranteed that the worker threads are pthreads.


 Thank you for your help.

 Sorin

What about apr_os_thread_current()? It returns a opaque value that's a
pthread_t on Unices and a pseudo-HANDLE on Windows. Read this[1] to
understand what that means.

As a recovering standards lawyer I should probably point out that
pthread_t is an opaque type that's not guaranteed to be convertible to
a numeric value (or to anything, really). That said, I've never seen a
pthreads implementation where that wasn't the case.

[1] 
http://msdn.microsoft.com/en-us/library/windows/desktop/ms683182%28v=vs.85%29.aspx


Re: thread ID

2012-03-01 Thread Sorin Manolache

On 03/02/12 00:21, Ben Noordhuis wrote:

On Thu, Mar 1, 2012 at 17:29,sorin.manola...@orange.com  wrote:

Hello,

I would need a memory buffer associated per worker thread (in the worker
MPM) or to each process (in the prefork MPM).

In order to do that, I would need a map thread-buffer. So, I would
need a sort of thread ID/key/handle that stays the same during the
lifetime of the thread and no two threads in the same process can have
the same ID/key/handle.

What is the most portable way to get this thread ID?

I thought of r-connection-id. It works but it is not very portable as
it is not guaranteed that two connections created by the same thread
will have the same id. They do for now.

If r-connection-sbh was not opaque it would be great, because
sbh-thread_num would be exactly what I need.

I could also use pthread_self. It works too but, in general, it is not
guaranteed that the worker threads are pthreads.


Thank you for your help.

Sorin


What about apr_os_thread_current()? It returns a opaque value that's a
pthread_t on Unices and a pseudo-HANDLE on Windows. Read this[1] to
understand what that means.

As a recovering standards lawyer I should probably point out that
pthread_t is an opaque type that's not guaranteed to be convertible to
a numeric value (or to anything, really). That said, I've never seen a
pthreads implementation where that wasn't the case.

[1] 
http://msdn.microsoft.com/en-us/library/windows/desktop/ms683182%28v=vs.85%29.aspx


Thank you, it's what I need.

Sorin




How to read PHP session in module

2012-03-01 Thread yokota
Hello,

I want to read PHP session in my private module.
PHP session id is in PHPSESSID cookie but
I want to read the contents of PHP session in my module.

Please let me know if you have any suggestions.
Thank you in advance.

Yokota Sakuko