Skip to content

Commit 220456d

Browse files
committed
first_setup.py sends the correct Content-Type header for HTML
1 parent a0fcefc commit 220456d

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

first_setup.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -459,16 +459,20 @@ def do_GET(self):
459459

460460
return self.get_not_found()
461461

462+
def send_response(self, status_code, body, content_type='text/html'):
463+
super().send_response(status_code)
464+
self.send_header('Content-Type', content_type)
465+
self.end_headers()
466+
self.wfile.write(body.encode('utf-8'))
467+
462468
def get_index(self):
463469
message = render_to_string(
464470
'index.html',
465471
{
466472
'command': Command(server=self.server),
467473
}
468474
)
469-
self.send_response(200)
470-
self.end_headers()
471-
self.wfile.write(message.encode('utf-8'))
475+
self.send_response(200, message)
472476

473477
def get_setup_status(self):
474478
if self.server.run_result is not None:
@@ -478,9 +482,7 @@ def get_setup_status(self):
478482
else:
479483
status = 'not-running'
480484

481-
self.send_response(200)
482-
self.end_headers()
483-
self.wfile.write(status.encode('utf-8'))
485+
self.send_response(200, status, content_type='text/plain')
484486

485487
def get_finished_response(self):
486488
if self.server.run_result is None:
@@ -492,9 +494,7 @@ def get_finished_response(self):
492494
'command': Command(server=self.server, dev=self.server.run_result['dev']),
493495
}
494496
)
495-
self.send_response(200)
496-
self.end_headers()
497-
self.wfile.write(message.encode('utf-8'))
497+
self.send_response(200, message)
498498

499499
def get_static(self):
500500
file = root_dir / 'static' / PurePath(self.path).relative_to('/static')
@@ -508,8 +508,7 @@ def get_static(self):
508508
self.wfile.write(f.read())
509509

510510
def get_not_found(self):
511-
self.send_response(404)
512-
self.end_headers()
511+
self.send_response(404, 'Not found')
513512

514513
def do_POST(self):
515514
body = self.rfile.read(int(self.headers.get('Content-Length'))).decode('utf-8')
@@ -525,26 +524,22 @@ def post_index(self):
525524
try:
526525
command.handle()
527526

528-
self.send_response(200)
529-
self.end_headers()
530527
message = render_to_string(
531528
'setup_waiting.html',
532529
{
533530
'command': command,
534531
}
535532
)
533+
return self.send_response(200, message)
536534
except ValidationError as error:
537-
self.send_response(400)
538-
self.end_headers()
539535
message = render_to_string(
540536
'index.html',
541537
{
542538
'command': command,
543539
'form_error': error,
544540
}
545541
)
546-
547-
self.wfile.write(message.encode('utf-8'))
542+
self.send_response(400, message)
548543

549544
def log_request(self, code = '-', size = '-'):
550545
pass

0 commit comments

Comments
 (0)