用户注册



邮箱:

密码:

用户登录


邮箱:

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

发表随想


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

CGI 服务

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

[c++]代码库

int get_args(map<string, string> & args, map<string, string> & orig_args)
{
	args.clear();
	orig_args.clear();

	string method;

	char * env;
	env = getenv("REQUEST_METHOD");
	if(env == NULL)
		return -1;
	
	string buf;
	if(strcmp(env, "POST") == 0) {
		cin >> buf;
		method = "POST";
	}
	else if(strcmp(env, "GET") == 0) {
		env = getenv("QUERY_STRING");
		if(env == NULL)
			return -1;
		buf = env;
		method = "GET";
	}
	else if(strcmp(env, "HEAD") == 0) {
		env = getenv("QUERY_STRING");
		if(env == NULL)
			return -1;
		buf = env;
		method = "HEAD";
	}
	else {
		return -1;
	}

	args.insert(map<string, string>::value_type("REQUEST_METHOD", method));
	orig_args.insert(map<string, string>::value_type("REQUEST_METHOD", method));
	
	int pos_start = 0, pos_eque, pos_and;
	while(1) {
		pos_eque = buf.find("=", pos_start);
		if(pos_eque == string::npos)
			break;
		
		pos_and = buf.find("&", pos_eque);
		if(pos_and == string::npos) {
			pos_and = buf.size();
		}
		
		string arg = buf.substr(pos_start, pos_and-pos_start);
		string argname, argvalue;
		int name = 1;
		int i;
		for(i = 0; i < arg.size(); i++)
		{
			if(arg[i] == '=') {
				name = 0;
				continue;
			}
			
			char ch;
			if(arg[i] == '+') {
				ch = ' ';
			}
			else if(arg[i] == '%' && i < arg.size()-2) {
				char str[3];
				INT64 n;
				str[0] = arg[i+1];
				str[1] = arg[i+2];
				str[2] = '\0';
				sscanf(str, "%x" , &n);
				ch = (unsigned char)n;
				
				i += 2;
			}
			else {
				ch = arg[i];
			}
			
			if(name) {
				argname += ch;
			}
			else {
				argvalue += ch;
			}
		}
		
		string orig_argvalue;
		if(arg.find("=") != string::npos) {
			orig_argvalue = arg.substr(arg.find("=")+1);
		}
		else {
			orig_argvalue = "";
		}

		args.insert(map<string, string>::value_type(argname, argvalue));
		orig_args.insert(map<string, string>::value_type(argname, orig_argvalue));

		pos_start = pos_and + 1; 
	}

	return 0;
}
#ifndef _WIN32
int GetModuleFileName( char* sModuleName, char* sFileName, int nSize)
{
 int ret = -1;
    char* p = getenv("_");
 if( p != NULL && strstr( p, sModuleName ) != NULL )
 {
  ret = 0;
  strcpy( sFileName, p );
 }
 return ret;
}
#endif

int get_arg(map<string, string> & args, string arg, string & val)
{
	val = "";
	if(args.find(arg) == args.end())
		return -1;
	
	val = args.find(arg)->second;
	
	return 0;
}

string get_ext(string fn) {
	string ext;
	if(fn.rfind(".") != string::npos) {
		ext = fn.substr(fn.rfind(".")+1);
	}
	string_lower(ext);
	
	return ext;
}

string get_mime(string fn) 
{
	string ext;
	if(fn.rfind(".") != string::npos) {
		ext = fn.substr(fn.rfind(".")+1);
	}
	string_lower(ext);

	if(ext.size() == 0)
		return "text/html";

	string mime_set = "|"
		"3gp video/3gpp|"
		"avi video/x-msvideo|"
		"asf video/x-ms-asf|"
		"asn application/astound|"
		"asp application/x-asap|"
		"asx video/x-ms-asf|"
		"au audio/basic|"
		"css text/css|"
		"cab application/vnd.ms-cab-compressed|"
		"dhtml text/html|"
		"doc application/msword|"
		"gif image/gif|"
		"gps application/x-gps|"
		"gtar application/x-gtar|"
		"gz application/x-gzip|"
		"hdf application/x-hdf|"
		"hdm text/x-hdml|"
		"hdml text/x-hdml|"
		"hts text/html|"
		"ifm image/gif|"
		"j2k image/j2k|"
		"jad text/vnd.sun.j2me.app-descriptor|"
		"jam application/x-jam|"
		"jar application/java-archive|"
		"jpe image/jpeg|"
		"jpeg image/jpeg|"
		"jpg image/jpeg|"
		"jpz image/jpeg|"
		"js application/x-javascript|"
		"mov video/quicktime|"
		"movie video/x-sgi-movie|"
		"mp2 audio/mpeg|"
		"mp3 audio/mpeg|"
		"mp4 video/mp4|"
		"mpc application/vnd.mpohun.certificate|"
		"mpe video/mpeg|"
		"mpeg video/mpeg|"
		"mpg video/mpeg|"
		"mpg4 video/mp4|"
		"mpga audio/mpeg|"
		"png image/png|"
		"pbm image/x-portable-bitmap|"
		"pcx image/x-pcx|"
		"pda image/x-pda|"
		"pac audio/x-pac|"
		"pae audio/x-epac|"
		"pdf application/pdf"
		"ppt application/vnd.ms-powerpoint"
		"nbmp image/nbmp|"
		"mil image/x-cals|"
		"mio audio/x-mio|"
		"mng video/x-mng|"
		"mod audio/x-mod|"
		"pict image/x-pict|"
		"nsnd audio/nsnd|"
		"pac audio/x-pac|"
		"pae audio/x-epac|"
		"pbm image/x-portable-bitmap|"
		"pcx image/x-pcx|"
		"pda image/x-pda|"
		"sgm text/x-sgml|"
		"sgml text/x-sgml|"
		"si6 image/si6|"
		"si7 image/vnd.stiwap.sis|"
		"si9 image/vnd.lgtwap.sis|"
		"pvx video/x-pv-pvx|"
		"qcp audio/vnd.qcelp|"
		"qt video/quicktime|"
		"qti image/x-quicktime|"
		"qtif image/x-quicktime|"
		"r3t text/vnd.rn-realtext3d|"
		"ra audio/x-pn-realaudio|"
		"ram audio/x-pn-realaudio|"
		"ras image/x-cmu-raster|"
		"rf image/vnd.rn-realflash|"
		"rgb image/x-rgb|"
		"rm application/vnd.rn-realmedia|"
		"rmf audio/x-rmf|"
		"rmm audio/x-pn-realaudio|"
		"rmvb application/vnd.rn-realmedia|"
		"rv video/vnd.rn-realvideo|"
		"s3m audio/x-mod|"
		"s3z audio/x-mod|"
		"svf image/vnd|"
		"svg image/svg-xml|"
		"svh image/svh|"
		"smz audio/x-smd|"
		"snd audio/basic|"
		"smd audio/x-smd|"
		"tif image/tiff|"
		"tiff image/tiff|"
		"z application/x-compress|"
		"zac application/x-zaurus-zac|"
		"zip application/zip|"
		"wav audio/x-wav|"
		"wax audio/x-ms-wax|"
		"wm video/x-ms-wm|"
		"wma audio/x-ms-wma|"
		"wmd application/x-ms-wmd|"
		"wmf application/x-msmetafile|"
		"wml text/vnd.wap.wml|"
		"wmlc application/vnd.wap.wmlc|"
		"wmls text/vnd.wap.wmlscript|"
		"wmlsc application/vnd.wap.wmlscriptc|"
		"wmlscript text/vnd.wap.wmlscript|"
		"wmv audio/x-ms-wmv|"
		"wmx video/x-ms-wmx|"
		"wmz application/x-ms-wmz|"
		"xml text/xml|"
		"xht application/xhtml+xml|"
		"xhtm application/xhtml+xml|"
		"xhtml application/xhtml+xml|"
		"wpng image/x-up-wpng|"
		"rar application/x-rar-compressed|"
		"htm text/html|"
		"html text/html|"
		"hts text/html|"
		"txt text/plain|"
		"log text/plain|"
		"ini text/plain|"
		"conf text/plain|"
		"swf application/x-shockwave-flash|"
		"ico image/x-icon|"
		"xls application/vnd.ms-excel|"
		"xsf text/xml|"
		"xsl text/xml|"
		"xslt text/xml|"
		"flv flv-application/octet-stream|";

	string::size_type p1 = mime_set.find("|" + ext+" ");
	if(p1 == string::npos)
		return "application/octet-stream";
	p1 += ext.size() + 2;

	string::size_type p2 = mime_set.find("|", p1);
	if(p2 == string::npos)
		return "application/octet-stream";

	string mime = mime_set.substr(p1, p2-p1);

	return mime;
}


网友评论    (发表评论)


发表评论:

评论须知:

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


扫码下载

加载中,请稍后...

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

加载中,请稍后...