Skip to content

Commit

Permalink
adjust the proxy logic based on the canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenifer Tabita Ciuciu-Kiss committed Jul 5, 2024
1 parent 3658201 commit 2b13d78
Show file tree
Hide file tree
Showing 4 changed files with 279 additions and 31 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ cp ca-signing-key.pem ~/ontology-time-machine/ca-signing-key.pem

### Not working:
- curl -x http://0.0.0.0:8899 -H "Accept: text/turtle" --cacert ca-cert.pem http://ontologi.es/days#


# https://archivo.dbpedia.org/download?o=http://linked-web-apis.fit.cvut.cz/ns/core&v=2020.07.16-115638&versionMatching=timeStampClosest
35 changes: 24 additions & 11 deletions ontologytimemachine/custom_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
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
from ontologytimemachine.utils.utils import proxy_logic, parse_arguments
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
from ontologytimemachine.utils.mock_responses import mock_response_403
from requests.exceptions import SSLError, Timeout, ConnectionError, RequestException
from http.client import responses
import proxy
import sys
import requests
import logging


Expand All @@ -24,19 +22,34 @@
class OntologyTimeMachinePlugin(HttpProxyBasePlugin):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
(self.ontoFormat, self.ontoVersion, self.only_ontologies,
self.https_intercept, self.inspect_redirects, self.forward_headers,
self.subject_binary_search_threshold) = parse_arguments()


def before_upstream_connection(self, request: HttpParser):
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':
return request
if request.method == b'CONNECT':
logger.info(f'HTTPS interception mode: {self.https_intercept}')
# Only intercept if interception is enabled
if self.https_intercept in ['all', 'archivo']:
return request
else:
return None


ontology_request = check_if_archivo_ontology_requested(request)
# If only ontology mode, return None in all other cases
if self.only_ontologies and not ontology_request:
logger.warning('Request denied: not an ontology request and only ontologies mode is enabled')
self.queue_response(mock_response_403)
return None

if ontology_request:
logger.debug('The request is for an ontology')
response = proxy_logic(request)
response = proxy_logic(request, self.ontoFormat, self.ontoVersion)
self.queue_response(response)
return None
return request
Expand All @@ -47,16 +60,15 @@ def handle_client_request(self, request: HttpParser):
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':
if request.method == b'CONNECT':
return request

ontology_request = check_if_archivo_ontology_requested(request)
if not ontology_request:
logger.info('The requested IRI is not part of DBpedia Archivo')
return request

response = proxy_logic(request)
response = proxy_logic(request, self.ontoFormat, self.ontoVersion)
self.queue_response(response)

return None
Expand All @@ -80,6 +92,7 @@ def queue_response(self, response):


if __name__ == '__main__':

sys.argv += [
'--ca-key-file', 'ca-key.pem',
'--ca-cert-file', 'ca-cert.pem',
Expand Down
38 changes: 38 additions & 0 deletions ontologytimemachine/utils/mock_responses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import requests


def mock_response_200():
mock_response = requests.Response()
mock_response.status_code = 200
mock_response.url = 'https://example.com/success'
mock_response.headers['Content-Type'] = 'text/html'
mock_response._content = b'<html><body><h1>To be implemented</h1></body></html>'
return mock_response


def mock_response_403():
mock_response = requests.Response()
mock_response.status_code = 403
mock_response.url = 'https://example.com/forbidden'
mock_response.headers['Content-Type'] = 'text/html'
mock_response._content = b'<html><body><h1>403 Forbidden</h1></body></html>'
return mock_response



def mock_response_404():
mock_response = requests.Response()
mock_response.status_code = 404
mock_response.url = 'https://example.com/resource-not-found'
mock_response.headers['Content-Type'] = 'text/html'
mock_response._content = b'<html><body><h1>404 Not Found</h1></body></html>'
return mock_response


def mock_response_500():
mock_response = requests.Response()
mock_response.status_code = 500
mock_response.url = 'https://example.com/internal-server-error'
mock_response.headers['Content-Type'] = 'text/html'
mock_response._content = b'<html><body><h1>500 Internal Server Error</h1></body></html>'
return mock_response
Loading

0 comments on commit 2b13d78

Please sign in to comment.