def handle(self, sock, address, internal=False):
try:<EOL><INDENT>sock = StatsSocket(sock)<EOL>rfile = sock.makefile('<STR_LIT:rb>', <NUM_LIT>)<EOL>first = rfile.readline().strip("<STR_LIT:\r\n>")<EOL>words = first.split()<EOL>if not (<NUM_LIT:2> <= len(words) <= <NUM_LIT:3>):<EOL><INDENT>sock.sendall("<STR_LIT>")<EOL>return<EOL><DEDENT>path = words[<NUM_LIT:1>]<EOL>headers = mimetools.Message(rfile, <NUM_LIT:0>)<EOL>try:<EOL><INDENT>host = headers['<STR_LIT>']<EOL><DEDENT>except KeyError:<EOL><INDENT>host = "<STR_LIT>"<EOL><DEDENT>headers['<STR_LIT>'] = "<STR_LIT>"<EOL>if not internal:<EOL><INDENT>headers['<STR_LIT>'] = address[<NUM_LIT:0>]<EOL>headers['<STR_LIT>'] = "<STR_LIT>"<EOL>headers['<STR_LIT>'] = "<STR_LIT>"<EOL><DEDENT>if "<STR_LIT>" in headers:<EOL><INDENT>sock.sendall("<STR_LIT>")<EOL>return<EOL><DEDENT>protocol = "<STR_LIT:http>"<EOL>if headers.get('<STR_LIT>', headers.get('<STR_LIT>', "<STR_LIT>")).lower() in ("<STR_LIT>", "<STR_LIT>"):<EOL><INDENT>protocol = "<STR_LIT>"<EOL><DEDENT>action = self.resolve_host(host, protocol)<EOL>stats_dict = self.stats.setdefault(action.matched_host, {})<EOL>stats_dict['<STR_LIT>'] = stats_dict.get('<STR_LIT>', <NUM_LIT:0>) + <NUM_LIT:1><EOL>try:<EOL><INDENT>rfile._rbuf.seek(<NUM_LIT:0>)<EOL>action.handle(<EOL>sock = sock,<EOL>read_data = first + "<STR_LIT:\r\n>" + str(headers) + "<STR_LIT:\r\n>" + rfile._rbuf.read(),<EOL>path = path,<EOL>headers = headers,<EOL>)<EOL><DEDENT>finally:<EOL><INDENT>stats_dict['<STR_LIT>'] -= <NUM_LIT:1><EOL>stats_dict['<STR_LIT>'] = stats_dict.get('<STR_LIT>', <NUM_LIT:0>) + <NUM_LIT:1><EOL>stats_dict['<STR_LIT>'] = stats_dict.get('<STR_LIT>', <NUM_LIT:0>) + sock.bytes_sent<EOL>stats_dict['<STR_LIT>'] = stats_dict.get('<STR_LIT>', <NUM_LIT:0>) + sock.bytes_received<EOL><DEDENT><DEDENT>except socket.error as e:<EOL><INDENT>if e.errno not in (errno.EPIPE, errno.ETIMEDOUT, errno.ECONNRESET):<EOL><INDENT>logging.error(traceback.format_exc())<EOL><DEDENT><DEDENT>except:<EOL><INDENT>logging.error(traceback.format_exc())<EOL>try:<EOL><INDENT>sock.sendall("<STR_LIT>")<EOL><DEDENT>except socket.error as e:<EOL><INDENT>if e.errno != errno.EPIPE:<EOL><INDENT>raise<EOL><DEDENT><DEDENT><DEDENT>finally:<EOL><INDENT>try:<EOL><INDENT>sock.close()<EOL>rfile.close()<EOL><DEDENT>except:<EOL><INDENT>logging.error(traceback.format_exc())<EOL><DEDENT><DEDENT>
Handles an incoming HTTP connection.