Changeset 314


Ignore:
Timestamp:
Jul 19, 2011, 2:11:09 PM (13 years ago)
Author:
Rick van der Zwet
Message:

Allow serving static files as well.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • py-tcpmultiplexer/TCPMultiplexer.py

    r313 r314  
    1515import time
    1616import urllib2
     17import os
    1718
    1819# Some boring defaults
     
    2021DEFAULT_PORT = 9999
    2122DEFAULT_CONFIG = 'streams.yaml'
     23DEFAULT_DOCUMENTROOT = './htdocs'
     24
    2225
    2326# URL : TARGET
     
    3336running = True
    3437recv_threads = []
     38document_root = None
    3539
    3640logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
     
    5054
    5155    def handle(self):
    52         global running, dataheader, urlheader, urldata
     56        global running, dataheader, document_root, urlheader, urldata
    5357        # self.request is the TCP socket connected to the client
    5458        self.data = self.request.recv(1024).strip()
    55         print self.data
    5659        urlid = self.data.split('\n')[0].split()[1]
    5760        req_type = self.data.split('\n')[0].split()[0]
     61        # XXX: Check for .. paths
     62        document_file = document_root + urlid
    5863        logging.info("Connection %s from '%s' for '%s'", req_type, self.client_address[0], urlid)
    5964        try:
    60           if not urldata.has_key(urlid):
    61             self.request.send("HTTP/1.1 404 NOT FOUND\n\nThe page '%s' does not exists" % urlid)
    62           else:
     65          if urlid == '/':
     66            self.request.send("HTTP/1.1 200 OK\n\n<html><head><title>Overview</title></head><body><h3>Streams</h3><ul>")
     67            for urlid in urldata.keys():
     68              self.request.send('<li><a href="%(urlid)s">%(urlid)s</a></li>' % {'urlid': urlid})
     69            self.request.send("</ul><h3>Files</h3><ul>")
     70            for root, dirs, files in os.walk(document_root):
     71              for name in files:
     72                self.request.send('<li><a href="%(item)s">%(item)s</li>' % { 'item' : os.path.join(root.replace(document_root,'',1),name) })
     73            self.request.send("</ul></body></html>")
     74          elif urldata.has_key(urlid):
    6375            self.request.send('HTTP/1.1 200 OK\n' + urlheader[urlid] + 'X-Proxy-Server: TCPMultiplexer.py\n' + '\n' + dataheader[urlid])
    6476            if req_type == 'HEAD':
     
    7183                  continue
    7284                self.request.send(urldata[urlid][self].pop(0))
    73             else:
    74                assert False, "Invalid request"
     85          elif os.path.isfile(document_file):
     86            self.request.send('HTTP/1.1 200 OK\n\n' + open(document_file,'r').read())
     87          else:
     88            self.request.send("HTTP/1.1 404 NOT FOUND\nContent-Type: text/html\n\n<h1>404</h1>The page '%s' does not exists" % urlid)
    7589        except IOError:
    7690          pass
     
    129143  parser.add_argument('--port', dest='port', default=DEFAULT_PORT, type=int, help='Listen to PORT [default: %s]' % DEFAULT_PORT)
    130144  parser.add_argument('--stream-cfg', dest='stream_cfg', default=DEFAULT_CONFIG, help='YAML Stream configuration [default: %s]' % DEFAULT_CONFIG)
     145  parser.add_argument('--document-root', dest='document_root', default=DEFAULT_DOCUMENTROOT, help='Document Root File Directory [default: %s]' % DEFAULT_DOCUMENTROOT)
    131146  args = parser.parse_args()
     147
     148  document_root = os.path.abspath(os.path.join(os.path.dirname(__file__),args.document_root))
     149  logging.info("Serving '/htdocs' from document_root '%s'", document_root)
    132150
    133151  # Inport streams
Note: See TracChangeset for help on using the changeset viewer.