You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
3.5 KiB

# -*- codding: utf-8 -*-
#!/usr/bin/env python3
from io import StringIO ## for Python 3
#from requests import requests as ReqServer
from http.server import BaseHTTPRequestHandler
import logging
from .jwt import genaratorSecretsToken
import json
class Route():
def __init__(self):
routes = self.setRoutes()
super().__init__(self.setRoutes)
def add(self, url, **opts):
pass
def find(self, url, handler):
if url in self.routes:
handler()
else:
return 1
class RouterOld(BaseHTTPRequestHandler):
#def __init__(self):
#self.P_list = []
#self.Response = ""
#self.Routes =[]
#self.r_t =""
#length = int(self.headers['content-length'])
# Not work code
#self.send_header['Access-Control-Allow-Origin', '*']
#self.send_header['Access-Control-Allow-Methods', 'GET']
#self.send_header['Access-Control-Allow-Headers', 'X-Requested-With,content-type']
#self.send_header['Access-Control-Allow-Credentials', true]
#self.end_headers()
#super().__init__(self, self.setRoutes())
def _set_response(self):
self.send_response(200)
self.send_header['Access-Control-Allow-Origin', '*']
self.send_header['Access-Control-Allow-Methods', 'GET']
self.send_header['Access-Control-Allow-Headers', 'X-Requested-With,content-type']
#self.send_header['Access-Control-Allow-Credentials', true]
self.end_headers()
def do_GET(self):
#logging.info("GET request,\nPath: %s\nHeaders:\n%s\n", str(self.path), str(self.headers))
#print(str(self.path), str(self.headers))
self.r_t = str(self.path), str(self.headers)
return self.response()
def do_POST(self):
logging.info("GET request,\nPath: %s\nHeaders:\n%s\n", str(self.path), str(self.headers))
self._set_response()
print(str(self.path), str(self.headers))
def do_HEAD(self):
pass
def do_REQUEST(self):
print("do_REQUEST:\n")
print(str(self.path), str(self.headers))
self.r_t = str(self.path), str(self.headers)
return self.response()
def _setRoutes(self, ListRoutes =[]):
self.Routes = ListRoutes
def response(self):
try:
return self.wfile.write(BytesIO(self.r_t).getvalue())
except TypeError:
#print("TypeError")
return self.wfile.write(bytes(self.r_t, 'UTF-8'))
def route(self, metod, url, handler):
print(url)
if self.path in self.Routes.url:
if metod == 'GET' or metod == 'get':
self.get(url=url,handler=handler)
#return true
elif metod == 'POST' or metod == 'post':
self.post(url=url, handler=handler)
elif metod == 'HEAD' or metod == 'head':
self.head()
else:
self.r_404()
else:
return self.r_404()
#return false
def get(self, url, handler):
#do_GET()
return url
def post(self, url, handler):
#do_POST()
return url
def head(self):
#do_HEAD()
pass
def __isAuth(selt, auth):
pass
def getToken(self, authResult):
print(self.client_address)
#if self.__isAuth() is True:
genaratorSecretsToken()
#else:
# self.r_403()
def parse_url(request):
print("request.rfile:\t" + str(request.rfile.read()))
#return json.loads(str(request.rfile.read()))
def r_403(self):
self.send_response(403)
self.end_headers()
print(self.client_address)
def r_404(self):
self.send_response(404)
self.end_headers()
print(self.client_address)
def r_static(self):
if os.path.exists('./views/' + self.path):
self.send_response(200)
self.end_headers()
with open('./views/' + self.path, 'tr') as f:
self.Response=f.read()
else:
self.send_response(404)
self.end_headers()
"""
def do_GET(self):
pass
def do_POST(self):
pass
"""