def handle_rpc(self, request): initial_ctx = HttpMethodContext(self.http_transport, request, self.http_transport.app.out_protocol.mime_type) initial_ctx.in_string = [request.content.getvalue()] contexts = self.http_transport.generate_contexts(initial_ctx) p_ctx, others = contexts[0], contexts[1:] if p_ctx.in_error: return self.handle_error(p_ctx, others, p_ctx.in_error, request) else: self.http_transport.get_in_object(p_ctx) if p_ctx.in_error: return self.handle_error(p_ctx, others, p_ctx.in_error, request) else: self.http_transport.get_out_object(p_ctx) if p_ctx.out_error: return self.handle_error(p_ctx, others, p_ctx.out_error, request) self.http_transport.get_out_string(p_ctx) process_contexts(self.http_transport, others) def _cb_request_finished(request): request.finish() producer = _Producer(p_ctx.out_string, request) producer.deferred.addErrback(err).addCallback(_cb_request_finished) request.registerProducer(producer, False) return NOT_DONE_YET
def serve_forever(self): """Runs the ZeroMQ server.""" while True: error = None initial_ctx = ZmqMethodContext(self) initial_ctx.in_string = [self.zmq_socket.recv()] contexts = self.generate_contexts(initial_ctx) p_ctx, others = contexts[0], contexts[1:] if p_ctx.in_error: p_ctx.out_object = p_ctx.in_error error = p_ctx.in_error else: self.get_in_object(p_ctx) if p_ctx.in_error: p_ctx.out_object = p_ctx.in_error error = p_ctx.in_error else: self.get_out_object(p_ctx) if p_ctx.out_error: p_ctx.out_object = p_ctx.out_error error = p_ctx.out_error self.get_out_string(p_ctx) process_contexts(self, others, error) self.zmq_socket.send(''.join(p_ctx.out_string))
def handle_error(self, p_ctx, others, error, request): resp_code = self.http_transport.app.out_protocol \ .fault_to_http_response_code(error) request.setResponseCode(int(resp_code[:3])) p_ctx.out_object = error self.http_transport.get_out_string(p_ctx) process_contexts(self.http_transport, others, error=error) return ''.join(p_ctx.out_string)
def handle_error(self, p_ctx, others, error, start_response): if p_ctx.transport.resp_code is None: p_ctx.transport.resp_code = \ self.app.out_protocol.fault_to_http_response_code(error) self.get_out_string(p_ctx) p_ctx.out_string = [''.join(p_ctx.out_string)] p_ctx.transport.resp_headers['Content-Length'] = str(len(p_ctx.out_string[0])) self.event_manager.fire_event('wsgi_exception', p_ctx) start_response(p_ctx.transport.resp_code, p_ctx.transport.resp_headers.items()) try: process_contexts(self, others, p_ctx, error=error) except Exception,e: # Report but ignore any exceptions from auxiliary methods. logger.exception(e)
def handle_rpc(self, req_env, start_response): initial_ctx = WsgiMethodContext(self, req_env, self.app.out_protocol.mime_type) # implementation hook self.event_manager.fire_event('wsgi_call', initial_ctx) initial_ctx.in_string, in_string_charset = \ self.__reconstruct_wsgi_request(req_env) contexts = self.generate_contexts(initial_ctx, in_string_charset) p_ctx, others = contexts[0], contexts[1:] if p_ctx.in_error: return self.handle_error(p_ctx, others, p_ctx.in_error, start_response) self.get_in_object(p_ctx) if p_ctx.in_error: logger.error(p_ctx.in_error) return self.handle_error(p_ctx, others, p_ctx.in_error, start_response) self.get_out_object(p_ctx) if p_ctx.out_error: return self.handle_error(p_ctx, others, p_ctx.out_error, start_response) if p_ctx.transport.resp_code is None: p_ctx.transport.resp_code = HTTP_200 self.get_out_string(p_ctx) if p_ctx.descriptor and p_ctx.descriptor.mtom: # when there is more than one return type, the result is # encapsulated inside a list. when there's just one, the result # is returned in a non-encapsulated form. the apply_mtom always # expects the objects to be inside an iterable, hence the # following test. out_type_info = p_ctx.descriptor.out_message._type_info if len(out_type_info) == 1: out_object = [out_object] p_ctx.transport.resp_headers, p_ctx.out_string = apply_mtom( p_ctx.transport.resp_headers, p_ctx.out_string, p_ctx.descriptor.out_message._type_info.values(), out_object ) # implementation hook self.event_manager.fire_event('wsgi_return', p_ctx) # the client has not set a content-length, so we delete it as the input # is just an iterable. if self.chunked: if 'Content-Length' in p_ctx.transport.resp_headers: del p_ctx.transport.resp_headers['Content-Length'] else: p_ctx.out_string = [''.join(p_ctx.out_string)] # if the out_string is a generator function, this hack lets the user # code run until first yield, which lets it set response headers and # whatnot beforce calling start_response. yes it causes an additional # copy of the first fragment of the response to be made, but if you know # a better way of having generator functions execute until first yield, # just let us know. try: len(p_ctx.out_string) # iterator? # nope p_ctx.transport.resp_headers['Content-Length'] = \ str(sum([len(a) for a in p_ctx.out_string])) start_response(p_ctx.transport.resp_code, p_ctx.transport.resp_headers.items()) retval = p_ctx.out_string except TypeError: retval_iter = iter(p_ctx.out_string) retval = retval_iter.next() start_response(p_ctx.transport.resp_code, p_ctx.transport.resp_headers.items()) retval = itertools.chain([retval], retval_iter) try: process_contexts(self, others, p_ctx, error=None) except Exception, e: # Report but ignore any exceptions from auxiliary methods. logger.exception(e)