From cf47186234a721d072a34fd6a4721d093af9fd5e Mon Sep 17 00:00:00 2001
From: houxiao <houxiao@454eff88-639b-444f-9e54-f578c98de674>
Date: 星期五, 09 十二月 2016 15:34:34 +0800
Subject: [PATCH] fix for GetSearchMetaInfo parse ser list

---
 ProxyServer/proxyserver.c |  207 +++++++++++++++++++++++++++++----------------------
 1 files changed, 116 insertions(+), 91 deletions(-)

diff --git a/ProxyServer/proxyserver.c b/ProxyServer/proxyserver.c
index c5a81da..52b930d 100644
--- a/ProxyServer/proxyserver.c
+++ b/ProxyServer/proxyserver.c
@@ -10,6 +10,11 @@
 #include <syslog.h>  
 
 
+#include <libxml/parser.h>
+#include <libxml/tree.h>
+#include <libxml/xpath.h>
+#include <libxml/xpathInternals.h>
+
 int RequestChannel;
 char* gAnalyzerIP;
 char* gUser;
@@ -47,7 +52,7 @@
 #define fileread_buf 128
 AlarmInfo RealAlarmEvent[16][1000];
 
-#define DBG_LOG
+//#define DBG_LOG
 #define ZIP_HOME "/tmp"
 
 /////////////////////////////////////////////////
@@ -734,117 +739,137 @@
 #endif
 		RequestMetaFiles(gAnalyzerIP,httpUri,sss,eee);
 		write(GSocket,byte_array,strlen(byte_array));
+
 		
-		//鐢ㄤ互涓嬫柟娉曞皢socket璁剧疆涓洪潪闃诲鏂瑰紡
-//		int flags = fcntl(GSocket, F_GETFL, 0);
-//		fcntl(GSocket, F_SETFL, flags | O_NONBLOCK);
-		while(1){
-			memset(buff,0,sizeof(buff));
-			length=read(GSocket,&buff,sizeof(buff));	   
+		//#test
+		{
+			length=read(GSocket,&buff,sizeof(buff));
+
+			//#test
+			//{
+			//	FILE * pFile = fopen ("pro.txt","rb");
+			//	length = fread ( buff, 1, sizeof(buff), pFile );
+			//	fclose(pFile);
+			//	bMetadataSaving = 0;
+			//}
+
 			if(length<=0)
 			{
 				close(GSocket);
-				break;
+				syslog(LOG_USER | LOG_INFO, "length<=0");
 			}
 			if(memorycmp((unsigned char*)buff,length, "WWW-Authenticate:",17)>0)
 			{
 				// analyse reception
 				MakeAuthorizedPacketSimp111(buff,length,"GET",httpUri,sss,eee);
 				write(GSocket,byte_array,strlen(byte_array));
-				continue;
 			}
-			if(bMetadataSaving==1){
-				memoryband[bandCount] = (unsigned char*)malloc(length);
-				memcpy(memoryband[bandCount], buff, length);
-				bandsize[bandCount] = length;
-				if(bandCount<99) bandCount++;
-				int eidx = stringcmp((char*)buff,"</urn:strings></urn:ViqStrings></urn:ViqStringsList>",52);
-				if(eidx >=0){
-					bMetadataSaving = 0;
-					int SSize=0;
-					for (int i=0;i<bandCount;i++) SSize+=bandsize[i];
-					char *tmpp = (char*)malloc(SSize);
-					int xx=0;
-					for (int i=0;i<bandCount;i++)
-					{
-						memcpy(&tmpp[xx],memoryband[i],bandsize[i]);
-						xx=bandsize[i];
-						free(memoryband[i]);
-					}
-					memcpy(buff,tmpp,SSize);
-					free(tmpp);
-					goto lpp123;
-					syslog(LOG_USER | LOG_INFO, "**********Special*******************************ZipSerFileNum = %d",ZipSerFileNum);
-				}
+			
+			//FILE * pFile = fopen ("pro.txt","rb");
+			//length = fread ( buff, 1, sizeof(buff), pFile );
+
+			xmlParserCtxtPtr ctxt;
+			char chars[5000];
+			xmlDocPtr doc; /* the resulting document tree */
+			int res,totalres=0;
+			
+			char* pBuff = buff;
+			while ((res = read(GSocket,chars,sizeof(chars))) > 0)
+			{
+				memcpy(pBuff, chars, res);
+				pBuff+=res;
+				totalres+=res;
 			}
-			else{
-				int sidx = stringcmp((char*)buff,"<urn:ViqStringsList urn:version=",32);
-				if(sidx >=0){
-					bMetadataSaving = 1;
-					bandCount = 0;
-					memoryband[bandCount] = (unsigned char*)malloc(length-sidx);
-					memcpy(memoryband[bandCount], &buff[sidx], length-sidx);
-					bandsize[bandCount] = length-sidx;
-					if(bandCount<99) bandCount++;
-					int eidx = stringcmp((char*)buff,"</urn:strings></urn:ViqStrings></urn:ViqStringsList>",52);
-					if(eidx >=0 && sidx<eidx){
-						free(memoryband[0]);
-						bandCount = 0;
-						bMetadataSaving = 0;
-						buff[eidx]=0; //meta buff complete
-						int ptr=0;
-						////// analyze buffer 
-lpp123:					sidx = stringcmp((char*)&buff[ptr],"http://changeit:80/media",24);
-						if(sidx >=0){//http://changeit:80/media/metadata/0/2016/8/25/1/2/s_5b4103706a6411e6855e574ec330e01d_20160825_013633_93003.zip
-							int zipidx =-1;zipidx =  stringcmp((char*)&buff[ptr],".zip",4);
-							int seridx =-1;seridx =  stringcmp((char*)&buff[ptr],".ser",4);
-							int kkk=-1;
-							if(zipidx>=0){
-								 if(seridx>=0){
-								   if(zipidx<seridx) kkk = zipidx;
-								   else kkk = seridx;
-							     }
-							     else kkk = zipidx;
-							 }
-							 else{
-								 if(seridx>=0) kkk = seridx;
-							 }
-							if(kkk>=0 && kkk>sidx+18)
-							{
-								//涓嬭浇metadata
-								char pathbuf[250] ;
-								strncpy(pathbuf,(char*)&buff[ptr+sidx+18],kkk-sidx-18+4);
-								pathbuf[kkk-sidx-18+4]=0;
-								syslog(LOG_USER | LOG_INFO, "pathbuf=%s\n", pathbuf);
-								int i;
-								for (i=0;i<ZipSerFileNum;i++)
-								{
-									if(strcmp(pathbuf,ZipSerFileName[i])==0) break;
-								}
-								if(i>=ZipSerFileNum){
-									strcpy(ZipSerFileName[ZipSerFileNum],pathbuf);
-									if(ZipSerFileNum<1000-1) ZipSerFileNum++;
-								}
-								ptr=ptr+kkk+4;
-								goto lpp123;
-							}
-						}
-					}
-				}
+			*pBuff='\0';
+			
+
+			//res = fread ( chars, 1, sizeof(chars), pFile );
+			char* urnbegin=strstr(buff, "<urn:");
+			
+			//ctxt = xmlCreatePushParserCtxt(NULL, NULL, urnbegin, totalres-(urnbegin-buff), NULL);
+			//if (ctxt == NULL) {
+			//	fprintf(stderr, "Failed to create parser context !\n");
+			//}
+			//
+			//xmlParseChunk(ctxt, urnbegin, totalres-(urnbegin-buff), 1);
+
+			//while ((res = fread ( chars, 1, sizeof(chars), pFile )) > 0) { // fread(chars, 4)
+			//while ((res = read(GSocket,chars,sizeof(chars))) > 0) {
+			//	xmlParseChunk(ctxt, chars, res, 0);
+			//}
+
+			//doc = ctxt->myDoc;
+			doc = xmlParseDoc(urnbegin);
+			//res = ctxt->wellFormed;
+			//xmlFreeParserCtxt(ctxt);
+
+			//if (!res) {
+			//	fprintf(stderr, "Failed to parse %s\n", "pro.txt");
+			//}
+
+			/* Create xpath evaluation context */
+			xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc);
+			if(xpathCtx == NULL) {
+				fprintf(stderr,"Error: unable to create new XPath context\n");
+				xmlFreeDoc(doc); 
+				return(-1);
 			}
- 		}// while end
+			
+			//xmlNode * root_element = xmlDocGetRootElement(doc);
+			//{
+			//	xmlNode *cur_node = NULL;
+            //
+			//	for (cur_node = root_element; cur_node; cur_node = cur_node->next) {
+			//		if (cur_node->type == XML_ELEMENT_NODE) {
+			//			syslog(LOG_USER | LOG_INFO, "node type: Element, name: %s    %s\n", cur_node->ns->prefix,cur_node->ns->href	);
+			//		}
+			//	}
+			//}
+
+			if(xmlXPathRegisterNs(xpathCtx, "urn", "urn:videoiq-com") != 0) {
+				fprintf(stderr,"Error: unable to register NS with prefix=\"\" and href=\"\"\n");
+				return(-1);	
+			}
+
+			xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression("//urn:strings", xpathCtx);
+			if(xpathObj == NULL) {
+				fprintf(stderr,"Error: unable to evaluate xpath expression \"%s\"\n", "urn:strings");
+				xmlXPathFreeContext(xpathCtx); 
+				xmlFreeDoc(doc); 
+				return(-1);
+			}
+			
+			ZipSerFileNum=0;
+			xmlNodeSetPtr nodes = xpathObj->nodesetval;
+			for(int i = 0; i<nodes->nodeNr; i++)
+			{
+				const char* uri=xmlNodeGetContent(nodes->nodeTab[i]);
+				strcpy(ZipSerFileName[ZipSerFileNum], uri+18);
+				ZipSerFileNum++;
+				syslog(LOG_USER | LOG_INFO, "%s\n", uri+18);
+			}
+			
+			syslog(LOG_USER | LOG_INFO, "ser parse finished\n");
+			
+
+			xmlFreeDoc(doc);
+			
+
+			//fclose(pFile);
+			close(GSocket);
+			
+			
+		}
+		
+
 	}//for 
 
 	//////// get
-#ifdef DBG_LOG
 	syslog(LOG_USER | LOG_INFO, "ZipSerFileNum=%d\n", ZipSerFileNum);
-#endif
 
 	for(ZipCountidx = 0; ZipCountidx<ZipSerFileNum; ZipCountidx++)
 	{
-#ifdef DBG_LOG
 	syslog(LOG_USER | LOG_INFO, "ZipSerFileName[%d]=%s\n", ZipCountidx, (char*)ZipSerFileName[ZipCountidx]);
-#endif
 
 		if(stringcmp((char*)ZipSerFileName[ZipCountidx],".ser",4)>=0)
 		{

--
Gitblit v1.8.0