houxiao
2016-12-09 cf47186234a721d072a34fd6a4721d093af9fd5e
fix for GetSearchMetaInfo parse ser list

git-svn-id: http://192.168.1.226/svn/proxy@5 454eff88-639b-444f-9e54-f578c98de674
2个文件已修改
199 ■■■■■ 已修改文件
ProxyServer/make.sh 6 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ProxyServer/proxyserver.c 193 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ProxyServer/make.sh
@@ -6,8 +6,8 @@
rm demo *.o *.so
rm /usr/local/lib/libproxy.so
gcc -std=gnu99 -g -c -fPIC -o proxy.o proxyserver.c
gcc -std=gnu99 -shared -pthread -o libproxy.so proxy.o -lmysqlclient
gcc -std=gnu99 -g -c -fPIC `xml2-config --cflags` -o proxy.o proxyserver.c
gcc -std=gnu99 -shared -pthread -o libproxy.so proxy.o -lmysqlclient `xml2-config --cflags` `xml2-config --libs`
if [ $? -ne 0 ]; then
    exit
@@ -15,6 +15,6 @@
cp libproxy.so /usr/local/lib
/sbin/ldconfig
gcc -std=gnu99 -g -o demo main.c -lproxy
gcc -std=gnu99 -g -o demo main.c -lproxy `xml2-config --cflags` `xml2-config --libs`
service apache2 restart
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"
/////////////////////////////////////////////////
@@ -735,116 +740,136 @@
        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));
        //#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++)
            //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(&tmpp[xx],memoryband[i],bandsize[i]);
                        xx=bandsize[i];
                        free(memoryband[i]);
                memcpy(pBuff, chars, res);
                pBuff+=res;
                totalres+=res;
                    }
                    memcpy(buff,tmpp,SSize);
                    free(tmpp);
                    goto lpp123;
                    syslog(LOG_USER | LOG_INFO, "**********Special*******************************ZipSerFileNum = %d",ZipSerFileNum);
            *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);
                }
            //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);
            }
            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;
            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);
                                 }
                                 else kkk = zipidx;
                             }
                             else{
                                 if(seridx>=0) kkk = seridx;
                             }
                            if(kkk>=0 && kkk>sidx+18)
            ZipSerFileNum=0;
            xmlNodeSetPtr nodes = xpathObj->nodesetval;
            for(int i = 0; i<nodes->nodeNr; i++)
                            {
                                //下载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;
                const char* uri=xmlNodeGetContent(nodes->nodeTab[i]);
                strcpy(ZipSerFileName[ZipSerFileNum], uri+18);
                ZipSerFileNum++;
                syslog(LOG_USER | LOG_INFO, "%s\n", uri+18);
                                }
                                if(i>=ZipSerFileNum){
                                    strcpy(ZipSerFileName[ZipSerFileNum],pathbuf);
                                    if(ZipSerFileNum<1000-1) ZipSerFileNum++;
            syslog(LOG_USER | LOG_INFO, "ser parse finished\n");
            xmlFreeDoc(doc);
            //fclose(pFile);
            close(GSocket);
                                }
                                ptr=ptr+kkk+4;
                                goto lpp123;
                            }
                        }
                    }
                }
            }
         }// while end
    }//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)
        {