コード例 #1
0
    def __init__(self, accept):
        from sys import exc_info
        from ZPublisher.Publish import publish_module
        from ZPublisher.WSGIPublisher import publish_module as publish_wsgi
        while 1:
            try:
                name, a, b = accept()
                if name == "Zope2":
                    try:
                        publish_module(name, request=a, response=b)
                    finally:
                        b._finish()
                        a = b = None

                elif name == "Zope2WSGI":
                    try:
                        res = publish_wsgi(a, b)
                        for r in res:
                            a['wsgi.output'].write(r)
                    finally:
                        # TODO: Support keeping connections open.
                        a['wsgi.output']._close = 1
                        a['wsgi.output'].close()
            except:
                LOG.error('exception caught', exc_info=True)
コード例 #2
0
 def __init__(self, accept):
     from ZPublisher.Publish import publish_module
     from ZPublisher.WSGIPublisher import publish_module as publish_wsgi
     while 1:
         try:
             name, a, b = accept()
             if name == "Zope2":
                 try:
                     publish_module(
                         name,
                         request=a,
                         response=b)
                 finally:
                     b._finish()
                     a = b = None
             elif name == "Zope2WSGI":
                 try:
                     res = publish_wsgi(a, b)
                     for r in res:
                         a['wsgi.output'].write(r)
                 finally:
                     # TODO: Support keeping connections open.
                     a['wsgi.output']._close = 1
                     a['wsgi.output'].close()
         except Exception:
             LOG.error('exception caught', exc_info=True)
コード例 #3
0
ファイル: functional.py プロジェクト: vedantc98/Plone-test
    def publish(self,
                path,
                basic=None,
                env=None,
                extra=None,
                request_method='GET',
                stdin=None,
                handle_errors=True):
        '''Publishes the object at 'path' returning a response object.'''

        from StringIO import StringIO
        from ZPublisher.Request import Request
        from ZPublisher.Response import Response
        from ZPublisher.Publish import publish_module

        # Commit the sandbox for good measure
        transaction.commit()

        if env is None:
            env = {}
        if extra is None:
            extra = {}

        request = self.app.REQUEST

        env['SERVER_NAME'] = request['SERVER_NAME']
        env['SERVER_PORT'] = request['SERVER_PORT']
        env['REQUEST_METHOD'] = request_method

        p = path.split('?')
        if len(p) == 1:
            env['PATH_INFO'] = p[0]
        elif len(p) == 2:
            [env['PATH_INFO'], env['QUERY_STRING']] = p
        else:
            raise TypeError, ''

        if basic:
            env['HTTP_AUTHORIZATION'] = "Basic %s" % base64.encodestring(basic)

        if stdin is None:
            stdin = StringIO()

        outstream = StringIO()
        response = Response(stdout=outstream, stderr=sys.stderr)
        request = Request(stdin, env, response)
        for k, v in extra.items():
            request[k] = v

        publish_module(
            'Zope2',
            debug=not handle_errors,
            request=request,
            response=response,
        )

        return ResponseWrapper(response, outstream, path)
コード例 #4
0
ファイル: testing.py プロジェクト: rnunez80/castle.cms
    def publish(self,
                path,
                basic=None,
                env=None,
                extra=None,
                request_method='GET',
                stdin=None,
                handle_errors=True):
        """
        Mostly pulled from Testing.functional
        """
        from ZPublisher.Request import Request
        from ZPublisher.Response import Response
        from ZPublisher.Publish import publish_module

        transaction.commit()

        if env is None:
            env = {}
        if extra is None:
            extra = {}

        env['SERVER_NAME'] = self.request['SERVER_NAME']
        env['SERVER_PORT'] = self.request['SERVER_PORT']
        env['REQUEST_METHOD'] = request_method

        p = path.split('?')
        if len(p) == 1:
            env['PATH_INFO'] = p[0]
        elif len(p) == 2:
            [env['PATH_INFO'], env['QUERY_STRING']] = p
        else:
            raise TypeError('')

        if basic:
            env['HTTP_AUTHORIZATION'] = "Basic %s" % base64.encodestring(basic)

        if stdin is None:
            stdin = StringIO()

        outstream = StringIO()
        response = Response(stdout=outstream, stderr=sys.stderr)
        request = Request(stdin, env, response)
        if extra:
            # Needed on Plone 3 when adding things to the path in a querystring
            # is not enough.
            for key, value in extra.items():
                request[key] = value

        publish_module('Zope2',
                       debug=not handle_errors,
                       request=request,
                       response=response)

        return ResponseWrapper(response, outstream, path)
コード例 #5
0
    def publish(
        self,
        path,
        basic=None,
        env=None,
        extra=None,
        request_method="GET",
        stdin=None,
        handle_errors=True,
    ):
        """
        Mostly pulled from Testing.functional
        """
        from ZPublisher.Request import Request
        from ZPublisher.Response import Response

        # Note: the next import fail in Python 3, because it needs ZServer.
        from ZPublisher.Publish import publish_module

        transaction.commit()

        if env is None:
            env = {}

        env["SERVER_NAME"] = self.request["SERVER_NAME"]
        env["SERVER_PORT"] = self.request["SERVER_PORT"]
        env["REQUEST_METHOD"] = request_method

        p = path.split("?")
        if len(p) == 1:
            env["PATH_INFO"] = p[0]
        elif len(p) == 2:
            [env["PATH_INFO"], env["QUERY_STRING"]] = p
        else:
            raise TypeError("")

        if basic:
            env["HTTP_AUTHORIZATION"] = "Basic %s" % base64.encodestring(basic)

        if stdin is None:
            stdin = BytesIO()

        outstream = BytesIO()
        response = Response(stdout=outstream, stderr=sys.stderr)
        request = Request(stdin, env, response)

        publish_module("Zope2",
                       debug=not handle_errors,
                       request=request,
                       response=response)

        return ResponseWrapper(response, outstream, path)
コード例 #6
0
    def publish(self, path, basic=None, env=None, extra=None,
                request_method='GET', stdin=None, handle_errors=True):
        '''Publishes the object at 'path' returning a response object.'''

        from StringIO import StringIO
        from ZPublisher.Request import Request
        from ZPublisher.Response import Response
        from ZPublisher.Publish import publish_module

        # Commit the sandbox for good measure
        transaction.commit()

        if env is None:
            env = {}
        if extra is None:
            extra = {}

        request = self.app.REQUEST

        env['SERVER_NAME'] = request['SERVER_NAME']
        env['SERVER_PORT'] = request['SERVER_PORT']
        env['REQUEST_METHOD'] = request_method

        p = path.split('?')
        if len(p) == 1:
            env['PATH_INFO'] = p[0]
        elif len(p) == 2:
            [env['PATH_INFO'], env['QUERY_STRING']] = p
        else:
            raise TypeError, ''

        if basic:
            env['HTTP_AUTHORIZATION'] = "Basic %s" % base64.encodestring(basic)

        if stdin is None:
            stdin = StringIO()

        outstream = StringIO()
        response = Response(stdout=outstream, stderr=sys.stderr)
        request = Request(stdin, env, response)
        for k, v in extra.items():
            request[k] = v

        publish_module('Zope2',
                       debug=not handle_errors,
                       request=request,
                       response=response,
                      )

        return ResponseWrapper(response, outstream, path)
コード例 #7
0
ファイル: ZCGI.py プロジェクト: MinasAbrahamyan/bobomail
if variables.has_key('IIS_HACK') and variables['IIS_HACK']:
    script = filter(None,split(strip(os.environ['SCRIPT_NAME']),'/'))
    path = filter(None,split(strip(os.environ['PATH_INFO']),'/'))
    os.environ['PATH_INFO'] = join(path[len(script):],'/')

# get publisher
try:
    from ZPublisher.Publish import publish_module
except ImportError:
    try:
        from cgi_module_publisher import publish_module
        import CGIResponse
    except ImportError:
        print 'Content-type: text/html\n\n' \
            '<html><h1>An error occurred</h1>\n' \
            '<p>Python cannot publish your module because it ' \
            'cannot find the Zope Publisher package.</p>\n' \
            '<p>Either move the ZPublisher package to somewhere ' \
            'in your PYTHON PATH or fix your PYTHON PATH ' \
            'with the INCLUDE_PATHS directive.<p>\n' \
            '<p>Your PYTHON PATH is currently set to: %s </p>\n' \
            '</html>'  % sys.path
        sys.exit()

# set environment
for k,v in variables.items():
    if upper(k)==k and not os.environ.has_key(k):
        os.environ[k]=str(v)
    
publish_module(variables['PUBLISHED_MODULE'])
コード例 #8
0
def http(request_string, handle_errors=True):
    """Execute an HTTP request string via the publisher

    This is used for HTTP doc tests.
    """
    import urllib
    import rfc822
    from cStringIO import StringIO
    from ZPublisher.Response import Response
    from ZPublisher.Publish import publish_module

    # Commit work done by previous python code.
    transaction.commit()

    # Discard leading white space to make call layout simpler
    request_string = request_string.lstrip()

    # Split off and parse the command line
    l = request_string.find('\n')
    command_line = request_string[:l].rstrip()
    request_string = request_string[l + 1:]
    method, path, protocol = command_line.split()
    path = urllib.unquote(path)

    instream = StringIO(request_string)

    env = {"HTTP_HOST": 'localhost',
           "REQUEST_METHOD": method,
           "SERVER_PROTOCOL": protocol,
           }

    p = path.split('?', 1)
    if len(p) == 1:
        env['PATH_INFO'] = p[0]
    elif len(p) == 2:
        [env['PATH_INFO'], env['QUERY_STRING']] = p
    else:
        raise TypeError('')

    header_output = functional.HTTPHeaderOutput(
        protocol, ('x-content-type-warning', 'x-powered-by',
                   'bobo-exception-type', 'bobo-exception-file',
                   'bobo-exception-value', 'bobo-exception-line'))

    headers = [functional.split_header(header)
               for header in rfc822.Message(instream).headers]

    # Store request body without headers
    instream = StringIO(instream.read())

    for name, value in headers:
        name = ('_'.join(name.upper().split('-')))
        if name not in ('CONTENT_TYPE', 'CONTENT_LENGTH'):
            name = 'HTTP_' + name
        env[name] = value.rstrip()

    if 'HTTP_AUTHORIZATION' in env:
        env['HTTP_AUTHORIZATION'] = functional.auth_header(
            env['HTTP_AUTHORIZATION'])

    outstream = StringIO()
    response = Response(stdout=outstream, stderr=sys.stderr)

    publish_module('Zope2',
                   response=response,
                   stdin=instream,
                   environ=env,
                   debug=not handle_errors,
                  )
    header_output.setResponseStatus(response.getStatus(), response.errmsg)
    header_output.setResponseHeaders(response.headers)
    header_output.headersl.extend(response._cookie_list())
    header_output.appendResponseHeaders(response.accumulated_headers)

    functional.sync()

    return functional.DocResponseWrapper(
        response, outstream, path, header_output)
コード例 #9
0
def http(request_string, handle_errors=True):
    """Execute an HTTP request string via the publisher

    This is used for HTTP doc tests.
    """
    import urllib
    import rfc822
    from cStringIO import StringIO
    from ZPublisher.Response import Response
    from ZPublisher.Publish import publish_module

    # Commit work done by previous python code.
    transaction.commit()

    # Discard leading white space to make call layout simpler
    request_string = request_string.lstrip()

    # Split off and parse the command line
    l = request_string.find('\n')
    command_line = request_string[:l].rstrip()
    request_string = request_string[l + 1:]
    method, path, protocol = command_line.split()
    path = urllib.unquote(path)

    instream = StringIO(request_string)

    env = {
        "HTTP_HOST": 'localhost',
        "HTTP_REFERER": 'localhost',
        "REQUEST_METHOD": method,
        "SERVER_PROTOCOL": protocol,
    }

    p = path.split('?', 1)
    if len(p) == 1:
        env['PATH_INFO'] = p[0]
    elif len(p) == 2:
        [env['PATH_INFO'], env['QUERY_STRING']] = p
    else:
        raise TypeError, ''

    header_output = HTTPHeaderOutput(
        protocol,
        ('x-content-type-warning', 'x-powered-by', 'bobo-exception-type',
         'bobo-exception-file', 'bobo-exception-value', 'bobo-exception-line'))

    headers = [
        split_header(header) for header in rfc822.Message(instream).headers
    ]

    # Store request body without headers
    instream = StringIO(instream.read())

    for name, value in headers:
        name = ('_'.join(name.upper().split('-')))
        if name not in ('CONTENT_TYPE', 'CONTENT_LENGTH'):
            name = 'HTTP_' + name
        env[name] = value.rstrip()

    if env.has_key('HTTP_AUTHORIZATION'):
        env['HTTP_AUTHORIZATION'] = auth_header(env['HTTP_AUTHORIZATION'])

    outstream = StringIO()
    response = Response(stdout=outstream, stderr=sys.stderr)

    publish_module(
        'Zope2',
        response=response,
        stdin=instream,
        environ=env,
        debug=not handle_errors,
    )
    header_output.setResponseStatus(response.getStatus(), response.errmsg)
    header_output.setResponseHeaders(response.headers)
    header_output.headersl.extend(response._cookie_list())
    header_output.appendResponseHeaders(response.accumulated_headers)

    sync()

    return DocResponseWrapper(response, outstream, path, header_output)