Skip to content

Commit

Permalink
v0.0.5 updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jvanasco committed Oct 6, 2015
1 parent 3ebc87c commit 5f89d84
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 43 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
@@ -1,3 +1,6 @@
0.0.5
moved distribution to a nested folder (was single-file)

0.0.4
flake8

Expand Down
66 changes: 32 additions & 34 deletions README.md
@@ -1,86 +1,84 @@
pyramid_subscribers_cookiexfer allows you to transfer cookies from the request to the response on an http exception. you might need this as redirects are subclasses of httpexceptions.
`pyramid_subscribers_cookiexfer` allows you to transfer cookies from the request to the response on an http exception.
you might need this as redirects are subclasses of httpexceptions, and cookies may not transfer.

overview
--------

a typical user flow might be this:

* user submits form to /account/login
* user submits form to /account/login
* backend authenticates, sets various cookies
* backend redirects to /account/home

if you only persist data through pyramid sessions this is not needed -- the pyramid session cookie is set before authentication, so data persists through the redirect.

this is useful if you're setting ancillary information through browser cookies , such as caching user data on the client.
this is useful if you're setting ancillary information through browser cookies, such as caching user data on the client.

Almost every browser respects a SetCookie header on a redirect -- only Safari is known to ignore this. Many developers have stored cookies in session data to show on future visits, this package automates that.
Almost every browser respects a `SetCookie` header on a redirect -- only Safari is known to ignore this. Many developers have stored cookies in session data to show on future visits, this package automates that.

two methods are available to persist information

- add_headers -- transfers cookie headers from the request to the response
- session_save -- saves the cookies you'd want to set into the session, migrates them into the response on the next pageview
* `add_headers` Transfers cookie headers from the request to the response
* `session_save` Saves the cookies you'd want to set into the session, migrates them into the response on the next pageview

additionally the package offers the ability to 'uniquely' manage the cookies to avoid duplicates. right now this behavior is recommended.
This package also offers the ability to uniquely manage the cookies to avoid duplicates. right now this behavior is recommended.

the package is configured through a few variables set in your .ini files , then enabled with an import and call to 'initialize' in your .ini
The package is configured through a few variables set in your `.ini` files , then enabled with an import and call to `initialize` in your `.ini`

the internal mechanics are pretty simple:
The internal mechanics are pretty simple:

config.add_subscriber(\
new_request,
new_request,
'pyramid.events.NewRequest')
config.add_subscriber(\
new_response,
new_response,
'pyramid.events.NewResponse')

in order to aid in debugging and cut down on processing:

- initialize_subscribers() will only install a NewResponse listener if sessioning will be used
- a configurable regex is used to eliminate paths from the module ( including debug statements )
In order to aid in debugging and cut down on processing:

* `initialize_subscribers()` will only install a `NewResponse` listener if sessioning will be used
* A configurable regex is used to eliminate paths from the module ( including debug statements )

Important Notes:

- This package will respect headers that are raised with the httpexception
- Because of how pyramid's internals work, you must 'return' the redirect -- not 'raise' it -- if you want cookies transferred from the request.response. If you 'raise' a redirect, only the headers used to initialize the redirect can be stored in the session ( they exist in the new response object and do not need to be transferred )
* This package will respect headers that are raised with the httpexception
* Because of how pyramid's internals work, you must `return` the redirect -- not `raise` it -- if you want cookies transferred from the `request.response`. If you "raise" a redirect, only the headers used to initialize the redirect can be stored in the session (they exist in the new response object and do not need to be transferred)

These situations will work:
These situations will work:

A few case examples

return HTTPFound(location='/new/location')
- any cookies set by request.response.set_cookie will be transferred

return HTTPFound(location='/new/location', headers=dict_of_headers )
- the headers in dict_of_headers are already in the new response , and can persist to the session
- any cookies set by request.response.set_cookie will be transferred

raise HTTPFound(location='/new/location')
- NO cookies set by request.response.set_cookie will be transferred
`return HTTPFound(location='/new/location')`
- any cookies set by `request.response.set_cookie` will be transferred

raise HTTPFound(location='/new/location', headers=dict_of_headers )
- the headers in dict_of_headers are already in the new response , and can persist to the session
- NO cookies set by request.response.set_cookie will be transferred
`return HTTPFound(location='/new/location', headers=dict_of_headers)`
- the headers in `dict_of_headers` are already in the new `response`, and can persist to the `session`
- any cookies set by `request.response.set_cookie` will be transferred

`raise HTTPFound(location='/new/location')`
- NO cookies set by `request.response.set_cookie` will be transferred

`raise HTTPFound(location='/new/location', headers=dict_of_headers )`
- the headers in `dict_of_headers` are already in the new `response`, and can persist to the `session`
- NO cookies set by `request.response.set_cookie` will be transferred


setup
-----


environment.ini
cookie_xfer.redirect_add_headers = True
cookie_xfer.redirect_add_headers__unique = True
cookie_xfer.redirect_session_save = False
cookie_xfer.redirect_session_save__unique = False
cookie_xfer.re_excludes = "^/(css|img|js|deform|_debug_toolbar)"


app/__init__.py

import pyramid_subscribers_cookiexfer

def main(global_config, **settings):
...
pyramid_subscribers_cookiexfer.initialize( config , settings )
...
...
Expand Up @@ -29,15 +29,20 @@ def new_response(event):

# cookies_request is populated if the exception is RETURNed
# cookies_request is not populated if the exception is RAISEd
cookies_request = [(k, v) for (k, v) in event.request.response.headers.iteritems() if k.lower() == 'set-cookie']
cookies_request = [(k, v) for (k, v)
in event.request.response.headers.iteritems()
if k.lower() == 'set-cookie'
]

# cookies_response is populated if the exception is created with headers specified
cookies_response = [(k, v) for (k, v) in event.response.headers.iteritems() if k.lower == 'set-cookie']
cookies_response = [(k, v) for (k, v)
in event.response.headers.iteritems()
if k.lower == 'set-cookie'
]

# debug
if False:
print "-------- cookies request || %s" % cookies_request
print "-------- cookies response || %s" % cookies_response
# print "-------- cookies request || %s" % cookies_request
# print "-------- cookies response || %s" % cookies_response

if cookies_request or cookies_response:
log.debug("cookie-xfer - migrating cookies INTO session")
Expand Down Expand Up @@ -99,12 +104,12 @@ def new_response(event):
def initialize_subscribers(config, settings):
# create a package settings hash
package_settings = {'re_excludes': re.compile(settings['cookie_xfer.re_excludes'])}
for i in [
for i in (
'redirect_add_headers',
'redirect_add_headers__unique',
'redirect_session_save',
'redirect_session_save__unique'
]:
'redirect_session_save__unique',
):
if settings['cookie_xfer.%s' % i].lower() == 'true':
package_settings[i] = True
else:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -15,7 +15,7 @@

setup(
name="pyramid_subscribers_cookiexfer",
version="0.0.4",
version="0.0.5",
description="transfers cookies from request to response on exceptions",
long_description=README,
classifiers=[
Expand Down

0 comments on commit 5f89d84

Please sign in to comment.