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

原创 2007年09月24日 16:37:00
**************************************************************************
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;
}
 

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

CGI程序设计指南.pdf

  • 2014-01-08 18:06
  • 9.90MB
  • 下载

学习用c、c++写cgi程序

  • 2012-04-07 15:52
  • 38KB
  • 下载

用Delphi编写CGI程序返回图象

珠海西门子表计公司  汪永荣  ---- Delphi 4.0对Internet编程提供了强大的支持,Delphi 4.0编写CGI非常容易, Delphi 4.0本身提供了例程。但是笔者在实践中...

CGI程序设计指南

  • 2013-05-24 18:55
  • 9.90MB
  • 下载

飞凌OK6410板boa移植 同时还有cgi程序测试

大家都知道飞凌自带的移植的boa

cgicc写cgi程序的库

  • 2010-06-03 17:09
  • 7.77MB
  • 下载

使用dll创建cgi程序(181KB)

  • 2006-02-23 09:05
  • 180KB
  • 下载

怎么样调用cgi程序

一、cgi将网页重定向到index.html的方法 printf("window.setTimeout(\"location.href = 'http://ip地址'\", 1000);\n"); ...

机器人大战CGI程序

  • 2002-09-23 00:00
  • 1.22MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)