Commits

Atsushi Odagiri committed 44ec2ef

refactoring.

Comments (0)

Files changed (1)

wsgiheaders/__init__.py

 import re
 from webob import Request, Response
 
+def addheaderhandler(res, name, value):
+    res.headers.add(name, value)
+
 def addheader(headerConditions):
+    return wsgiheader(headerConditions, addheaderhandler)
+
+def replaceheaderhandler(res, name, value):
+    res.headers[name] = value
+
+def replaceheader(headerConditions):
+    return wsgiheader(headerConditions, replaceheaderhandler)
+
+def wsgiheader(headerConditions, handler):
     headerConditions = [(re.compile(p) if isinstance(p, basestring) else p,
                          headers)
                         for p,headers in headerConditions ]
             for pred, headers in headerConditions:
                 if pred.match(req.url):
                     for name, value in headers:
-                        res.headers.add(name, value)
+                        handler(res, name, value)
             return res(environ, start_response)
         return wrap
     return decorator
-
-def replaceheader(headerConditions):
-    headerConditions = [(re.compile(p) if isinstance(p, basestring) else p,
-                         headers)
-                        for p,headers in headerConditions ]
-    def decorator(app):
-        def wrap(environ, start_response):
-            req = Request(environ)
-            res = req.get_response(app)
-            for pred, headers in headerConditions:
-                if pred.match(req.url):
-                    for name, value in headers:
-                        res.headers[name] = value
-            return res(environ, start_response)
-        return wrap
-    return decorator