def getDoc(src, dst, doc):
	temp=src.split(":")
	src=temp[0]
	sport=temp[1]
	temp=dst.split(":")
	dst=temp[0]
	dport=temp[1]
	#print (src, sport, dst, dport)
	db = connectMongo()
	#get data from column stream for specified fields
	nb=0
	spec = {"proto": "TCP", "src" : src, "dst" : dst, "sport" : int(sport), "dport" : int(dport),"session" : globals.sessionId}
	stream=db.stream.find_one(spec)#, "sport" : sport, "dport" : dport})
	if stream!=None:
		smartFlow=reassemble_stream(stream["src"], stream["dst"], stream["sport"], stream["dport"])
		#for an update lianaTreeSize=getLianaTreeDataSize(smartFlow)
		for data in smartFlow:
			(mostProbableMedia,infos)=inspectStreamForMedia(data,stream["sport"],stream["dport"])
			if mostProbableMedia.startswith("HTTP"):
				mydoc=getHTTPDoc(data["payload"],doc)
				#print mydoc
				if mydoc==None:
					return (None,None,None)
			#	print "header : "+str(mydoc["header"])+"\nfin"
				contentType=getContentType(mydoc)
				contentEncoding=getContentEncoding(mydoc)
				return (contentType,contentEncoding,mydoc["body"])
def getSniffedPackets(indexFrom,indexTo):
	db = connectMongo()
	#get data from column stream for specified fields
	nb=0
	finalJson="["
	for stream in db.stream.find({"proto": "TCP", "session":globals.sessionId}):
		if stream['initTS']!=None and stream['initTS']>indexFrom and (stream['initTS']<=indexTo or indexTo==-1):
			smartFlow=reassemble_stream(stream["src"], stream["dst"], stream["sport"], stream["dport"])
			lianaTreeSize=getLianaTreeDataSize(smartFlow)
			if lianaTreeSize!=0:
				stream["media"]=""
				for data in smartFlow:
					(mostProbableMedia,infos)=inspectStreamForMedia(data,stream["sport"],stream["dport"])
					if mostProbableMedia!="":
						stream["media"]=mostProbableMedia+" "+infos
					else:
						stream["media"]=""	
				finalJson+=packetToJson(stream, view="global",size=lianaTreeSize)+", "
				nb+=1
	if nb>0:
		finalJson=finalJson[:len(finalJson)-2]
	finalJson+="]"
	return finalJson
def getPacketsData(src2, dst2):
	temp=src2.split(":")
	src=temp[0]
	sport=temp[1]
	temp=dst2.split(":")
	dst=temp[0]
	dport=temp[1]
	#print (src, sport, dst, dport)
	db = connectMongo()
	#get data from column stream for specified fields
	nb=0
	finalJson="["
	spec = {"proto": "TCP", "src" : src, "dst" : dst, "sport" : int(sport), "dport" : int(dport),"session" : globals.sessionId}
	stream=db.stream.find_one(spec)#, "sport" : sport, "dport" : dport})
	if stream!=None:
		smartFlow=reassemble_stream(stream["src"], stream["dst"], stream["sport"], stream["dport"])
		#for an update lianaTreeSize=getLianaTreeDataSize(smartFlow)
		for data in smartFlow:
			(mostProbableMedia,infos)=inspectStreamForMedia(data,stream["sport"],stream["dport"])
			if mostProbableMedia.startswith("HTTP"):
				streamTab=decodeAndEscapeHTML(data["payload"])
				#streamTab=splitHTMLStream(data["payload"])
				count=0
	
				for doc in streamTab:
					stream['data']=""
					stream['data']+="Header :<br />"+cgi.escape(doc["header"])+"<br />Body :<br />"+cgi.escape(doc["body"])+"<br /><br />"
					globals.docNumber+=1
					count+=1
					ct=getContentType(doc)
					infos={}
					if ct!=None:
						infos["link"]=""	
						if ct.strip().startswith("image"):
							infos["type"]="image"
						else:
							infos["type"]="text"
							writeHTTPToFile(doc);
							#finalJson+="link:doc"+str(globals.docNumber)+".html"
							infos["link"]="temp/"+globals.sessionId+"doc"+str(globals.docNumber)+".html"
					else:
						infos["type"]="text"
						writeHTTPToFile(doc);
						#finalJson+="link:doc"+str(globals.docNumber)+".html"
						infos["link"]="temp/"+globals.sessionId+"doc"+str(globals.docNumber)+".html"
					stream["infos"]=infos
					finalJson+=packetToJson(stream,view="data")+", "
					nb+=1
			else:
				
				stream['data']=cgi.escape(data["payload"])
				infos={"link":"", "type":""}
				stream['infos']=infos
				finalJson+=packetToJson(stream,view="data")+", "
				nb+=1
			
	if nb>0:
		finalJson=finalJson[:len(finalJson)-2]
	finalJson+="]"
	
	return finalJson