@@ -59,62 +59,56 @@ def updir():
59
59
60
60
61
61
class HtmlOnlyHandler (BaseHTTPRequestHandler ):
62
- """Http handler."""
62
+ """Handler for HTML responses and JSON files."""
63
+
64
+ def _serve_page (self , page_number ):
65
+ """Serve a dynamically generated HTML page."""
66
+ html = f"""<html><head><title>Page{ page_number } </title></head>
67
+ <body>Page number <span id="pageNumber">{ page_number } </span>
68
+ <p><a href="../xhtmlTest.html" target="_top">top</a></p>
69
+ </body></html>"""
70
+ return html .encode ("utf-8" )
71
+
72
+ def _serve_file (self , file_path ):
73
+ """Serve a file from the HTML root directory."""
74
+ with open (file_path , encoding = "latin-1" ) as f :
75
+ return f .read ().encode ("utf-8" )
76
+
77
+ def _send_response (self , content_type = "text/html" ):
78
+ """Send a response."""
79
+ self .send_response (200 )
80
+ self .send_header ("Content-type" , content_type )
81
+ self .end_headers ()
63
82
64
83
def do_GET (self ):
65
84
"""GET method handler."""
66
85
try :
67
86
path = self .path [1 :].split ("?" )[0 ]
68
- if path [:5 ] == "page/" :
69
- html = """<html><head><title>Page{page_number}</title></head>
70
- <body>Page number <span id=\" pageNumber\" >{page_number}</span>
71
- <p><a href=\" ../xhtmlTest.html\" target=\" _top\" >top</a>
72
- </body></html>""" .format (
73
- page_number = path [5 :]
74
- )
75
- html = html .encode ("utf-8" )
87
+ file_path = os .path .join (HTML_ROOT , path )
88
+ if path .startswith ("page/" ):
89
+ html = self ._serve_page (path [5 :])
90
+ self ._send_response ("text/html" )
91
+ self .wfile .write (html )
92
+ elif os .path .isfile (file_path ):
93
+ content_type = "application/json" if file_path .endswith (".json" ) else "text/html"
94
+ content = self ._serve_file (file_path )
95
+ self ._send_response (content_type )
96
+ self .wfile .write (content )
76
97
else :
77
- with open (os .path .join (HTML_ROOT , path ), encoding = "latin-1" ) as f :
78
- html = f .read ().encode ("utf-8" )
79
- self .send_response (200 )
80
- self .send_header ("Content-type" , "text/html" )
81
- self .end_headers ()
82
- self .wfile .write (html )
98
+ self .send_error (404 , f"File Not Found: { path } " )
83
99
except OSError :
84
100
self .send_error (404 , f"File Not Found: { path } " )
85
101
86
102
def do_POST (self ):
87
103
"""POST method handler."""
88
104
try :
89
105
remaining_bytes = int (self .headers ["content-length" ])
90
- contents = ""
91
- line = self .rfile .readline ()
92
- contents += line .decode ("utf-8" )
93
- remaining_bytes -= len (line )
94
- line = self .rfile .readline ()
95
- contents += line .decode ("utf-8" )
96
- remaining_bytes -= len (line )
97
- fn = re .findall (r'Content-Disposition.*name="upload"; filename="(.*)"' , line .decode ("utf-8" ))
98
- if not fn :
99
- self .send_error (500 , f"File not found. { contents } " )
106
+ contents = self .rfile .read (remaining_bytes ).decode ("utf-8" )
107
+ fn_match = re .search (r'Content-Disposition.*name="upload"; filename="(.*)"' , contents )
108
+ if not fn_match :
109
+ self .send_error (500 , f"File not found in content. { contents } " )
100
110
return
101
- line = self .rfile .readline ()
102
- remaining_bytes -= len (line )
103
- contents += line .decode ("utf-8" )
104
- line = self .rfile .readline ()
105
- remaining_bytes -= len (line )
106
- contents += line .decode ("utf-8" )
107
- preline = self .rfile .readline ()
108
- remaining_bytes -= len (preline )
109
- while remaining_bytes > 0 :
110
- line = self .rfile .readline ()
111
- remaining_bytes -= len (line )
112
- contents += line .decode ("utf-8" )
113
-
114
- self .send_response (200 )
115
- self .send_header ("Content-type" , "text/html" )
116
- self .end_headers ()
117
-
111
+ self ._send_response ("text/html" )
118
112
self .wfile .write (
119
113
f"""<!doctype html>
120
114
{ contents }
0 commit comments