import string
import cgi
import cgitb; cgitb.enable()
import sys
import os
import socket
from fcgi import WSGIServer
from bugmanager import BugReport, OsInfo, MemInfo, fastCGIHandler, log

PORT=9966
bannedips = ['127.0.0.2','128.0.0.1']
maxtime= 10 #15 minutes (900 seconds). User can't load the script before 15 minutes. it should prevent DoS

######################################### MAIN
def mainHandler(env, response):
    # script main variables
    hostname="127.0.0.1"

    method=env["REQUEST_METHOD"]
    if env['REMOTE_ADDR']:
        hostname=env['REMOTE_ADDR']

    log("[console] REQUEST_URI: " + env['REQUEST_URI'])
    req_uri = env['REQUEST_URI']
    
    # URLs inside paths
    if req_uri.startswith('/bug-manager'):
        log('[console] bug report handler invoked...')
        resp = fastCGIHandler(env, response)
        log('[console] bug report handler response: ' + resp)
    else:
        log('[console] invalid path: ' + req_uri)
        resp = 'WTF'
    response('200 OK',[('Content-Type','text/html; charset-utf-8')])
    return [resp]    
try:
  log("Start WSGI Server.")
  WSGIServer(mainHandler, bindAddress = ('127.0.0.1', PORT)).run()
except Exception, e:
  f=open('crash.log','a')
  f.write(str(e))
  f.close()
