def __init__ (self, local_ip=None): """__init__(local_ip=None) -> ftp_client Creates a new ftp_client. Binds to the local_ip if it is given. NOTE: You MUST del the stream object when you are done, or this will leak. """ self.local_ip = local_ip self.s = coro.make_socket (socket.AF_INET, socket.SOCK_STREAM) if local_ip: self.s.bind((local_ip,0)) self.stream = read_stream.stream_reader (self.recv)
def __init__ (self, server, conn, addr, session_id): self.server = server self.current_mode = 'a' self.addr = addr self.conn = conn self.session_id = session_id self.thread_id = None # client data port. Defaults to 'the same as the control connection'. self.client_addr = (addr[0], 21) self.in_buffer = '' self.closing = 0 self.passive_acceptor = None self.filesystem = None self.authorized = 0 self.stream = read_stream.stream_reader (self.read) self.user = None
def run (self, conn, peer, server_obj, handlers): self.conn = conn self.server = server_obj self.peer = peer # Note that peer could be a fake address, and server_obj can be None. # These indicate a "backdoor" request from the gui. try: try: count = 0 qlog.write('WEBUI.CONN_INIT', 'http', id(self), peer[0], peer[1]) while 1: if self.server and self.server.shutdown_flag: break try: # We use self.stream to read the header line-by-line # and then switch to reading directly from the socket # for the body (if needed). Reuse the previous # instance if it exists, to support HTTP pipelining. if not self.stream: self.stream = read_stream.stream_reader(self.conn.recv) request_line = self.read_line() if not request_line: break except socket.error: qlog.write('WEBUI.CONN_ERROR', 'http', id(self), 'socket error') break count = count + 1 headers = self.read_header() #print '\n'.join (headers) + '\n\n' request = http_request (self, request_line, headers) request.read_body() if request._error: # Bad Request request.error (400) return else: try: try: handler = self.pick_handler (handlers, request) if handler: handler.handle_request (request) else: self.not_found (request) if not request._done: request.done() except OSError, err: if err[0] == errno.EPIPE: pass # ignore broken pipe error else: raise # process exception in outer try # These exceptions are used inside the coro # stuff and shouldn't be thrown away except (coro.TimeoutError, coro.Interrupted): raise except: tb = coro.compact_traceback() ## sys.stderr.write (repr(tb)) request.error (500, tb) qlog.write('COMMON.APP_FAILURE', tb + ' request: ' + `request`) tb = None if request._close: # ok, this gets interesting. the connection needs to close # here. the finally clause below isn't getting hit because # the session and client are running in the same coroutine. # that's bad, I think. conn.close() break # this should be a policy decision of the owner of logfp # self.logfp.flush() except read_stream.BufferOverflow: # Indicates a request header that exceeded the line # buffer, which may indicate an attack on the server. # We just close the connection without a response. # TODO:lrosenstein - log this since it may be an attack? qlog.write('WEBUI.CONN_ERROR', 'http', id(self), 'line buffer limit exceeded') pass except sslip.Error, why: # Most likely a problem with SSL negotiation qlog.write('WEBUI.CONN_ERROR', 'https', id(self), why[1]) pass except OSError, err: # We got some kind of I/O error that wasn't handled # elsewhere. Since this seem to happen because the # client closed the connection, it is safe to ignore # the exception. qlog.write('WEBUI.CONN_ERROR', 'http', id(self), 'OS error %s' % str(err[1])) pass
def run(self, conn, peer, server_obj, handlers): self.conn = conn self.server = server_obj self.peer = peer # Note that peer could be a fake address, and server_obj can be None. # These indicate a "backdoor" request from the gui. try: try: count = 0 qlog.write('WEBUI.CONN_INIT', 'http', id(self), peer[0], peer[1]) while 1: if self.server and self.server.shutdown_flag: break try: # We use self.stream to read the header line-by-line # and then switch to reading directly from the socket # for the body (if needed). Reuse the previous # instance if it exists, to support HTTP pipelining. if not self.stream: self.stream = read_stream.stream_reader( self.conn.recv) request_line = self.read_line() if not request_line: break except socket.error: qlog.write('WEBUI.CONN_ERROR', 'http', id(self), 'socket error') break count = count + 1 headers = self.read_header() #print '\n'.join (headers) + '\n\n' request = http_request(self, request_line, headers) request.read_body() if request._error: # Bad Request request.error(400) return else: try: try: handler = self.pick_handler(handlers, request) if handler: handler.handle_request(request) else: self.not_found(request) if not request._done: request.done() except OSError, err: if err[0] == errno.EPIPE: pass # ignore broken pipe error else: raise # process exception in outer try # These exceptions are used inside the coro # stuff and shouldn't be thrown away except (coro.TimeoutError, coro.Interrupted): raise except: tb = coro.compact_traceback() ## sys.stderr.write (repr(tb)) request.error(500, tb) qlog.write('COMMON.APP_FAILURE', tb + ' request: ' + ` request `) tb = None if request._close: # ok, this gets interesting. the connection needs to close # here. the finally clause below isn't getting hit because # the session and client are running in the same coroutine. # that's bad, I think. conn.close() break # this should be a policy decision of the owner of logfp # self.logfp.flush() except read_stream.BufferOverflow: # Indicates a request header that exceeded the line # buffer, which may indicate an attack on the server. # We just close the connection without a response. # TODO:lrosenstein - log this since it may be an attack? qlog.write('WEBUI.CONN_ERROR', 'http', id(self), 'line buffer limit exceeded') pass except sslip.Error, why: # Most likely a problem with SSL negotiation qlog.write('WEBUI.CONN_ERROR', 'https', id(self), why[1]) pass except OSError, err: # We got some kind of I/O error that wasn't handled # elsewhere. Since this seem to happen because the # client closed the connection, it is safe to ignore # the exception. qlog.write('WEBUI.CONN_ERROR', 'http', id(self), 'OS error %s' % str(err[1])) pass