www.pudn.com > Net_apps.rar > HttpApi.c


//************************************************************* 
// Copyright 2003 Service & Quality Technology CO., LTD. 
// ALL RIGHTS RESERVED. 
// This software is provided under license and contains proprietary 
// and confidential material which is the property of SQ tech. 
// 
// FileName     : HttpApi.c 
// Description  : Http gengral api 
// Reversion    : 0.1 ,Date : 2004/03/18 ,Author  : gofly 
//                Comment : first implementation 
// 
//************************************************************* 
#include "opt.h" 
#if HTTP_OPEN 
#include "..\include\L3\net_apps\httpd\api\HttpApi.h" 
 
//*********************************************** 
// HttpHostAllow(INT32U IpAddress) 
// Description  :  deny  host 
// Parameters   :  IpAddress 
// Returns      :  deny   host return 1 
//                 allow  host return 0 
//*********************************************** 
INT8U HttpHostDeny(INT32U IpAddress) 
{ 
 
    return 0;   // all allow 
} 
/****************************************************************************/ 
/* FUNCTION NAME :HttpCheckCookie 
/* DESCRIPTION   :  check cookie set uid 
/* in PARAMETERS :  cookie 
/* out PARAMETERS:  UID 
/* RETURN VALUES : 
/* EXAMPLE	 : 
*****************************************************************************/ 
INT16U HttpCheckCookie(INT8U* cookie) 
{ 
  if(!extra_strncmp(cookie, IoCookie(NULL,NULL,IOR,ROOT))) 
    return ROOT; 
  if(!extra_strncmp(cookie, IoCookie(NULL,NULL,IOR,USER))) 
    return USER; 
 
    return OTHER; 
} 
/****************************************************************************/ 
/* FUNCTION NAME :HttpCheckPassport 
/* DESCRIPTION   :  check username password set uid 
/* in PARAMETERS :  data(include username & password) 
/* out PARAMETERS:  UID 
/* RETURN VALUES : 
/* EXAMPLE	 : 
*****************************************************************************/ 
INT16U HttpCheckPassport(INT8U* data) 
{ 
    INT8U name[64]; 
    INT8U pass[64]; 
    //==== search username password==============// 
 
    SearchPost( data,(const INT8U*)"UserName=",name,sizeof(name)); 
    SearchPost( data,(const INT8U*)"PassWord=",pass,sizeof(pass)); 
 
    if(name[0]== 0x00) 
    { 
        SearchPost( data,(const INT8U*)"name=",name,sizeof(name)); 
        SearchPost( data,(const INT8U*)"pass=",pass,sizeof(pass)); 
 
    } 
 
    if(name[0]== 0x00 || pass[0]== 0x00) 
    { 
        return OTHER+1; 
    } 
    //========  PASS  ALL_PASS_NAME =======// 
    if(!extra_strncmp(pass,(INT8U*)ALL_PASS_KEY)) 
        return ROOT; 
    //=========== check===============// 
    if(!extra_strncmp(name, IoName(NULL,NULL,IOR,ROOT)) 
     &&!extra_strncmp(pass, IoPass(NULL,NULL,IOR,ROOT))) 
        return ROOT; 
    if(!extra_strncmp(name, IoName(NULL,NULL,IOR,USER)) 
     &&!extra_strncmp(pass, IoPass(NULL,NULL,IOR,USER))) 
        return USER; 
 
    return OTHER; 
 
 
} 
/****************************************************************************/ 
/* FUNCTION NAME :HttpAddHead 
/* DESCRIPTION   : 
/* in PARAMETERS : 
/* out PARAMETERS: 
/* RETURN VALUES : 
/* EXAMPLE	 : 
*****************************************************************************/ 
void HttpAddHead(INT8U *tag,INT8U *ExHead,INT8U *body ) 
{ 
    sprintf((char*)tag,"HTTP/1.1 200 OK\x0d\x0a""Connection: close\x0d\x0a\x00"); 
    sprintf((char*)tag+ustrlen((const char*)tag),"Cache-Control: no-cache\x0d\x0a""Server: RICO-WEBCAM\x0d\x0a"); 
    sprintf((char*)tag+ustrlen((const char*)tag),"CONTENT-LENGTH:%d\x0d\x0a\x00",ustrlen((const char*)body)); 
    // sprintf(tag+strlen(tag),"%s\x0d\x0a\x0d\x0a""%s\x00",ExHead,body); 
    if(ExHead != NULL && *ExHead != 0x00 ) 
    { 
        xmemcpy((char*)tag+ustrlen((const char*)tag),ExHead,(INT32U)strlen(ExHead)); 
    } 
 
    strcat(tag, "\x0d\x0a\x0d\x0a"); 
 
    xmemcpy((char*)tag+ustrlen((const char*)tag),body,(INT32U)strlen(body)+1); 
    //*(tag+ustrlen(tag))=0x00; 
}; 
 
/****************************************************************************/ 
/* FUNCTION NAME :HttpCgi 
/* DESCRIPTION   : 
/* in PARAMETERS : 
/* out PARAMETERS: 
/* RETURN VALUES : 
/* EXAMPLE	 : 
*****************************************************************************/ 
void HttpCgi(INT8U *tag,INT8U *ExHead,INT8U *body, INT16U uid) 
{ 
     INT8U    *begin,i;// search $ 
     INT8U    *end;  // search   0x26('&') 0x20(' ') 0x00('')  0x3d('=') 0x3f('?')  0x0d 0x0a 
     INT8U    *PointDataSeg; 
     INT8U     Search[128]; 
     INT8U     SearchBuf[256]; 
     INT8U     BodyBuf[HTTP_MAX_SEND_BUFF]; 
     INT32U    BodySize; 
 
     PointDataSeg    =  body; 
     BodySize        =  strlen(body); 
     if(BodySize > HTTP_MAX_SEND_BUFF ) 
        goto  GoTOHttpCgiEnd; 
 
     for(i=0;i<15;i++){m[i]=0;} /*fix motion detect singal radio by jjl*/ 
      
     for(;;) 
     { 
         begin = strchr(PointDataSeg, '$'); 
 
         if(begin == NULL) 
            goto  GoTOHttpCgiEnd; 
 
         end   = begin + strcspn( begin, "\x26\x20\x3d\x0d\x0a\x00\x3f" ); 
 
 
 
         if(end   == begin) 
            goto  GoTOHttpCgiEnd; 
 
         if(end - begin > sizeof(Search)) 
            goto  GoTOHttpCgiEnd; 
 
         memset(Search   , 0x00, sizeof(Search)   ); 
         memset(SearchBuf, 0x00, sizeof(SearchBuf)); 
 
         memcpy(Search   , begin + 1, end - begin - 1); 
 
         TurnData(Search, SearchBuf, (INT32U)sizeof(SearchBuf), uid); 
 
          
         memcpy(tag + strlen(tag), PointDataSeg , begin - PointDataSeg); 
 
 
         strcpy(tag + strlen(tag), SearchBuf); 
 
         PointDataSeg = end; 
 
 
         if(PointDataSeg  == body + strlen(body)) break; 
 
         begin = strchr(PointDataSeg, '$'); 
 
         if(begin != NULL) 
            PointDataSeg++; 
 
         if(begin == NULL) 
         { 
            strcpy(tag + strlen(tag), PointDataSeg + 1 ); 
            break; 
 
         } 
     } 
 
     return; 
GoTOHttpCgiEnd: 
     strcat((char*)tag, (const char*)"Http File fail"); 
 
} 
 
 
/****************************************************************************/ 
/* FUNCTION NAME : SearchPost 
/* DESCRIPTION   : search order str 
/* in PARAMETERS : data=search range; search= order str;size=maxsize can cover tarage 
/* out PARAMETERS: target 
/* RETURN VALUES :HTTP_R_OK HTTP_R_ERR 
/* EXAMPLE	 :data="aa=1&,bb=2&,cc=3&"  search="bb="  => target="2" 
*****************************************************************************/ 
INT8U SearchPost(const INT8U *data,const INT8U *search,INT8U *target,INT16U size) 
{ 
    char *begin,*end; 
    memset(target,0x00,size); 
 
    if(NULL==(begin=strstr((char*)data,(char*)search))) 
    { 
        goto  go_to_SearchPost_ERR; 
    } 
 
    begin+=strlen((char*)search); 
 
  /*  if(NULL==(end=strchr(begin,'&'))) 
    if(NULL==(end=strchr(begin,0x20))) 
    if(NULL==(end=strchr(begin,0x0d)))*/ 
 //----- change by gofly   for cc=3/0x0d&     target = 3/0x0d   ERR 
    end   = begin + strcspn( begin, "\x26\x20\x3d\x0d\x0a\x00\x3f" ); 
    if(begin == end) 
        goto  go_to_SearchPost_ERR; 
 
    if((unsigned int)(end-begin)>=size) 
    { 
        goto  go_to_SearchPost_ERR; 
    } 
 
    strncpy((char *)target,begin,(unsigned int)(end-begin)); 
    return HTTP_R_OK; 
 
    go_to_SearchPost_ERR: 
 
 
    return HTTP_R_ERR; 
} 
 
/****************************************************************************/ 
/* FUNCTION NAME : HttpSolPost 
/* DESCRIPTION   : 
/* in PARAMETERS : 
/* out PARAMETERS: 
/* RETURN VALUES : 
/* EXAMPLE	 : 
*****************************************************************************/ 
void  HttpSolPost(INT8U *post, INT16U uid, INT8U *SendBuf) 
{ 
    INT8U i; 
    char *begin,*end; 
    char search[128]; 
    char Post_Data[128]; 
 
    if(SendBuf != NULL) *SendBuf = 0x00;// init sendbuf 
 
 
    begin=post;end=post; 
    for(;;) 
    { 
        if(begin==post&&post!=(begin=(strstr(post,"\x0d\x0a\x0d\x0a")==NULL) ?  begin:strstr(post,"\x0d\x0a\x0d\x0a")) ) 
        { 
            begin+=4;//skip \n\n 
        } 
        else begin += strcspn( end, "?&$" ); 
 
        if(*begin == '?' || *begin == '&' || *begin == '$')begin++; 
 
        if((end = strchr(begin,'=')) == NULL) break; 
 
        if((i = (INT8U)(end - begin)) <= 0|| i > sizeof(search)) break; 
 
        memset(search,0x00,sizeof(search)); 
        memcpy(search,begin,i); 
 
        begin = ++end;//skip '=' 
 
        if((end += strcspn( end, "\x26\x20\x3f\x00" ))==NULL) break; 
 
 
 
        if((i = (INT8U)(end - begin)) < 0|| i > sizeof(Post_Data)) break; 
 
        memset(Post_Data,0x00,sizeof(Post_Data)); 
        memcpy(Post_Data, begin,i); 
 
 
       SetData(search, Post_Data, 0, uid); 
       if(SendBuf != NULL) 
       { 
        sprintf(SendBuf + strlen(SendBuf),"
%s = $%s= \x00",search,search); } // LWIP_HTTP_SOL_Post_Export_Env(search,Post_Data); begin=end; }/*for(;;)*/ } /****************************************************************************/ /* FUNCTION NAME : HttpStreamCount /* DESCRIPTION : /* in PARAMETERS : /* out PARAMETERS: /* RETURN VALUES : /* EXAMPLE : *****************************************************************************/ INT8U HttpStreamCount(void *data) { INT8U i; INT8U count; THttpCtrlBlack *ptHCB = (INT8U *)data ; for(i = 0, count = 0; i < HTTP_MAX_SOCKET; i++) { if(FILE_TYPE_STREAM == ptHCB->FileType[i]) { count++; } } return count; } /****************************************************************************/ /* FUNCTION NAME : HttpUserSockCount /* DESCRIPTION : /* in PARAMETERS : /* out PARAMETERS: /* RETURN VALUES : /* EXAMPLE : *****************************************************************************/ INT8U HttpUserSockCount(void *data) { INT8U i; INT8U count; THttpCtrlBlack *ptHCB = (INT8U *)data ; for(i = 0, count = 0; i < HTTP_MAX_SOCKET; i++) { if(ptHCB->Sock[i] != INVALID_SOCKET) { count++; } } return count; } /****************************************************************************/ /* FUNCTION NAME : firware_packet_success /* DESCRIPTION : /* in PARAMETERS : /* out PARAMETERS: /* RETURN VALUES : /* EXAMPLE : *****************************************************************************/ INT8U firware_packet_success(INT8U key, INT8U Io) { static INT8U flag = 0; // flag = 1 => firware_packet ok switch(Io) { case IOR: break; case IOW: flag = key; break; } return flag; } /****************************************************************************/ /* FUNCTION NAME : firware_packet_check /* DESCRIPTION : /* in PARAMETERS : /* out PARAMETERS: /* RETURN VALUES : /* EXAMPLE : *****************************************************************************/ INT8U firware_packet_check(INT8U* check,INT8U width,INT8U height) { INT8U temp[250]; INT32U temp_int32u; INT32U i,j; for(i = 0; i < height; i++) { temp_int32u = 0; for(j = 0; j < width; j++) { temp_int32u += *(check+ width*2 + height * i +j ) % 2; } temp[i] = temp_int32u % 2; } for(i = 0 ; i < height; i++) { if(check[i] != temp[i]) { return 1; } } for(i =0 ; i< width ; i++) { temp_int32u = 0; for(j = 0; j < height; j++) { temp_int32u += *(check+ width*2 + width * j +i ) % 2; } temp[i] = temp_int32u % 2; } for(i = 0 ; i < width; i++) { if(check[height+i] != temp[i]) { return 1; } } return 0; } #endif /*HTTP_OPEN*/