The main Bubble source repository. Contains the Bubble API server, the web UI, documentation and utilities. https://getbubblenow.com
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

4 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. """
  2. This inline script modifies a streamed response.
  3. If you do not need streaming, see the modify_response_body example.
  4. Be aware that content replacement isn't trivial:
  5. - If the transfer encoding isn't chunked, you cannot simply change the content length.
  6. - If you want to replace all occurrences of "foobar", make sure to catch the cases
  7. where one chunk ends with [...]foo" and the next starts with "bar[...].
  8. """
  9. import aiohttp
  10. import urllib
  11. from bubble_config import bubble_port
  12. from bubble_api import HEADER_BUBBLE_MATCHERS
  13. BUFFER_SIZE = 4096
  14. def stream_data(stream):
  15. yield stream.read_nowait(BUFFER_SIZE)
  16. async def fetch(session, url, chunks):
  17. async with session.post(url, data=chunks) as response:
  18. if response.status != 200:
  19. raise RuntimeError("Error fetching "+url+", HTTP status "+str(response.status))
  20. return stream_data(response.content)
  21. async def filter_chunks_with_matchers(chunks, matchers):
  22. rule_string = urllib.parse.quote_plus(matchers)
  23. url = 'http://127.0.0.1:'+bubble_port+'/api/filter/apply/' + rule_string
  24. async with aiohttp.ClientSession() as session:
  25. await fetch(session, url, chunks)
  26. def filter_with_matchers(matchers):
  27. return lambda chunks: filter_chunks_with_matchers(chunks, matchers)
  28. def responseheaders(flow):
  29. if HEADER_BUBBLE_MATCHERS in flow.request.headers:
  30. matchers = flow.request.headers[HEADER_BUBBLE_MATCHERS]
  31. if matchers:
  32. flow.response.stream = filter_with_matchers(matchers)
  33. else:
  34. pass
  35. else:
  36. pass