Create an api gw response from a wsgi app and environ.
3813
def create_gw_response(app, wsgi_env):
    
    response = {}
    buf = []
    result = []

    def start_response(status, headers, exc_info=None):
        result[:] = [status, headers]
        return buf.append

    appr = app(wsgi_env, start_response)
    close_func = getattr(appr, 'close', None)
    try:
        buf.extend(list(appr))
    finally:
        close_func and close_func()

    response['body'] = ''.join(buf)
    response['statusCode'] = result[0].split(' ', 1)[0]
    response['headers'] = {}

    for k, v in result[1]:
        response['headers'][k] = v
    if 'Content-Length' not in response['headers']:
        response['headers']['Content-Length'] = str(len(response['body']))
    if 'Content-Type' not in response['headers']:
        response['headers']['Content-Type'] = 'text/plain'
    return response
176036
def _make_response(sijax_response):
    """"""
    from types import GeneratorType

    if isinstance(sijax_response, GeneratorType):
        # Streaming response using a generator (non-JSON response).
        # Upon returning a response, Flask would automatically destroy
        # the request data and uploaded files - done by `flask.ctx.RequestContext.auto_pop()`
        # We can't allow that, since the user-provided callback we're executing
        # from within the generator may want to access request data/files.
        # That's why we'll tell Flask to preserve the context and we'll clean up ourselves.

        request.environ['flask._preserve_context'] = True

        # Clean-up code taken from `flask.testing.TestingClient`
        def clean_up_context():
            top = _request_ctx_stack.top
            if top is not None and top.preserved:
                top.pop()

        # As per the WSGI specification, `close()` would be called on iterator responses.
        # Let's wrap the iterator in another one, which will forward that `close()` call to our clean-up callback.
        response = Response(ClosingIterator(sijax_response, clean_up_context), direct_passthrough=True)
    else:
        # Non-streaming response - a single JSON string
        response = Response(sijax_response)

    return response
1174538
def run_wsgi(settings_module):
    """"""
    global settingsmodule, DEBUG #pylint: disable=global-statement
    printdebug("Initialising WSGI service")

    globals()['settings'] = settings_module
    settingsmodule = settings_module.__name__

    try:
        if settings.DEBUG:
            DEBUG = True
            setdebug(True)
    except:
        pass

    test_version()
    if DEBUG:
        setlog(sys.stderr)
    else:
        setlog(None)
    try:
        if settings.LOGFILE:
            setlogfile(settings.LOGFILE)
    except:
        pass
    set_defaults() #host, port
    test_dirs()

    if DEBUG:
        from werkzeug.debug import DebuggedApplication
        return DebuggedApplication(CLAMService('wsgi').service.wsgi_app, True)
    else:
        return CLAMService('wsgi').service.wsgi_app
824551
def create_environment(self, environment):
        

        uri = 'api/v3/environment/'

        data = dict()
        data['environments'] = list()
        data['environments'].append(environment)

        return super(ApiEnvironment, self).post(uri, data)
937828
def handler(self, environ, start_response):
        """"""
        logger = logging.getLogger(__name__ + '.WSGIProxyApplication.handler')
        url = urlparse(reconstruct_url(environ, self.port))

        # Create connection object
        try:
            connection = self.connection_class(url.netloc)
            # Build path
            path = url.geturl().replace('%s://%s' % (url.scheme, url.netloc),
                                        '')
        except Exception:
            start_response('501 Gateway Error', [('Content-Type', 'text/html')])
            logger.exception('Could not Connect')
            yield '<H1>Could not connect</H1>'
            return

        # Read in request body if it exists
        body = length = None
        try:
            length = int(environ['CONTENT_LENGTH'])
        except (KeyError, ValueError):

            # This is a situation where client HTTP POST is missing content-length.
            # This is also situation where (WebOb?) may screw up encoding and isert extranous = in the body.
            # https://github.com/ipython/ipython/issues/8416
            if environ["REQUEST_METHOD"] == "POST":
                if environ.get("CONTENT_TYPE") == 'application/x-www-form-urlencoded; charset=UTF-8':
                    body = environ['wsgi.input'].read()
                    try:
                        body = unquote_plus(body.decode("utf-8"))

                        # Fix extra = at end of JSON payload
                        if body.startswith("{") and body.endswith("}="):
                            body = body[0:len(body) - 1]

                    except Exception as e:
                        logger.exception(e)
                        logger.error("Could not decode body: %s", body)

                    length = len(body)
        else:
            body = environ['wsgi.input'].read(length)

        # Build headers
        logger.debug('environ = %r', environ)
        headers = dict(
            (key, value)
            for key, value in (
                # This is a hacky way of getting the header names right
                (key[5:].lower().replace('_', '-'), value)
                for key, value in environ.items()
                # Keys that start with HTTP_ are all headers
                if key.startswith('HTTP_')
            )
            if not is_hop_by_hop(key)
        )

        # Handler headers that aren't HTTP_ in environ
        try:
            headers['content-type'] = environ['CONTENT_TYPE']
        except KeyError:
            pass

        # Add our host if one isn't defined
        if 'host' not in headers:
            headers['host'] = environ['SERVER_NAME']

        # Make the remote request
        try:

            logger.debug('%s %s %r',
                         environ['REQUEST_METHOD'], path, headers)
            connection.request(environ['REQUEST_METHOD'], path,
                               body=body, headers=headers)
        except Exception as e:
            # We need extra exception handling in the case the server fails
            # in mid connection, it's an edge case but I've seen it
            if isinstance(e, ConnectionRefusedError):
                # The notebook was shutdown by the user
                pass
            else:
                # This might be a genuine error
                logger.exception(e)

            start_response('501 Gateway Error', [('Content-Type', 'text/html')])
            yield '<H1>Could not proxy IPython Notebook running localhost:{}</H1>'.format(self.port).encode("utf-8")
            return

        try:
            response = connection.getresponse()
        except ConnectionResetError:
            # Notebook shutdown
            start_response('501 Gateway Error', [('Content-Type', 'text/html')])
            yield '<H1>Could not proxy IPython Notebook running localhost:{}</H1>'.format(self.port).encode("utf-8")
            return

        hopped_headers = response.getheaders()
        headers = [(key, value)
                   for key, value in hopped_headers
                   if not is_hop_by_hop(key)]

        start_response('{0.status} {0.reason}'.format(response), headers)
        while True:
            chunk = response.read(4096)
            if chunk:
                yield chunk
            else:
                break
871295
def lambda_handler(event, context=None, settings_name="zappa_settings"):  # NoQA
    
    time_start = datetime.datetime.now()

    # If in DEBUG mode, log all raw incoming events.
    if settings.DEBUG:
        logger.info('Zappa Event: {}'.format(event))

    # This is a normal HTTP request
    if event.get('method', None):

        # Create the environment for WSGI and handle the request
        environ = create_wsgi_request(event, script_name=settings.SCRIPT_NAME)

        # We are always on https on Lambda, so tell our wsgi app that.
        environ['HTTPS'] = 'on'
        environ['wsgi.url_scheme'] = 'https'

        wrap_me = get_wsgi_application()
        app = ZappaWSGIMiddleware(wrap_me)

        # Execute the application
        response = Response.from_app(app, environ)
        response.content = response.data

        # Prepare the special dictionary which will be returned to the API GW.
        returnme = {'Content': response.data}

        # Pack the WSGI response into our special dictionary.
        for (header_name, header_value) in response.headers:
            returnme[header_name] = header_value
        returnme['Status'] = response.status_code

        # To ensure correct status codes, we need to
        # pack the response as a deterministic B64 string and raise it
        # as an error to match our APIGW regex.
        # The DOCTYPE ensures that the page still renders in the browser.
        exception = None
        if response.status_code in ERROR_CODES:
            content = u"<!DOCTYPE html>" + unicode(response.status_code) + unicode('<meta charset="utf-8" />') + response.data.encode('utf-8')
            b64_content = base64.b64encode(content)
            exception = (b64_content)
        # Internal are changed to become relative redirects
        # so they still work for apps on raw APIGW and on a domain.
        elif 300 <= response.status_code < 400 and response.has_header('Location'):
            location = returnme['Location']
            location = '/' + location.replace("http://zappa/", "")
            exception = location

        # Calculate the total response time,
        # and log it in the Common Log format.
        time_end = datetime.datetime.now()
        delta = time_end - time_start
        response_time_ms = delta.total_seconds() * 1000
        common_log(environ, response, response_time=response_time_ms)

        # Finally, return the response to API Gateway.
        if exception:
            raise Exception(exception)
        else:
            return returnme

    # This is a management command invocation.
    elif event.get('command', None):
        from django.core import management

        # Couldn't figure out how to get the value into stdout with StringIO..
        # Read the log for now. :[]
        management.call_command(*event['command'].split(' '))
        return {}
    elif event.get('detail'):
        module, function = event['detail'].rsplit('.', 1)

        app_module = importlib.import_module(module)
        app_function = getattr(app_module, function)

        # Execute the function!
        app_function()
        return
    else:
        logger.error('Unhandled event: {}'.format(json.dumps(event)))
1145753
def app_create(self, data, **kwargs):
        ""
        api_path = "/api/v2/apps.json"
        return self.call(api_path, method="POST", data=data, **kwargs)
278098
def start_response(self, status, headers, exc_info = None):
    """"""
    if self.started_response:
      if not exc_info:
        raise AssertionError("WSGI start_response called a second "
                   "time with no exc_info.")
      else:
        try:
          raise exc_info[0], exc_info[1], exc_info[2]
        finally:
          exc_info = None
    self.started_response = True
    self.status = status
    self.outheaders.extend(headers)

    return self.write_buffer.write
558077
def wsgi_handler(func):
    

    @wraps(func)
    def inner(*args):
        self, environ, start_response, *args = args
        request = Request(environ)

        try:
            response = func(self, request)
        except Exception as err:
            raise
        else:
            return response(environ, start_response)

    return inner
1169456
def wsgi(self, environ, start_response):
        """"""
        request = Request(environ)
        ctx = Context(request)
        try:
            try:
                response = self(request, ctx)
                ctx._run_callbacks('finalize', (request, response))
                response = response.conditional_to(request)
            except HTTPException as e:
                response = e.response
            except Exception:
                self.handle_error(request, ctx)
                response = InternalServerError().response

            response.add_callback(lambda: ctx._run_callbacks('close'))
            return response(environ, start_response)
        finally:
            ctx._run_callbacks('teardown', log_errors=True)
121631
def get_environ(self):
        """"""
        req = self.req
        env_10 = super(Gateway_u0, self).get_environ()
        env = dict(map(self._decode_key, env_10.items()))

        # Request-URI
        enc = env.setdefault(six.u('wsgi.url_encoding'), six.u('utf-8'))
        try:
            env['PATH_INFO'] = req.path.decode(enc)
            env['QUERY_STRING'] = req.qs.decode(enc)
        except UnicodeDecodeError:
            # Fall back to latin 1 so apps can transcode if needed.
            env['wsgi.url_encoding'] = 'ISO-8859-1'
            env['PATH_INFO'] = env_10['PATH_INFO']
            env['QUERY_STRING'] = env_10['QUERY_STRING']

        env.update(map(self._decode_value, env.items()))

        return env
937080
def load_environment(global_conf, app_conf):
    """"""
    conf.update(strings.deep_decode(global_conf))
    conf.update(strings.deep_decode(app_conf))
    conf.update(conv.check(conv.struct(
        {
            'app_conf': conv.set_value(app_conf),
            'app_dir': conv.set_value(app_dir),
            'country_package': conv.pipe(
                conv.make_input_to_slug(separator = u'_'),
                conv.not_none,
                ),
            'debug': conv.pipe(conv.guess_bool, conv.default(False)),
            'global_conf': conv.set_value(global_conf),
            'i18n_dir': conv.default(os.path.join(app_dir, 'i18n')),
            'load_alert': conv.pipe(conv.guess_bool, conv.default(False)),
            'log_level': conv.pipe(
                conv.default('WARNING'),
                conv.function(lambda log_level: getattr(logging, log_level.upper())),
                ),
            'package_name': conv.default('openfisca-web-api'),
            'realm': conv.default(u'OpenFisca Web API'),
            'reforms': conv.ini_str_to_list,  # Another validation is done below.
            'extensions': conv.ini_str_to_list,
            },
        default = 'drop',
        ))(conf))

    # Configure logging.
    logging.basicConfig(level = conf['log_level'], stream = sys.stderr)

    errorware = conf.setdefault('errorware', {})
    errorware['debug'] = conf['debug']
    if not errorware['debug']:
        errorware['error_email'] = conf['email_to']
        errorware['error_log'] = conf.get('error_log', None)
        errorware['error_message'] = conf.get('error_message', 'An internal server error occurred')
        errorware['error_subject_prefix'] = conf.get('error_subject_prefix', 'OpenFisca Web API Error: ')
        errorware['from_address'] = conf['from_address']
        errorware['smtp_server'] = conf.get('smtp_server', 'localhost')
        errorware['show_exceptions_in_wsgi_errors'] = conf.get('show_exceptions_in_wsgi_errors', True)

    # Initialize tax-benefit system.
    country_package = importlib.import_module(conf['country_package'])
    tax_benefit_system = country_package.CountryTaxBenefitSystem()

    extensions = conf['extensions']
    if extensions is not None:
        for extension in extensions:
            tax_benefit_system.load_extension(extension)

    class Scenario(tax_benefit_system.Scenario):
        instance_and_error_couple_cache = {} if conf['debug'] else weakref.WeakValueDictionary()  # class attribute

        @classmethod
        def make_json_to_cached_or_new_instance(cls, ctx, repair, tax_benefit_system):
            def json_to_cached_or_new_instance(value, state = None):
                key = (unicode(ctx.lang), unicode(value), repair, tax_benefit_system)
                instance_and_error_couple = cls.instance_and_error_couple_cache.get(key)
                if instance_and_error_couple is None:
                    instance_and_error_couple = cls.make_json_to_instance(repair, tax_benefit_system)(
                        value, state = state or conv.default_state)
                    # Note: Call to ValueAndError() is needed below, otherwise it raises TypeError: cannot create
                    # weak reference to 'tuple' object.
                    cls.instance_and_error_couple_cache[key] = ValueAndError(instance_and_error_couple)
                return instance_and_error_couple

            return json_to_cached_or_new_instance

    tax_benefit_system.Scenario = Scenario

    model.tax_benefit_system = tax_benefit_system

    log.debug(u'Pre-fill tax and benefit system cache.')
    tax_benefit_system.prefill_cache()

    log.debug(u'Initialize reforms.')
    reforms = conv.check(
        conv.uniform_sequence(
            conv.module_and_function_str_to_function,
            )
        )(conf['reforms'])
    model.reforms = {}
    model.reformed_tbs = {}
    if reforms is not None:
        for reform in reforms:
            reformed_tbs = reform(tax_benefit_system)
            key = reformed_tbs.key
            full_key = reformed_tbs.full_key
            model.reforms[key] = reform
            model.reformed_tbs[full_key] = reformed_tbs

    log.debug(u'Cache default decomposition.')
    if tax_benefit_system.decomposition_file_path is not None:
        # Ignore the returned value, because we just want to pre-compute the cache.
        model.get_cached_or_new_decomposition_json(tax_benefit_system)

    log.debug(u'Initialize lib2to3-based input variables extractor.')

    global country_package_dir_path
    # - Do not use pkg_resources.get_distribution(conf["country_package"]).location
    #   because it returns a wrong path in virtualenvs (<venv>/lib versus <venv>/local/lib)
    # - Use os.path.abspath because when the web API is runned in development with "paster serve",
    #   __path__[0] == 'openfisca_france' for example. Then, get_relative_file_path won't be able
    #   to find the relative path of an already relative path.
    country_package_dir_path = os.path.abspath(country_package.__path__[0])

    global api_package_version
    api_package_version = pkg_resources.get_distribution('openfisca_web_api').version

    global country_package_version
    country_package_version = pkg_resources.get_distribution(conf["country_package"]).version

    log.debug(u'Cache legislation parmeters')
    legislation = tax_benefit_system.parameters
    parameters = []
    walk_legislation(
        legislation,
        descriptions = [],
        parameters = parameters,
        path_fragments = [],
        )
    model.parameters_cache = parameters

    if not conf['debug']:
        # Do this after tax_benefit_system.get_legislation().
        log.debug(u'Compute and cache compact legislation for each first day of month since at least 2 legal years.')
        today = periods.instant(datetime.date.today())
        first_day_of_year = today.offset('first-of', 'year')
        instant = first_day_of_year.offset(-2, 'year')
        two_years_later = first_day_of_year.offset(2, 'year')
        while instant < two_years_later:
            tax_benefit_system.get_parameters_at_instant(instant)
            instant = instant.offset(1, 'month')

    # Initialize multiprocessing and load_alert
    if conf['load_alert']:
        global cpu_count
        cpu_count = multiprocessing.cpu_count()

    if conf.get('tracker_url') and conf.get('tracker_idsite'):
        wsgihelpers.init_tracker(conf['tracker_url'], conf['tracker_idsite'])
962827
def call(env)
      @request = Rack::Request.new(env)
      xml_response = @xmlrpc_handler.process(request_body)
      [200, {'Content-Type' => 'text/xml'}, [xml_response]]
    end
403006
def wsgi_app(self, environ, start_response):
        

        path = environ['PATH_INFO']

        if re.match(self.path_pattern, path):
            ws = environ['wsgi.websocket']
            address = (environ.get('REMOTE_ADDR'), environ.get('REMOTE_PORT'))
            self._handle_stream(ws, address)
        else:
            if self.merge_wsgi_app:
                return self.merge_wsgi_app(environ, start_response)
            else:
                start_response("400 Bad Request", [])
                return ['400 Bad Request']
1357270
def fetch_internal(item, request):
    """"""
    # Break client dependence on Flask if internal fetches aren't being used.
    from flask import make_response
    from werkzeug.test import EnvironBuilder
    # Break circular dependencies.
    from dpxdt.server import app

    # Attempt to create a Flask environment from a urllib2.Request object.
    environ_base = {
        'REMOTE_ADDR': '127.0.0.1',
    }

    # The data object may be a generator from poster.multipart_encode, so we
    # need to convert that to raw bytes here. Unfortunately EnvironBuilder
    # only works with the whole request buffered in memory.
    data = request.get_data()
    if data and not isinstance(data, str):
        data = ''.join(list(data))

    builder = EnvironBuilder(
        path=request.get_selector(),
        base_url='%s://%s' % (request.get_type(), request.get_host()),
        method=request.get_method(),
        data=data,
        headers=request.header_items(),
        environ_base=environ_base)

    with app.request_context(builder.get_environ()):
        response = make_response(app.dispatch_request())
        LOGGER.info('"%s" %s via internal routing',
                    request.get_selector(), response.status_code)
        item.status_code = response.status_code
        item.content_type = response.mimetype
        if item.result_path:
            # TODO: Is there a better way to access the response stream?
            with open(item.result_path, 'wb') as result_file:
                for piece in response.iter_encoded():
                    result_file.write(piece)
        else:
            item.data = response.get_data()

    return item
576221
def bind(self, environ, app=None):
        
        if isinstance(environ, Request): # Recycle already parsed content
            for key in self.__dict__: #TODO: Test this
                setattr(self, key, getattr(environ, key))
            self.app = app
            return
        self._GET = self._POST = self._GETPOST = self._COOKIES = None
        self._body = self._header = None
        self.environ = environ
        self.app = app
        # These attributes are used anyway, so it is ok to compute them here
        self.path = '/' + environ.get('PATH_INFO', '/').lstrip('/')
        self.method = environ.get('REQUEST_METHOD', 'GET').upper()
1027781
def wsgi(self, environ, start_response):
        
        try:
            out = self._cast(self._handle(environ))
            # rfc2616 section 4.3
            if response._status_code in (100, 101, 204, 304)\
            or environ['REQUEST_METHOD'] == 'HEAD':
                if hasattr(out, 'close'): out.close()
                out = []
            start_response(response._status_line, response.headerlist)
            return out
        except (KeyboardInterrupt, SystemExit, MemoryError):
            raise
        except Exception:
            if not self.catchall: raise
            err = '<h1>Critical error while processing request: %s</h1>' \
                  % html_escape(environ.get('PATH_INFO', '/'))
            if DEBUG:
                err += '<h2>Error:</h2>\n<pre>\n%s\n</pre>\n' \
                       '<h2>Traceback:</h2>\n<pre>\n%s\n</pre>\n' \
                       % (html_escape(repr(_e())), html_escape(format_exc()))
            environ['wsgi.errors'].write(err)
            headers = [('Content-Type', 'text/html; charset=UTF-8')]
            start_response('500 INTERNAL SERVER ERROR', headers, sys.exc_info())
            return [tob(err)]