def connectionMade(self): print self.request.__dict__ # reset response parser self.status_parsed = None # build header and vars vars = '' if self.request.stream.length: vars += self.build_uwsgi_var('CONTENT_LENGTH',str(self.request.stream.length)) for hkey, hval in self.request.headers.getAllRawHeaders(): # use a list, probably it will be extended if hkey.lower() not in ('content-type'): vars += self.build_uwsgi_var('HTTP_'+hkey.upper().replace('-','_'),','.join(hval)) else: vars += self.build_uwsgi_var(hkey.upper().replace('-','_'),','.join(hval)) vars += self.build_uwsgi_var('REQUEST_METHOD', self.request.method) vars += self.build_uwsgi_var('SCRIPT_NAME', self.request.uwsgi_app) vars += self.build_uwsgi_var('PATH_INFO', self.request.path[len(self.request.uwsgi_app):]) vars += self.build_uwsgi_var('QUERY_STRING', self.request.querystring) vars += self.build_uwsgi_var('SERVER_NAME', self.request.host) vars += self.build_uwsgi_var('SERVER_PORT', str(self.request.port)) vars += self.build_uwsgi_var('SERVER_PROTOCOL', self.request.scheme.upper()+'/'+str(self.request.clientproto[0]) +'.'+str(self.request.clientproto[1])) vars += self.build_uwsgi_var('REQUEST_URI', self.request.uri) vars += self.build_uwsgi_var('REMOTE_ADDR', self.request.remoteAddr.host) self.transport.write(struct.pack('<BHB',0, len(vars),0)) self.transport.write(vars) # send request data stream.StreamProducer(self.request.stream).beginProducing(self.transport)
def backup(path, type, callback=None, errback=None): """ Sets up and returns a StreamProducer containing the compress stream for a backup. @param path: the path to backup @param type: one of C{tar.gz}, C{tar.bz2}, C{zip} @param callback: the callback for the ProcessStreamer's run deferred @param errback: the errback for the ProcessStreamer's run deferred type = (tar.gz, tar.bz2, zip) """ if not isKnownType(type): raise KeyError("Type %s is not implemented." % type) command = commands[type] process = stream.ProcessStreamer(stream.MemoryStream(''), command[0], command + [path]) producer = stream.StreamProducer(process.outStream) d = process.run() if callback is not None and errback is not None: d.addBoth(callback, errback) else: if callback is not None: d.addCallback(callback) if errback is not None: d.addErrback(errback) return producer
def submit(self): l = [] request = self.request if request.method == "HEAD": # No incoming data will arrive. self.length = 0 l.append('%s %s %s\r\n' % (request.method, request.uri, self.outgoing_version)) if request.headers is not None: for name, valuelist in request.headers.getAllRawHeaders(): for value in valuelist: l.append("%s: %s\r\n" % (name, value)) if request.stream is not None: if request.stream.length is not None: l.append("%s: %s\r\n" % ('Content-Length', request.stream.length)) else: # Got a stream with no length. Send as chunked and hope, against # the odds, that the server actually supports chunked uploads. l.append("%s: %s\r\n" % ('Transfer-Encoding', 'chunked')) self.chunkedOut = True if self.closeAfter: l.append("%s: %s\r\n" % ('Connection', 'close')) else: l.append("%s: %s\r\n" % ('Connection', 'Keep-Alive')) l.append("\r\n") self.transport.writeSequence(l) d = stream_mod.StreamProducer(request.stream).beginProducing(self) d.addCallback(self._finish).addErrback(self._error)
def connectionMade(self): # Send input data over to the CGI script. def _failedProducing(reason): # If you really care. #log.err(reason) pass def _finishedProducing(result): self.transport.closeChildFD(0) s = stream.StreamProducer(self.request.stream) producingDeferred = s.beginProducing(self.transport) producingDeferred.addCallback(_finishedProducing) producingDeferred.addErrback(_failedProducing)
def connectionMade(self): # Ooh, look someone did all the hard work for me :). env = twcgi.createCGIEnvironment(self.request) # Send the headers. The Content-Length header should always be sent # first and must be 0 if not present. # The whole lot is sent as one big netstring with each name and value # separated by a '\0'. contentLength = str(env.pop('CONTENT_LENGTH', 0)) env['SCGI'] = '1' scgiHeaders = [] scgiHeaders.append('%s\x00%s\x00'%('CONTENT_LENGTH', str(contentLength))) scgiHeaders.append('SCGI\x001\x00') for name, value in env.iteritems(): if name in ('CONTENT_LENGTH', 'SCGI'): continue scgiHeaders.append('%s\x00%s\x00'%(name,value)) scgiHeaders = ''.join(scgiHeaders) self.transport.write('%d:%s,' % (len(scgiHeaders), scgiHeaders)) stream.StreamProducer(self.request.stream).beginProducing(self.transport)
def writeResponse(self, response): """ Write a response. """ if self.stream.doStartReading is not None: # Expect: 100-continue was requested, but 100 response has not been # sent, and there's a possibility that data is still waiting to be # sent. # # Ideally this means the remote side will not send any data. # However, because of compatibility requirements, it might timeout, # and decide to do so anyways at the same time we're sending back # this response. Thus, the read state is unknown after this. # We must close the connection. self.chanRequest.channel.setReadPersistent(False) # Nothing more will be read self.chanRequest.allContentReceived() if response.code != responsecode.NOT_MODIFIED: # Not modified response is *special* and doesn't get a content-length. if response.stream is None: response.headers.setHeader('content-length', 0) elif response.stream.length is not None: response.headers.setHeader('content-length', response.stream.length) self.chanRequest.writeHeaders(response.code, response.headers) # if this is a "HEAD" request, or a special response code, # don't return any data. if self.method == "HEAD" or response.code in NO_BODY_CODES: if response.stream is not None: response.stream.close() self._finished(None) return d = stream.StreamProducer(response.stream).beginProducing(self.chanRequest) d.addCallback(self._finished).addErrback(self._error)