⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 http.c

📁 ucos移植到44b0
💻 C
📖 第 1 页 / 共 3 页
字号:
            else
            {
                return USER_PSW_ERR;
            }            
        }
    }
    //Uart_Printf("out CheckUserData: \n");
    return USER_NOT_EXIST;

}
//初始化用户数据和文件数据
void initData(void)
{
    /**********init userdata***************/
   initUserInfo();
   initFileInfo();   
   /**************************************/
}
void handleConnection(struct netconn * newconn)
{
    struct fileHdrStruct filehdr;
    struct netbuf *buf;
    struct fileSetStruct file;
    
    char  loop;
    void  *data;//内存缓冲区指针
    char  *datab;
    
    //curlen:报文缓冲区长度标志
    //len:   当前读取内存缓冲区长度标志
    _US  len,curlen;
    //ret:      比较值临时变量 
    //PostMark: 当前请求是否为POST操作标志
    //finddata: POST操作搜索数据区标志
    //g_DataPtr:POST操作数据区指针
    //cont_lenth:POST操作数据长度(字符串表示)
    //dataLen   :POST操作数据长度
    _US  ret;
    _US PostMark = 0;
    _UC finddata = 0;
    char  *g_DataPtr;
    char  cont_lenth[5]={0};
    _US   dataLen=0;
    _US   userNo = 0;
    
    
    char username[11] = {0};
    char password[7]  = {0};
    ERROR_CODE errCode;    
    
    _US  sendLen = 0;   
    _US  templenth = 0;

  //read the requrst Header
    loop  = 1;
    curlen = 0;
    PostMark = 0;
    finddata = 0;
    dataLen  = 0;
    
    memset(username,0,11);
    memset(password,0,7);
    memset(cont_lenth,0,5); 
    memset(sendbuffer,0,200);
    memset(buffer,0,MAX_CONNECTION_BUFFER);

    while(loop)//是否循环读包:  判断请求报文是否完整
    {
       // Uart_Printf("come into while(loop)\n");
        buf = netconn_recv(newconn);
        if (NULL == buf)
        {//返回到主处理函数关闭socket
            return;
        }         
        //Uart_Printf("past netconn_recv\n");
        do
		{//读缓冲区信息
            errCode = (char)netbuf_data(buf, &data, &len);
            if (ERR_OK != errCode)
            {//返回到主处理函数关闭socket
                return;
            }
            if (0 == len)
            {//返回到主处理函数关闭socket
                return;
            }
            
            if ((len + curlen)>=MAX_CONNECTION_BUFFER)
            {//返回到主处理函数关闭socket
                 return;               
            }
            
            datab = (char *) data;

            //Uart_Printf("datab =");
            //for(templenth = 0;templenth<len;templenth++)
            //{
                //Uart_Printf("%c",datab[templenth]);
            //}
            //Uart_Printf("\n");
                

            //templenth = sprintf(&buffer[curlen],datab);//写到报文缓冲区
            strncpy(&buffer[curlen],datab,len);
            curlen = curlen + len;
            //Uart_Printf("len=%lu\n",len);
            //Uart_Printf("templenth=%lu\n",templenth);
            //templenth = 0;
           #if 0
            Uart_Printf("buffer=%c",buffer[0]);
            Uart_Printf("%c",buffer[1]);
            Uart_Printf("%c",buffer[2]);
            Uart_Printf("%c",buffer[3]);
            Uart_Printf("%c",buffer[4]);
            Uart_Printf("%c\n",buffer[5]);
            Uart_Printf("curlen=%lu\n",curlen);
            
            if (curlen > 500)
            {
                Uart_Printf("buffer = ");
                for(templenth = 0;templenth<curlen;templenth++)
                {
                    Uart_Printf("%c",buffer[templenth]);
                }
                Uart_Printf("\n");
                
            }
            #endif
            

            if (finddata == 0)//not find the data start
            {
                g_DataPtr = strstr(buffer,"\r\n\r\n");
                if (g_DataPtr)
                {
                    g_DataPtr = strstr(buffer,"Content-Length:");
                    if (g_DataPtr == NULL)
                    {//没有Content-Length标志,则认为报文接收完毕
                       loop = 0;//请求报文是否完整标志符号 ....                       
                       //Uart_Printf("PostMark == 0\n");
                       break;
                    }
                    
                    g_DataPtr = g_DataPtr+15;
                    strncpy(cont_lenth,g_DataPtr,4);
                    //Uart_Printf("cont_lenth = %s\n",cont_lenth);
                    dataLen = atoi(cont_lenth);
                    //Uart_Printf("dataLen = %lu\n",dataLen);
                    finddata = 1;
                    //结束标志存在于最后一个报文中
                    g_DataPtr=strstr(buffer,"\r\n\r\n");
                    if ((g_DataPtr-buffer+dataLen+4)>=curlen)
                    {
                          loop = 0;
                          break;
                    }                    
                } 
                else
                {
                    continue;//继续接收
                }    
            }
            else //find the data start
            {//判断是否接收完毕               
               g_DataPtr=strstr(buffer,"\r\n\r\n");
               if (g_DataPtr)
               {
                  if ((g_DataPtr-buffer+dataLen+4)>=curlen)
                  {
                      //Uart_Printf("complete\n");
                      //Uart_Printf("total = %lu\n",(g_DataPtr-buffer+dataLen+4));
                      //Uart_Printf("curlen = %lu\n",curlen);
                      loop = 0;
                      break;
                  }
                  else
                  {
                     // Uart_Printf(" str = %s\n",(g_DataPtr+4)); 
                     ;
                  }

                  continue;
               }
               else
               {//异常情况,丢掉接收包
                   return;
               }            
            }
		}
		while(netbuf_next(buf) >= 0);
		netbuf_delete(buf);         
    }
    //Uart_Printf("strncmp(buffer)\n");
    
    //开始处理请求
    if(!strncmp(buffer,"GET",3))
    {//查看是否GET操作
       //得到请求的文件名字
       
       getFilename((char *)buffer,(char *)filename,4);
       //Uart_Printf("getFilename = %s\n",filename);

       if (!strcmp(filename,"check.html"))
       {
           netconn_write(newconn, (char *)Forbidden, strlen(Forbidden), NETCONN_COPY);	           
           return;
       }
       
       if (!strcmp(filename,"log"))
       {
           //发送操作日志文件
           sendLog(newconn);           
       }
       else
       {   //查找对应的文件
           //Uart_Printf("filename = %s\n",filename);
           //Uart_Printf("fileSet[0].name = %s\n",fileSet[0].name);
           //Uart_Printf("filename = %s\n",filename);
           ret = lookupFile(filename, &file);   
           //Uart_Printf("ret = %d\n",ret);
           if (ret > 0)
           {//相应请求的文件处理
               //returnFile(newconn,&filehdr);
               if (1 == file.isVerify)//需要鉴权
               {
                  netconn_write(newconn, (char *)Forbidden, strlen(Forbidden), NETCONN_COPY);
                  return;
               }
               sendFile(newconn,&file);
           }
           else
           {//相应的文件不存在
               emitByte(PREFIX_BYTE);
               emitByte(UNKNOWN_FILE);
               emitString(filename);
               emitByte(PREFIX_BYTE);
               netconn_write(newconn, (char *)notfound, strlen(notfound), NETCONN_COPY);	
           }
       }
    }
    else if(!strncmp(buffer,"POST",4))
        {//查看是否POST操作 
           //Uart_Printf("come into POST\n");
           getFilename((char *)buffer,(char *)filename,5); 
           //Uart_Printf("filename = %s\n",filename);
           if (!strncmp(filename,"check.html",6))
           {
               if (  (!parseVariable((buffer+curlen-dataLen),"UserName",username))
                   &&(!parseVariable((buffer+curlen-dataLen),"Password",password)) )
               {
                   //Uart_Printf("username = %s\n",username);
                   //Uart_Printf("password  = %s\n",password);
                   errCode = CheckUserData(username,password,&userNo);
                   //Uart_Printf("errCode  = %lu\n",errCode);
                   switch (errCode)
                   {
                      #if 0
                       case USER_INPUT_ERR: 
                       {
                            sendLen = sprintf(sendbuffer,UserCheckFail,param);
                            netconn_write(newconn, (char *)sendbuffer, sendLen, NETCONN_COPY);	
                            return;
                       }
                       case USER_NOT_EXIST:
                       {
                            sendLen = sprintf(sendbuffer,UserCheckFail,"user is not exist");
                            netconn_write(newconn, (char *)sendbuffer, sendLen, NETCONN_COPY);	
                            return;
                       }
                       case USER_PSW_ERR:
                       {
                            sendLen = sprintf(sendbuffer,UserCheckFail,"password is error");
                            netconn_write(newconn, (char *)sendbuffer, sendLen, NETCONN_COPY);	
                            return;
                       }
                       #endif
                       case USER_VALID:
                       {
                            //sendLen = sprintf(sendbuffer,UserCheckFail,"user is valid");
                            //netconn_write(newconn, (char *)sendbuffer, sendLen, NETCONN_COPY);	

                            strcpy(filename,"disp.html");
                            ret = lookupFile(filename, &file);
                            if (ret > 0)
                            {//相应请求的文件处理
                               //returnFile(newconn,&filehdr);
                               sendFile(newconn,&file);
                            }
                            else
                            {
                                netconn_write(newconn, (char *)sendbuffer, sendLen, NETCONN_COPY);	                            
                            }
                            return;
                       }
                       default :
                       {
                            strcpy(filename,"regerror.html");
                            ret = lookupFile(filename, &file);
                            if (1 == file.isVerify)//需要鉴权
                            {
                              netconn_write(newconn, (char *)Forbidden, strlen(Forbidden), NETCONN_COPY);
                              return;
                            }
                            sendFile(newconn,&file);
                            return;
                       }
                   }                 
               }               
               else 
               {  
                   sendLen = sprintf(sendbuffer,UserCheckFail,"check failure");
                   netconn_write(newconn, (char *)sendbuffer, strlen(sendbuffer), NETCONN_COPY);	
                   return;
               }               
           }  
           else if (!strcmp(filename,"login.html"))
               {   
                   //Uart_Printf("strcmp(filename,login.html\n");
                   ret = lookupFile(filename, &file);   
                   //Uart_Printf("ret = %d\n",ret);
                   if (ret > 0)
                   {//相应请求的文件处理
                       //returnFile(newconn,&filehdr);
                       if (1 == file.isVerify)//需要鉴权
                       {
                          netconn_write(newconn, (char *)Forbidden, strlen(Forbidden), NETCONN_COPY);
                          return;
                       }
                       sendFile(newconn,&file);
                   }
                   else
                   {//相应的文件不存在
                       emitByte(PREFIX_BYTE);
                       emitByte(UNKNOWN_FILE);
                       emitString(filename);
                       emitByte(PREFIX_BYTE);
                       netconn_write(newconn, (char *)notfound, strlen(notfound), NETCONN_COPY);	
                   }
               }
           else if (!strcmp(filename,"register.html"))
               {   
                   //Uart_Printf("strcmp(filename,login.html\n");
                   
                   if (  (!parseVariable((buffer+curlen-dataLen),"UserName",username))
                   &&(!parseVariable((buffer+curlen-dataLen),"Password",password)) )
                   {
                       //Uart_Printf("username = %s\n",username);
                       //Uart_Printf("password  = %s\n",password);
                       errCode = addUserData(username,password,100);
                       if (USER_SUCCESS == errCode)
                       {
                            strcpy(filename,"regSuccess.html");
                            //printUserData();
                       }
                       else
                       {
                            strcpy(filename,"regMaxout.html");
                            //Uart_Printf("addUserData failure\n");
                       }                      
                   }
                   else
                   {
                       //Uart_Printf("no parseVariable\n");
                       strcpy(filename,"regMaxout.html");
                   }
                   
                   ret = lookupFile(filename, &file);   
                   //Uart_Printf("ret = %d\n",ret);
                   if (ret > 0)
                   {//相应请求的文件处理
                       //returnFile(newconn,&filehdr);
                       if (1 == file.isVerify)//需要鉴权
                       {
                          netconn_write(newconn, (char *)Forbidden, strlen(Forbidden), NETCONN_COPY);
                          return;
                       }
                       sendFile(newconn,&file);
                   }
                   else
                   {//相应的文件不存在
                       emitByte(PREFIX_BYTE);
                       emitByte(UNKNOWN_FILE);
                       emitString(filename);
                       emitByte(PREFIX_BYTE);
                       netconn_write(newconn, (char *)notfound, strlen(notfound), NETCONN_COPY);	
                   }
               }
           #if 0
           //ret = callDynamicHander(filename,findMsgBody(buffer));
           ret = callDynamicHander(filename,buffer);
           if (ret == 0)
           {
               netconn_write(newconn, created, strlen(created), NETCONN_COPY);	
           }
           else
           {
               netconn_write(newconn, notfound, strlen(notfound), NETCONN_COPY);	 
           }  

           emitByte(PREFIX_BYTE);
           emitByte(POST_REQUSET);
           emitString(filename);
           emitByte(SUFFIX_BYTE);
           #endif
        }
        else
        {
           netconn_write(newconn, notimplemented, strlen(notimplemented), NETCONN_COPY);	 
        }      
}

void getFilename(char * inbuf,char * out,_UL start)
{
    _UL i = start,j = 0;

    while(inbuf[i] == ' ')
    {//skip the spaces
        i++;
    }

    i++;//去掉'/'
    //for (;i < strlen(inbuf);i++)
    for (;i <(MAX_FILE_NAME_LENTH);i++)
    {
        if (inbuf[i] == ' ')
        {
            out[j] = 0;
            break;
        }
        out[j++] = inbuf[i];
    }

    if (!strcmp(out,""))
    {
        //strcpy(out,"/index.html");
        strcpy(out,"login.html");
    }   
}

void returnFile(struct netconn * newconn,struct fileHdrStruct * filehdr)
{
    _UL ct;    

    ct = determineContentType(filehdr->hdrStart);

    returnFileHeader(newconn,ct);

    if ( (ct == TEXT_HTML)||(ct == TEXT_HDML) )
    {
        parseAndEmitFile(newconn,filehdr);
    }
    else
    {//将文件传回客户端
        netconn_write(newconn, &filedata[filehdr->fileStart], filehdr->size, NETCONN_COPY);	 
    }
}

_UL determineContentType(_UL fileOffset)
{
    char suffix[20];
    _UL i;

    fileOffset+=2;
    for (;filedata[fileOffset] != 0;fileOffset++)
    {
        if (filedata[fileOffset] == '.')
        {
            break;
        }
    }

    if (filedata[fileOffset] == 0)
    {
        return (TEXT_PLAIN);
    }
    else

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -