用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c++代码库

CGI

2013-03-01 作者: 云代码会员举报

[c++]代码库

int output_file(string fn, INT64 offset, INT64 length, int partial) {
	if(offset < 0)
		offset = 0;
	if(length < 0)
		length = 0;

	string_trim(fn);
	if(fn.size() == 0)
		fn = "/";

		//CFile in_file;
		//if(in_file.Open(fn.c_str(), CFile::modeRead | CFile::shareDenyWrite) == FALSE) {
	//FILE * fp;
	CFile in_file;
	if(fn[fn.size()-1] == '/') {
		string idx;
		idx = fn + "index.html";
		//fp = fopen(idx.c_str(), "rb");
		//if(fp == NULL) {
		if(in_file.Open(idx.c_str(), CFile::modeRead | CFile::shareDenyWrite) == FALSE) {
			idx = fn + "index.htm";
			//fp = fopen(idx.c_str(), "rb");
			//if(fp == NULL) {
			if(in_file.Open(idx.c_str(), CFile::modeRead | CFile::shareDenyWrite) == FALSE) {
				printf("Param-Status: %d\r\n", 404);
				printf("Param-Desc: %s\r\n", NOT_FOUND);
				printf("Content-type: text/html\r\n");
				printf("Content-Length: %d\r\n", strlen(NOT_FOUND));
				printf("\r\n");
				printf("%s", NOT_FOUND);
				
				return -1;
			}
		}
	}
	else {
		//fp = fopen(fn.c_str(), "rb");
		//if(fp == NULL) {
		if(in_file.Open(fn.c_str(), CFile::modeRead | CFile::shareDenyWrite) == FALSE) {
			printf("Param-Status: %d\r\n", 404);
			printf("Param-Desc: %s\r\n", NOT_FOUND);
			printf("Content-type: text/html\r\n");
			printf("Content-Length: %d\r\n", strlen(NOT_FOUND));
			printf("\r\n");
			printf("%s", NOT_FOUND);
			
			return -1;
		}
	}

	string ext;
	if(fn.rfind(".") != string::npos) {
		ext = fn.substr(fn.rfind(".")+1);
	}
	string_lower(ext);
	
	//printf("output_file()\n"); //debug

	//fseek(fp, 0, SEEK_END);
	//int flen = ftell(fp);

	INT64 flen = in_file.GetLength();

	if(offset >= flen) {
		printf("Param-Status: %d\r\n", 416);
		printf("Param-Desc: %s\r\n", RANGE_INVALID);
		printf("Content-type: text/html\r\n");
		printf("Content-Length: %d\r\n", strlen(RANGE_INVALID));
		printf("\r\n");
		printf("%s", RANGE_INVALID);
		
		return -1;
	}

	if(length > flen-offset || length == 0)
		length = flen-offset;

	int status = 200;
	if(length < flen || partial) {
		status = 206;
	}

	printf("Accept-Ranges: bytes\r\n");
	printf("Content-Length: %I64d\r\n", length);
	printf("Content-type: %s\r\n", get_mime(ext).c_str());
	printf("Param-Status: %d\r\n", status);
	if(status == 206) {
		printf("Param-Desc: %s\r\n", PARTIAL_CONTENT);
		printf("Param-Range: bytes %I64d-%I64d/%I64d\r\n", offset, offset+length-1, flen);
	}
	printf("\r\n");

	//fseek(fp, offset, SEEK_SET);
	in_file.Seek(offset, CFile::begin);
	
	char buf[100*1024];
	INT64 read_len = 0;
	//while(!feof(fp) && read_len < length) {
	while(in_file.GetPosition() < in_file.GetLength() && read_len < length) {
		/*
		int buf_len = sizeof(buf);
		if(buf_len > length-read_len) {
			buf_len = length-read_len;
		}
		*/
		//int ret = fread(buf, buf_len, 1, fp);
		int len = in_file.Read(buf, sizeof(buf));
		if(len > 0) {
			fwrite(buf, len, 1, stdout);
		}
		else {
			break;
		}
		read_len += len;
	}
	//fclose(fp);
	in_file.Close();

	return 0;
}
#endif



unsigned int xrand()
{
        unsigned int num = (rand() )* (rand());
        return num;

}

string get_sign(const string & val) {
	string key;
	key.assign((const char *)KEY, 16);
	string sign = md5(val + key);

	return sign;
}

int output_http_header(int status, string desc, string mime = "text/html") {
	//_SETmode( _fileno( stdout ), _O_BINARY );
	
	printf("Param-Status: %d\r\n", status);
	printf("Param-Desc: %s\r\n", desc.c_str());
	printf("Content-type: %s\r\n", mime.c_str());
	printf("Content-Length: %d\r\n", strlen(desc.c_str())); // note: desc.size() is different from strlen(desc.c_str())
	printf("\r\n");
	printf("%s", desc.c_str());

	return 0;
}


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...