On 12 Dec 2012, at 7:09 PM, Massimo Di Pierro <massimo.dipie...@gmail.com> wrote: > I added a ticket about this until I have time to review it: > > http://code.google.com/p/web2py/issues/detail?id=1220&thanks=1220&ts=1355368119 > > @Jonathan, what do you think?
What are we really trying to accomplish here? Ordinarily, static requests a) won't be handled by web2py at all; they'll be intercepted by the server, and b) they'll be references to resources embedded in a higher-level page. What is is we want the error routing to do in the case of a static request? > > On Sunday, 2 December 2012 09:15:57 UTC-6, Chris wrote: > On Friday, September 9, 2011 9:17:12 AM UTC-4, kachna wrote: > I made some investigation and here is results. > > 1) "routes_onerror" do not work with static files. It is cased by > lines 488 and 489 in gluon.main > > if static_file: > return http_response.to(responder) > > I comment them and static files HTTP errors works now. > > On Sep 9, 2:38 am, kachna <petr.marti...@gmail.com> wrote: > > Hi, > > I am trying to catch "invalid request" after requesting non-existing > > file from static folder of application "eshop". ... > > ... > > On my PC it works good. Just request.vars.requested_uri in HTTP_error > > function is None. It stop working When I move the code on hosting. > > > > On issue #1, I agree -- when localhost:8000/default/static/images/logo.png > downloads properly and I change to a non-existent file name ...fake.png, I > always receive the plain "invalid request" screen even though I have > routes_on_error in place that works fine for all kinds of other errors. > Commenting out the two lines fixes it. Thanks! Massimo and others, can you > comment on whether this is a correct fix? It solves the proximate cause but > may have a ripple effect ... > > > On issue #2, there are other posts that say request.vars.requested_uri won't > always be available. For anyone who wants to reproduce this problem ... if > http://localhost:8000/index.html works, just make the extension invalid like > this -- http://localhost:8000/index.html&& . The error handler is invoked, > but the request_url, requested_uri and text fields are missing from > request.vars or '' or None. (Hard to provide a meaningful error message > since all the details are blank!) > > However there is a way around this specific problem -- in > gluon.rewrite.try_rewrite_on_error: > > if uri.startswith('http://') or uri.startswith('https://'): > # make up a response > url = path_info+'?'+query_string > message = 'You are being redirected <a href="%s">here</a>' > return HTTP(303, message % url, Location=url), environ > else: > error_raising_path = environ['PATH_INFO'] > environ['PRAGMA_error_raising_path'] = error_raising_path > # Rewrite routes_onerror path. > path_info = '/' + path_info.lstrip('/') # add leading '/' if missing > environ['PATH_INFO'] = path_info > error_handling_path = url_in(request, environ)[1]['PATH_INFO'] > # Avoid infinite loop. > if error_handling_path != error_raising_path: > # wsgibase will be called recursively with the routes_onerror path. > environ['PATH_INFO'] = path_info > environ['QUERY_STRING'] = query_string > return None, environ > > I added the part in red. Then in gluon.rewrite.map_function: > > if not self.router._acfe_match.match(self.function): > self.env['PRAGMA_web2py_error'] = 'Invalid request' > raise HTTP(400, thread.routes.error_message % 'invalid request', > web2py_error='invalid function') > if self.extension and not self.router._acfe_match.match(self.extension): > self.env['PRAGMA_web2py_error'] = 'Invalid extension' > raise HTTP(400, thread.routes.error_message % 'invalid request', > web2py_error='invalid extension') > > Then at the top of your error handler controller, attempt to fill in the > blanks: > > request.vars.request_url = request.vars.request_url or > request.env.pragma_error_raising_path > request.vars.text = request.vars.text or request.env.pragma_web2py_error > > The result (from my example above) is request_url = '/index.html&&' and text > = 'invalid extension' > > The way I "fixed" this isn't particularly clever. There are other places > where web2py_error is passed to HTTP that I didn't handle in this way; I > didn't want to make unnecessary changes in gluon, but I don't like the lack > of parallelism. If it were true that all should be handled this way, then > setting the pragma variable inside HTTP() would be cleaner. > > Massimo and others can judge what's the right way to fix this. My changes > here are pretty quick-and-dirty. --