<commit_before># encoding: utf-8
"""
parser.py

Created by <NAME> on 2012-02-27.
Copyright (c) 2011-2013 Exa Networks. All rights reserved.
"""

import traceback

from exaproxy.util.log.logger import Logger
from exaproxy.network.functions import isip

from .request import Request
from .headers import Headers,ExpectationFailed,InvalidRequest

class HostMismatch(Exception):
	pass

class HTTP (object):
	http_versions = ('1.0', '1.1')

	def __init__(self,configuration,headers,remote_ip):
		self.raw = headers
		self.client = remote_ip
		self.proxy_name = "X-Proxy-Version: ExaProxy version %s" % configuration.proxy.version
		self.forward = configuration.http.forward
		self.log = Logger('header', configuration.log.header)
		self.reply_code = 0
		self.reply_string = ''
		# NOTE: ugly ugly remove me
		self.request = None
		self.headers = None
		self.expect = configuration.http.expect
		self.validated = False

	def parse (self,transparent):
		self.log.debug('parsing header [[%s]]' % str(self.raw).replace('\t','\\t').replace('\r','\\r').replace('\n','\\n\n'))

		try:
			if '\r' in self.raw:
				self.separator = '\r\n'
			else:
				self.separator = '\n'

			self.request = Request(self.raw).parse()
			self.headers = Headers(self.request.version,self.separator,self.expect).parse(transparent,self.request.remaining)

			self.headerhost = self.extractHost()

			if self.request.host and self.request.host != '*':
				self.host = self.request.host
			else:
				# can raise KeyError, but host is a required header
				self.host = self.headerhost
			self.port = self.request.port

			if not self.host:
				raise InvalidRequest('can not forward the request, we have no host information')

			# That does not let us use ICAP and connect to redirect things :p
			#if self.headerhost:
			#	if self.host != self.headerhost and self.request.method != 'OPTIONS' and self.host != '*':
			#		raise HostMismatch, 'Make up your mind: %s - %s' % (self.host, self.headerhost)

			# Is this the best place to add headers?
			self.headers.replace('x-proxy-version',self.proxy_name)

			if self.forward:
				client = self.headers.get(self.forward, ':%s' % self.client)[0].split(':', 1)[1].split(',')[-1].strip()
				if isip(client):
					self.client = client
				#else:
				#	self.log.info('Invalid address in Client identifier header: %s' % client)

			# encoding can contain trailers and other information see RFC2516 section 14.39
			encoding = self.headers.get('transfer-encoding', [':'])[0].split(':', 1)[1].strip()
			if not encoding:<commit_msg>Remove x-forwarded-for when running in transparent mode<commit_after># encoding: utf-8
"""
parser.py

Created by <NAME> on 2012-02-27.
Copyright (c) 2011-2013 Exa Networks. All rights reserved.
"""

import traceback

from exaproxy.util.log.logger import Logger
from exaproxy.network.functions import isip

from .request import Request
from .headers import Headers,ExpectationFailed,InvalidRequest

class HostMismatch(Exception):
	pass

class HTTP (object):
	http_versions = ('1.0', '1.1')

	def __init__(self,configuration,headers,remote_ip):
		self.raw = headers
		self.client = remote_ip
		self.proxy_name = "X-Proxy-Version: ExaProxy version %s" % configuration.proxy.version
		self.forward = configuration.http.forward
		self.log = Logger('header', configuration.log.header)
		self.reply_code = 0
		self.reply_string = ''
		# NOTE: ugly ugly remove me
		self.request = None
		self.headers = None
		self.expect = configuration.http.expect
		self.validated = False

	def parse (self,transparent):
		self.log.debug('parsing header [[%s]]' % str(self.raw).replace('\t','\\t').replace('\r','\\r').replace('\n','\\n\n'))

		try:
			if '\r' in self.raw:
				self.separator = '\r\n'
			else:
				self.separator = '\n'

			self.request = Request(self.raw).parse()
			self.headers = Headers(self.request.version,self.separator,self.expect).parse(transparent,self.request.remaining)

			self.headerhost = self.extractHost()

			if self.request.host and self.request.host != '*':
				self.host = self.request.host
			else:
				# can raise KeyError, but host is a required header
				self.host = self.headerhost
			self.port = self.request.port

			if not self.host:
				raise InvalidRequest('can not forward the request, we have no host information')

			# That does not let us use ICAP and connect to redirect things :p
			#if self.headerhost:
			#	if self.host != self.headerhost and self.request.method != 'OPTIONS' and self.host != '*':
			#		raise HostMismatch, 'Make up your mind: %s - %s' % (self.host, self.headerhost)

			# Is this the best place to add headers?
			self.headers.replace('x-proxy-version',self.proxy_name)

			if self.forward:
				client = self.headers.get(self.forward, ':%s' % self.client)[0].split(':', 1)[1].split(',')[-1].strip()
				if isip(client):
					self.client = client
				#else:
				#	self.log.info('Invalid address in Client identifier header: %s' % client)

				# Remove x-forwarded-for when running in transparent mode
				if transparent:
					self.headers.pop(self.forward, None)

			# encoding can contain trailers and other information see RFC2516 section 14.39
			encoding = self.headers.get('transfer-encoding', [':'])[0].split(':', 1)[1].strip()
			if not encoding:
{'fraction_non_alphanumeric': 0.14190349036810987, 'fraction_numerical': 0.014114056837688346, 'mean_word_length': 3.767272727272727, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2857643', 'n_tokens_mistral': 1925, 'n_tokens_neox': 1667, 'n_words': 444}