# Copyright (c) 2018, George Tokmaji # Permission to use, copy, modify, and/or distribute this software for any # purpose with or without fee is hereby granted, provided that the above # copyright notice and this permission notice appear in all copies. # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. import sys import os, re, json, math import requests from bson.objectid import ObjectId from bottle import install, run, Bottle, static_file, response, hook, JSONPlugin, get, post, error import threading class ParryEncoder(json.JSONEncoder): _default = json.JSONEncoder.default def default(self, obj): if isinstance(obj, ObjectId): return str(obj) return self._default(obj) install(JSONPlugin(json_dumps=lambda s: json.dumps(s, cls=ParryEncoder))) os.chdir(os.path.dirname(__file__)) from .auth import * def calculateHashForResource(resource : requests.Response) -> object: hashobj = hashlib.sha1() calculateHashForFile(resource.raw, hashobj) if "content-length" not in resource.headers: resource.headers["Content-Length"] = resource.raw.tell() return hashobj def calculateHashForFile(file, hashobj : object = None) -> object: if hashobj is None: hashobj = hashlib.sha1() while True: block = file.read(BLOCKSIZE) if not block: break hashobj.update(block) return hashobj def notAllowed(): raise HTTPResponse(f"Cannot {request.method} {request.path}") @route("", method="OPTIONS") def options(path): return HTTPResponse(status=204, headers={"Access-Control-Allow-Origin" : "*", "Access-Control-Allow-Methods" : "GET, POST, HEAD, PUT, PATCH, DELETE", "Access-Control-Allow-Headers" : "*"}) @hook("after_request") def enable_cors(): response.headers["Access-Control-Allow-Origin"] = "*" response.headers["Access-Control-Allow-Methods"] = "POST, GET, PUT, DELETE, PATCH, HEAD" @error(500) def internal_error(error): if request.json is None: error.exception = None error._status_code = 400 error._body = "Invalid JSON specified" def request_data(): return request.json if request.json else request.forms