<commit_before>import os
import re
import requests
from django.contrib.auth.decorators import login_required
from django.shortcuts import get_object_or_404
from django.http import HttpResponse, StreamingHttpResponse, QueryDict
from wsgiref.util import FileWrapper
from django.conf import settings

from django.core.exceptions import PermissionDenied
from xbrowse_server.base.models import Project, Individual
from xbrowse_server.decorators import log_request


# Hop-by-hop HTTP response headers shouldn't be forwarded.
# More info at: http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.5.1
EXCLUDE_HTTP_HEADERS = set([
        'connection', 'keep-alive', 'proxy-authenticate',
        'proxy-authorization', 'te', 'trailers', 'transfer-encoding',
        'upgrade', 'content-encoding'
])


@login_required
@log_request('fetch_igv_track')
def fetch_igv_track(request, project_id, igv_track_name):

    # make sure user has permissions to access the project
    project = get_object_or_404(Project, project_id=project_id)
    if not project.can_view(request.user):
        raise PermissionDenied

    individual_id = igv_track_name.split('.')[0]
    individuals = Individual.objects.filter(project=project, indiv_id=individual_id)
    if not individuals:
        return HttpResponse("invalid")
    individual = individuals[0]
    if not individual.bam_file_path:
        return HttpResponse("reads not available for " + individual)

    #print("Request: %s %s" % (request.method, request.path))
    absolute_path = os.path.join(settings.READ_VIZ_BAM_PATH, individual.bam_file_path)
    if igv_track_name.endswith(".bai"):
        absolute_path += ".bai"

    #print("Proxy Request: %s %s" % (request.method, absolute_path))
    if absolute_path.startswith('http'):
        return fetch_proxy(request, absolute_path)
    else:
        return fetch_local_file(request, absolute_path)


def fetch_proxy(request, path):
    """
    Retrieve the HTTP headers from a WSGI environment dictionary.  See
    https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.META
    """
    # based on https://github.com/mjumbewu/django-proxy/blob/master/proxy/views.py
    # forward common HTTP headers after converting them from Django's all-caps syntax (eg. 'HTTP_RANGE') back to regular HTTP syntax (eg. 'Range')
    headers = { key[5:].replace('_', '-').title() : value for key, value in request.META.iteritems() if key.startswith('HTTP_') and key != 'HTTP_HOST'}
    response = requests.request(request.method, path, auth=('xbrowse-bams', 'xbrowse-bams'), headers=headers)

    proxy_response = HttpResponse(response.content, status=response.status_code)

    for key, value in response.headers.iteritems():<commit_msg>disable cert checking for readviz queries<commit_after>import os
import re
import requests
from django.contrib.auth.decorators import login_required
from django.shortcuts import get_object_or_404
from django.http import HttpResponse, StreamingHttpResponse, QueryDict
from wsgiref.util import FileWrapper
from django.conf import settings

from django.core.exceptions import PermissionDenied
from xbrowse_server.base.models import Project, Individual
from xbrowse_server.decorators import log_request

# Hop-by-hop HTTP response headers shouldn't be forwarded.
# More info at: http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.5.1
EXCLUDE_HTTP_HEADERS = set([
        'connection', 'keep-alive', 'proxy-authenticate',
        'proxy-authorization', 'te', 'trailers', 'transfer-encoding',
        'upgrade', 'content-encoding'
])


@login_required
@log_request('fetch_igv_track')
def fetch_igv_track(request, project_id, igv_track_name):

    # make sure user has permissions to access the project
    project = get_object_or_404(Project, project_id=project_id)
    if not project.can_view(request.user):
        raise PermissionDenied

    individual_id = igv_track_name.split('.')[0]
    individuals = Individual.objects.filter(project=project, indiv_id=individual_id)
    if not individuals:
        return HttpResponse("invalid")
    individual = individuals[0]
    if not individual.bam_file_path:
        return HttpResponse("reads not available for " + individual)

    #print("Request: %s %s" % (request.method, request.path))
    absolute_path = os.path.join(settings.READ_VIZ_BAM_PATH, individual.bam_file_path)
    if igv_track_name.endswith(".bai"):
        absolute_path += ".bai"

    #print("Proxy Request: %s %s" % (request.method, absolute_path))
    if absolute_path.startswith('http'):
        return fetch_proxy(request, absolute_path)
    else:
        return fetch_local_file(request, absolute_path)


def fetch_proxy(request, path):
    """
    Retrieve the HTTP headers from a WSGI environment dictionary.  See
    https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.META
    """
    # based on https://github.com/mjumbewu/django-proxy/blob/master/proxy/views.py
    # forward common HTTP headers after converting them from Django's all-caps syntax (eg. 'HTTP_RANGE') back to regular HTTP syntax (eg. 'Range')
    headers = { key[5:].replace('_', '-').title() : value for key, value in request.META.iteritems() if key.startswith('HTTP_') and key != 'HTTP_HOST'}
    response = requests.request(request.method, path, auth=('xbrowse-bams', 'xbrowse-bams'), headers=headers, verify=False)

    proxy_response = HttpResponse(response.content, status=response.status_code)

    for key, value in response.headers.iteritems():
{'fraction_non_alphanumeric': 0.11072914781979373, 'fraction_numerical': 0.008684639044689704, 'mean_word_length': 4.539078156312625, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6187240', 'n_tokens_mistral': 1737, 'n_tokens_neox': 1661, 'n_words': 403}