utils.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. import urllib.parse
  2. from collections import namedtuple
  3. from collections.abc import Mapping, Sequence
  4. from functools import partial
  5. from os import path
  6. import fnmatch
  7. from urllib.parse import urlparse
  8. from tornado import template
  9. import json
  10. from collections import defaultdict
  11. from ..__version__ import __version__ as version
  12. from ..exceptions import PyWebIOWarning
  13. from ..utils import isgeneratorfunction, iscoroutinefunction, get_function_name, get_function_doc, \
  14. get_function_seo_info
  15. """
  16. The maximum size in bytes of a http request body or a websocket message, after which the request or websocket is aborted
  17. Set by `start_server()` or `path_deploy()`
  18. Used in `file_upload()` as the `max_size`/`max_total_size` parameter default or to validate the parameter.
  19. """
  20. MAX_PAYLOAD_SIZE = 0
  21. DEFAULT_CDN = "https://cdn.jsdelivr.net/gh/wang0618/PyWebIO-assets@v{version}/"
  22. AppMeta = namedtuple('App', 'title description')
  23. _here_dir = path.dirname(path.abspath(__file__))
  24. _index_page_tpl = template.Template(open(path.join(_here_dir, 'tpl', 'index.html'), encoding='utf8').read())
  25. def render_page(app, protocol, cdn):
  26. """渲染前端页面的HTML框架, 支持SEO
  27. :param callable app: PyWebIO app
  28. :param str protocol: 'ws'/'http'
  29. :param bool/str cdn: Whether to use CDN, also accept string as custom CDN URL
  30. :return: bytes content of rendered page
  31. """
  32. assert protocol in ('ws', 'http')
  33. meta = parse_app_metadata(app)
  34. if cdn is True:
  35. cdn = DEFAULT_CDN.format(version=version)
  36. elif not cdn:
  37. cdn = ''
  38. else: # user custom cdn
  39. cdn = cdn.rstrip('/') + '/'
  40. return _index_page_tpl.generate(title=meta.title or 'PyWebIO Application',
  41. description=meta.description, protocol=protocol,
  42. script=True, content='', base_url=cdn)
  43. def cdn_validation(cdn, level='warn', stacklevel=3):
  44. """CDN availability check
  45. :param bool/str cdn: cdn parameter
  46. :param level: warn or error
  47. """
  48. assert level in ('warn', 'error')
  49. if cdn is True and 'dev' in version:
  50. if level == 'warn':
  51. import warnings
  52. warnings.warn("Default CDN is not supported in dev version. Ignore the CDN setting", PyWebIOWarning,
  53. stacklevel=stacklevel)
  54. return False
  55. else:
  56. raise ValueError("Default CDN is not supported in dev version. Please host static files by yourself.")
  57. return cdn
  58. def parse_app_metadata(func):
  59. """解析pywebio app元数据"""
  60. seo_info = get_function_seo_info(func)
  61. if seo_info:
  62. return AppMeta(*seo_info)
  63. doc = get_function_doc(func)
  64. parts = doc.strip().split('\n\n', 1)
  65. if len(parts) == 2:
  66. title, description = parts
  67. else:
  68. title, description = parts[0], ''
  69. return AppMeta(title, description)
  70. _app_list_tpl = template.Template("""
  71. <h1>Applications index</h1>
  72. <ul>
  73. {% for name,meta in apps_info.items() %}
  74. <li>
  75. {% if other_arguments is not None %}
  76. <a href="?app={{name}}{{other_arguments}}">{{ meta.title or name }}</a>:
  77. {% else %}
  78. <a href="javascript:WebIO.openApp('{{ name }}', true)">{{ meta.title or name }}</a>:
  79. {% end %}
  80. {% if meta.description %}
  81. {{ meta.description }}
  82. {% else %}
  83. <i>No description.</i>
  84. {% end %}
  85. </li>
  86. {% end %}
  87. </ul>
  88. """.strip())
  89. def get_static_index_content(apps, query_arguments=None):
  90. """生成默认的静态主页
  91. :param callable apps: PyWebIO apps
  92. :param str query_arguments: Url Query Arguments。为None时,表示使用WebIO.openApp跳转
  93. :return: bytes
  94. """
  95. apps_info = {
  96. name: parse_app_metadata(func)
  97. for name, func in apps.items()
  98. }
  99. qs = urllib.parse.parse_qs(query_arguments)
  100. qs.pop('app', None)
  101. other_arguments = urllib.parse.urlencode(qs, doseq=True)
  102. if other_arguments:
  103. other_arguments = '&' + other_arguments
  104. else:
  105. other_arguments = None
  106. content = _app_list_tpl.generate(apps_info=apps_info, other_arguments=other_arguments).decode('utf8')
  107. return content
  108. def _generate_default_index_app(apps):
  109. """默认的主页任务函数"""
  110. content = get_static_index_content(apps)
  111. def index():
  112. from pywebio.output import put_html
  113. put_html(content)
  114. return index
  115. def make_applications(applications):
  116. """格式化 applications 为 任务名->任务函数 的映射, 并提供默认主页
  117. :param applications: 接受 单一任务函数、字典、列表 类型
  118. :return dict: 任务名->任务函数 的映射
  119. """
  120. if isinstance(applications, Sequence): # 列表 类型
  121. applications, app_list = {}, applications
  122. for func in app_list:
  123. name = get_function_name(func)
  124. if name in applications:
  125. raise ValueError("Duplicated application name:%r" % name)
  126. applications[name] = func
  127. elif not isinstance(applications, Mapping): # 单一任务函数 类型
  128. applications = {'index': applications}
  129. # covert dict key to str
  130. applications = {str(k): v for k, v in applications.items()}
  131. for app in applications.values():
  132. assert iscoroutinefunction(app) or isgeneratorfunction(app) or callable(app), \
  133. "Don't support application type:%s" % type(app)
  134. if 'index' not in applications:
  135. applications['index'] = _generate_default_index_app(applications)
  136. return applications
  137. class OriginChecker:
  138. @classmethod
  139. def check_origin(cls, origin, allowed_origins, host):
  140. if cls.is_same_site(origin, host):
  141. return True
  142. return any(
  143. fnmatch.fnmatch(origin, patten)
  144. for patten in allowed_origins
  145. )
  146. @staticmethod
  147. def is_same_site(origin, host):
  148. """判断 origin 和 host 是否一致。origin 和 host 都为http协议请求头"""
  149. parsed_origin = urlparse(origin)
  150. origin = parsed_origin.netloc
  151. origin = origin.lower()
  152. # Check to see that origin matches host directly, including ports
  153. return origin == host
  154. def deserialize_binary_event(data: bytes):
  155. """
  156. Data format:
  157. | event | file_header | file_data | file_header | file_data | ...
  158. The 8 bytes at the beginning of each segment indicate the number of bytes remaining in the segment.
  159. event: {
  160. event: "from_submit",
  161. task_id: that.task_id,
  162. data: {
  163. input_name => input_data
  164. }
  165. }
  166. file_header: {
  167. 'filename': file name,
  168. 'size': file size,
  169. 'mime_type': file type,
  170. 'last_modified': last_modified timestamp,
  171. 'input_name': name of input field
  172. }
  173. Example:
  174. b'\x00\x00\x00\x00\x00\x00\x00E{"event":"from_submit","task_id":"main-4788341456","data":{"data":1}}\x00\x00\x00\x00\x00\x00\x00Y{"filename":"hello.txt","size":2,"mime_type":"text/plain","last_modified":1617119937.276}\x00\x00\x00\x00\x00\x00\x00\x02ss'
  175. """
  176. parts = []
  177. start_idx = 0
  178. while start_idx < len(data):
  179. size = int.from_bytes(data[start_idx:start_idx + 8], "big")
  180. start_idx += 8
  181. content = data[start_idx:start_idx + size]
  182. parts.append(content)
  183. start_idx += size
  184. event = json.loads(parts[0])
  185. files = defaultdict(list)
  186. for idx in range(1, len(parts), 2):
  187. f = json.loads(parts[idx])
  188. f['content'] = parts[idx+1]
  189. input_name = f.pop('input_name')
  190. files[input_name].append(f)
  191. for input_name in list(event['data'].keys()):
  192. if input_name in files:
  193. event['data'][input_name] = files[input_name]
  194. return event
  195. def seo(title, description=None, app=None):
  196. """Set the SEO information of the PyWebIO application (web page information provided when indexed by search engines)
  197. :param str title: Application title
  198. :param str description: Application description
  199. :param callable app: PyWebIO task function
  200. If not ``seo()`` is not used, the `docstring <https://www.python.org/dev/peps/pep-0257/>`_ of the task function will be regarded as SEO information by default.
  201. ``seo()`` can be used in 2 ways: direct call and decorator::
  202. @seo("title", "description")
  203. def foo():
  204. pass
  205. def bar():
  206. pass
  207. def hello():
  208. \"""Application title
  209. Application description...
  210. (A empty line is used to separate the description and title)
  211. \"""
  212. start_server([
  213. foo,
  214. hello,
  215. seo("title", "description", bar),
  216. ])
  217. .. versionadded:: 1.1
  218. """
  219. if app is not None:
  220. return seo(title, description)(app)
  221. def decorator(func):
  222. try:
  223. func = partial(func)
  224. func._pywebio_title = title
  225. func._pywebio_description = description or ''
  226. except Exception:
  227. pass
  228. return func
  229. return decorator