Esempio n. 1
0
def getdata(clientSocket,n,reqheadersdic,resheadersdic,entheadersdic,genheadersdic,address,pathdic,date):
	num=n
	message=''
	while True:
            try:
                data = clientSocket.recv(1)
                if data:
                    message+= data.decode()
		if len(message)==int(num):
		    return message	
	    except timeout:
		rv=codes.error_4(408,"confiles/error/408.html",reqheadersdic,resheadersdic,entheadersdic,genheadersdic,0)
		writealog(pathdic,address,date,'408',len(rv))
		clientSocket.sendall(rv)	
		clientSocket.close()
		break
	    except:
                continue
Esempio n. 2
0
def recvdata(clientSocket,address,date,pathdic):
	clientSocket.settimeout(1000)
	message=''
	CRLF='\r\n\r\n'
	while True:
            try:
                data = clientSocket.recv(1)
                if data:
                    message+= data.decode()
		if message and CRLF in message:
		    return message	
	    except timeout:
		rv=codes.error_4(408,"confiles/error/408.html",reqheadersdic,resheadersdic,entheadersdic,genheadersdic,0)
		writealog(pathdic,address,date,'408',len(rv))
		clientSocket.sendall(rv)
		#Canputsleep	
		clientSocket.close()
		break
	    except:
                continue
Esempio n. 3
0
def handledelete(message, headers, reqheadersdic, resheadersdic, entheadersdic,
                 genheadersdic, num, pathdic, cookie_dict, address):
    f_flag = 0
    flag = 0
    try:
        filename, flag = pathreturn(message[1], flag)
        filename1 = str(pathdic['docpath']) + '/' + filename
        f = open(filename1)
    except:
        loc, code = compressedclean.checkredirect(filename, pathdic)
        if loc != 'N/A' and code != 000:
            resheadersdic['Location'] = loc
            rv = codes.error_3(code, 'confiles/error/' + str(code) + '.html',
                               reqheadersdic, resheadersdic, entheadersdic,
                               genheadersdic, num)
            return rv, str(code)
        #print filename1
        rv = codes.error_4(404, "confiles/error/404.html", reqheadersdic,
                           resheadersdic, entheadersdic, genheadersdic, num)
        return rv, '404'
    if len(message) > 3:
        codes.parseHeaders("", headers[1:-2], reqheadersdic, '')
        #print reqheadersdic
    #print flag
    if ((reqheadersdic['Host'] == ''
         or reqheadersdic['Host'] != 'localhost:1234') and flag == 0):
        rv = codes.error_4(400, 'confiles/error/400.html', reqheadersdic,
                           resheadersdic, entheadersdic, genheadersdic, num)
        return rv, '400'
    un, pw, ty = compressedclean.checkprotect(filename, pathdic)
    if un != 'N/A' and pw != 'N/A' and reqheadersdic[
            'Authorization'] == '' and ty == 'protect':
        resheadersdic[
            'WWW-Authenticate'] = 'Basic realm="Access to the protected file", charset="UTF-8"'
        rv = codes.error_4(401, 'confiles/error/401.html', reqheadersdic,
                           resheadersdic, entheadersdic, genheadersdic, num)
        return rv, '401'
    if un != 'N/A' and pw != 'N/A' and reqheadersdic[
            'Authorization'] != '' and ty == 'protect':
        un1, pw1 = codes.parseHeaders("", "", reqheadersdic, 'auth')
        #print un,pw
        if pw1 != pw:
            rv = codes.error_4(403, 'confiles/error/403.html', reqheadersdic,
                               resheadersdic, entheadersdic, genheadersdic,
                               num)
            return rv, '403'
        if un != 'N/A' and pw != 'N/A' and reqheadersdic[
                'Proxy-Authorization'] == '' and ty == 'proxy':
            resheadersdic[
                'Proxy-Authenticate'] = 'Basic realm="Access to the protected file", charset="UTF-8"'
            rv = codes.error_4(407, 'confiles/error/407.html', reqheadersdic,
                               resheadersdic, entheadersdic, genheadersdic,
                               num)
            return rv, '407'

    if un != 'N/A' and pw != 'N/A' and reqheadersdic[
            'Proxy-Authorization'] != '' and ty == 'proxy':
        un1, pw1 = codes.parseHeaders("", "", reqheadersdic, 'pauth')
        #print un,pw
        if pw1 != pw:
            rv = codes.error_4(403, 'confiles/error/403.html', reqheadersdic,
                               resheadersdic, entheadersdic, genheadersdic,
                               num)
            return rv, '403'

    suc_code = delete_file(filename, reqheadersdic, pathdic)
    if (suc_code == 1):
        rv = codes.success_200(204, filename1, reqheadersdic, resheadersdic,
                               entheadersdic, genheadersdic, '', num,
                               cookie_dict, address)
        return rv, '204'
    else:
        rv = codes.error_4(404, "confiles/error/404.html", reqheadersdic,
                           resheadersdic, entheadersdic, genheadersdic, num)
        return rv, '404'
Esempio n. 4
0
def handleexpect(message, headers, reqheadersdic, resheadersdic, entheadersdic,
                 genheadersdic, pathdic, address):
    f_flag = 0
    flag = 0
    newfileflag = 0
    num = 0
    try:
        filename, flag = pathreturn(message[1], flag)
        filename1 = str(pathdic['docpath']) + '/' + filename
        f = open(filename1)
    except:
        loc, code = compressedclean.checkredirect(filename, pathdic)
        if loc != 'N/A' and code != 000:
            rv = codes.error_4(417, 'confiles/error/417.html', reqheadersdic,
                               resheadersdic, entheadersdic, genheadersdic,
                               num)
            return 417, rv
        newfileflag = 1
    if len(message) > 3:
        codes.parseHeaders("", headers[1:-2], reqheadersdic, '')
    if ((reqheadersdic['Host'] == ''
         or reqheadersdic['Host'] != 'localhost:1234') and flag == 0):
        rv = codes.error_4(417, 'confiles/error/417.html', reqheadersdic,
                           resheadersdic, entheadersdic, genheadersdic, num)
        return 417, rv
    if reqheadersdic['Last-Modified'] != '' and newfileflag == 0:
        dt = os.path.getmtime(filename1)
        date1 = str(datetime.datetime.utcfromtimestamp(dt))
        given_date = str(
            datetime.datetime.strptime(reqheadersdic['Last-Modified'],
                                       '%a, %d %b %Y %H:%M:%S GMT'))
        if date1 > given_date:
            rv = codes.error_4(417, 'confiles/error/417.html', reqheadersdic,
                               resheadersdic, entheadersdic, genheadersdic,
                               num)
            return 417, rv
    un, pw, ty = compressedclean.checkprotect(filename, pathdic)
    if un != 'N/A' and pw != 'N/A' and ty == 'protect' and reqheadersdic[
            'Authorization'] == '' and newfileflag == 0:
        rv = codes.error_4(417, 'confiles/error/417.html', reqheadersdic,
                           resheadersdic, entheadersdic, genheadersdic, num)
        return 417, rv
    if un != 'N/A' and pw != 'N/A' and ty == 'protect' and reqheadersdic[
            'Authorization'] != '' and newfileflag == 0:
        un1, pw1 = codes.parseHeaders("", "", reqheadersdic, 'auth')
        #print(pw)
        if pw1 != pw:
            rv = codes.error_4(417, 'confiles/error/417.html', reqheadersdic,
                               resheadersdic, entheadersdic, genheadersdic,
                               num)
            return 417, rv
    if un != 'N/A' and pw != 'N/A' and ty == 'proxy' and reqheadersdic[
            'Proxy-Authorization'] == '' and newfileflag == 0:
        rv = codes.error_4(417, 'confiles/error/417.html', reqheadersdic,
                           resheadersdic, entheadersdic, genheadersdic, num)
        return 417, rv
    if un != 'N/A' and pw != 'N/A' and ty == 'proxy' and reqheadersdic[
            'Proxy-Authorization'] != '' and newfileflag == 0:
        un, pw = codes.parseHeaders("", "", reqheadersdic, 'pauth')
        if pw != pw:
            rv = codes.error_4(417, 'confiles/error/417.html', reqheadersdic,
                               resheadersdic, entheadersdic, genheadersdic,
                               num)
            return 417, rv
    rv = 'HTTP/1.1 100 Continue\n'
    return 100, rv
Esempio n. 5
0
def handleput(message, headers, reqheadersdic, resheadersdic, entheadersdic,
              genheadersdic, data, pathdic, cookie_dict, address):
    f_flag = 0
    flag = 0
    newfileflag = 0
    num = 0
    try:
        filename, flag = pathreturn(message[1], flag)
        filename1 = str(pathdic['docpath']) + '/' + filename
        f = open(filename1)
    except:
        loc, code = compressedclean.checkredirect(filename, pathdic)
        if loc != 'N/A' and code != 000:
            resheadersdic['Location'] = loc
            if code == 301 or code == 302:
                code = 307
            rv = codes.error_3(code, 'confiles/error/' + str(code) + '.html',
                               reqheadersdic, resheadersdic, entheadersdic,
                               genheadersdic, num)
            return rv, str(code)
        newfileflag = 1
    if len(message) > 3:
        codes.parseHeaders("", headers[1:-2], reqheadersdic, '')
    if ((reqheadersdic['Host'] == ''
         or reqheadersdic['Host'] != 'localhost:1234') and flag == 0):
        rv = codes.error_4(400, 'confiles/error/400.html', reqheadersdic,
                           resheadersdic, entheadersdic, genheadersdic, num)
        return rv, '400'
    if reqheadersdic['Last-Modified'] != '' and newfileflag == 0:
        dt = os.path.getmtime(filename1)
        date1 = str(datetime.datetime.utcfromtimestamp(dt))
        given_date = str(
            datetime.datetime.strptime(reqheadersdic['Last-Modified'],
                                       '%a, %d %b %Y %H:%M:%S GMT'))
        if date1 > given_date:
            rv = codes.error_4(409, 'confiles/error/409.html', reqheadersdic,
                               resheadersdic, entheadersdic, genheadersdic,
                               num)
            return rv, '409'
    un, pw, ty = compressedclean.checkprotect(filename, pathdic)
    if un != 'N/A' and pw != 'N/A' and ty == 'protect' and reqheadersdic[
            'Authorization'] == '' and newfileflag == 0:
        resheadersdic[
            'WWW-Authenticate'] = 'Basic realm="Access to the protected file", charset="UTF-8"'
        rv = codes.error_4(401, 'confiles/error/401.html', reqheadersdic,
                           resheadersdic, entheadersdic, genheadersdic, num)
        return rv, '401'
    if un != 'N/A' and pw != 'N/A' and ty == 'protect' and reqheadersdic[
            'Authorization'] != '' and newfileflag == 0:
        un1, pw1 = codes.parseHeaders("", "", reqheadersdic, 'auth')
        if pw1 != pw:
            rv = codes.error_4(403, 'confiles/error/403.html', reqheadersdic,
                               resheadersdic, entheadersdic, genheadersdic,
                               num)
            return rv, '403'
    if un != 'N/A' and pw != 'N/A' and ty == 'proxy' and reqheadersdic[
            'Proxy-Authorization'] == '' and newfileflag == 0:
        resheadersdic[
            'Proxy-Authenticate'] = 'Basic realm="Access to the protected file", charset="UTF-8"'
        rv = codes.error_4(407, 'confiles/error/407.html', reqheadersdic,
                           resheadersdic, entheadersdic, genheadersdic, num)
        return rv, '407'
    if un != 'N/A' and pw != 'N/A' and ty == 'proxy' and reqheadersdic[
            'Proxy-Authorization'] != '' and newfileflag == 0:
        un1, pw1 = codes.parseHeaders("", "", reqheadersdic, 'pauth')
        if pw1 != pw:
            rv = codes.error_4(403, 'confiles/error/403.html', reqheadersdic,
                               resheadersdic, entheadersdic, genheadersdic,
                               num)
            return rv, '403'
    if newfileflag == 1:
        f = open(filename1, "w+")
        f.write(data)
        f.close()
        mime_type = ""
        if (reqheadersdic['Content-Type'] == ""):
            mime = magic.Magic(mime=True)
            mime_type = mime.from_file(filename1)
        else:
            mime_type = reqheadersdic['Content-Type']
        check_var(filename, reqheadersdic, mime_type, pathdic)
        resheadersdic['Location'] = filename
        rv = codes.success_200(201, filename1, reqheadersdic, resheadersdic,
                               entheadersdic, genheadersdic, '', num,
                               cookie_dict, address)
        return rv, '201'
    if newfileflag == 0:
        if (reqheadersdic['Content-Range'] == ""):
            f = open(filename1, "w")
            f.write(data)
            f.close()
        else:
            f = open(filename1, "r+b")
            ranges = reqheadersdic['Content-Range'].split()
            range_av = ranges[1].split("/")
            range_occ = range_av[0].split("-")
            f.seek(int(range_occ[0].strip()), 0)
            f.write(data.strip("\r\n"))
            f.close()
        mime_type = ""
        if (reqheadersdic['Content-Type'] == ""):
            mime = magic.Magic(mime=True)
            mime_type = mime.from_file(filename1)
        else:
            mime_type = reqheadersdic['Content-Type']
        check_existing_var(filename, reqheadersdic, mime_type)
        rv = codes.success_200(204, filename1, reqheadersdic, resheadersdic,
                               entheadersdic, genheadersdic, '', num,
                               cookie_dict, address)
        return rv, '204'
Esempio n. 6
0
def server(clientSocket,address,pathdic):
	try:
		#clientSocket,address=serverSocket.accept()
		reqheadersdic={'Accept':'','Accept-Charset':'','Accept-Encoding':'',
	'Accept-Language':'','Authorization':'','Expect':'','From':'','Host':'','Range':'','If-Modified-Since':'','If-Range':'',
	'If-Unmodified-Since':'','Proxy-Authorization':'','TE':'','User-Agent':'','Allow':'','Content-Encoding':'','Content-Language':'','Content-Length':'','Content-Location':'','Content-MD5':'','Content-Range':'','Content-Type':'','Last-Modified':''}
		#Headers covered are Accept, Accept-Charset,Accept-Encoding,Accept-Language,AUthorization,From,Host,Range,If-Modified-Since,If-Range,If-Unmodified-Since,Proxy-Authorization,User-Agent
		resheadersdic={'Accept-Ranges':'bytes','Age':'','Location':'','Proxy-Authenticate':'','Retry-After':'','Server':'HTTP Server','Vary':'','WWW-Authenticate':''}
		#Headers coverec are Accept-Ranges,Proxy-Authenticate,Retry-After,Server,WWW-Authenticate
		entheadersdic={'Allow':'','Content-Encoding':'','Content-Language':'','Content-Length':'','Content-Location':'','Content-MD5':'','Content-Range':'','Content-Type':'','Last-Modified':''}
		#Headers covered are Allow,Content-Encoding,Content-Language,Content-Location,Content-MD5,Content-Range,Content Length,Content Type,Connection,Last Modified
		genheadersdic={'Cache-Control':'no-cache','Connection':'close','Date':'','Pragma':'no-cache','Trailer':'','Transfer-Encoding':'','Upgrade':'','Via':'','Warning':''}
		#Headers covered are Cache-Control,Connection,Date,Pragma
		set_cookie(address[0])
		date=strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime()) 
		genheadersdic['Date']="{}".format(date)
		reqMess=str(recvdata(clientSocket,address,date,pathdic))
		#print reqMess
		if reqMess==None:
			#Canputsleep
			clientSocket.close()
			return
		message=reqMess.split()
		headers=reqMess.split("\n")
		if message[0]=='HEAD':
			num=1
		else: 
			num=0
		if len(message)<3:
			rv=codes.error_4(400,"confiles/error/400.html",reqheadersdic,resheadersdic,entheadersdic,genheadersdic,num)
			string='"'+headers[0][:-1]+'"'
			writealog(pathdic,address,date,'400',len(rv),string)
			clientSocket.sendall(rv)
			#Canputsleep	
			clientSocket.close()
			return
		if(message[2] and message[2][:5]=="HTTP/" and message[2]!="HTTP/1.1") :
			rv=codes.error_4(505,"confiles/error/505.html",reqheadersdic,resheadersdic,entheadersdic,genheadersdic,num)
			string='"'+headers[0][:-1]+'"'
			writealog(pathdic,address,date,'505',len(rv),string)
			clientSocket.sendall(rv)
			#Canputsleep	
			clientSocket.close()
			return
		if(message[2]!="HTTP/1.1") :
			rv=codes.error_4(400,"confiles/error/400.html",reqheadersdic,resheadersdic,entheadersdic,genheadersdic,num)
			string='"'+headers[0][:-1]+'"'
			writealog(pathdic,address,date,'400',len(rv),string)
			clientSocket.sendall(rv)
			#Canputsleep	
			clientSocket.close()
			return
		if(message[0] not in ['GET','POST','HEAD','DELETE','PUT','TRACE','CONNECT','OPTIONS']):
			rv=codes.error_4(405,'confiles/error/405.html',reqheadersdic,resheadersdic,entheadersdic,genheadersdic,num)
			string='"'+headers[0][:-1]+'"'
			clientSocket.sendall(rv)
			writealog(pathdic,address,date,'405',len(rv),string)
			#Canputsleep	
			clientSocket.close()
			return
		if(message[0] in ['TRACE','CONNECT','OPTIONS']):
			rv=codes.error_4(501,'confiles/error/501.html',reqheadersdic,resheadersdic,entheadersdic,genheadersdic,num)
			string='"'+headers[0][:-1]+'"'
			clientSocket.sendall(rv)
			writealog(pathdic,address,date,'501',len(rv),string)
			#Canputsleep	
			clientSocket.close()
			return
		if(message[0]=="GET"):
			rv,code=get.handleget(message,headers,reqheadersdic,resheadersdic,entheadersdic,genheadersdic,num,pathdic,cookie_dict,address[0])
			string='"'+headers[0][:-1]+'"'
			clientSocket.sendall(rv)
			writealog(pathdic,address,date,code,len(rv),string)
			#Canputsleep	
			#print rv
			clientSocket.close()		
			return
		if(message[0]=="HEAD"):
			rv,code=get.handleget(message,headers,reqheadersdic,resheadersdic,entheadersdic,genheadersdic,num,pathdic,cookie_dict,address[0])
			string='"'+headers[0][:-1]+'"'
			clientSocket.sendall(rv)
			writealog(pathdic,address,date,code,len(rv),string)
			#Canputsleep	
			clientSocket.close()		
			return
		if(message[0]=="PUT"):
			if 'Expect' in reqMess:
				if 'Content-Length' not in reqMess:
					string='"'+headers[0][:-1]+'"'
					rv=codes.error_4(417,'confiles/error/417.html',reqheadersdic,resheadersdic,entheadersdic,genheadersdic,num)
					clientSocket.sendall(rv)
					writealog(pathdic,address,date,'417',len(rv),string)
					#Canputsleep
					clientSocket.close()
					return
				if 'Content-Length' in reqMess:
					lock = threading.Lock()
					lock.acquire()
					code,rv=put.handleexpect(message,headers,reqheadersdic,resheadersdic,entheadersdic,genheadersdic,pathdic,address[0])
					lock.release()
					clientSocket.sendall(rv)
					if code==417:
						string='"'+headers[0][:-1]+'"'
						writealog(pathdic,address,date,'417',len(rv),string)
						#Canputsleep
						clientSocket.close()
						return
				string='"'+headers[0][:-1]+'"'
				writealog(pathdic,address,date,'100',20,string)
			if 'Content-Length' not in reqMess:
					string='"'+headers[0][:-1]+'"'
					rv=codes.error_4(411,'confiles/error/411.html',reqheadersdic,resheadersdic,entheadersdic,genheadersdic,num)
					clientSocket.sendall(rv)
					writealog(pathdic,address,date,'411',len(rv),string)
					#Canputsleep
					clientSocket.close()
					return
			dlen=getcl(reqMess)
			data=getdata(clientSocket,int(dlen),reqheadersdic,resheadersdic,entheadersdic,genheadersdic,address,pathdic,date)
			#print dlen	
			#print data
			lock = threading.Lock()
			lock.acquire()
			rv,code =put.handleput(message,headers,reqheadersdic,resheadersdic,entheadersdic,genheadersdic,data,pathdic,cookie_dict,address[0])
			lock.release()
			string='"'+headers[0][:-1]+'"'
			clientSocket.sendall(rv)
			writealog(pathdic,address,date,code,len(rv),string)
			#Canputsleep
			clientSocket.close()
			return
		if(message[0]=="DELETE"):
			lock = threading.Lock()
			lock.acquire()
			rv,code= delete.handledelete(message,headers,reqheadersdic,resheadersdic,entheadersdic,genheadersdic,num,pathdic,cookie_dict,address[0])
			lock.release()	
			string='"'+headers[0][:-1]+'"'
			clientSocket.sendall(rv)
			writealog(pathdic,address,date,code,len(rv),string)
			#Canputsleep	
			clientSocket.close()	
			return	
		if(message[0]=="POST"):
			if 'Content-Length' not in reqMess:
					string='"'+headers[0][:-1]+'"'
					rv=codes.error_4(411,'confiles/error/411.html',reqheadersdic,resheadersdic,entheadersdic,genheadersdic,num)
					clientSocket.sendall(rv)
					writealog(pathdic,address,date,'411',len(rv),string)
					#Canputsleep
					clientSocket.close()
					return
			dlen=getcl(reqMess)
			data=getdata(clientSocket,int(dlen),reqheadersdic,resheadersdic,entheadersdic,genheadersdic,address,pathdic,date)
			lock = threading.Lock()
			lock.acquire()
			rv,code=post.handlepost(message,headers,reqheadersdic,resheadersdic,entheadersdic,genheadersdic,data,pathdic,cookie_dict,address[0])
			string='"'+headers[0][:-1]+'"'
			lock.release()	
			clientSocket.sendall(rv)
			writealog(pathdic,address,date,code,len(rv),string)
			#Canputsleep	
			clientSocket.close()	
			return	
	except:
		try:
			string='"'+headers[0][:-1]+'"'
		except:
			string="Unexpected Error"
		clientSocket.sendall("Unexpected Error")
		writelog(pathdic,address,date,string)
		#Canputsleep
		clientSocket.close()
Esempio n. 7
0
def handleget(message, headers, reqheadersdic, resheadersdic, entheadersdic,
              genheadersdic, num, pathdic, cookie_dict, address):
    f_flag = 0
    flag = 0
    try:
        filename, flag = pathreturn(message[1], flag)
        filename1 = str(pathdic['docpath']) + '/' + filename
        #print filename1
        f = open(filename1)
    except:
        loc, code = compressedclean.checkredirect(filename, pathdic)
        if loc != 'N/A' and code != 000:
            resheadersdic['Location'] = loc
            rv = codes.error_3(code, 'confiles/error/' + str(code) + '.html',
                               reqheadersdic, resheadersdic, entheadersdic,
                               genheadersdic, num)
            return rv, str(code)
        #print filename1
        rv = codes.error_4(404, "confiles/error/404.html", reqheadersdic,
                           resheadersdic, entheadersdic, genheadersdic, num)
        return rv, '404'
    if len(message) > 3:
        codes.parseHeaders("", headers[1:-2], reqheadersdic, '')
        #print reqheadersdic
    #print flag
    if ((reqheadersdic['Host'] == ''
         or reqheadersdic['Host'] != 'localhost:1234') and flag == 0):
        rv = codes.error_4(400, 'confiles/error/400.html', reqheadersdic,
                           resheadersdic, entheadersdic, genheadersdic, num)
        return rv, '400'
    un, pw, ty = compressedclean.checkprotect(filename, pathdic)
    if un != 'N/A' and pw != 'N/A' and reqheadersdic[
            'Authorization'] == '' and ty == 'protect':
        resheadersdic[
            'WWW-Authenticate'] = 'Basic realm="Access to the protected file", charset="UTF-8"'
        rv = codes.error_4(401, 'confiles/error/401.html', reqheadersdic,
                           resheadersdic, entheadersdic, genheadersdic, num)
        return rv, '401'
    if un != 'N/A' and pw != 'N/A' and reqheadersdic[
            'Authorization'] != '' and ty == 'protect':
        un1, pw1 = codes.parseHeaders("", "", reqheadersdic, 'auth')
        #print un,pw
        if pw1 != pw:
            rv = codes.error_4(403, 'confiles/error/403.html', reqheadersdic,
                               resheadersdic, entheadersdic, genheadersdic,
                               num)
            return rv, '403'
    if un != 'N/A' and pw != 'N/A' and reqheadersdic[
            'Proxy-Authorization'] == '' and ty == 'proxy':
        resheadersdic[
            'Proxy-Authenticate'] = 'Basic realm="Access to the protected file", charset="UTF-8"'
        rv = codes.error_4(407, 'confiles/error/407.html', reqheadersdic,
                           resheadersdic, entheadersdic, genheadersdic, num)
        return rv, '407'
    if un != 'N/A' and pw != 'N/A' and reqheadersdic[
            'Proxy-Authorization'] != '' and ty == 'proxy':
        un1, pw1 = codes.parseHeaders("", "", reqheadersdic, 'pauth')
        #print un,pw
        if pw1 != pw:
            rv = codes.error_4(403, 'confiles/error/403.html', reqheadersdic,
                               resheadersdic, entheadersdic, genheadersdic,
                               num)
            return rv, '403'
    #print(reqheadersdic)
    enco = priority = lang = chars = {}
    ranges = []

    if (reqheadersdic['Accept-Encoding'] != ''):
        enco = codes.parseHeaders("", "", reqheadersdic, 'enco')
    if (reqheadersdic['Accept'] != ""):
        priority = codes.parseHeaders("", "", reqheadersdic, 'split')
    if (reqheadersdic['Accept-Language'] != ""):
        lang = codes.parseHeaders("", "", reqheadersdic, 'lang')
    if (reqheadersdic['Accept-Charset'] != ""):
        chars = codes.parseHeaders("", "", reqheadersdic, 'ch')
    if (reqheadersdic['Range'] != "" and reqheadersdic['If-Range'] == ""):
        ranges = codes.parseHeaders(filename1, "", reqheadersdic, "range")
    check = checkcontentnego(filename, pathdic)
    if check == 'A':
        filen = compressedclean.contentnego(filename, reqheadersdic,
                                            resheadersdic, entheadersdic, enco,
                                            priority, lang, chars)
        if filen == 'N/A':
            rv = codes.error_4(406, 'confiles/error/406.html', reqheadersdic,
                               resheadersdic, entheadersdic, genheadersdic,
                               num)
            return rv, '406'
    if check == 'N/A':
        filen = filename
    #print filen
    if filen != filename:
        entheadersdic['Content-Location'] = filen
    filen1 = pathdic['docpath'] + '/' + filen
    if reqheadersdic['If-Modified-Since'] != '' and reqheadersdic[
            'If-Unmodified-Since'] == '' and reqheadersdic['If-Range'] == '':
        dt = os.path.getmtime(filen1)
        date1 = str(datetime.datetime.utcfromtimestamp(dt))
        given_date = str(
            datetime.datetime.strptime(reqheadersdic['If-Modified-Since'],
                                       '%a, %d %b %Y %H:%M:%S GMT'))
        if date1 > given_date:
            rv = codes.success_200(200, filen1, reqheadersdic, resheadersdic,
                                   entheadersdic, genheadersdic, ranges, num,
                                   cookie_dict, address)
            code = '200'
        else:
            rv = codes.error_3(304, '', reqheadersdic, resheadersdic,
                               entheadersdic, genheadersdic, num)
            return rv, '304'
        f_flag = 1
    if reqheadersdic['If-Unmodified-Since'] != '' and reqheadersdic[
            'If-Modified-Since'] == '' and reqheadersdic['If-Range'] == '':
        dt = os.path.getmtime(filen1)
        #print filen1
        date1 = str(datetime.datetime.utcfromtimestamp(dt))
        given_date = str(
            datetime.datetime.strptime(reqheadersdic['If-Unmodified-Since'],
                                       '%a, %d %b %Y %H:%M:%S GMT'))
        #print date1
        if date1 < given_date:
            rv = codes.success_200(200, filen1, reqheadersdic, resheadersdic,
                                   entheadersdic, genheadersdic, '', num,
                                   cookie_dict, address)
            code = '200'
        else:
            rv = codes.error_4(412, 'confiles/error/412.html', reqheadersdic,
                               resheadersdic, entheadersdic, genheadersdic,
                               num)
            return rv, '412'
        f_flag = 1
    if (reqheadersdic['Range'] != ""):
        ranges = codes.parseHeaders(filen1, "", reqheadersdic, "range")
        if ranges == []:
            rv = codes.error_4(416, 'confiles/error/416.html', reqheadersdic,
                               resheadersdic, entheadersdic, genheadersdic,
                               num)
            return rv, '416'
        else:
            if reqheadersdic['If-Range'] == '':
                entity = codes.read_data(ranges, filen1, entheadersdic,
                                         genheadersdic)
                rv = codes.success_200(206, entity, reqheadersdic,
                                       resheadersdic, entheadersdic,
                                       genheadersdic, ranges, num, cookie_dict,
                                       address)
                code = '206'
            elif reqheadersdic['If-Range'] != '' and reqheadersdic[
                    'If-Unmodified-Since'] == '' and reqheadersdic[
                        'If-Modified-Since'] == '':
                dt = os.path.getmtime(filen1)
                date1 = str(datetime.datetime.utcfromtimestamp(dt))
                given_date = str(
                    datetime.datetime.strptime(reqheadersdic['If-Range'],
                                               '%a, %d %b %Y %H:%M:%S GMT'))
                if date1 < given_date:
                    entity = codes.read_data(ranges, filen1, entheadersdic,
                                             genheadersdic)
                    rv = codes.success_200(206, entity, reqheadersdic,
                                           resheadersdic, entheadersdic,
                                           genheadersdic, ranges, num,
                                           cookie_dict, address)
                    code = '206'
                else:
                    rv = codes.success_200(200, filen1, reqheadersdic,
                                           resheadersdic, entheadersdic,
                                           genheadersdic, '', num, cookie_dict,
                                           address)
                    code = '200'
        f_flag = 1
    if (f_flag == 0):
        rv = codes.success_200(200, filen1, reqheadersdic, resheadersdic,
                               entheadersdic, genheadersdic, '', num,
                               cookie_dict, address)
        code = 200
    return rv, code