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.
 
 

172 lines
4.5 KiB

  1. #!/usr/bin/env python3.7
  2. import json
  3. #from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
  4. from http.server import BaseHTTPRequestHandler
  5. import os
  6. from io import BytesIO
  7. from utils.utils import get_list_overlays, load_config, write_config, sort_inatll_pkg, scan_config_portage
  8. from package import Package
  9. from findfsdb import on_find
  10. from src.route import Router
  11. #repl = '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<!DOCTYPE repositories SYSTEM "http://www.gentoo.org/dtd/repositories.dtd">'
  12. class Handler(BaseHTTPRequestHandler):
  13. def r_403(self):
  14. self.send_response(403)
  15. self.end_headers()
  16. print(self.client_address)
  17. def r_404(self):
  18. self.send_response(404)
  19. self.end_headers()
  20. print(self.client_address)
  21. def route(self):
  22. if self.path in route:
  23. return true
  24. else:
  25. return false
  26. def parse_url(self):
  27. pass
  28. def do_GET(self):
  29. self.r_t =""
  30. #print(Router.parse_url(self))
  31. #print("data:\t" + self.rfile.read())
  32. #request = Router.parse_url(self).decode('utf-8')
  33. if self.client_address[0] == '127.0.0.1' or self.client_address[0].startswith('10.0'):
  34. self.send_response(200)
  35. self.end_headers()
  36. if self.path =="/":
  37. #content_len = int(self.headers(['Content-Length']))
  38. #post_body = self.rfile.read('content_len')
  39. #data = json.loads(post_body)
  40. #print(data)
  41. with open('./views/index.html', 'tr') as f:
  42. self.r_t=f.read()
  43. print(self.client_address)
  44. elif self.path == '/main':
  45. print(self.rfile/read())
  46. with open("./README.txt", 'r') as f:
  47. self.r_t = str(f.read())
  48. elif self.path == '/ovelays':
  49. overlays = get_list_overlays()
  50. #print(ovls)
  51. if overlays: # == "":
  52. overlays ="Error"
  53. self.r_t=json.dumps({"repositories": overlays})
  54. elif self.path == "/favicon.ico":
  55. with open('./favicon.png', 'rb') as f:
  56. self.r_t = f.read()
  57. elif self.path == '/logo.png':
  58. pass
  59. elif self.path.startswith( "/static/"):
  60. self.r_static()
  61. self.send_response(200)
  62. #print(self.r_t)
  63. elif self.path == '/get_dump_list':
  64. try:
  65. with open('./pkgs.json', 'r') as fn:
  66. data = fn.read()
  67. pkg_list = json.load
  68. s(data)
  69. print(pkg_list)
  70. except Exception (e):
  71. print(str(e))
  72. self.r_t = json.dumps({"dump_portage": pkg_list})
  73. elif self.path.startswith("/?st_app="):
  74. #config = load_config()
  75. #param = self.path.replace("/?st_app=", "")
  76. #list_param = param.split(',')
  77. #print(list_param)
  78. for i in list_param:
  79. if i.startswith('port'):
  80. port = int(i.split('=')[1])
  81. elif i.startswith('Lang'):
  82. Lang = i.split('=')[1]
  83. write_config(port, Lang)
  84. print(config)
  85. print(param)
  86. elif self.path == 'find':
  87. param = request['params']['name']
  88. pk_list = []
  89. search_result = {}
  90. #if len(param.split('/')) == 2:
  91. # param = param.split('/')[1]
  92. #p_list = on_find(param)
  93. #print(p_list)
  94. #if len(p_list) == 0:
  95. # print("Never Found")
  96. # self.r_t = str(json.dumps({"Package_result": p_list}))
  97. #else:
  98. #for p in p_list:
  99. #print(p)
  100. if len(param.split("/")) == 2:
  101. pk_list.append(Package().search(param.split("/")[1]))
  102. else:
  103. pk_list.append(Package().search(param))
  104. #print(pk)
  105. search_result = {"Package_result": pk_list}
  106. self.r_t = str(json.dumps(search_result))
  107. elif self.path == "get_settings_app":
  108. self.r_t = str(json.dumps(load_config()))
  109. elif self.path == 'get_portage':
  110. #self.r_t = str(sort_inatll_pkg())
  111. self.r_t = str(json.dumps(scan_config_portage()))
  112. elif '.py?' in self.path:
  113. print("loading")
  114. self.path = "/static/app" + str(self.path.split('?')[0])
  115. print(self.path)
  116. self.r_static()
  117. else:
  118. self.send_response(404)
  119. self.end_headers()
  120. print(str(self.client_address[0]) +"\t" + str(404))
  121. # Send the html message
  122. #self.wfile.write(bytes(self.r_t, "utf-8"))
  123. try:
  124. return self.wfile.write(self.r_t)
  125. except TypeError:
  126. #print("TypeError")
  127. return self.wfile.write(bytes(self.r_t, 'utf-8'))
  128. else:
  129. self.r_403()
  130. def r_static(self):
  131. if os.path.exists('./views/' + self.path):
  132. #self.send_response(200)
  133. #self.end_headers()
  134. with open('./views/' + self.path, 'tr') as f:
  135. self.r_t=f.read()
  136. else:
  137. self.send_response(404)
  138. self.end_headers()
  139. def do_POST(self):
  140. self._set_headers()
  141. content_len = int(self.headers.getheader('content-length', 0))
  142. post_body = self.rfile.read(content_len)
  143. data = json.loads(post_body)
  144. print(data)