Skip to content

Commit

Permalink
fixing comments from Johannes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenifer Tabita Ciuciu-Kiss committed Jul 2, 2024
1 parent c275ab1 commit 3658201
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 301 deletions.
133 changes: 3 additions & 130 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,142 +22,15 @@ cp ca-signing-key.pem ~/ontology-time-machine/ca-signing-key.pem

### Curl tests:
- curl -x http://0.0.0.0:8899 --cacert ca-cert.pem http://www.google.com
- curl -x http://0.0.0.0:8899 -H "Accept: text/turtle" --cacert ca-cert.pem http://ontologi.es/days#
- curl -x http://0.0.0.0:8899 -H "Accept: text/turtle" --cacert ca-cert.pem http://linked-web-apis.fit.cvut.cz/ns/core
- curl -x http://0.0.0.0:8899 --cacert ca-cert.pem https://www.w3id.org/simulation/ontology/
- curl -x http://0.0.0.0:8899 --cacert ca-cert.pem https://www.w3.org/ns/ldt#
- curl -x http://0.0.0.0:8899 --cacert ca-cert.pem https://raw.githubusercontent.com/br0ast/simulationontology/main/Ontology/simulationontology.owl
- curl -x http://0.0.0.0:8899 -H "Accept: text/turtle" --cacert ca-cert.pem http://bblfish.net/work/atom-owl/2006-06-06/
- curl -x http://0.0.0.0:8899 -H "Accept: text/turtle" --cacert ca-cert.pem http://purl.org/makolab/caont/


### Not working:
- curl -x http://0.0.0.0:8899 --cacert ca-cert.pem https://vocab.eccenca.com/auth/
- curl -x http://0.0.0.0:8899 -H "Accept: text/turtle" --cacert ca-cert.pem http://dbpedia.org/ontology/Person




from proxy.http.proxy import HttpProxyBasePlugin
from proxy.http.parser import HttpParser, httpParserTypes
from proxy.common.utils import build_http_response
from proxy.http.methods import HttpMethods
from ontologytimemachine.utils.utils import proxy_logic_http, proxy_logic_https
from ontologytimemachine.utils.utils import check_if_archivo_ontology_requested
from ontologytimemachine.utils.utils import get_headers_and_expected_type
from requests.exceptions import SSLError, Timeout, ConnectionError, RequestException
from http.client import responses
import proxy
import sys
import requests
import logging


logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)


class OntologyTimeMachinePlugin(HttpProxyBasePlugin):
#def __init__(self, *args, **kwargs):
# super().__init__(*args, **kwargs)


def before_upstream_connection(self, request: HttpParser):
logger.debug('Before upstream')
print(request.method)
scheme = 'https' if request.method == b'CONNECT' else 'http'
if scheme == 'https':
logger.debug('The request is HTTPS, forward as it is')
return request

ontology_request = check_if_archivo_ontology_requested(request)
if ontology_request:
logger.debug('The request is for an ontology')
try:
ontology_url = str(request._url)
headers, _ = get_headers_and_expected_type(request)
response = requests.get(ontology_url, headers=headers)
if response.status_code == 502:
logger.error('Received 502 Bad Gateway error')
response = proxy_logic_http(request)
logger.debug('Queue response')
self.queue_response(response)
return None
else:
logger.debug('The request is correct')
return request
except (SSLError, Timeout, ConnectionError, RequestException) as e:
logger.error(f'Network-related exception occurred {e}')
response = proxy_logic_http(request)
logger.debug('Queue response')
self.queue_response(response)
return None
return request


def handle_client_request(self, request: HttpParser):
logger.debug('HTTP call')
logger.debug(request._url)

ontology_request = check_if_archivo_ontology_requested(request)
if not ontology_request:
logger.info('No ontology is asked, forward original request')
return request
response = proxy_logic_http(request)
self.queue_response(response)

return None


def handle_upstream_chunk(self, chunk: memoryview):
logger.info('HTTPS call')

try:
# Parse the HTTP response to handle different cases
parser = HttpParser(httpParserTypes.RESPONSE_PARSER)
parser.parse(memoryview(chunk))
code = int(parser.code.decode('utf-8'))
if code >= 100 and code < 200:
return chunk
elif code >= 201 and code <= 204:
return chunk
elif code == 451:
return chunk
else:
response = proxy_logic_https(parser)
logger.debug('Queue response')
self.queue_response(response)
return None
except UnicodeDecodeError:
logger.warning('Received non-text chunk, cannot decode')
except Exception as e:
logger.error(f'Exception occurred while handling upstream chunk: {e}')
return chunk

def queue_response(self, response):
self.client.queue(
build_http_response(
response.status_code,
reason=bytes(responses[response.status_code], 'utf-8'),
headers={
b'Content-Type': bytes(response.headers.get('Content-Type'), 'utf-8')
},
body=response.content
)
)


if __name__ == '__main__':
sys.argv += [
'--ca-key-file', 'ca-key.pem',
'--ca-cert-file', 'ca-cert.pem',
'--ca-signing-key-file', 'ca-signing-key.pem',
]
sys.argv += [
'--hostname', '0.0.0.0',
'--port', '8899',
'--plugins', __name__ + '.OntologyTimeMachinePlugin'
]
logger.info("Starting OntologyTimeMachineProxy server...")
proxy.main()
### Not working:
- curl -x http://0.0.0.0:8899 -H "Accept: text/turtle" --cacert ca-cert.pem http://ontologi.es/days#
47 changes: 12 additions & 35 deletions ontologytimemachine/custom_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from proxy.http.parser import HttpParser, httpParserTypes
from proxy.common.utils import build_http_response
from proxy.http.methods import HttpMethods
from ontologytimemachine.utils.utils import proxy_logic_http, proxy_logic_https
from ontologytimemachine.utils.utils import proxy_logic
from ontologytimemachine.utils.utils import check_if_archivo_ontology_requested
from ontologytimemachine.utils.utils import get_headers_and_expected_type
from ontologytimemachine.utils.utils import get_ontology_from_request
Expand All @@ -18,74 +18,51 @@
PORT = '8899'


logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)


class OntologyTimeMachinePlugin(HttpProxyBasePlugin):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)


def before_upstream_connection(self, request: HttpParser):
logger.debug('Before upstream')
logger.debug(request.method)
logger.info('Before upstream connection hook')
logger.info(f'Request method: {request.method} - Request host: {request.host} - Request path: {request.path} - Request headers: {request.headers}')

scheme = 'https' if request.method == b'CONNECT' else 'http'
if scheme == 'https':
logger.debug('The request is HTTPS, forward as it is')
logger.debug(f'Request host: {request.host}')
logger.debug(f'Request path: {request.path}')
return request

ontology_request = check_if_archivo_ontology_requested(request)
if ontology_request:
logger.debug('The request is for an ontology')
try:
ontology_url = str(request._url)
headers, _ = get_headers_and_expected_type(request)
response = requests.get(ontology_url, headers=headers, timeout=5)
if response.status_code == 502:
logger.error('Received 502 Bad Gateway error')
response = proxy_logic_http(request)
logger.debug('Queue response')
self.queue_response(response)
return None
else:
logger.debug('The request is correct')
return request
except (SSLError, Timeout, ConnectionError, RequestException) as e:
logger.error(f'Network-related exception occurred {e}')
response = proxy_logic_http(request)
logger.debug('Queue response')
self.queue_response(response)
return None
response = proxy_logic(request)
self.queue_response(response)
return None
return request


def handle_client_request(self, request: HttpParser):
logger.debug('HTTP call')
logger.info('Handle client request hook')
logger.info(f'Request method: {request.method} - Request host: {request.host} - Request path: {request.path} - Request headers: {request.headers}')

logger.debug(request.method)
scheme = 'https' if request.method == b'CONNECT' else 'http'
if scheme == 'https':
logger.debug('The request is HTTPS, forward as it is')
return request

ontology_request = check_if_archivo_ontology_requested(request)
if not ontology_request:
logger.info('No ontology is asked, forward original request')
logger.info('The requested IRI is not part of DBpedia Archivo')
return request

logger.debug('Call proxy logic')
response = proxy_logic_http(request)
response = proxy_logic(request)
self.queue_response(response)

return None


def handle_upstream_chunk(self, chunk: memoryview):
logger.info('HTTPS call')

return chunk


Expand Down
Loading

0 comments on commit 3658201

Please sign in to comment.