def send_content(self, connection, request_body): """ Completes the request headers and sends the request body of a JSON-RPC request over a HTTPConnection :param connection: An HTTPConnection object :param request_body: JSON-RPC request body """ # Convert the body first request_body = utils.to_bytes(request_body) # "static" headers connection.putheader("Content-Type", self._config.content_type) connection.putheader("Content-Length", str(len(request_body))) # Emit additional headers here in order not to override content-length additional_headers = self.emit_additional_headers(connection) # Add the user agent, if not overridden if "user-agent" not in additional_headers: connection.putheader("User-Agent", self.user_agent) connection.endheaders() if request_body: connection.send(request_body)
def do_POST(self): """ Handles POST requests """ if not self.is_rpc_path_valid(): self.report_404() return # Retrieve the configuration config = getattr(self.server, 'json_config', jsonrpclib.config.DEFAULT) try: # Read the request body max_chunk_size = 10 * 1024 * 1024 size_remaining = int(self.headers["content-length"]) chunks = [] while size_remaining: chunk_size = min(size_remaining, max_chunk_size) chunks.append(utils.from_bytes(self.rfile.read(chunk_size))) size_remaining -= len(chunks[-1]) data = ''.join(chunks) # Execute the method response = self.server._marshaled_dispatch(data) # No exception: send a 200 OK self.send_response(200) except: # Exception: send 500 Server Error self.send_response(500) err_lines = traceback.format_exc().splitlines() trace_string = '{0} | {1}'.format(err_lines[-3], err_lines[-1]) fault = jsonrpclib.Fault(-32603, 'Server error: {0}'.format(trace_string), config=config) response = fault.response() if response is None: # Avoid to send None response = '' # Convert the response to the valid string format response = utils.to_bytes(response) # Send it self.send_header("Content-type", config.content_type) self.send_header("Content-length", str(len(response))) self.end_headers() self.wfile.write(response) self.wfile.flush() self.connection.shutdown(1)
def do_POST(self): """ Handles POST requests """ if not self.is_rpc_path_valid(): self.report_404() return # Retrieve the configuration config = getattr(self.server, 'json_config', jsonrpclib.config.DEFAULT) try: # Read the request body max_chunk_size = 10 * 1024 * 1024 size_remaining = int(self.headers["content-length"]) chunks = [] while size_remaining: chunk_size = min(size_remaining, max_chunk_size) chunks.append(utils.from_bytes(self.rfile.read(chunk_size))) size_remaining -= len(chunks[-1]) data = ''.join(chunks) # Execute the method response = self.server._marshaled_dispatch(data) # No exception: send a 200 OK self.send_response(200) except: # Exception: send 500 Server Error self.send_response(500) err_lines = traceback.format_exc().splitlines() trace_string = '{0} | {1}'.format(err_lines[-3], err_lines[-1]) fault = jsonrpclib.Fault(-32603, 'Server error: {0}' .format(trace_string), config=config) response = fault.response() if response is None: # Avoid to send None response = '' # Convert the response to the valid string format response = utils.to_bytes(response) # Send it self.send_header("Content-type", config.content_type) self.send_header("Content-length", str(len(response))) self.end_headers() self.wfile.write(response) self.wfile.flush() self.connection.shutdown(1)
def send_content(self, connection, request_body): # Convert the body first request_body = utils.to_bytes(request_body) # "static" headers connection.putheader("Content-Type", self._config.content_type) connection.putheader("Content-Length", str(len(request_body))) # Emit additional headers here in order not to override content-length self.emit_additional_headers(connection) connection.endheaders() if request_body: connection.send(request_body)
def do_POST(self): """ Handles POST requests """ if not self.is_rpc_path_valid(): self.report_404() return # Retrieve the configuration config = getattr(self.server, 'json_config', jsonrpclib.config.DEFAULT) try: # Read the request body max_chunk_size = 10 * 1024 * 1024 size_remaining = int(self.headers["content-length"]) chunks = [] while size_remaining: chunk_size = min(size_remaining, max_chunk_size) raw_chunk = self.rfile.read(chunk_size) if not raw_chunk: break chunks.append(utils.from_bytes(raw_chunk)) size_remaining -= len(chunks[-1]) data = ''.join(chunks) try: # Decode content data = self.decode_request_content(data) if data is None: # Unknown encoding, response has been sent return except AttributeError: # Available since Python 2.7 pass # Execute the method response = self.server._marshaled_dispatch( data, getattr(self, '_dispatch', None), self.path) # No exception: send a 200 OK self.send_response(200) except: # Exception: send 500 Server Error self.send_response(500) err_lines = traceback.format_exc().splitlines() trace_string = '{0} | {1}'.format(err_lines[-3], err_lines[-1]) fault = jsonrpclib.Fault(-32603, 'Server error: {0}' .format(trace_string), config=config) _logger.exception("Server-side error: %s", fault) response = fault.response() if response is None: # Avoid to send None response = '' # Convert the response to the valid string format response = utils.to_bytes(response) # Send it self.send_header("Content-type", config.content_type) self.send_header("Content-length", str(len(response))) self.end_headers() if response: self.wfile.write(response)