关闭

接收浏览器上传文件的cgi程序

459人阅读 评论(0) 收藏 举报
**************************************************************************
2007-1-5 11:42 establish by lzh.A cgi program.
get a file from user's explorer.
***************************************************************************/
#include <stdio.h>
#include <string.h>

#define DEAL_BUF_LEN  1024
#define SIGN_CODE_LEN  100
#define FILE_NAME_LEN 64
#define FILE_SAVE_DIR "/var/log/"
enum
{
 STATE_START,
 STATE_GET_SIGN_CODE,
 STATE_GET_FILE_NAME,
 STATE_GET_FILE_START,
 STATE_GET_FILE_CONTENT,
 STATE_CHECK_END,
 STATE_END
};
/***************************************************************************
ShowErrorInfo
****************************************************************************/
static void ShowErrorInfo(char * error)
{

 printf("Content-type:text/plain/n/n");
 printf(error);
}

/* 主体从这里开始 */

int main(void)
{
 FILE *fp; /* 文件指针,保存我们要获得的文件 */
 int getState = STATE_START;
 int contentLength;/*标准输入内容长度*/
 int nowReadLen;
 int signCodeLen;
 int tmpLen;
 char *nowReadP;
 char *nowWriteP;
 char dealBuf[DEAL_BUF_LEN];
 char signCode[SIGN_CODE_LEN]; /*存储本次的特征码*/
 char tmpSignCode[SIGN_CODE_LEN];
 char fileName[FILE_NAME_LEN];
 memset(dealBuf,0,DEAL_BUF_LEN);
 memset(signCode,0,SIGN_CODE_LEN);
 memset(fileName,0,FILE_NAME_LEN);
 nowReadLen = 0;
 if((char *)getenv("CONTENT_LENGTH")!=NULL)
 {
  contentLength = atoi((char *)getenv("CONTENT_LENGTH"));
 }
 else
 {
  ShowErrorInfo("没有数据!");
  exit(1);
 }
 
 while(contentLength > 0)
 {
  if(contentLength >= DEAL_BUF_LEN)
  {
   nowReadLen = DEAL_BUF_LEN;
  }
  else
  {
   nowReadLen = contentLength;
  }
  contentLength -= nowReadLen;
  if(fread(dealBuf,sizeof(char),nowReadLen,stdin) != nowReadLen)
  {
   ShowErrorInfo("读取数据错误!");
   exit(1);
  }
  nowReadP = dealBuf;
  while(nowReadLen > 0)
  {
   switch (getState)
   {
    case STATE_START:
     nowWriteP = signCode;
     getState = STATE_GET_SIGN_CODE;
    case STATE_GET_SIGN_CODE:
     if(strncmp(nowReadP,"/r/n",2) == 0)
     {
      signCodeLen = nowWriteP - signCode;
      nowReadP++;
      nowReadLen--;
      *nowWriteP = 0;
      getState = STATE_GET_FILE_NAME;
      //ShowErrorInfo(signCode);
     }
     else
     {
      *nowWriteP = *nowReadP;
      nowWriteP++;
     }
     break;
    case STATE_GET_FILE_NAME:
     if(strncmp(nowReadP,"filename=",strlen("filename=")) == 0)
     {
      nowReadP += strlen("filename=");
      nowReadLen -= strlen("filename=");
      nowWriteP = fileName + strlen(FILE_SAVE_DIR);
      while(*nowReadP != '/r')
      {
       if(*nowReadP == '//')
       {
        nowWriteP = fileName + strlen(FILE_SAVE_DIR);
       }
       else if(*nowReadP != '/"')
       {
        *nowWriteP = *nowReadP;
        nowWriteP++;
       }
       nowReadP++;
       nowReadLen--;
      }
      *nowWriteP = 0;
      nowReadP++;
      nowReadLen--;
      getState = STATE_GET_FILE_START;
      memcpy(fileName,FILE_SAVE_DIR,strlen(FILE_SAVE_DIR));
      if((fp=fopen(fileName,"w"))==NULL)
      {
       fprintf(stderr,"open file error/n");
       exit(1);
      }
      //ShowErrorInfo(fileName);
     }
     break;
    case STATE_GET_FILE_START:
     if(strncmp(nowReadP,"/r/n/r/n",4) == 0)
     {
      nowReadP += 3;
      nowReadLen -= 3;
      getState = STATE_GET_FILE_CONTENT;
      //ShowErrorInfo("get");
     }
     break;
    case STATE_GET_FILE_CONTENT:
     if(*nowReadP != '/r')
     {
      fputc(*nowReadP,fp);
     }
     else
     {
      if(nowReadLen >= (signCodeLen + 2))
      {
       if(strncmp(nowReadP + 2,signCode,signCodeLen) == 0)
       {
        getState = STATE_END;
        nowReadLen = 1;
        ShowErrorInfo("传输完成");
       }
       else
       {
        fputc(*nowReadP,fp);
       }
      }
      else
      {
       getState = STATE_CHECK_END;
       nowWriteP = tmpSignCode;
       *nowWriteP = *nowReadP;
       nowWriteP++;
       tmpLen = 1;
      }
     }
     break;
    case STATE_CHECK_END:
     if(*nowReadP != '/r')
     {
      if(tmpLen < signCodeLen + 2)
      {
       *nowWriteP = *nowReadP;
       nowWriteP++;
       tmpLen++;
       if(tmpLen == signCodeLen + 2)
       {
        *nowWriteP = 0;
        if((tmpSignCode[1] == '/n')&&(strncmp(tmpSignCode + 2,signCode,signCodeLen) == 0))
        {
         getState = STATE_END;
         nowReadLen = 1;
         ShowErrorInfo("传输完成");
        }
        else
        {
         //fprintf(fp,tmpSignCode);
         fwrite(tmpSignCode,sizeof(char),tmpLen,fp);
         getState = STATE_GET_FILE_CONTENT;
        }
       }
      }
     }
     else
     {
      *nowWriteP = 0;
      //fprintf(fp,tmpSignCode);
      fwrite(tmpSignCode,sizeof(char),tmpLen,fp);
      nowWriteP = tmpSignCode;
      *nowWriteP = *nowReadP;
      nowWriteP++;
      tmpLen = 1;
     }
     break;
    case STATE_END:
     nowReadLen = 1;
     break;
    default:break;
   }
   nowReadLen--;
   nowReadP++;
  }
 }
 if(fp != NULL)
 {
  fclose(fp);
 }
 return 0;
}
 

0
0

猜你在找
【套餐】Hadoop生态系统零基础入门
【套餐】嵌入式Linux C编程基础
【套餐】2017软考系统集成项目——任铄
【套餐】Android 5.x顶级视频课程——李宁
【套餐】深度学习入门视频课程——唐宇迪
【直播】广义线性模型及其应用——李科
【直播】从0到1 区块链的概念到实践
【直播】计算机视觉原理及实战——屈教授
【直播】机器学习之凸优化——马博士
【直播】机器学习&数据挖掘7周实训--韦玮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:183175次
    • 积分:3366
    • 等级:
    • 排名:第9921名
    • 原创:146篇
    • 转载:0篇
    • 译文:0篇
    • 评论:25条
    文章分类