1
0
mirror of https://github.com/webrecorder/pywb.git synced 2025-03-24 15:09:54 +01:00
pywb/pywb/rewrite/jsonp_rewriter.py
John Berlin 56fc26333e server side rewriting: ()
- fixed edge case in jsonP rewriting where no callback name is supplied only ? but body has normal jsonP callback (url = https://geolocation.onetrust.com/cookieconsentpub/v1/geo/countries/EU?callback=?)
  - made the `!self.__WB_pmw` server side inject match the client side one done via wombat
  - added regex's for eval override to JSWombatProxyRules
2019-07-02 19:24:28 -07:00

32 lines
1.2 KiB
Python

import re
from pywb.rewrite.content_rewriter import StreamingRewriter
# ============================================================================
class JSONPRewriter(StreamingRewriter):
#JSONP = re.compile(r'^(?:\s*\/\*(?:.*)\*\/)*\s*(\w+)\(\{')
# Match a single /* and // style comments at the beginning
JSONP = re.compile(r'(?:^[ \t]*(?:(?:\/\*[^\*]*\*\/)|(?:\/\/[^\n]+[\n])))*[ \t]*(\w+)\(\{', re.M)
CALLBACK = re.compile(r'[?].*callback=([^&]+)')
def rewrite(self, string):
# see if json is jsonp, starts with callback func
m_json = self.JSONP.match(string)
if not m_json:
return string
# see if there is a callback param in current url
m_callback = self.CALLBACK.search(self.url_rewriter.wburl.url)
if not m_callback:
return string
if m_callback.group(1) == '?':
# this is a very sharp edge case e.g. callback=?
# since we only have this string[m_json.end(1):]
# would cut off the name of the CB if any is included
# so we just pass the string through
return string
string = m_callback.group(1) + string[m_json.end(1):]
return string