Example #1
0
 def flush(self):
     result = ASStreamOut.flush(self)
     if result: # a true return value means we can send
         reslen = len(self._buffer)
         self._file.write(self._buffer)
         self._file.flush()
         sent = reslen
         self.pop(sent)
Example #2
0
	def doTransaction(self, env, myInput):
		streamOut = ASStreamOut()
		requestDict = {
			'format': 'CGI',
			'time': time.time(),
			'environ': env,
			'input': StringIO(myInput),
			}
		self.dispatchRawRequest(requestDict, streamOut)
		self.processResponse(streamOut._buffer)
		self._sock.shutdown(2)
 def flush(self):
     debug = 0
     result = ASStreamOut.flush(self)
     if result:  ##a true return value means we can send
         reslen = len(self._buffer)
         sent = 0
         while sent < reslen:
             try:
                 sent = sent + self._socket.send(
                     self._buffer[sent:sent + 8192])
             except socket.error, e:
                 if e[0] == errno.EPIPE:  #broken pipe
                     pass
                 else:
                     print "StreamOut Error: ", e
                 break
         self.pop(sent)
 def __init__(self, sock):
     ASStreamOut.__init__(self)
     self._socket = sock
Example #5
0
    def run(self):

        timestamp = time.time() # start time

        # to capture the console output of the application
        stdout, sys.stdout = sys.stdout, StringIO()

        try:

            # MS Windows: no special translation of end-of-lines
            if os.name == 'nt':
                import msvcrt
                msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)

            requestDict = dict(
                format='CGI', time=timestamp,
                # ce: a little tricky. We use marshal which only works
                # on built-in types, so we need environ's dictionary:
                environ=os.environ.data, input=sys.stdin)

            from WebKit import Profiler
            Profiler.startTime = time.time()

            print 'ONE SHOT MODE\n'

            from WebKit.OneShotAppServer import OneShotAppServer
            appSvr = OneShotAppServer(self._webKitDir)

            # It is important to call transaction.die() after using it, rather than
            # just letting it fall out of scope, to avoid circular references
            from WebKit.ASStreamOut import ASStreamOut
            rs = ASStreamOut()
            transaction = appSvr.dispatchRawRequest(requestDict, rs)
            rs.close()
            if transaction:
                transaction.die()
                del transaction

            appSvr.shutDown()
            appSvr = None

            print "AppServer run time %.2f seconds" % (time.time() - Profiler.startTime)

            sys.stdout = stdout

            # MS Windows: no special translation of end-of-lines
            if os.name == 'nt':
                import msvcrt
                msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)

            response = rs._buffer
            if response:
                sys.stdout.write(response)
            else:
                sys.stdout.write('''Content-Type: text/html\n
<html><head><title>WebKit CGI Error</title><body>
<h3>WebKit CGI Error</h3>
<h4>No response from application server</h4>
</body></html>\n''')

            if self.setting('ShowConsole'):
                # show the contents of the console,
                # but only if we are serving up an HTML file
                endheaders = response.find("\r\n\r\n")
                if endheaders < 0:
                    endheaders = response.find("\n\n")
                if not endheaders:
                    print "No Headers Found"
                    return
                headers = response[:endheaders].split("\n")
                entries = []
                for header in headers:
                    if header:
                        entries.append(header.split(":", 1))
                found = False
                for name, value in entries:
                    if name.lower() == 'content-type':
                        found = True
                        break
                if found and value.strip().lower() == 'text/html':
                    self.showConsole(_console.getvalue())
        except:
            import traceback
            sys.stderr.write('[%s] [error] WebKit.OneShotAdapter:'
                ' Error while responding to request (unknown)\n'
                % (time.asctime(time.localtime(time.time()))))
            sys.stderr.write('Python exception:\n')
            traceback.print_exc(file=sys.stderr)
            sys.stdout = stdout
            output = ''.join(traceback.format_exception(*sys.exc_info()))
            output = htmlEncode(output)
            sys.stdout.write('''Content-Type: text/html\n
<html><head><title>WebKit CGI Error</title><body>
<h3>WebKit CGI Error</h3>
<pre>%s</pre>
</body></html>\n''' % output)
Example #6
0
    def run(self):

        try:
            #			myInput = ''
            #			if os.environ.has_key('CONTENT_LENGTH'):
            #				length = int(os.environ['CONTENT_LENGTH'])
            #				if length:
            #					myInput = sys.stdin.read(length)
            #myInput = sys.stdin.read()

            # MS Windows: no special translation of end-of-lines
            if os.name == 'nt':
                import msvcrt
                msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)

            dict = {
                'format': 'CGI',
                'time': _timestamp,
                'environ': os.environ.
                data,  # ce: a little tricky. We use marshal which only works on built-in types, so we need environ's dictionary
                #				'input':   myInput,
                'input': sys.stdin,
            }

            from WebKit import Profiler
            Profiler.startTime = time.time()

            print 'ONE SHOT MODE\n'

            from WebKit.OneShotAppServer import OneShotAppServer
            appSvr = OneShotAppServer(self._webKitDir)

            # It is important to call transaction.die() after using it, rather than just
            # letting it fall out of scope, to avoid circular references
            from WebKit.ASStreamOut import ASStreamOut
            rs = ASStreamOut()
            transaction = appSvr.dispatchRawRequest(dict, rs)
            rs.close()
            transaction.die()
            del transaction

            appSvr.shutDown()
            appSvr = None

            print "AppServer Run Time %.2f seconds" % (time.time() -
                                                       Profiler.startTime)
            sys.stdout = _real_stdout

            # MS Windows: no special translation of end-of-lines
            if os.name == 'nt':
                import msvcrt
                msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)

            write = sys.stdout.write
            write(rs._buffer)

            if self.setting('ShowConsole'):
                # show the contents of the console, but only if we
                # are serving up an HTML file
                endheaders = string.find(rs._buffer, "\r\n\r\n")
                if endheaders == None:
                    endheaders = string.find(rs._buffer, "\n\n")
                if not endheaders:
                    print "No Headers Found"
                    return
                headers = string.split(rs._buffer[:endheaders], "\n")
                entries = []
                for i in headers:
                    entries.append(string.split(i, ":"))
                found = 0
                for name, value in entries:
                    if name.lower() == 'content-type':
                        found = 1
                        break
                if found and string.strip(string.lower(value)) == 'text/html':
                    self.showConsole(_console.getvalue())
        except:
            import traceback

            sys.stderr.write(
                '[%s] [error] WebKit.OneShotAdapter: Error while responding to request (unknown)\n'
                % (time.asctime(time.localtime(time.time()))))
            sys.stderr.write('Python exception:\n')
            traceback.print_exc(file=sys.stderr)

            output = apply(traceback.format_exception, sys.exc_info())
            output = string.join(output, '')
            output = string.replace(output, '&', '&amp;')
            output = string.replace(output, '<', '&lt;')
            output = string.replace(output, '>', '&gt;')
            output = string.replace(output, '"', '&quot;')
            sys.stdout.write('''Content-type: text/html

<html><body>
<p><pre>ERROR

%s</pre>
</body></html>\n''' % output)
Example #7
0
 def __init__(self, file):
     ASStreamOut.__init__(self)
     self._file = file
Example #8
0
    def run(self):

        timestamp = time.time()  # start time

        # to capture the console output of the application
        stdout, sys.stdout = sys.stdout, StringIO()

        try:

            # MS Windows: no special translation of end-of-lines
            if os.name == 'nt':
                import msvcrt
                msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)

            requestDict = dict(
                format='CGI',
                time=timestamp,
                # ce: a little tricky. We use marshal which only works
                # on built-in types, so we need environ's dictionary:
                environ=os.environ.data,
                input=sys.stdin)

            from WebKit import Profiler
            Profiler.startTime = time.time()

            print 'ONE SHOT MODE\n'

            from WebKit.OneShotAppServer import OneShotAppServer
            appSvr = OneShotAppServer(self._webKitDir)

            # It is important to call transaction.die() after using it, rather than
            # just letting it fall out of scope, to avoid circular references
            from WebKit.ASStreamOut import ASStreamOut
            rs = ASStreamOut()
            transaction = appSvr.dispatchRawRequest(requestDict, rs)
            rs.close()
            if transaction:
                transaction.die()
                del transaction

            appSvr.shutDown()
            appSvr = None

            print "AppServer run time %.2f seconds" % (time.time() -
                                                       Profiler.startTime)

            sys.stdout = stdout

            # MS Windows: no special translation of end-of-lines
            if os.name == 'nt':
                import msvcrt
                msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)

            response = rs._buffer
            if response:
                sys.stdout.write(response)
            else:
                sys.stdout.write('''Content-Type: text/html\n
<html><head><title>WebKit CGI Error</title><body>
<h3>WebKit CGI Error</h3>
<h4>No response from application server</h4>
</body></html>\n''')

            if self.setting('ShowConsole'):
                # show the contents of the console,
                # but only if we are serving up an HTML file
                endheaders = response.find("\r\n\r\n")
                if endheaders < 0:
                    endheaders = response.find("\n\n")
                if not endheaders:
                    print "No Headers Found"
                    return
                headers = response[:endheaders].split("\n")
                entries = []
                for header in headers:
                    if header:
                        entries.append(header.split(":", 1))
                found = False
                for name, value in entries:
                    if name.lower() == 'content-type':
                        found = True
                        break
                if found and value.strip().lower() == 'text/html':
                    self.showConsole(_console.getvalue())
        except:
            import traceback
            sys.stderr.write('[%s] [error] WebKit.OneShotAdapter:'
                             ' Error while responding to request (unknown)\n' %
                             (time.asctime(time.localtime(time.time()))))
            sys.stderr.write('Python exception:\n')
            traceback.print_exc(file=sys.stderr)
            sys.stdout = stdout
            output = ''.join(traceback.format_exception(*sys.exc_info()))
            output = htmlEncode(output)
            sys.stdout.write('''Content-Type: text/html\n
<html><head><title>WebKit CGI Error</title><body>
<h3>WebKit CGI Error</h3>
<pre>%s</pre>
</body></html>\n''' % output)