MCPcopy Create free account
hub / github.com/emscripten-core/emscripten / do_POST

Method do_POST

emrun.py:667–745  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

665 sys.stderr.write(msg)
666
667 def do_POST(self): # # noqa: DC04
668 global page_exit_code, have_received_messages
669
670 (_, _, path, query, _) = urlsplit(self.path)
671 logv(f'POST: "{self.path}" (path: "{path}", query: "{query}")')
672 if query.startswith('file='):
673 # Binary file dump/upload handling. Requests to
674 # "stdio.html?file=filename" will write binary data to the given file.
675 data = self.rfile.read(int(self.headers['Content-Length']))
676 filename = unquote_u(query[len('file='):])
677 filename = os.path.join(emrun_options.dump_out_directory, os.path.normpath(filename))
678 try:
679 os.makedirs(os.path.dirname(filename))
680 except OSError:
681 pass
682 with open(filename, 'wb') as fh:
683 fh.write(data)
684 logi(f'Wrote {len(data)} bytes to file "{filename}".')
685 have_received_messages = True
686 elif path == '/system_info':
687 system_info = json.loads(get_system_info(format_json=True))
688 try:
689 browser_info = json.loads(get_browser_info(browser_exe, format_json=True))
690 except ValueError:
691 browser_info = ''
692 data = {'system': system_info, 'browser': browser_info}
693 self.send_response(200)
694 self.send_header('Content-type', 'application/json')
695 self.send_header('Cache-Control', 'no-cache, must-revalidate')
696 self.send_header('Connection', 'close')
697 self.send_header('Expires', '-1')
698 self.end_headers()
699 self.wfile.write(json.dumps(data))
700 return
701 else:
702 data = self.rfile.read(int(self.headers['Content-Length']))
703 if str is not bytes and isinstance(data, bytes):
704 data = data.decode('utf-8')
705 data = data.replace("+", " ")
706 data = unquote_u(data)
707
708 if data == '^pageload^': # Browser is just notifying that it has successfully launched the page.
709 have_received_messages = True
710 elif data.startswith('^exit^'):
711 if not emrun_options.serve_after_exit:
712 page_exit_code = int(data[6:])
713 logv(f'Web page has quit with a call to exit() with return code ${page_exit_code}. Shutting down web server. Pass --serve-after-exit to keep serving even after the page terminates with exit().')
714 # Set server socket to nonblocking on shutdown to avoid sporadic deadlocks
715 self.server.socket.setblocking(False)
716 self.server.shutdown()
717 return
718 else:
719 # The user page sent a message with POST. Parse the message and log it to stdout/stderr.
720 is_stdout = False
721 is_stderr = False
722 seq_num = -1
723 # The html shell is expected to send messages of form ^out^(number)^(message) or ^err^(number)^(message).
724 if data.startswith('^err^'):

Callers

nothing calls this directly

Calls 13

logvFunction · 0.85
unquote_uFunction · 0.85
logiFunction · 0.85
get_system_infoFunction · 0.85
get_browser_infoFunction · 0.85
shutdownMethod · 0.80
openFunction · 0.50
readMethod · 0.45
joinMethod · 0.45
writeMethod · 0.45
end_headersMethod · 0.45

Tested by

no test coverage detected