2019-02-22 10:55:12 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import argparse
|
2020-09-16 04:26:10 +00:00
|
|
|
import csv
|
2019-09-12 20:11:25 +00:00
|
|
|
import socket
|
2019-02-22 10:55:12 +00:00
|
|
|
import ssl
|
2020-10-02 16:54:07 +00:00
|
|
|
from http.server import BaseHTTPRequestHandler, HTTPServer
|
2019-02-22 10:55:12 +00:00
|
|
|
|
|
|
|
|
2020-01-11 09:50:41 +00:00
|
|
|
# Decorator used to see if authentication works for external dictionary who use a HTTP source.
|
2019-09-26 10:27:22 +00:00
|
|
|
def check_auth(fn):
|
|
|
|
def wrapper(req):
|
2022-03-22 16:39:58 +00:00
|
|
|
auth_header = req.headers.get("authorization", None)
|
|
|
|
api_key = req.headers.get("api-key", None)
|
|
|
|
if (
|
|
|
|
not auth_header
|
|
|
|
or auth_header != "Basic Zm9vOmJhcg=="
|
|
|
|
or not api_key
|
|
|
|
or api_key != "secret"
|
|
|
|
):
|
2019-09-26 10:27:22 +00:00
|
|
|
req.send_response(401)
|
|
|
|
else:
|
|
|
|
fn(req)
|
2020-09-16 04:26:10 +00:00
|
|
|
|
2019-09-26 10:27:22 +00:00
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
2019-09-12 20:11:25 +00:00
|
|
|
def start_server(server_address, data_path, schema, cert_path, address_family):
|
2019-02-22 10:55:12 +00:00
|
|
|
class TSVHTTPHandler(BaseHTTPRequestHandler):
|
2019-09-26 10:27:22 +00:00
|
|
|
@check_auth
|
2019-09-12 20:11:25 +00:00
|
|
|
def do_GET(self):
|
|
|
|
self.__send_headers()
|
|
|
|
self.__send_data()
|
|
|
|
|
2019-09-26 10:27:22 +00:00
|
|
|
@check_auth
|
2019-09-12 20:11:25 +00:00
|
|
|
def do_POST(self):
|
|
|
|
ids = self.__read_and_decode_post_ids()
|
2020-10-02 16:54:07 +00:00
|
|
|
print("ids=", ids)
|
2019-09-12 20:11:25 +00:00
|
|
|
self.__send_headers()
|
|
|
|
self.__send_data(ids)
|
|
|
|
|
|
|
|
def __send_headers(self):
|
2019-02-22 10:55:12 +00:00
|
|
|
self.send_response(200)
|
2022-03-22 16:39:58 +00:00
|
|
|
self.send_header("Content-type", "text/tsv")
|
2019-02-22 10:55:12 +00:00
|
|
|
self.end_headers()
|
|
|
|
|
2020-09-16 04:26:10 +00:00
|
|
|
def __send_data(self, only_ids=None):
|
2022-03-22 16:39:58 +00:00
|
|
|
with open(data_path, "r") as fl:
|
|
|
|
reader = csv.reader(fl, delimiter="\t")
|
2019-02-22 10:55:12 +00:00
|
|
|
for row in reader:
|
2019-09-12 20:11:25 +00:00
|
|
|
if not only_ids or (row[0] in only_ids):
|
2022-03-22 16:39:58 +00:00
|
|
|
self.wfile.write(("\t".join(row) + "\n").encode())
|
2019-02-22 10:55:12 +00:00
|
|
|
|
2019-09-12 20:11:25 +00:00
|
|
|
def __read_and_decode_post_ids(self):
|
|
|
|
data = self.__read_and_decode_post_data()
|
2020-10-02 16:54:07 +00:00
|
|
|
return [_f for _f in data.split() if _f]
|
2019-02-22 10:55:12 +00:00
|
|
|
|
2019-09-12 20:11:25 +00:00
|
|
|
def __read_and_decode_post_data(self):
|
|
|
|
transfer_encoding = self.headers.get("Transfer-encoding")
|
2022-03-22 16:39:58 +00:00
|
|
|
decoded = ""
|
2019-09-12 20:11:25 +00:00
|
|
|
if transfer_encoding == "chunked":
|
|
|
|
while True:
|
2020-10-02 16:54:07 +00:00
|
|
|
s = self.rfile.readline().decode()
|
2019-09-12 20:11:25 +00:00
|
|
|
chunk_length = int(s, 16)
|
|
|
|
if not chunk_length:
|
|
|
|
break
|
2020-10-02 16:54:07 +00:00
|
|
|
decoded += self.rfile.read(chunk_length).decode()
|
|
|
|
self.rfile.readline().decode()
|
2019-09-12 20:11:25 +00:00
|
|
|
else:
|
|
|
|
content_length = int(self.headers.get("Content-Length", 0))
|
2020-10-02 16:54:07 +00:00
|
|
|
decoded = self.rfile.read(content_length).decode()
|
2019-09-12 20:11:25 +00:00
|
|
|
return decoded
|
|
|
|
|
|
|
|
if address_family == "ipv6":
|
|
|
|
HTTPServer.address_family = socket.AF_INET6
|
2019-02-22 10:55:12 +00:00
|
|
|
httpd = HTTPServer(server_address, TSVHTTPHandler)
|
2019-09-12 20:11:25 +00:00
|
|
|
if schema == "https":
|
2022-03-22 16:39:58 +00:00
|
|
|
httpd.socket = ssl.wrap_socket(
|
|
|
|
httpd.socket, certfile=cert_path, server_side=True
|
|
|
|
)
|
2019-02-22 10:55:12 +00:00
|
|
|
httpd.serve_forever()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2022-03-22 16:39:58 +00:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description="Simple HTTP server returns data from file"
|
|
|
|
)
|
2019-02-22 10:55:12 +00:00
|
|
|
parser.add_argument("--host", default="localhost")
|
|
|
|
parser.add_argument("--port", default=5555, type=int)
|
2019-09-12 20:11:25 +00:00
|
|
|
parser.add_argument("--data-path", required=True)
|
|
|
|
parser.add_argument("--schema", choices=("http", "https"), required=True)
|
2019-02-22 10:55:12 +00:00
|
|
|
parser.add_argument("--cert-path", default="./fake_cert.pem")
|
2022-03-22 16:39:58 +00:00
|
|
|
parser.add_argument("--address-family", choices=("ipv4", "ipv6"), default="ipv4")
|
2019-02-22 10:55:12 +00:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
start_server(
|
|
|
|
(args.host, args.port),
|
|
|
|
args.data_path,
|
|
|
|
args.schema,
|
|
|
|
args.cert_path,
|
|
|
|
args.address_family,
|
|
|
|
)
|