from http.server import BaseHTTPRequestHandler, HTTPServer
import threading, pickle, re, subprocess
    
class MyHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        f_type_map = {'.html': 'text/html', '.css': 'text/css', '.ico': 'image/x-icon', '.jpg': 'image/jpeg', '.png': 'image/png', '.gif': 'image/gif', '.js': 'text/javascript', '.py': 'test/python'}
        t_type = re.compile('\/|(\.\w*)')
        r_file = self.path.split('?')
        requested_type = t_type.findall(self.path)
        print(requested_type)
        ex = requested_type[-1]
        if ex != '.py':
            
            try:
                self.send_response(200)
                self.send_header('Content-type', f_type_map[ex])
                self.send_header('Content-Encoding', 'utf-8')
                self.end_headers()
                try:
                    with open('C:\\Users\jackt.JACK-IS-AWESOME\OneDrive\LaptopProjects\Google test signin\Google%s'% r_file[0]) as file:
                        f = file.read()
                        #f = 'This is my secret message'
                        self.wfile.write(bytes(f, 'utf8'))
                except UnicodeDecodeError:
                    with open('C:\\Users\jackt.JACK-IS-AWESOME\OneDrive\LaptopProjects\Google test signin\Google%s'% r_file[0], 'rb') as f:
                        file = f.read()
                        self.wfile.write(file)
            except IOError:
                self.send_response(404, 'File Not Found')
                self.wfile.write(bytes('404 file not found', 'utf8'))
            except KeyError:
                self.send_response(200)
                self.send_header('Content-type', 'text/html')
                self.end_headers()
                with open('C:\\Users\jackt.JACK-IS-AWESOME\OneDrive\LaptopProjects\Google test signin\Google\index.html') as file:
                    f = 'This is my secret message'
                    #f = file.read()
                    self.wfile.write(bytes(f, 'utf8'))
            return
        else:
            file = subprocess.run(['python3 C:\\Users\jackt.JACK-IS-AWESOME\OneDrive\LaptopProjects\Python server-client\cgi\firstcgi.py'], stdout=subprocess.PIPE)
            self.wfile.write(bytes(file, 'utf-8'))
            
server_address = ('192.168.1.233', 6789)
def run():
    print('starting server ...')
    httpd = HTTPServer(server_address, MyHandler)
    httpd.serve_forever()
	
bg_server= threading.Thread(target = run)

###Uncomment the next line if you want to have the server start when the file is run###
bg_server.start()
print('\nserver started at %s:%s'% server_address)
