#include <stdlib.h>
#include <locale>
#include <map>
#include <io.h>
#include <fcntl.h>
#include <string.h>
#include <iostream>
using namespace std;
void OUTPUT(char* content)
{
printf(content);
printf("\n");
}
void cgi_file_upload()
{
//client submit method //GET OR POST
char *method = getenv("REQUEST_METHOD");
char *content_type = getenv("CONTENT_TYPE");
if (content_type != NULL)
{
}
if (strcmp(method,"POST") == 0)
{
//file upload content-type
if(content_type !=NULL && string(content_type).find("multipart/form-data") != string::npos)
{
int contentLen = 0;
if( getenv("CONTENT_LENGTH") )
{
contentLen = atoi( getenv("CONTENT_LENGTH") );
}
if (contentLen == 0)
{
OUTPUT("<script>");
OUTPUT("alert(\"file is error...\");</script>");
}
//default 2MB buffer
const int FILEBUFFER = 1024*1024*2;
// const int FILEBUFFER = 1024;
//total data package
int count = contentLen / FILEBUFFER;
//byte buffer for write the file bytes
int buffer = 0;
//is need handle extra package
bool append = false;
//sign first resolve http head
bool resolve_head = true;
//occur the 0x0d 0x0a 0x2d
bool end_file = false;
///////////////////////////////////////////////
FILE *ff = fopen("temp_file","w");
char temp_data[1];
fwrite(temp_data,0,0,ff);
fclose(ff);
///////////////////////////////////////////////
FILE *e = fopen("temp_file","ab");
//contenteLen < buffer
if (count == 0)
{
buffer = contentLen;
count++;
}
else
{
buffer = FILEBUFFER;
append = true;
}
for (int i=0;i<count;i++)
{
char *file_data = new char[buffer];
_setmode(_fileno(stdin),_O_BINARY);
fread(file_data,buffer,1,stdin);
_setmode(_fileno(stdin),_O_TEXT);
char sp[] = {0x0d,0x0a,0x0d,0x0a,'\0'};
char sp1[] = {0x0d,0x0a,0x2d,0x2d,'\0'};
int start_index = 0;
int end_index = 0;
int abstract_head_file_len= 0;
if (resolve_head)
{
char *index_point = strstr(file_data,sp);
start_index = index_point - file_data;
//move the point end the {0x0d,0x0a,0x0d,0x0a,'\0'}
start_index+=4;
resolve_head = false;
}
abstract_head_file_len = buffer-start_index;
// log_<<"abstract_head_file_len:"<<abstract_head_file_len<<"\n";
//search the 0x0d 0x0a index
for (int j=0;j<abstract_head_file_len;j++)
{
bool code1 = false;
bool code2 = false;
bool code3 = false;
bool code4 = false;
if (*(file_data+start_index+j) == 0X0D)
code1 = true;
if ((j+1<abstract_head_file_len) && *(file_data+start_index+j+1)==0X0A)
code2 = true;
if ((j+2<abstract_head_file_len) && *(file_data+start_index+j+2)==0X2d)
code3 = true;
if ((j+3<abstract_head_file_len) && *(file_data+start_index+j+3)==0X2d)
code4 = true;
if (code1 && code2 && code3 && code4)
{
end_file = true;
break;
}
end_index ++;
}//end for2
// log_<<"end_index:"<<end_index<<"\n";
fwrite(file_data+start_index,1,end_index,e);
//if occur the end of the file than break the for ...
if (end_file)
{
break;
}
delete[] file_data;
}//end for1
//the last hos not read bytes...
if (append && !end_file)
{
int last_file_len = (contentLen - FILEBUFFER * count);
int end_index = 0;
char *file_data = new char[last_file_len];
_setmode(_fileno(stdin),_O_BINARY);
fread(file_data,last_file_len,1,stdin);
_setmode(_fileno(stdin),_O_TEXT);
//search the 0x0d 0x0a index
for (int i=0;i<buffer;i++)
{
bool code1 = false;
bool code2 = false;
bool code3 = false;
bool code4 = false;
if (*(file_data+i) == 0X0D)
code1 = true;
if ((i+1<last_file_len) && *(file_data+i+1)==0X0A)
code2 = true;
if ((i+2<last_file_len) && *(file_data+i+2)==0X2d)
code3 = true;
if ((i+3<last_file_len) && *(file_data+i+3)==0X2d)
code4 = true;
if (code1 && code2 && code3 && code4)
{
end_file = true;
break;
}
end_index ++;
}
fwrite(file_data,1,end_index,e);
delete[] file_data;
}
fclose(e);
printf("<script>alert('file update success...');</script>");
printf("<script>window.history.back();</script>");
}
}
}
int main(int argc,char *argv[])
{
cgi_file_upload();
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
cgi_file_upload
共25个文件
pdb:4个
manifest:3个
obj:2个
需积分: 14 15 下载量 57 浏览量
2014-03-20
16:01:46
上传
评论
收藏 703KB RAR 举报
温馨提示
用纯C++语言写的CGI 处理http协议文件上传,程序中可以设置缓存区,不是把文件一下子全部的写入硬盘,而是从stdin中读取流,然后放置到缓存区,再由缓存区写入文件中,经测试可以上传好几G大小的文件。适用在arm或者是 linux windows上面。若有任何问题联系qq:987458781
资源推荐
资源详情
资源评论
收起资源包目录
cgi_file_upload.rar (25个子文件)
cgi_file_upload
Release
cgi_file_upload.pdb 363KB
cgi_file_upload.exe 10KB
cgi_file_upload
Release
cgi_file_upload.exe.intermediate.manifest 616B
BuildLog.htm 10KB
vc90.pdb 204KB
vc90.idb 99KB
mt.dep 67B
upload.obj 677KB
upload.cpp 5KB
cgi_file_upload.vcproj.dadi--20130626H.Administrator.user 1KB
cgi_file_upload.vcproj 4KB
Debug
cgi_file_upload.exe.intermediate.manifest 621B
BuildLog.htm 14KB
vc90.pdb 244KB
vc90.idb 203KB
mt.dep 67B
cgi_file_upload.exe.embed.manifest.res 728B
upload.obj 38KB
cgi_file_upload.exe.embed.manifest 663B
cgi_file_upload.sln 911B
cgi_file_upload.ncb 779KB
cgi_file_upload.suo 17KB
Debug
cgi_file_upload.ilk 355KB
cgi_file_upload.pdb 595KB
cgi_file_upload.exe 39KB
共 25 条
- 1
资源评论
I'M老顽童
- 粉丝: 8
- 资源: 46
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功