Injection of livereload.js fails when head section is too long
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 50/100
Hướng nghiên cứu
Start in livereload/server.py at the LiveScriptInjector implementation linked in the issue, then read Tornado's OutputTransform chunk lifecycle and the provided monkey-patch examples. Reproduce the issue with the MVE and livereload .; done means both long_page.html and longer_page.html receive livereload.js without a Content-Length error.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
When serving an html file with a long head section, python-livereload fails to inject the livereload.js script into the html.
This seems to be caused by the injector chopping the page into bytes chunks of length 65536. The injector only acts on the first of these chunks by injecting the script before the </head> tag, but if this tag falls outside of the first chunk, this fails. See
https://github.com/lepture/python-livereload/blob/ecabebaeee2a40743f5a8216e1856c9f9150913f/livereload/server.py#L105-L115
MVE
The following python generates two html files, one of which causes the bug, the one does not.
page = """<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8>
<title>tester</title>
<!-- {header_fill} -->
</head>
<body>
<p>Hi</p>
</body>
</html>
"""
nl = 65536
n = nl - 113
print(page.format(header_fill='a'*n), file=open("longer_page.html", "w"))
n = nl - 114
print(page.format(header_fill='a'*n), file=open("long_page.html", "w"))
After running this code in python, run livereload . and open the two files long_page.html and longer_page.html in the generated server session. The first file includes the livereload.js script (see developer tools of the browser used), but the second one does not.
Failed solution attempt
The web.OutputTransform class that LiveScriptInjector extends contains a transform_chunk method (the web.OutputTransform class comes from tornado.web) . I tried monkey patching this with
from livereload.server import LiveScriptInjector, HEAD_END
from livereload import Server
def transform_chunk(self, chunk: bytes, finishing: bool) -> bytes:
if HEAD_END in chunk:
chunk = chunk.replace(HEAD_END, self.script + HEAD_END)
return chunk
LiveScriptInjector.transform_chunk = transform_chunk
server = Server()
server.watch("longer_page.html")
server.serve(root='.')
but I got the following error
Uncaught exception GET /longer_page.html (127.0.0.1)
HTTPServerRequest(protocol='http', host='127.0.0.1:5500', method='GET', uri='/longer_page.html', version='HTTP/1.1', remote_ip='127.0.0.1')
Traceback (most recent call last):
File "/home/jens/miniconda3/envs/test-livereload/lib/python3.9/site-packages/tornado/web.py", line 1704, in _execute
result = await result
File "/home/jens/miniconda3/envs/test-livereload/lib/python3.9/site-packages/tornado/web.py", line 2648, in get
await self.flush()
File "/home/jens/miniconda3/envs/test-livereload/lib/python3.9/site-packages/tornado/web.py", line 1102, in flush
return self.request.connection.write(chunk)
File "/home/jens/miniconda3/envs/test-livereload/lib/python3.9/site-packages/tornado/http1connection.py", line 499, in write
self._pending_write = self.stream.write(self._format_chunk(chunk))
File "/home/jens/miniconda3/envs/test-livereload/lib/python3.9/site-packages/tornado/http1connection.py", line 475, in _format_chunk
raise httputil.HTTPOutputError(
tornado.httputil.HTTPOutputError: Tried to write more data than Content-Length
Workaround
As a temporary workaround, the LiveScriptInjector class can be monkey patched to inject the code after the opening <head> tag, rather than before the closing </head> tag.
from livereload.server import LiveScriptInjector
HEAD_START = b'<head>'
def transform_first_chunk(self, status_code, headers, chunk, finishing):
if HEAD_START in chunk:
chunk = chunk.replace(HEAD_START, HEAD_START+self.script)
if 'Content-Length' in headers:
length = int(headers['Content-Length']) + len(self.script)
headers['Content-Length'] = str(length)
return status_code, headers, chunk
LiveScriptInjector.transform_first_chunk = transform_first_chunk
- Ngôn ngữ chính
- Python
- Star
- 1.1k
- Fork
- 148
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của lepture/python-livereload
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 70/100
lepture/python-livereload#159 · 3 reaction ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
lepture/python-livereload#288 ·
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 38/100
lepture/python-livereload#283 · 1 bình luận ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 25/100
lepture/python-livereload#282 ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 25/100
lepture/python-livereload#279 ·
Tất cả issue của lepture/python-livereload
Issue tương tự
-
request-theme
Độ khó 2/5 Dưới một giờ Mức phù hợp với người mới 70/100
LizardByte/ThemerrDB#8877 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
area/install-update comp/gateway P0 sweeper:risk-compatibility type/bug
Độ khó 2/5 Dưới một giờ Mức phù hợp với người mới 72/100
NousResearch/hermes-agent#135997 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
deepset-ai/haystack#13199 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[BUG] JSONLoader rejects valid UTF-8 BOM filesCó thể đã có người làm @zouyonghe đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
anthropics/knowledge-work-plugins#1298 ·
Maintainer thường phản hồi trong vòng 1 ngày