How to return an error code/page from output filter?
When I encounter certain error conditions in my output filter, I would like to return a specific error code/page (eg 503 - service not available). However, the following code results in a 500 beeing returned to the browser: apr_status_t my_out_filter(ap_filter_t *f, apr_bucket_brigade *bb) { if (I_should_return_503) { ap_remove_output_filter(f); // f-r-status = HTTP_SERVICE_UNAVAILABLE; return HTTP_SERVICE_UNAVAILABLE; } } In fact, no matter what error code I return, the browser always gets a 500 error. Also, if I uncomment the f-r-... line, the browser gets an empty (200) page. Any suggestions? Thanks in advance, John
Can you set the request timeout dynamically within a module?
Hi Everyone, We have an output filter. Some requests take a VERY long time for the filter to process (I know many of you have questions here. But the logic is in the page). We would like to know if there is a way for the module (our output filter) to tell apache to not terminate THIS request. We do not want to increase the default timeout. I looked at the server_rec struct, it has a timeout member (default = 300 secs, yes this is a very long time). Is this the one that controls the request timeout? If yes, I guess it controls every request. And is it a good idea for a request to change the server_rec struct? Even if it is OK to change it, will it take affect for the current request? Or I am looking at a wrong place/parameter. Thanks in advance! John
Re: Static compilation of a module
--- On Thu, 7/31/08, Brian J. France [EMAIL PROTECTED] wrote: At work we have moved away from using static libraries to shared libraries because of duplicate symbols issues. I have a smilar question: I am using apxs to compile my .so module on Linux. The module requires another lib. When I used -c -l TheLib, I got a huge static .so file. How do I generate a dynamic .so file? I tried -Wc,-static -Wl,-static, it gives a .a file instead.
Issue when saving a copy of the postdata in input filter
I have a module that saves a copy of the postdata in an input filter. Here is how it works: In the ap_hook_fixups function, I check to see if I am interested in this request (examine url). If yes, I then check if r-method_number == M_POST. If yes, then I add my input filter function to capture the postdata. All worked fine until someone did this: conf: Directory /test DirectoryIndex index.php index.php3 index.html index.htm /Directory index.htm: htmlbody form action= method=post input type=text name=test/ input type=submit/ /form /body/html Now, load the above page with http://localhost/test/ (NOTE, without index.htm), and then sumbit the form. By the time I decided if I should add my input filter for index.htm, the test r-method_number == M_POST is false (it is a M_GET). Some debugging showed that the referer header is not null in this case, so I modified the test from r-method_number == M_POST to r-method_number == M_POST || apr_table_get(r-headers_in, Referer) and in my input filter, I check to see if the 1st bucket is EOS before saving a copy of the postdata (because a non-null referer does not mean it is really a POST request. Is this the right way? I would like it to work on apaceh 2.* Thanks John
Re: enable override (allow htaccess use)
--- On Tue, 6/10/08, Andy Grant [EMAIL PROTECTED] wrote: From: Andy Grant [EMAIL PROTECTED] Subject: enable override (allow htaccess use) To: modules-dev@httpd.apache.org Date: Tuesday, June 10, 2008, 9:40 AM How do you enable/disable the use of your directives in htaccess files? I've read about context and overrides, but can't find them in reference to developing a module, just in reference to using them (i.e. AllowOverride FileInfo). Thanks. In your command table, when you specify your directive. The 4th argument of the macros (eg AP_INIT_TAKE12(...)) specifies where/how your directive can be placed. See http_config.h for the meanings. Here is one define from the .h file #define OR_LIMIT 1 /** *.conf inside Directory or Location and .htaccess when AllowOverride Limit */
Re: How do I know the character encoding?
--- On Sat, 5/3/08, Nick Kew [EMAIL PROTECTED] wrote: From: Nick Kew [EMAIL PROTECTED] Subject: Re: How do I know the character encoding? To: modules-dev@httpd.apache.org Date: Saturday, May 3, 2008, 5:17 AM In my output filter, I need to parse the document to search for certain patterns. Where can I get the information about the (character) encoding so that I can parse the document correctly? Eg the document may contain unicode characters and are encoded in a special encoding. See http://apache.webthing.com/mod_xml2enc/ If your filter uses libxml2, just use mod_xml2enc alongside it. If not, you can still use the charset detection and transcoding. Thanks Nick! I've never used the modules you mentioned. I will have a look at them. Thanks again! John
How do I know the character encoding?
In my output filter, I need to parse the document to search for certain patterns. Where can I get the information about the (character) encoding so that I can parse the document correctly? Eg the document may contain unicode characters and are encoded in a special encoding. Thanks, John
perform a redirect in output filter
I am using apache 2.0/2.2, in my output filter, I would like to have the ability to redirect. Some tests indicated that I can redirect to a page on the same site, but not to an external site. Here is my code for redirect: char* location = apr_table_get(r-headers_out, Location); //Note this Location is set by my filter during the process, so I know I need to do a redirect. if(location location[0]) { //r-status = 301; r-method = apr_pstrdup(r-pool, GET); r-method_number = M_GET; apr_table_unset(r-headers_in, Content-Length); ap_remove_output_filter(my_filter); ap_internal_redirect_handler(location, r); return APR_SUCCESS; } //tests: (if my site is http://localhost/foo) and when http://localhost/foo/bar.html (my test page)is loaded I want to redirect it to a) /foo/foo2/existing.html (works) b) http://localhost/foo/foo2/existing.html (works) c) http://www.google.com (does not work), instead I get http://localhost/foo displayed on the browser. Is there anything I did is incorrect? I noticed the method is called *internal_redirect*, but did not know another one for external redirect. Thanks, John
Configure question -- allow access only from local machine
We have a situation that we would like to restrict the access to certain folders only to requests from the local machine. Here is why: When a page is processed by our filter, the filter (based on page logic) may request pages (just like a regular web page request) that should never go to the browser. We put these pages in a folder. And would like to use apache config to restrict the access to only the local machine. Here is the config LocationMatch /secrete-stuff/ Order Deny,Allow Deny from all Allow from 127.0.0.1 #Allow from localhost /LocationMatch The issue we face: When our filter issues the request, we use the hostname from the original request. eg, original request http://1.2.3.4/index.html our filter might issue http://1.2.3.4/something/secrete-stuff/server.js In order to make the above directive work, we will have to put the ip (1.2.3.4) in the Allow section. However, we are planning to deply many servers, it would be very hard for us to edit each config file. So we are wondering if there are anyway we can achieve the same result without make ip-specific changes. Thanks in advanvce for your help. John
Re: Is there are any way to know if the request is regular (http) or SSL (https) in a module?
--- Graham Dumpleton [EMAIL PROTECTED] wrote: On 18/12/2007, Sander Temme [EMAIL PROTECTED] wrote: On Dec 17, 2007, at 6:36 PM, Eric Covener wrote: I would like to know the request type in my module (handler/filter), is there any way to know that (HTTP vs HTTPS)? apr_table_get(r-subprocess_env, HTTPS) might be what you want That gets set in the Fixup hook, relatively late in the request processing process. You could call ap_http_scheme(r) to run the request scheme hook, which will return https if SSL is disabled or optional on the vhost that accepted the request. Or better still, use the function registered by the ssl module for the purpose: #if AP_SERVER_MAJORVERSION_NUMBER = 2 APR_DECLARE_OPTIONAL_FN(int, ssl_is_https, (conn_rec *)); static APR_OPTIONAL_FN_TYPE(ssl_is_https) *wsgi_is_https = NULL; #endif ... if (!wsgi_is_https) wsgi_is_https = APR_RETRIEVE_OPTIONAL_FN(ssl_is_https); if (wsgi_is_https wsgi_is_https(r-connection)) apr_table_set(r-subprocess_env, HTTPS, 1); Ie., HTTPS is set by the above sort of process, but as pointed out that is done only in fixup phase, but you can call the ssl_is_https() function direct if in earlier phase. Thanks everyone! All very useful to me!
Re: Memory issue in apache modules
--- Joe Lewis [EMAIL PROTECTED] wrote: Your buckets can still be created using the request-pool . My created buckets in my output filter are done that way. Have you tried it? You still use the request-connection-bucket_alloc for the other parameter, but the request-pool for the memory pool to be used. Thanks Joe!
Is there are any way to know if the request is regular (http) or SSL (https) in a module?
I would like to know the request type in my module (handler/filter), is there any way to know that (HTTP vs HTTPS)? Thanks, John
Re: Memory issue in apache modules
--- Ray Morris [EMAIL PROTECTED] wrote: I am using the apr_* functions to allocate memory (most of the time from the request-pool). If there are few places where you allocate from othr than the reqquest pool, I'd look at those first. I used the bucket/brigade for my data that requires the use of request-connection-bucket_alloc. I read a very old post stating that when keep alive is on (which is true in my case), then the memory associated with the connection will not be freed? But no answer to that post. Is that a valid concern? If so , how can I reclaim the connection-related memory or use a different memory pool? Thanks, John
Re: Memory issue in apache modules
--- Eric Covener [EMAIL PROTECTED] wrote: It should level out as each thread in the process has had a chance to run and allocate some memory for a range of requests. MaxMemFree can be used to return the heap memory that would otherwise be used by subsequent requests on that thread. Thanks Eric, But I see consistent memory increase when I load the SAME page repeatedly. So something is wrong with my module. But I do not know what. Thanks again. John
Re: How does set status code in filter work
Thanks a lot, Joe! --- Joe Lewis [EMAIL PROTECTED] wrote: John Zhang wrote: Joe, Thanks for you rreply, but I do not quite understand Your filter should run before the content type is set. Could you elaberated on that? My filter is an output filter. The filter types are AP_FTYPE_ (followed by one of the following ) RESOURCE, CONTENT_SET, PROTOCOL, TRANSCODE, CONNECTION or NETWORK. The two most common ones are AP_FTYPE_RESOURCE, and AP_FTYPE_CONTENT_SET. The difference between the two is one (RESOURCE) runs prior to the Content-Type header being set. (Meaning they can change any of those headers). The other (CONTENT_SET) runs after that header is set, and are not supposed to change that header (I expect there are other headers, such as the status, that may also be effected here, but I do not know for sure). It is in your register_hook function that you declare when you want your filter to be run that those are specified. Also, if I issue a redirect in my (output) filter to (say /errors/HTTP_FORBIDDEN.html) and I am a filter for .html, will I be called in loading the error page? Error pages do not run in the standard filter chain by default. If you want your filter running on the error HTML files in addition to others, you would have to use an error_filter hook as well. Joe -- Joseph Lewis http://sharktooth.org/ Divide the fire, and you will sooner put it out. - Publius Syrus
Re: How does set status code in filter work
Joe, Thanks for you rreply, but I do not quite understand Your filter should run before the content type is set. Could you elaberated on that? My filter is an output filter. Also, if I issue a redirect in my (output) filter to (say /errors/HTTP_FORBIDDEN.html) and I am a filter for .html, will I be called in loading the error page? Thanks, John --- Joe Lewis [EMAIL PROTECTED] wrote: Your filter should run before the content type is set. Then, you can do the usual redirect to /errors/HTTP_FORBIDDEN.html or whatever document you should be returning. Yes, a filter should only be working on the through-put, so you should be handling that. Perhaps someone knows about the pre-content-set filter types - I'm definitely not the expert. Joe John Zhang wrote: In an output filter. If something is wrong, and I set the request object's status to an error code, then am I responsible for providing the proper error page content? What is the correct way to handle errors in an output filter? Thanks, John
Re: How to issue a subrequest in my module
--- Joe Lewis wrote: John Zhang wrote: I have a module (act as a filter or handler). While processing a request, I encountered a url (similar to an include, eg /foo/bar.xyz, or abc.txt). How can I 1) ask apache to process this request and 2) give me the result back (I am waiting for it before I can continue)? I did is available on the web : http://sharktooth.org/tutorials/whitepapers/apache_includes.html Joe, Thanks for the reply. I will look into your sample and see if I can do something similar. Thanks Again! John
How to issue a subrequest in my module
I have a module (act as a filter or handler). While processing a request, I encountered a url (similar to an include, eg /foo/bar.xyz, or abc.txt). How can I 1) ask apache to process this request and 2) give me the result back (I am waiting for it before I can continue)? I got an answer for a similar question, and have looked at the mod_include, but have not figured out how to do this: the mod_include has a include_ctx, and it does a lot of work using that (eg mapping the include uri to an absolute path). Thanks in advance! John
How to request a file in apache module?
I have the follow need: While processing a request in my module (handler or output filter), I need to ask apache to give me the content of another page. Is there any way to do that, or do I have to find the file and read it just within my module? Thanks in Advance for any help! John
Re: apache 2.0.61 do not read config settings
--- Joe Lewis wrote: John Zhang wrote: --- Joe Lewis wrote: Comment out the IfModule pieces surrounding your directive just to eliminate one more thing. Commenting out the IfModule does make it work! Now, how do I figure out the EXACT module name? When you compile your module using apxs, there could (or should) be a -n option. This is specifying the module name and is required in -g, but guessed in a -i mode. Just type man apxs for more information. Use that name when testing an IfModule. Thanks for the help. I never used apxs. My module was compiled using Visual studio. Through debugging the apache code here is what I found out: In apache 2.2x, I can say LoadModule my_module the location of my module/mod_my.so IfModule my_module ... /IfModule but in 2.0, I have to change the IfModule to IfModule mod_my.c With IfModule my_module, the entire block is skipped because it cannot find a model with name my_module. Not sure if this the right way. Any comments would be preciated. Thanks, John
Re: apache 2.0.61 do not read config settings
--- William A. Rowe, Jr. wrote: Is your cmd array listed in your module data section? yes. Everything worked fine in apache 2.2. The structure looked like this: static const command_rec mod_my_cmds[] = { AP_INIT_TAKEx(MyName, my_func, NULL, RSRC_CONF, my comments, ..., {NULL} }; module AP_MODULE_DECLARE_DATA my_module = { STANDARD20_MODULE_STUFF, ... mod_my_cmds, register_hooks }; my_func is declared and defined somewhere. But appears never called.
Re: apache 2.0.61 do not read config settings
--- Joe Lewis wrote: Comment out the IfModule pieces surrounding your directive just to eliminate one more thing. How do we know that the problem is the directive itself, when it could be that apxs compiled it with a different my_mod.c definition and the IfModule is what is really preventing your directive from being called? If that doesn't reveal anything additional, try providing us with the my_func function (and at least the declaration). Thanks Joe, Commenting out the IfModule does make it work! Now, how do I figure out the EXACT module name? I am still in the early stage of using apache. Here are some of the config stuff LoadModule my_module C:/foo/.../mod_my.so IfModule my_module MyName foo /IfModule mod_my.so is compiled against apache 2.2 first (and the above config structure worked). When I tried apache 2.0, I merely pointing the *.lib and *.h files to the apache 2.0 folders (to generate the mod_my.so for apache 2.0). Thanks again for your help any further suggestions. John
Re: Where is the right place to setup a connection pool?
Thanks Nick! I am actually reading your book. Will your example on Thread Safety (4.5.1) work (setup the pool in init_child hook with thread mutex)? I do not need a global mutex, right? I have not read the late chapters yet. With Regards, John --- Nick Kew [EMAIL PROTECTED] wrote: On Wed, 31 Oct 2007 13:41:59 -0700 (PDT) John Zhang [EMAIL PROTECTED] wrote: Thanks in advance for any help. Use an apr_reslist to manage the pool. See mod_dbd for an example. -- Nick Kew Application Development with Apache - the Apache Modules Book http://www.apachetutor.org/
apache 2.0.61 do not read config settings
I have a module for 2.2x, and have some config settings like IfModule my_module ... MyName foo ... /IfModule and I can get the configurations per the AP_INIT_TAKEx macro. However, when I compiled my module against apache 2.0.61, my functions for setting the parameters are not called. Anything specif I have to do to make the functions to be called (so the config parameters are read). Thanks, John
Where is the right place to setup a connection pool?
I am writing a module talking to a remote server that handles the real work. I need to build/keep a pool of connections for reuse almong the different Apache worker threads. Should I build that in the ap_hook_post_config or ap_hook_child_init function? When apache creates multiple processes, will each process have its own copy of the connection pool automatically (taken care by apache) or I will have to do more work? Thanks in advance for any help. John