| 360 | | def wrap_body(body): |
|---|
| 361 | | """Response.body wrapper which saves session data.""" |
|---|
| 362 | | if isinstance(body, types.GeneratorType): |
|---|
| 363 | | # If the body is a generator, we have to save the data |
|---|
| 364 | | # *after* the generator has been consumed |
|---|
| 365 | | for line in body: |
|---|
| 366 | | yield line |
|---|
| 367 | | cherrypy.session.save() |
|---|
| 368 | | else: |
|---|
| 369 | | # If the body is not a generator, we save the data |
|---|
| 370 | | # before the body is returned (so we can release the lock). |
|---|
| 371 | | cherrypy.session.save() |
|---|
| 372 | | for line in body: |
|---|
| 373 | | yield line |
|---|
| 374 | | cherrypy.response.body = wrap_body(cherrypy.response.body) |
|---|
| | 360 | # Guard against running twice |
|---|
| | 361 | if hasattr(cherrypy.request, "_sessionsaved"): |
|---|
| | 362 | return |
|---|
| | 363 | cherrypy.request._sessionsaved = True |
|---|
| | 364 | |
|---|
| | 365 | if cherrypy.response.stream: |
|---|
| | 366 | # If the body is being streamed, we have to save the data |
|---|
| | 367 | # *after* the response has been written out |
|---|
| | 368 | cherrypy.request.hooks.attach('on_end_request', cherrypy.session.save) |
|---|
| | 369 | else: |
|---|
| | 370 | # If the body is not being streamed, we save the data now |
|---|
| | 371 | # (so we can release the lock). |
|---|
| | 372 | if isinstance(cherrypy.response.body, types.GeneratorType): |
|---|
| | 373 | cherrypy.response.collapse_body() |
|---|
| | 374 | cherrypy.session.save() |
|---|