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

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

import socket
import errno

from exaproxy.network.errno_list import errno_block

def ishex (s):
	return bool(s) and not bool(s.strip('0123456789abcdefABCDEF'))

def count_quotes (data):
	return data.count('"') - data.count('\\"')

class Client (object):
	eor = ['\r\n\r\n', '\n\n']

	def __init__(self, name, sock, peer, logger):
		self.name = name
		self.sock = sock
		self.peer = peer
		self.reader = self._read(sock)
		self.writer = self._write(sock)

		self.log = logger
		self.blockupload = None

		# start the _read coroutine
		self.reader.next()

	def checkRequest (self, r_buffer, seek=0):
		# XXX: max buffer size

		buff = ''
		for eor in self.eor:
			pos = r_buffer[seek:].find(eor)
			buff = r_buffer[:seek+pos] if pos >=0 else ''

			if buff:
				if not count_quotes(buff) % 2:
					request, r_buffer = buff + eor, r_buffer[seek+pos+len(eor):]
					break
				else:
					seek += pos + len(eor)

		else:
			request, r_buffer = '', r_buffer

		return request, r_buffer, seek

	def checkChunkSize (self, r_buffer):
		chunked = True
		size = 0

		if '\r' in r_buffer:
			eol = '\r\n'
		else:
			eol = '\n'

		# make sure our buffer does not become massive
		max_len = len('ffff') + (2 * len(eol))
		sc = ';'

		while r_buffer:
			if sc in r_buffer:
				size_s, r_buffer = r_buffer.split(sc, 1)
				eol = sc

			elif eol in r_buffer:
				size_s, r_buffer = r_buffer.split(eol, 1)

			else:
				size_s = None

			if size_s is not None:
				if ishex(size_s):
					chunk_size = int(size_s, 16)

					if chunk_size > 0:
						# semi colon can follow only the end of chunked data
						if eol == sc:
							size = None
							break

						size += chunk_size + len(size_s) + (2 * len(eol))
						r_buffer = r_buffer[chunk_size + len(eol):]
					else:
						size += chunk_size + len(eol)
						chunked = False
						break
				else:
					size = None
					break

			elif len(r_buffer) > max_len:
				size = None
				break

			elif not ishex(r_buffer.rstrip(eol)):
				size = None
				break

			else:
				size = 0
				break

		return chunked, size


	def _read (self, sock, read_size=64*1024):<commit_msg>make some of the code easier to read<commit_after># encoding: utf-8
"""
server.py

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

import socket
import errno

from exaproxy.network.errno_list import errno_block

def ishex (s):
	return bool(s) and not bool(s.strip('0123456789abcdefABCDEF'))

def count_quotes (data):
	return data.count('"') - data.count('\\"')

class Client (object):
	eor = ['\r\n\r\n', '\n\n']

	def __init__(self, name, sock, peer, logger):
		self.name = name
		self.sock = sock
		self.peer = peer
		self.reader = self._read(sock)
		self.writer = self._write(sock)

		self.log = logger
		self.blockupload = None

		# start the _read coroutine
		self.reader.next()

	def checkRequest (self, r_buffer, seek=0):
		# XXX: max buffer size
		for eor in self.eor:
			pos = r_buffer[seek:].find(eor)
			if pos == -1: continue

			buff = r_buffer[:seek+pos]
			if not buff: continue

			if not count_quotes(buff) % 2:  # we have matching pairs
				return buff + eor, r_buffer[seek+pos+len(eor):], seek

			seek += pos + len(eor)

		return '', r_buffer, seek


	def checkChunkSize (self, r_buffer):
		# return a tuple : bool, length
		# * the bool is : is there more chunk to come
		# * the len contains the size of the chunk(s) extracted
		#   a size of None means that we could not decode the data

		total_len = 0

		while r_buffer:
			if not '\n' in r_buffer:
				return True,total_len or None

			header,r_buffer = r_buffer.split('\n', 1)
			len_header = len(header) + 1

			if header.endswith('\r'):
				header = header[:-1]
				len_eol = 2
			else:
				len_eol = 1

			if ';' in header:
				header = header.split(';',1)[0]

			if not ishex(header):
				# XXX: huston the data is invalid - we should cause a read abortion
				return True,None

			len_chunk = int(header, 16)

			if len_chunk > 0xFFFF:
				# XXX: huston we have a problem - we should cause a read abortion
				return True,None

			if len_chunk == 0:
				total_len += len_header + len_eol
				return False, total_len
			else:
				total = len_header + len_chunk + len_eol
				total_len += total
				r_buffer = r_buffer[total:]

		return True,total_len or None


	def _read (self, sock, read_size=64*1024):
{'fraction_non_alphanumeric': 0.12084726867335563, 'fraction_numerical': 0.02073578595317726, 'mean_word_length': 2.8705780845556514, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '2476288', 'n_tokens_mistral': 2008, 'n_tokens_neox': 1611, 'n_words': 497}