def _cb_deferred(ret, request, p_ctx, others, resource, cb=True): resp_code = p_ctx.transport.resp_code # If user code set its own response code, don't touch it. if resp_code is None: resp_code = HTTP_200 request.setResponseCode(int(resp_code[:3])) _set_response_headers(request, p_ctx.transport.resp_headers) om = p_ctx.descriptor.out_message if cb and ((not issubclass(om, ComplexModelBase)) or len(om._type_info) <= 1): p_ctx.out_object = [ret] else: p_ctx.out_object = ret retval = None if isinstance(ret, PushBase): retval = _init_push(ret, request, p_ctx, others, resource) else: resource.http_transport.get_out_string(p_ctx) producer = Producer(p_ctx.out_string, request) producer.deferred.addCallback(_cb_request_finished, request, p_ctx) producer.deferred.addErrback(_eb_request_finished, request, p_ctx) request.registerProducer(producer, False) process_contexts(resource.http_transport, others, p_ctx) return retval
def _cb_deferred(retval, request, p_ctx, others, resource, cb=True): if cb and len(p_ctx.descriptor.out_message._type_info) <= 1: p_ctx.out_object = [retval] else: p_ctx.out_object = retval resource.http_transport.get_out_string(p_ctx) process_contexts(resource.http_transport, others, p_ctx) producer = Producer(p_ctx.out_string, request) producer.deferred.addCallback(_cb_request_finished, p_ctx, others, resource) producer.deferred.addErrback(_eb_request_finished, p_ctx, others, resource) request.registerProducer(producer, False)
def _cb_deferred(ret, request, p_ctx, others, resource, cb=True): resp_code = p_ctx.transport.resp_code # If user code set its own response code, don't touch it. if resp_code is None: resp_code = HTTP_200 request.setResponseCode(int(resp_code[:3])) _set_response_headers(request, p_ctx.transport.resp_headers) om = p_ctx.descriptor.out_message single_class = None if cb: if p_ctx.descriptor.body_style in (BODY_STYLE_BARE, BODY_STYLE_EMPTY): p_ctx.out_object = [ret] elif (not issubclass(om, ComplexModelBase)) or len(om._type_info) <= 1: p_ctx.out_object = [ret] if len(om._type_info) == 1: single_class, = om._type_info.values() else: p_ctx.out_object = ret else: p_ctx.out_object = ret retval = NOT_DONE_YET p_ctx.out_stream = request if isinstance(ret, PushBase): retval = _init_push(ret, request, p_ctx, others, resource) elif ((isclass(om) and issubclass(om, File)) or (isclass(single_class) and issubclass(single_class, File))) and \ isinstance(p_ctx.out_protocol, HttpRpc) and \ getattr(ret, 'abspath', None) is not None: file = static.File(ret.abspath, defaultType=str(ret.type) or 'application/octet-stream') retval = _render_file(file, request) if retval != NOT_DONE_YET and cb: request.write(retval) request.finish() p_ctx.close() else: def _close_only_context(ret): p_ctx.close() request.notifyFinish().addCallback(_close_only_context) request.notifyFinish().addErrback(_eb_request_finished, request, p_ctx) else: resource.http_transport.get_out_string(p_ctx) producer = Producer(p_ctx.out_string, request) producer.deferred.addCallback(_cb_request_finished, request, p_ctx) producer.deferred.addErrback(_eb_request_finished, request, p_ctx) request.registerProducer(producer, False) process_contexts(resource.http_transport, others, p_ctx) return retval
def _cb_deferred(ret, request, p_ctx, others, resource, cb=True): ### set response headers resp_code = p_ctx.transport.resp_code # If user code set its own response code, don't touch it. if resp_code is None: resp_code = HTTP_200 request.setResponseCode(int(resp_code[:3])) _set_response_headers(request, p_ctx.transport.resp_headers) ### normalize response data om = p_ctx.descriptor.out_message single_class = None if cb: if p_ctx.descriptor.is_out_bare(): p_ctx.out_object = [ret] elif (not issubclass(om, ComplexModelBase)) or len(om._type_info) <= 1: p_ctx.out_object = [ret] if len(om._type_info) == 1: single_class, = om._type_info.values() else: p_ctx.out_object = ret else: p_ctx.out_object = ret ### start response retval = NOT_DONE_YET if isinstance(ret, PushBase): resource.http_transport.init_root_push(ret, p_ctx, others) elif ((isclass(om) and issubclass(om, File)) or (isclass(single_class) and issubclass(single_class, File))) and \ isinstance(p_ctx.out_protocol, HttpRpc) and \ getattr(ret, 'abspath', None) is not None: file = static.File(ret.abspath, defaultType=str(ret.type) or 'application/octet-stream') retval = _render_file(file, request) if retval != NOT_DONE_YET and cb: request.write(retval) request.finish() p_ctx.close() else: def _close_only_context(ret): p_ctx.close() request.notifyFinish() \ .addCallback(_close_only_context) \ .addErrback(_eb_request_finished, request, p_ctx) \ .addErrback(log_and_let_go, logger) else: ret = resource.http_transport.get_out_string(p_ctx) if not isinstance(ret, Deferred): producer = Producer(p_ctx.out_string, request) producer.deferred \ .addCallback(_cb_request_finished, request, p_ctx) \ .addErrback(_eb_request_finished, request, p_ctx) \ .addErrback(log_and_let_go, logger) try: request.registerProducer(producer, False) except Exception as e: logger_server.exception(e) try: _eb_deferred(Failure(), request, p_ctx, others, resource) except Exception as e: logger_server.exception(e) raise else: def _cb(ret): if isinstance(ret, Deferred): return ret \ .addCallback(_cb) \ .addErrback(_eb_request_finished, request, p_ctx) \ .addErrback(log_and_let_go, logger) else: return _cb_request_finished(ret, request, p_ctx) ret \ .addCallback(_cb) \ .addErrback(_eb_request_finished, request, p_ctx) \ .addErrback(log_and_let_go, logger) process_contexts(resource.http_transport, others, p_ctx) return retval