<commit_before>from BaseHTTPServer import BaseHTTPRequestHandler
from inspect import getargspec
import re
import cgi
import sys
from sqlite3 import OperationalError
import traceback

from Session import Session, timestamp
from Box import Box, ErrorBox
from code import showCode, highlightCode
from ResponseWriter import ResponseWriter
from FrameworkException import FrameworkException
from utils import *

handlers = {'get': {}, 'post': {}}

@globalize
def get(index):
	def wrap(f):
		handlers['get'][re.compile("^%s$" % index)] = f
		return f
	return wrap

@globalize
def post(index):
	def wrap(f):
		handlers['post'][re.compile("^%s$" % index)] = f
		return f
	return wrap

def log(str, target = sys.stdout):
	print >> target, str

class HTTPHandler(BaseHTTPRequestHandler):
	def __init__(self, request, address, server):
		self.session = None
		self.replacements = {}
		# self.leftMenu = LeftMenu()
		BaseHTTPRequestHandler.__init__(self, request, address, server)

	def buildResponse(self, method, data):
		# log("Building response")
		self.contentType = 'text/html'
		self.forceDownload = False

		path = self.path
		query = {}
		# log("Original path: %s" % path)

		writer = ResponseWriter()

		request = {'path': [], 'wrappers': True, 'code': 200}
		try: # raise DoneRendering; starts here to catch self.error calls
			self.title(None)

			# Pull out get variables
			if '?' in path:
				path, queryStr = path.split('?', 1)
				parts = queryStr.split('&')
				query = dict(map(lambda x: x.split('=') if '=' in x else (x, True), parts))
				if len(parts) != len(query):
					self.error("Invalid request", "Query key collision")

			# Validate query keys (can't start with p_ to avoid collisions with post variables)
			for key in query:
				if key[:2] == 'p_':
					self.error("Invalid request", "Illegal query key: %s" % key)

			# Add p_ prefix to post variables
			if data:
				query.update(dict([('p_' + k, v) for (k, v) in data.iteritems()]))

			# Convert foo[bar] keys to maps (epic nesting!)
			oldQuery = query
			query = {}
			for key in oldQuery:
				match = re.match('(.*)\\[(.*)\\]', key)
				if match:
					name, realKey = match.groups()
					if re.match('^\\d+$', realKey):
						realKey = int(realKey)
					if name in query:
						if not isinstance(query[name], dict):
							self.error("Invalid request", "Name/map collision on query key: %s" % key)
					else:
						query[name] = {}
					query[name][realKey] = oldQuery[key]
				elif key in query:
					self.error("Invalid request", "Duplicate key in request: %s" % key)
				else:
					query[key] = oldQuery[key]

			# log("Stripped path: %s" % path)

			assert path[0] == '/'
			path = path[1:]
			if len(path) and path[-1] == '/': path = path[:-1]
			fn = None
			for pattern in handlers[method]:
				match = pattern.match(path)
				if match:
					fn = handlers[method][pattern]
					for k, v in match.groupdict().items():
						if k in query:
							self.error("Invalid request", "Duplicate key in request: %s" % k)
						if re.match('^\\d+$', v):
							v = int(v)
						query[k] = v
					break

			if not fn:
				self.error("Invalid request", "Unknown action <b>%s</b>" % path if path != '' else "No empty action handler")

			given = query.keys()
			expected, _, _, defaults = getargspec(fn)
			defaults = defaults or []

			givenS, expectedS = set(given), set(expected)<commit_msg>Added the method to the Invalid Request error message<commit_after>from BaseHTTPServer import BaseHTTPRequestHandler
from inspect import getargspec
import re
import cgi
import sys
from sqlite3 import OperationalError
import traceback

from Session import Session, timestamp
from Box import Box, ErrorBox
from code import showCode, highlightCode
from ResponseWriter import ResponseWriter
from FrameworkException import FrameworkException
from utils import *

handlers = {'get': {}, 'post': {}}

@globalize
def get(index):
	def wrap(f):
		handlers['get'][re.compile("^%s$" % index)] = f
		return f
	return wrap

@globalize
def post(index):
	def wrap(f):
		handlers['post'][re.compile("^%s$" % index)] = f
		return f
	return wrap

def log(str, target = sys.stdout):
	print >> target, str

class HTTPHandler(BaseHTTPRequestHandler):
	def __init__(self, request, address, server):
		self.session = None
		self.replacements = {}
		# self.leftMenu = LeftMenu()
		BaseHTTPRequestHandler.__init__(self, request, address, server)

	def buildResponse(self, method, data):
		# log("Building response")
		self.contentType = 'text/html'
		self.forceDownload = False

		path = self.path
		query = {}
		# log("Original path: %s" % path)

		writer = ResponseWriter()

		request = {'path': [], 'wrappers': True, 'code': 200}
		try: # raise DoneRendering; starts here to catch self.error calls
			self.title(None)

			# Pull out get variables
			if '?' in path:
				path, queryStr = path.split('?', 1)
				parts = queryStr.split('&')
				query = dict(map(lambda x: x.split('=') if '=' in x else (x, True), parts))
				if len(parts) != len(query):
					self.error("Invalid request", "Query key collision")

			# Validate query keys (can't start with p_ to avoid collisions with post variables)
			for key in query:
				if key[:2] == 'p_':
					self.error("Invalid request", "Illegal query key: %s" % key)

			# Add p_ prefix to post variables
			if data:
				query.update(dict([('p_' + k, v) for (k, v) in data.iteritems()]))

			# Convert foo[bar] keys to maps (epic nesting!)
			oldQuery = query
			query = {}
			for key in oldQuery:
				match = re.match('(.*)\\[(.*)\\]', key)
				if match:
					name, realKey = match.groups()
					if re.match('^\\d+$', realKey):
						realKey = int(realKey)
					if name in query:
						if not isinstance(query[name], dict):
							self.error("Invalid request", "Name/map collision on query key: %s" % key)
					else:
						query[name] = {}
					query[name][realKey] = oldQuery[key]
				elif key in query:
					self.error("Invalid request", "Duplicate key in request: %s" % key)
				else:
					query[key] = oldQuery[key]

			# log("Stripped path: %s" % path)

			assert path[0] == '/'
			path = path[1:]
			if len(path) and path[-1] == '/': path = path[:-1]
			fn = None
			for pattern in handlers[method]:
				match = pattern.match(path)
				if match:
					fn = handlers[method][pattern]
					for k, v in match.groupdict().items():
						if k in query:
							self.error("Invalid request", "Duplicate key in request: %s" % k)
						if re.match('^\\d+$', v):
							v = int(v)
						query[k] = v
					break

			if not fn:
				self.error("Invalid request", "Unknown %s action <b>%s</b>" % (method.upper(), path if path != '' else "No empty action handler"))

			given = query.keys()
			expected, _, _, defaults = getargspec(fn)
			defaults = defaults or []

			givenS, expectedS = set(given), set(expected)
{'fraction_non_alphanumeric': 0.14546794399410465, 'fraction_numerical': 0.0029476787030213707, 'mean_word_length': 3.395077720207254, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '191214', 'n_tokens_mistral': 2616, 'n_tokens_neox': 2170, 'n_words': 705}