String MIMETypeRegistry::getMIMETypeForExtension(const String &ext) { ASSERT(isMainThread()); if (ext.isEmpty()) return String(); initMIMETypeEntensionMap(); String result = mimetypeMap.get(ext.lower()); if (result.isEmpty()) { result = mimeTypeForExtension(ext); if (!result.isEmpty()) mimetypeMap.add(ext, result); } return result.isEmpty() ? "unknown/unknown" : result; }
String MIMETypeRegistry::getMIMETypeForExtension(const String &ext) { if (ext.isEmpty()) return String(); static HashMap<String, String> mimetypeMap; if (mimetypeMap.isEmpty()) { //fill with initial values mimetypeMap.add("txt", "text/plain"); mimetypeMap.add("pdf", "application/pdf"); mimetypeMap.add("ps", "application/postscript"); mimetypeMap.add("html", "text/html"); mimetypeMap.add("htm", "text/html"); mimetypeMap.add("xml", "text/xml"); mimetypeMap.add("xsl", "text/xsl"); mimetypeMap.add("js", "application/x-javascript"); mimetypeMap.add("xhtml", "application/xhtml+xml"); mimetypeMap.add("rss", "application/rss+xml"); mimetypeMap.add("webarchive", "application/x-webarchive"); mimetypeMap.add("svg", "image/svg+xml"); mimetypeMap.add("svgz", "image/svg+xml"); mimetypeMap.add("jpg", "image/jpeg"); mimetypeMap.add("jpeg", "image/jpeg"); mimetypeMap.add("png", "image/png"); mimetypeMap.add("tif", "image/tiff"); mimetypeMap.add("tiff", "image/tiff"); mimetypeMap.add("ico", "image/ico"); mimetypeMap.add("cur", "image/ico"); mimetypeMap.add("bmp", "image/bmp"); mimetypeMap.add("wml", "text/vnd.wap.wml"); mimetypeMap.add("wmlc", "application/vnd.wap.wmlc"); } String result = mimetypeMap.get(ext); if (result.isEmpty()) { result = mimeTypeForExtension(ext); if (!result.isEmpty()) mimetypeMap.add(ext, result); } return result; }
void MyResourceLoader::loadFile(const String& file) { LOGD("Loading file (%s) ...", file.latin1().data()); FILE* f = fopen(file.latin1().data(), "r"); ResourceHandleClient* client = m_handle->client(); if (!f) { client->didFail(m_handle, ResourceError("", -14, file, "Could not open file")); } else { ResourceResponse response; response.setTextEncodingName("utf-8"); response.setMimeType(mimeTypeForExtension(file)); client->didReceiveResponse(m_handle, response); char buf[512]; while (true) { int res = fread(buf, 1, sizeof(buf), f); if (res <= 0) break; client->didReceiveData(m_handle, buf, res, 0); } fclose(f); client->didFinishLoading(m_handle, 0); } }