欢迎来到 嗅灵易学

零基础也能上手的脚本技术课,一对一答疑带你入门

[原创]使用hash匹配mime。

[原创]使用hash匹配mime。

我错了,向伟大的技术道歉。
先看一段网上的mime匹配代码,接下来我们对这段代码稍微的优化一下。

const char *get_mime_type(const char *name) {

	char *ext = strrchr((char*)name, '.');

	if (!ext)

		return NULL;

	if (strcmp(ext, ".html") == 0 || strcmp(ext, ".htm") == 0)

		return "text/html";

	if (strcmp(ext, ".jpg") == 0 || strcmp(ext, ".jpeg") == 0)

		return "image/jpeg";

	if (strcmp(ext, ".gif") == 0)

		return "image/gif";

	if (strcmp(ext, ".png") == 0)

		return "image/png";

	if (strcmp(ext, ".css") == 0)

		return "text/css";

	if (strcmp(ext, ".au") == 0)

		return "audio/basic";

	if (strcmp(ext, ".wav") == 0)

		return "audio/wav";

	if (strcmp(ext, ".avi") == 0)

		return "video/x-msvideo";

	if (strcmp(ext, ".mpeg") == 0 || strcmp(ext, ".mpg") == 0)

		return "video/mpeg";

	if (strcmp(ext, ".mp3") == 0)

		return "audio/mpeg";

	return NULL;

}

下面这段代码是根据hash来匹配mime原型,请看大屏幕。
namespace vws {

const char* vws_http_mime::get_ext_name(const char* filename) {

	const char* ext = strrchr(filename, '.');

	if (!ext) {

		return (char*) 0;

	} else {

		return ext;

	}

}

const char* vws_http_mime::to_lower_string(const char* string) {

	static char full[PATH_MAX];

	int i = 0;

	for (i = 0; *string; i++) {

		full[i] = tolower(*string++);

	}

	full[i] = 0;

	return full;

}

const char* vws_http_mime::find_ext_name_mime(const char* filename) {

	const char* ext = vws_http_mime::get_ext_name(filename);

	const char* ext_low = vws_http_mime::to_lower_string(ext);

	unsigned long value = vws_crc32::calculate((unsigned char*) ext_low,

			strlen(ext_low));

	if (sizeof(value) == 8) // 64 bit

	{

		value &= 0x00000000ffffffff;

	}

	switch (value) {

	case 0x81ee4181:

	case 0x5f3d3c9e:

	case 0x0b2b7803:

		return ("text/html");

	case 0xe62678db:

		return ("text/css");

	case 0xaf1c0660:

		return ("text/xml");

	case 0x3cdfce37:

		return ("image/gif");

	case 0x73e57256:

	case 0xd800c4ea:

		return ("image/jpeg");

	case 0x9b8c1153:

		return ("application/x-javascript");

	case 0xb9d75c1f:

		return ("application/atom+xml");

	case 0xfbc2b19c:

		return ("application/rss+xml");

	case 0xb5f167fb:

		return ("text/mathml");

	case 0x82df8146:

		return ("text/plain");

	case 0x12d0b640:

		return ("text/vnd.sun.j2me.app-descriptor");

	case 0xa440415d:

		return ("text/vnd.wap.wml");

	case 0xb8851199:

		return ("text/x-component");

	case 0x1df0dd93:

		return ("image/png");

	case 0x22bfd31e:

	case 0x8cfea950:

		return ("image/tiff");

	case 0x853e85c5:

		return ("image/vnd.wap.wbmp");

	case 0xb572b313:

		return ("image/x-icon");

	case 0x0c41fb35:

		return ("image/x-jng");

	case 0xaaac7c89:

		return ("image/x-ms-bmp");

	case 0x9dadfb93:

	case 0x0b4918aa:

		return ("image/svg+xml");

	case 0x07e98f8f:

		return ("image/webp");

	case 0xe6040311:

	case 0xf2fa3332:

	case 0xed58442c:

		return ("application/java-archive");

	case 0x4f972c30:

		return ("application/mac-binhex40");

	case 0x18a92367:

		return ("application/msword");

	case 0x9018058f:

		return ("application/pdf");

	case 0x2ba1eb88:

	case 0xc98657aa:

	case 0x851a31e2:

		return ("application/postscript");

	case 0xd95ec3b0:

		return ("application/rtf");

	case 0x3b0f3ad4:

		return ("application/vnd.ms-excel");

	case 0x4d0fa392:

		return ("application/vnd.ms-powerpoint");

	case 0x13c7b267:

		return ("application/vnd.wap.wmlc");

	case 0xb17c1b49:

		return ("application/vnd.google-earth.kml+xml");

	case 0x45a8ae18:

		return ("application/vnd.google-earth.kmz");

	case 0xed458aee:

		return ("application/x-7z-compressed");

	case 0xb8e536c5:

		return ("application/x-cocoa");

	case 0x82964b41:

		return ("application/x-java-archive-diff");

	case 0x374cc2a2:

		return ("application/x-java-jnlp-file");

	case 0xce9e7ac3:

		return ("application/x-makeself");

	case 0xa6a9e67d:

	case 0xd1aed6eb:

		return ("application/x-perl");

	case 0xfcea44d7:

	case 0x9775c196:

		return ("application/x-pilot");

	case 0xf431f1d9:

		return ("application/x-rar-compressed");

	case 0x2ae0df3c:

		return ("application/x-redhat-package-manager");

	case 0x15211f34:

		return ("application/x-sea");

	case 0xf3b1fa44:

		return ("application/x-shockwave-flash");

	case 0xd449b4d3:

		return ("application/x-stuffit");

	case 0x3885d28a:

	case 0x5ca1b6da:

		return ("application/x-tcl");

	case 0x88f6eb1f:

	case 0x1ed1ed46:

	case 0x6159dc39:

		return ("application/x-x509-ca-cert");

	case 0x201a9ef3:

		return ("application/x-xpinstall");

	case 0x7cfb4912:

		return ("application/xhtml+xml");

	case 0xdcf54b45:

		return ("application/zip");

	case 0x34cf84ee:

	case 0xf58b68f3:

	case 0xa33b6d35:

		return ("application/octet-stream");

	case 0x9541fb7b:

		return ("application/octet-stream");

	case 0x2df285fc:

		return ("application/octet-stream");

	case 0x9ab8cc97:

		return ("application/octet-stream");

	case 0xffb0a142:

	case 0x252a16af:

		return ("application/octet-stream");

	case 0x11daacab:

	case 0x75b1046b:

	case 0x16b768b2:

		return ("application/octet-stream");

	case 0xdf462acd:

	case 0x0366ce56:

	case 0xe7c66926:

		return ("audio/midi");

	case 0xb1494782:

		return ("audio/mpeg");

	case 0xdb488297:

		return ("audio/ogg");

	case 0xa039fd5b:

		return ("audio/x-m4a");

	case 0xea2ef842:

		return ("audio/x-realaudio");

	case 0x68efe458:

	case 0x3d3dd084:

		return ("video/3gpp");

	case 0x2f2dd221:

		return ("video/mp4");

	case 0xee324aef:

	case 0xdd4fd26f:

		return ("video/mpeg");

	case 0x7aa5fc03:

		return ("video/quicktime");

	case 0x64efe356:

		return ("video/webm");

	case 0x5ddd4021:

		return ("video/x-flv");

	case 0x23ea789c:

		return ("video/x-m4v");

	case 0x090eedb0:

		return ("video/x-mng");

	case 0x7270753d:

	case 0x887f485e:

		return ("video/x-ms-asf");

	case 0x5922b827:

		return ("video/x-ms-wmv");

	case 0x65b7a18a:

		return ("video/x-msvideo");

	default:

		break;

	}

	return ("text/unknown");

}

}

讲解完毕,谢谢观看。

注意:上传附件及图片大小不得大于30M。

⚠️ 版权声明:
本博客所有内容(含教程、源码、工具)仅供个人技术学习与研究交流使用,严禁商用、倒卖、二次分发及非法用途。
未经作者书面授权,任何组织或个人不得转载、复制或用于其他平台,违者将追究相关责任。

0 0 0 举报
复制成功