#coding=utf8
import config
import json
import sys, time
py3k = sys.version_info.major > 2
import os.path
import urllib

if py3k:
	from urllib import parse as urlparse
else:
	import urlparse

def get_one():
	return config.dbconn().fetch_rows('http', condition={'checked': 0}, order="id asc", limit="1", fetchone=True)

def check_key(key):
	'''
	是否需要保留这个key
	'''
	blacklist = ['t', 'r', 'submit']
	if key.lower() in blacklist:
		return False
	return True

def check_value(value, vtype):
	'''
	是否需要保留这个value
	'''
	if vtype == 'array': return False
	return True

def get_type(key, value):
	if type(value) == type([]):	return 'array'
	value = value[0]
	if value.isdigit(): return 'int'
	try:
		float(value)
		return 'float'
	except:
		pass
	# url check
	u = urlparse.urlparse(value)
	if u.scheme and u.netloc:
		return 'url'
	try:
		j = json.loads(value)
		if type(j) == type([]) or type(j) == type({}):
			return 'json'
	except:
		pass
	return 'str'

while True:
	http = get_one()
	if not http:
		time.sleep(3)
		continue
	req = json.loads(http['req'])

	if req['rtype'] not in ['qs', 'rewrite']:
		config.dbconn().insert('requests', {'requestid': http['id'], 'method': req['method'], 'key': '', 'type': 'special|'+req['rtype']})
	else:
		# support array like a[]=1&a[]=2
		parsed = urlparse.urlparse(req['uri'])
		get_parts = urlparse.parse_qs(parsed.query)
		if get_parts:
			for k,v in get_parts.items():
				v = v[0] if len(v) == 1 else v
				vtype = get_type(k, v)
				if check_key(k) and check_value(v, vtype):
					config.dbconn().insert('requests', {'requestid': http['id'], 'method': "GET", 'key': k, 'type': vtype})

		if not parsed.query and not os.path.splitext(parsed.path)[1] and len(parsed.path.split('/')) > 3:
			path_parts = parsed.path.split('/')
			for i in range(3, len(path_parts)):
				vtype = 'rewrite|'+get_type('rewrite', path_parts[i])
				config.dbconn().insert('requests', {'requestid': http['id'], 'method': "GET", 'key': str(i), 'type': vtype})


		if req['method'] == "POST":
			post_parts = urlparse.parse_qs(urlparse.urlparse(req['body']).query)
			if post_parts:
				for k,v in post_parts.items():
					v = v[0] if len(v) == 1 else v
					vtype = get_type(k, v)
					if check_key(k) and check_value(v, vtype):
						config.dbconn().insert('requests', {'requestid': http['id'], 'method': "POST", 'key': k, 'type': vtype})

	config.dbconn().update('http', {'checked': 1}, {'id': http['id']})