home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / python2.4 / site-packages / BitTorrent / HTTPHandler.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-08-31  |  5.8 KB  |  219 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. from cStringIO import StringIO
  5. from sys import stdout
  6. import time
  7. from gzip import GzipFile
  8. DEBUG = False
  9. weekdays = [
  10.     'Mon',
  11.     'Tue',
  12.     'Wed',
  13.     'Thu',
  14.     'Fri',
  15.     'Sat',
  16.     'Sun']
  17. months = [
  18.     None,
  19.     'Jan',
  20.     'Feb',
  21.     'Mar',
  22.     'Apr',
  23.     'May',
  24.     'Jun',
  25.     'Jul',
  26.     'Aug',
  27.     'Sep',
  28.     'Oct',
  29.     'Nov',
  30.     'Dec']
  31.  
  32. class HTTPConnection:
  33.     
  34.     def __init__(self, handler, connection):
  35.         self.handler = handler
  36.         self.connection = connection
  37.         self.buf = ''
  38.         self.closed = False
  39.         self.done = False
  40.         self.donereading = False
  41.         self.next_func = self.read_type
  42.  
  43.     
  44.     def get_ip(self):
  45.         return self.connection.get_ip()
  46.  
  47.     
  48.     def data_came_in(self, data):
  49.         if self.donereading or self.next_func is None:
  50.             return True
  51.         
  52.         self.buf += data
  53.         while True:
  54.             
  55.             try:
  56.                 i = self.buf.index('\n')
  57.             except ValueError:
  58.                 self
  59.                 self
  60.                 return True
  61.             except:
  62.                 self
  63.  
  64.             val = self.buf[:i]
  65.             self.buf = self.buf[i + 1:]
  66.             self.next_func = self.next_func(val)
  67.             if self.donereading:
  68.                 return True
  69.             
  70.             if self.next_func is None or self.closed:
  71.                 return False
  72.                 continue
  73.  
  74.     
  75.     def read_type(self, data):
  76.         self.header = data.strip()
  77.         words = data.split()
  78.         if len(words) == 3:
  79.             (self.command, self.path, garbage) = words
  80.             self.pre1 = False
  81.         elif len(words) == 2:
  82.             (self.command, self.path) = words
  83.             self.pre1 = True
  84.             if self.command != 'GET':
  85.                 return None
  86.             
  87.         else:
  88.             return None
  89.         if self.command not in ('HEAD', 'GET'):
  90.             return None
  91.         
  92.         self.headers = { }
  93.         return self.read_header
  94.  
  95.     
  96.     def read_header(self, data):
  97.         data = data.strip()
  98.         if data == '':
  99.             self.donereading = True
  100.             if self.headers.has_key('accept-encoding'):
  101.                 ae = self.headers['accept-encoding']
  102.                 if DEBUG:
  103.                     print 'Got Accept-Encoding: ' + ae + '\n'
  104.                 
  105.             else:
  106.                 ae = 'identity'
  107.             if ae.find('gzip') != -1:
  108.                 self.encoding = 'gzip'
  109.             else:
  110.                 self.encoding = 'identity'
  111.             r = self.handler.getfunc(self, self.path, self.headers)
  112.             if r is not None:
  113.                 self.answer(r)
  114.             
  115.             return None
  116.         
  117.         
  118.         try:
  119.             i = data.index(':')
  120.         except ValueError:
  121.             return None
  122.  
  123.         self.headers[data[:i].strip().lower()] = data[i + 1:].strip()
  124.         if DEBUG:
  125.             print data[:i].strip() + ': ' + data[i + 1:].strip()
  126.         
  127.         return self.read_header
  128.  
  129.     
  130.     def answer(self, .2):
  131.         (responsecode, responsestring, headers, data) = .2
  132.         if self.closed:
  133.             return None
  134.         
  135.         if self.encoding == 'gzip':
  136.             compressed = StringIO()
  137.             gz = GzipFile(fileobj = compressed, mode = 'wb', compresslevel = 9)
  138.             gz.write(data)
  139.             gz.close()
  140.             compressed.seek(0, 0)
  141.             cdata = compressed.read()
  142.             compressed.close()
  143.             if len(cdata) >= len(data):
  144.                 self.encoding = 'identity'
  145.             elif DEBUG:
  146.                 print 'Compressed: %i  Uncompressed: %i\n' % (len(cdata), len(data))
  147.             
  148.             data = cdata
  149.             headers['Content-Encoding'] = 'gzip'
  150.         
  151.         if self.encoding == 'identity':
  152.             ident = '-'
  153.         else:
  154.             ident = self.encoding
  155.         username = '-'
  156.         referer = self.headers.get('referer', '-')
  157.         useragent = self.headers.get('user-agent', '-')
  158.         (year, month, day, hour, minute, second, a, b, c) = time.localtime(time.time())
  159.         print '%s %s %s [%02d/%3s/%04d:%02d:%02d:%02d] "%s" %i %i "%s" "%s"' % (self.connection.get_ip(), ident, username, day, months[month], year, hour, minute, second, self.header, responsecode, len(data), referer, useragent)
  160.         t = time.time()
  161.         if t - self.handler.lastflush > self.handler.minflush:
  162.             self.handler.lastflush = t
  163.             stdout.flush()
  164.         
  165.         self.done = True
  166.         r = StringIO()
  167.         r.write('HTTP/1.0 ' + str(responsecode) + ' ' + responsestring + '\r\n')
  168.         if not self.pre1:
  169.             headers['Content-Length'] = len(data)
  170.             for key, value in headers.items():
  171.                 r.write(key + ': ' + str(value) + '\r\n')
  172.             
  173.             r.write('\r\n')
  174.         
  175.         if self.command != 'HEAD':
  176.             r.write(data)
  177.         
  178.         self.connection.write(r.getvalue())
  179.         if self.connection.is_flushed():
  180.             self.connection.shutdown(1)
  181.         
  182.  
  183.  
  184.  
  185. class HTTPHandler:
  186.     
  187.     def __init__(self, getfunc, minflush):
  188.         self.connections = { }
  189.         self.getfunc = getfunc
  190.         self.minflush = minflush
  191.         self.lastflush = time.time()
  192.  
  193.     
  194.     def external_connection_made(self, connection):
  195.         self.connections[connection] = HTTPConnection(self, connection)
  196.  
  197.     
  198.     def connection_flushed(self, connection):
  199.         if self.connections[connection].done:
  200.             connection.shutdown(1)
  201.         
  202.  
  203.     
  204.     def connection_lost(self, connection):
  205.         ec = self.connections[connection]
  206.         ec.closed = True
  207.         del ec.connection
  208.         del ec.next_func
  209.         del self.connections[connection]
  210.  
  211.     
  212.     def data_came_in(self, connection, data):
  213.         c = self.connections[connection]
  214.         if not c.data_came_in(data) and not (c.closed):
  215.             c.connection.shutdown(1)
  216.         
  217.  
  218.  
  219.