Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/jarabe/http.py
blob: 3964236688b92ab2042f3eb9107185401638ecb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import mimetypes
import threading
import BaseHTTPServer
import os

from jarabe.model import bundleregistry

class HTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    def do_GET(self):
        splitted = self.path.split("/")
        bundle_id = splitted[1]
        path = splitted[2:]

        registry = bundleregistry.get_registry()
        bundle = registry.get_bundle(bundle_id)

        file_path = os.path.join(bundle.get_path(), *path)
        with open(file_path) as f:
            self.send_response(200)
            self.send_header('Content-type', mimetypes.guess_type(file_path))
            self.end_headers()
            self.wfile.write(f.read())
 
class ServerThread(threading.Thread):
    def run(self):
        httpd = BaseHTTPServer.HTTPServer(('', 8000), HTTPRequestHandler)
        httpd.serve_forever()

def start_server():
    thread = ServerThread()
    thread.start()