|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 |
|
| 3 | +import html |
3 | 4 | import os |
4 | 5 | import subprocess |
| 6 | +import sys |
| 7 | + |
| 8 | +# Disable chunked encoding - must be set before any output |
| 9 | +os.environ["no-gzip"] = "1" |
| 10 | +os.environ["no-chunked-encoding"] = "1" |
| 11 | +os.environ["HTTP_NO_CHUNKED_ENCODING"] = "1" |
5 | 12 |
|
6 | 13 | try: |
7 | 14 | # Do all work first |
|
37 | 44 | "rm -rf book/python && " |
38 | 45 | "cp -r /tmp/manip_doc/html book/python", |
39 | 46 | ], |
| 47 | + stdin=subprocess.DEVNULL, |
40 | 48 | stdout=log_file, |
41 | 49 | stderr=subprocess.STDOUT, |
42 | 50 | start_new_session=True, |
| 51 | + close_fds=True, |
43 | 52 | ) |
44 | 53 |
|
45 | | - print("Content-Type: text/html\n") |
46 | | - print("<html><body>") |
47 | | - print("<p>pulling repo...</p>") |
48 | | - print(f"<pre>{git_output}</pre>") |
49 | | - print("<p>done.</p>") |
50 | | - print("<p>Documentation build started in the background.</p>") |
51 | | - print("<p>Check /tmp/manipulation_build_docs.log for progress.</p>") |
52 | | - print("</body></html>") |
| 54 | + # Build response body first |
| 55 | + body = ( |
| 56 | + "<html><body>\n" |
| 57 | + "<p>pulling repo...</p>\n" |
| 58 | + f"<pre>{html.escape(git_output)}</pre>\n" |
| 59 | + "<p>done.</p>\n" |
| 60 | + "<p>Documentation build started in the background.</p>\n" |
| 61 | + "<p>Check /tmp/manipulation_build_docs.log for progress.</p>\n" |
| 62 | + "</body></html>\n" |
| 63 | + ) |
| 64 | + |
| 65 | + # Build complete response |
| 66 | + response = ( |
| 67 | + "Content-Type: text/html\r\n" |
| 68 | + "\r\n" |
| 69 | + "<html><body>\n" |
| 70 | + "<p>pulling repo...</p>\n" |
| 71 | + f"<pre>{html.escape(git_output)}</pre>\n" |
| 72 | + "<p>done.</p>\n" |
| 73 | + "<p>Documentation build started in the background.</p>\n" |
| 74 | + "<p>Check /tmp/manipulation_build_docs.log for progress.</p>\n" |
| 75 | + "</body></html>\n" |
| 76 | + ) |
| 77 | + |
| 78 | + # Write response and immediately exit |
| 79 | + sys.stdout.write(response) |
| 80 | + sys.stdout.flush() |
53 | 81 |
|
54 | 82 | except Exception as e: |
55 | | - print("Content-Type: text/html\n") |
56 | | - print("<html><body>") |
57 | | - print(f"<p>Error: {str(e)}</p>") |
58 | | - print("</body></html>") |
| 83 | + error_response = ( |
| 84 | + "Content-Type: text/html\r\n" |
| 85 | + "\r\n" |
| 86 | + "<html><body>\n" |
| 87 | + f"<p>Error: {html.escape(str(e))}</p>\n" |
| 88 | + "</body></html>\n" |
| 89 | + ) |
| 90 | + sys.stdout.write(error_response) |
| 91 | + sys.stdout.flush() |
0 commit comments