Skip to content

Commit

Permalink
Merge pull request #4353 from geoadmin/feat-PB-130-apache-config-OPTIONS
Browse files Browse the repository at this point in the history


PB-130: HTTP 405 when method not allowed.
  • Loading branch information
rebert authored Mar 12, 2024
2 parents 5a8f5f1 + 9530684 commit 4395ea4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
5 changes: 5 additions & 0 deletions apache/25-mf-chsdi3.conf.in
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,9 @@ ErrorLog /dev/stdout
# ServerAlias mf-chsdi3.prod.bgdi.ch
# ServerAlias s.geo.admin.ch
SetEnvIf X-Forwarded-For "^.*\..*\..*\..*" forwarded

# WSGI response on OPTIONS is an empty body
# no need to zip an empty body
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [PT,E=no-gzip:1]
</VirtualHost>
25 changes: 14 additions & 11 deletions chsdi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,6 @@ def __init__(self, environ, **kwargs):
super().__init__(environ, **kwargs)


# This is a wrapper function around all views. If OPTIONS is given, an empty string will be returned
# As HTTP OPTIONS is not cached, this wrapper will safe some power
def options_view(view, info):
def wrapper_view(context, request):
if request.method == 'OPTIONS':
return Response('')
return view(context, request)
return wrapper_view


def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
Expand All @@ -58,8 +48,21 @@ def main(global_config, **settings):
config = Configurator(settings=settings, request_factory=WsgiSchemeAdaptedRequest)
config.include('pyramid_mako')
config.include('akhet.static')

# wrapper around all views
config.add_view_deriver(options_view)
# This is a wrapper function around all views. If OPTIONS is given, an empty string will be returned
# As HTTP OPTIONS is not cached, this wrapper will safe some power
def methods_view(view, info):
def wrapper_view(context, request):
if request.method == 'OPTIONS':
return Response()
# HTTP 405: method not allowed
elif request.method not in request_method:
context.code = context.status_code = 405
return view(context, request)
return wrapper_view

config.add_view_deriver(methods_view)

# configure 'locale' dir as the translation dir for chsdi app
config.add_translation_dirs('chsdi:locale/')
Expand Down

0 comments on commit 4395ea4

Please sign in to comment.