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

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

from .response import ResponseEncoder as Respond
from exaproxy.icap.parser import ICAPParser

from .worker import Redirector


class ICAPRedirector (Redirector):
	ICAPParser = ICAPParser

	def __init__ (self, configuration, name, program, protocol):
		self.icap_parser = self.ICAPParser(configuration)
		self.protocol = protocol
		self.icap = protocol[len('icap://'):].split('/')[0]

		Redirector.__init__ (self, configuration, name, program, protocol)

	def readChildResponse (self):
		try:
			header_string = self.process.stdout.readline()
			while True:
				line = self.process.stdout.readline()
				header_string += line

				if not line:
					header_string = None
					break

				elif not line.rstrip():
					break


			header = self.icap_parser.parseResponseHeader(header_string)

			body_string = ''
			bytes_to_read = header.content_length
			read_bytes = 0

			while bytes_to_read > 0:
				while read_bytes < bytes_to_read:
					headers_s = self.process.stdout.read(bytes_to_read-read_bytes)
					body_string += headers_s
					read_bytes += len(headers_s)

				if header.body_complete:
					bytes_to_read = 0

				else:
					line = self.process.stdout.readline()
					bytes_to_read = int(line.strip(), 16)
					read_bytes = 0

			if bytes_to_read != 0:
				header_string = None
				body_string = None

			elif header.code is None:
				header_string = None
				body_string = None

			elif header.code != '304' and bytes_to_read is None:
				header_string = None
				body_string = None

		except IOError, e:
			header_string = None
			body_string = None

		try:
			child_stderr = self.process.stderr.read(4096)
		except Exception:
			child_stderr = ''

		if child_stderr:
			header_string = None
			body_string = None

		if header_string is None:
			return None

		return self.icap_parser.continueResponse(header, body_string)

	def createChildRequest (self, peer, message, http_header):
		return self.createICAPRequest(peer, message, None, http_header)

	def createICAPRequest (self, peer, message, icap_message, http_header):
		username = icap_message.headers.get('x-authenticated-user', '').strip() if icap_message else None
		groups = icap_message.headers.get('x-authenticated-groups', '').strip() if icap_message else None
		ip_addr = icap_message.headers.get('x-client-ip', '').strip() if icap_message else None
		customer = icap_message.headers.get('x-customer-name', '').strip() if icap_message else None

		icap_request = """\
REQMOD %s ICAP/1.0
Host: %s
Pragma: transport=%s
Pragma: client=%s
Pragma: host=%s
Pragma: path=%s
Pragma: method=%s""" % (

			self.protocol, self.icap, message.request.protocol,
			peer, message.request.host, message.request.path, message.request.method,
			)

		if ip_addr:
			icap_request += """
X-Client-IP: %s""" % ip_addr

		if username:
			icap_request += """
X-Authenticated-User: %s""" % username

		if groups:
			icap_request += """
X-Authenticated-Groups: %s""" % groups

		if customer:
			icap_request += """
X-Customer-Name: %s""" % customer

		return icap_request + """
Encapsulated: req-hdr=0, null-body=%d

%s""" % (len(http_header), http_header)

<commit_msg>read host from http.message<commit_after># encoding: utf-8
"""
process.py

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

from .response import ResponseEncoder as Respond
from exaproxy.icap.parser import ICAPParser

from .worker import Redirector


class ICAPRedirector (Redirector):
	ICAPParser = ICAPParser

	def __init__ (self, configuration, name, program, protocol):
		self.icap_parser = self.ICAPParser(configuration)
		self.protocol = protocol
		self.icap = protocol[len('icap://'):].split('/')[0]

		Redirector.__init__ (self, configuration, name, program, protocol)

	def readChildResponse (self):
		try:
			header_string = self.process.stdout.readline()
			while True:
				line = self.process.stdout.readline()
				header_string += line

				if not line:
					header_string = None
					break

				elif not line.rstrip():
					break


			header = self.icap_parser.parseResponseHeader(header_string)

			body_string = ''
			bytes_to_read = header.content_length
			read_bytes = 0

			while bytes_to_read > 0:
				while read_bytes < bytes_to_read:
					headers_s = self.process.stdout.read(bytes_to_read-read_bytes)
					body_string += headers_s
					read_bytes += len(headers_s)

				if header.body_complete:
					bytes_to_read = 0

				else:
					line = self.process.stdout.readline()
					bytes_to_read = int(line.strip(), 16)
					read_bytes = 0

			if bytes_to_read != 0:
				header_string = None
				body_string = None

			elif header.code is None:
				header_string = None
				body_string = None

			elif header.code != '304' and bytes_to_read is None:
				header_string = None
				body_string = None

		except IOError, e:
			header_string = None
			body_string = None

		try:
			child_stderr = self.process.stderr.read(4096)
		except Exception:
			child_stderr = ''

		if child_stderr:
			header_string = None
			body_string = None

		if header_string is None:
			return None

		return self.icap_parser.continueResponse(header, body_string)

	def createChildRequest (self, peer, message, http_header):
		return self.createICAPRequest(peer, message, None, http_header)

	def createICAPRequest (self, peer, message, icap_message, http_header):
		username = icap_message.headers.get('x-authenticated-user', '').strip() if icap_message else None
		groups = icap_message.headers.get('x-authenticated-groups', '').strip() if icap_message else None
		ip_addr = icap_message.headers.get('x-client-ip', '').strip() if icap_message else None
		customer = icap_message.headers.get('x-customer-name', '').strip() if icap_message else None

		icap_request = """\
REQMOD %s ICAP/1.0
Host: %s
Pragma: transport=%s
Pragma: client=%s
Pragma: host=%s
Pragma: path=%s
Pragma: method=%s""" % (

			self.protocol, self.icap, message.request.protocol,
			peer, message.host, message.request.path, message.request.method,
			)

		if ip_addr:
			icap_request += """
X-Client-IP: %s""" % ip_addr

		if username:
			icap_request += """
X-Authenticated-User: %s""" % username

		if groups:
			icap_request += """
X-Authenticated-Groups: %s""" % groups

		if customer:
			icap_request += """
X-Customer-Name: %s""" % customer

		return icap_request + """
Encapsulated: req-hdr=0, null-body=%d

%s""" % (len(http_header), http_header)


{'fraction_non_alphanumeric': 0.11865443425076452, 'fraction_numerical': 0.010703363914373088, 'mean_word_length': 3.823746312684366, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '2478899', 'n_tokens_mistral': 2560, 'n_tokens_neox': 2146, 'n_words': 529}