mirror of
https://github.com/webrecorder/pywb.git
synced 2025-03-24 06:59:52 +01:00
amf rewriting: move to separate file, mark as experimental, and don't include as default (for now)
This commit is contained in:
parent
527a3bc89c
commit
9f299eb8e9
52
pywb/rewrite/rewrite_amf.py
Normal file
52
pywb/rewrite/rewrite_amf.py
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
from io import BytesIO
|
||||||
|
from six.moves import zip
|
||||||
|
from pywb.rewrite.rewrite_content import RewriteContent
|
||||||
|
|
||||||
|
|
||||||
|
# ============================================================================
|
||||||
|
# Expiermental: not fully tested
|
||||||
|
class RewriteContentAMF(RewriteContent): #pragma: no cover
|
||||||
|
def handle_custom_rewrite(self, text_type, status_headers, stream, env):
|
||||||
|
|
||||||
|
if status_headers.get_header('Content-Type') == 'application/x-amf':
|
||||||
|
stream = self.rewrite_amf(stream, env)
|
||||||
|
|
||||||
|
return (super(RewriteContentAMF, self).
|
||||||
|
handle_custom_rewrite(text_type, status_headers, stream, env))
|
||||||
|
|
||||||
|
def rewrite_amf(self, stream, env):
|
||||||
|
try:
|
||||||
|
from pyamf import remoting
|
||||||
|
|
||||||
|
iobuff = BytesIO()
|
||||||
|
while True:
|
||||||
|
buff = stream.read()
|
||||||
|
if not buff:
|
||||||
|
break
|
||||||
|
iobuff.write(buff)
|
||||||
|
|
||||||
|
iobuff.seek(0)
|
||||||
|
res = remoting.decode(iobuff)
|
||||||
|
|
||||||
|
if env and env.get('pywb.inputdata'):
|
||||||
|
inputdata = env.get('pywb.inputdata')
|
||||||
|
|
||||||
|
new_list = []
|
||||||
|
|
||||||
|
for src, target in zip(inputdata.bodies, res.bodies):
|
||||||
|
#print(target[0] + ' = ' + src[0])
|
||||||
|
|
||||||
|
#print('messageId => corrId ' + target[1].body.correlationId + ' => ' + src[1].body[0].messageId)
|
||||||
|
target[1].body.correlationId = src[1].body[0].messageId
|
||||||
|
|
||||||
|
new_list.append((src[0], target[1]))
|
||||||
|
|
||||||
|
res.bodies = new_list
|
||||||
|
|
||||||
|
return BytesIO(remoting.encode(res).getvalue())
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
|
print(e)
|
||||||
|
return stream
|
@ -4,9 +4,8 @@ import webencodings
|
|||||||
import yaml
|
import yaml
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from chardet.universaldetector import UniversalDetector
|
#from chardet.universaldetector import UniversalDetector
|
||||||
from io import BytesIO, BufferedReader
|
from io import BytesIO
|
||||||
from six.moves import zip
|
|
||||||
|
|
||||||
from pywb.rewrite.header_rewriter import RewrittenStatusAndHeaders
|
from pywb.rewrite.header_rewriter import RewrittenStatusAndHeaders
|
||||||
|
|
||||||
@ -371,55 +370,3 @@ class RewriteContent(object):
|
|||||||
stream.close()
|
stream.close()
|
||||||
|
|
||||||
|
|
||||||
# =================================================================
|
|
||||||
class RewriteContentAMF(RewriteContent):
|
|
||||||
def handle_custom_rewrite(self, text_type, status_headers, stream, env):
|
|
||||||
|
|
||||||
if status_headers.get_header('Content-Type') == 'application/x-amf':
|
|
||||||
stream = self.rewrite_amf(stream, env)
|
|
||||||
|
|
||||||
return (super(RewriteContentAMF, self).
|
|
||||||
handle_custom_rewrite(text_type, status_headers, stream, env))
|
|
||||||
|
|
||||||
def rewrite_amf(self, stream, env):
|
|
||||||
try:
|
|
||||||
from pyamf import remoting
|
|
||||||
|
|
||||||
iobuff = BytesIO()
|
|
||||||
while True:
|
|
||||||
buff = stream.read()
|
|
||||||
if not buff:
|
|
||||||
break
|
|
||||||
iobuff.write(buff)
|
|
||||||
|
|
||||||
iobuff.seek(0)
|
|
||||||
res = remoting.decode(iobuff)
|
|
||||||
|
|
||||||
print('rewrite amf')
|
|
||||||
|
|
||||||
print(env.get('pywb.inputdata'))
|
|
||||||
|
|
||||||
if env and env.get('pywb.inputdata'):
|
|
||||||
inputdata = env.get('pywb.inputdata')
|
|
||||||
|
|
||||||
new_list = []
|
|
||||||
|
|
||||||
for src, target in zip(inputdata.bodies, res.bodies):
|
|
||||||
print(target[0] + ' = ' + src[0])
|
|
||||||
|
|
||||||
print('messageId => corrId ' + target[1].body.correlationId + ' => ' + src[1].body[0].messageId)
|
|
||||||
target[1].body.correlationId = src[1].body[0].messageId
|
|
||||||
|
|
||||||
new_list.append((src[0], target[1]))
|
|
||||||
|
|
||||||
res.bodies = new_list
|
|
||||||
|
|
||||||
return BytesIO(remoting.encode(res).getvalue())
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
import traceback
|
|
||||||
traceback.print_exc()
|
|
||||||
print(e)
|
|
||||||
return stream
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@ from pywb.utils.timeutils import timestamp_now
|
|||||||
from pywb.framework.wbrequestresponse import WbResponse
|
from pywb.framework.wbrequestresponse import WbResponse
|
||||||
from pywb.framework.memento import MementoResponse
|
from pywb.framework.memento import MementoResponse
|
||||||
|
|
||||||
from pywb.rewrite.rewrite_content import RewriteContentAMF
|
from pywb.rewrite.rewrite_content import RewriteContent
|
||||||
from pywb.warc.recordloader import ArchiveLoadFailed
|
from pywb.warc.recordloader import ArchiveLoadFailed
|
||||||
|
|
||||||
from pywb.webapp.views import HeadInsertView
|
from pywb.webapp.views import HeadInsertView
|
||||||
@ -40,7 +40,7 @@ class ReplayView(object):
|
|||||||
self.content_loader = content_loader
|
self.content_loader = content_loader
|
||||||
|
|
||||||
framed = config.get('framed_replay')
|
framed = config.get('framed_replay')
|
||||||
self.content_rewriter = RewriteContentAMF(is_framed_replay=framed)
|
self.content_rewriter = RewriteContent(is_framed_replay=framed)
|
||||||
|
|
||||||
self.head_insert_view = HeadInsertView.init_from_config(config)
|
self.head_insert_view = HeadInsertView.init_from_config(config)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user