void test_mime_types(void) { char test1[] = "html"; char test2[] = "js"; char test3[] = "css"; char test4[] = "json"; CU_ASSERT_STRING_EQUAL(mime_type(test1), "text/html"); CU_ASSERT_STRING_EQUAL(mime_type(test2), "application/javascript"); CU_ASSERT_STRING_EQUAL(mime_type(test3), "text/css"); CU_ASSERT_STRING_EQUAL(mime_type(test4), "application/json"); }
std::string ShmdataUtils::get_category(const std::string &caps){ std::string category; std::string mime_type(caps.begin(), std::find(caps.begin(), caps.end(), (','))); if (std::string::npos != mime_type.find("video/x-raw")) { category = "video"; } else if (std::string::npos != mime_type.find("video/x-")) { category = "compressed video"; } else if (std::string::npos != mime_type.find("audio/midi")) { category = "midi"; } else if (std::string::npos != mime_type.find("audio/x-raw")) { category = "audio"; } else if (std::string::npos != mime_type.find("audio/x-")) { category = "compressed audio"; } else if (std::string::npos != mime_type.find("application/x-libloserialized-osc")) { category = "osc"; } else if (std::string::npos != mime_type.find("application/x-")) { auto it = std::find(mime_type.begin(), mime_type.end(), '-'); it++; if (mime_type.end() == it) category = "unknown"; else category = std::string(it, mime_type.end()); } else { category = mime_type; } return category; }
/* main */ int main(void) { int ret = 0; Mime * mime; struct { char const * filename; char const * type; } tests[] = { { "Makefile", "text/x-makefile" }, { "../include/Desktop/mime.h", "text/x-chdr" }, { "mime.c", "text/x-csrc" } }; size_t i; char const * p; if((mime = mime_new(NULL)) == NULL) return 2; for(i = 0; i < sizeof(tests) / sizeof(*tests); i++) if((p = mime_type(mime, tests[i].filename)) == NULL) /* XXX ignore this as shared-mime-info may be missing */ printf("%s: %s (expected: %s)\n", tests[i].filename, "Unknown type", tests[i].type); else if(strcmp(tests[i].type, p) != 0) { printf("%s: %s (expected: %s)\n", tests[i].filename, p, tests[i].type); ret = 3; } else printf("%s: %s\n", tests[i].filename, p); mime_delete(mime); return ret; }
static void _cvs_on_add(gpointer data) { CVS * cvs = data; gchar * dirname; gchar * basename; char * argv[] = { "cvs", "add", "--", NULL, NULL, NULL }; Mime * mime; char const * type; if(cvs->filename == NULL) return; dirname = g_path_get_dirname(cvs->filename); basename = g_path_get_basename(cvs->filename); argv[3] = basename; mime = cvs->helper->get_mime(cvs->helper->browser); type = mime_type(mime, cvs->filename); if(_add_is_binary(type)) { argv[4] = argv[3]; argv[3] = argv[2]; argv[2] = "-kb"; } _cvs_add_task(cvs, "cvs add", dirname, argv); g_free(basename); g_free(dirname); }
/* compose_attach */ int compose_attach(Compose * compose, char const * filename) { const int iconsize = 48; GtkIconTheme * theme; char const * type; GdkPixbuf * pixbuf; GtkTreeIter iter; char * p; FILE * fp; if(filename == NULL) return compose_attach_dialog(compose); if((fp = fopen(filename, "rb")) == NULL) return -compose_error(compose, strerror(errno), 1); if((p = strdup(filename)) == NULL) { fclose(fp); return -compose_error(compose, strerror(errno), 1); } compose_set_modified(compose, TRUE); theme = gtk_icon_theme_get_default(); pixbuf = NULL; if((type = mime_type(compose->mime, filename)) != NULL) mime_icons(compose->mime, type, iconsize, &pixbuf, -1); if(pixbuf == NULL) pixbuf = gtk_icon_theme_load_icon(theme, GTK_STOCK_FILE, iconsize, 0, NULL); gtk_list_store_append(compose->a_store, &iter); gtk_list_store_set(compose->a_store, &iter, CAC_FILENAME, filename, CAC_BASENAME, basename(p), CAC_ICON, pixbuf, CAC_FILE_POINTER, fp, -1); g_object_unref(pixbuf); free(p); return 0; }
// Return a file response to an HTTP GET request // http::response<beast::http::file_body> get(boost::filesystem::path const& full_path, beast::error_code& ec) const { http::response<http::file_body> res; res.set(http::field::server, BEAST_VERSION_STRING); res.set(http::field::content_type, mime_type(full_path)); res.set(http::field::connection, "close"); res.body.open(full_path.string<std::string>().c_str(), beast::file_mode::scan, ec); if(ec) return res; res.set(http::field::content_length, res.body.size()); return res; }
bool FileBundle::add(const std::string & path) { // TODO make it more reliable with more tests. fwk::MimeType mime_type(path); bool added = true; if(mime_type.isImage()) { if(mime_type.isDigicamRaw()) { if(!m_main.empty() && m_jpeg.empty()) { m_jpeg = m_main; m_type = LibFile::FileType::RAW_JPEG; } else { m_type = LibFile::FileType::RAW; } m_main = path; } else { if(!m_main.empty()) { m_jpeg = path; m_type = LibFile::FileType::RAW_JPEG; } else { m_main = path; m_type = LibFile::FileType::IMAGE; } } } else if(mime_type.isXmp()) { m_xmp_sidecar = path; } else if(mime_type.isMovie()) { m_main = path; m_type = LibFile::FileType::VIDEO; } else { DBG_OUT("Unkown file %s of type %s\n", path.c_str(), mime_type.string().c_str()); added = false; } return added; }
/* browser_vfs_mime_type */ char const * browser_vfs_mime_type(Mime * mime, char const * filename, mode_t mode) { char const * ret = NULL; struct stat st; struct stat pst; char * p = NULL; if(mode == 0 && filename != NULL && browser_vfs_lstat(filename, &st) == 0) mode = st.st_mode; if(S_ISDIR(mode)) { /* look for mountpoints */ if(filename != NULL && (p = strdup(filename)) != NULL && browser_vfs_lstat(filename, &st) == 0 && browser_vfs_lstat(dirname(p), &pst) == 0 && (st.st_dev != pst.st_dev || st.st_ino == pst.st_ino)) ret = "inode/mountpoint"; else ret = "inode/directory"; free(p); return ret; } else if(S_ISBLK(mode)) return "inode/blockdevice"; else if(S_ISCHR(mode)) return "inode/chardevice"; else if(S_ISFIFO(mode)) return "inode/fifo"; else if(S_ISLNK(mode)) return "inode/symlink"; #ifdef S_ISSOCK else if(S_ISSOCK(mode)) return "inode/socket"; #endif if(mime != NULL && filename != NULL) ret = mime_type(mime, filename); if(ret == NULL && (mode & S_IXUSR) != 0) ret = "application/x-executable"; return ret; }
static int send_content_type(struct http_request *req) { char buf[128]; int r; const char *type; type = mime_type(req->url); if (!type) return -1; r = snprintf(buf, 128, "Content-Type: %s\r\n", type); if (r > 127) panic("buffer too small!"); if (write(req->sock, buf, r) != r) return -1; return 0; }
/* compose_attach_dialog */ void compose_attach_dialog(Compose * compose) { GtkWidget * dialog; GSList * filenames = NULL; GSList * p; char const * type; GdkPixbuf * pixbuf; GtkIconTheme * theme; GtkTreeIter iter; dialog = gtk_file_chooser_dialog_new(_("Attach file..."), GTK_WINDOW(compose->window), GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL); gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE); if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK) filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER( dialog)); gtk_widget_destroy(dialog); if(filenames == NULL) return; compose_set_modified(compose, TRUE); theme = gtk_icon_theme_get_default(); for(p = filenames; p != NULL; p = p->next) { pixbuf = NULL; if((type = mime_type(compose->mime, p->data)) != NULL) mime_icons(compose->mime, type, 48, &pixbuf, -1); if(pixbuf == NULL) pixbuf = gtk_icon_theme_load_icon(theme, GTK_STOCK_FILE, 48, 0, NULL); gtk_list_store_append(compose->a_store, &iter); gtk_list_store_set(compose->a_store, &iter, CAC_FILENAME, p->data, CAC_BASENAME, basename(p->data), CAC_ICON, pixbuf, -1); } g_slist_foreach(filenames, (GFunc)g_free, NULL); g_slist_free(filenames); gtk_widget_show(compose->a_window); }
static void _preview_refresh(Preview * preview, GList * selection) { char * path = (selection != NULL) ? selection->data : NULL; Mime * mime = preview->helper->get_mime(preview->helper->browser); struct stat st; char const image[6] = "image/"; char const text[5] = "text/"; char const * types[] = { "application/x-perl", "application/x-shellscript", "application/xml", "application/xslt+xml" }; char const * type; size_t i; _refresh_reset(preview); if(path == NULL) return; if(_refresh_name(preview, path) != 0) return; /* ignore directories */ if(lstat(path, &st) == 0 && S_ISDIR(st.st_mode)) return; /* XXX avoid stat() and use vfs_mime_type() instead */ if((type = mime_type(mime, path)) == NULL) return; _refresh_mime(preview, mime, type); if(strncmp(type, image, sizeof(image)) == 0) preview->source = g_idle_add(_preview_on_idle_image, preview); else if(strncmp(type, text, sizeof(text)) == 0) preview->source = g_idle_add(_preview_on_idle_text, preview); else for(i = 0; i < sizeof(types) / sizeof(*types); i++) if(strcmp(types[i], type) == 0) { preview->source = g_idle_add( _preview_on_idle_text, preview); break; } }
/* desktopicon_new */ DesktopIcon * desktopicon_new(Desktop * desktop, char const * name, char const * path) { DesktopIcon * desktopicon; struct stat st; Mime * mime; char const * mimetype = NULL; gboolean isdir = FALSE; gboolean isexec = FALSE; GdkPixbuf * image = NULL; GError * error = NULL; char * p; GtkTargetEntry targets[] = { { "deforaos_browser_dnd", 0, 0 } }; size_t targets_cnt = sizeof(targets) / sizeof(*targets); #ifdef DEBUG fprintf(stderr, "DEBUG: %s(%p, \"%s\", \"%s\")\n", __func__, (void *)desktop, name, path); #endif if(path != NULL && stat(path, &st) == 0) { mime = desktop_get_mime(desktop); if(S_ISDIR(st.st_mode)) { isdir = TRUE; image = desktop_get_folder(desktop); } else if(st.st_mode & S_IXUSR) { /* FIXME use access() for this */ isexec = TRUE; mime_icons(desktop_get_mime(desktop), "application/x-executable", DESKTOPICON_ICON_SIZE, &image, -1); } else if((mimetype = mime_type(mime, path)) != NULL) mime_icons(mime, mimetype, DESKTOPICON_ICON_SIZE, &image, -1); } if(name == NULL) { if((p = g_filename_to_utf8(path, -1, NULL, NULL, &error)) == NULL) { fprintf(stderr, "%s%s\n", "desktop: ", error->message); name = path; } else name = p; if((name = strrchr(name, '/')) != NULL) name++; } if((desktopicon = _desktopicon_new_do(desktop, image, name)) == NULL) return NULL; gtk_drag_source_set(desktopicon->event, GDK_BUTTON1_MASK, targets, targets_cnt, GDK_ACTION_COPY | GDK_ACTION_MOVE); gtk_drag_dest_set(desktopicon->event, GTK_DEST_DEFAULT_ALL, targets, targets_cnt, GDK_ACTION_COPY | GDK_ACTION_MOVE); g_signal_connect(G_OBJECT(desktopicon->event), "drag-data-get", G_CALLBACK(_on_icon_drag_data_get), desktopicon); g_signal_connect(G_OBJECT(desktopicon->event), "drag-data-received", G_CALLBACK(_on_icon_drag_data_received), desktopicon); desktopicon->isdir = isdir; desktopicon_set_executable(desktopicon, isexec); desktopicon->mimetype = mimetype; if(path != NULL && (desktopicon->path = strdup(path)) == NULL) { desktopicon_delete(desktopicon); return NULL; } return desktopicon; }
/* Called by ares library for each result. */ static void result_callback (ASSearch *search, ASResult *r, as_bool duplicate) { Share *share; char *url, *filename, *user; if (!r) { AS_DBG_1 ("Search complete. Id: %d.", search->id); /* Tell giFT we're finished. */ PROTO->search_complete (PROTO, search->udata); /* The above does _not_ make giFT call asp_cb_search_cancel() so * remove the search now. */ if (!as_searchman_remove (AS->searchman, search)) { AS_ERR ("Failed to remove complete search"); assert (0); } return; } /* Create a share object for giFT. */ if (!(share = share_new (NULL))) return; share->p = PROTO; share->size = r->filesize; filename = r->filename; /* Try to find file name and size in hash table if none was returned. */ if (search->type == SEARCH_LOCATE && (!filename || share->size == 0)) { size_t size; char *name; /* Lookup this hash in the evil hash map to find the * size and filename for giFT. */ if (asp_hashmap_lookup (r->hash, &name, &size)) { if (share->size == 0) share->size = size; if (!filename && name && *name) filename = name; } } /* If we still don't have a file name fake one to prevent things from * blowing up elsewhere. */ if (!filename) filename = "<Unknown>"; share_set_path (share, filename); share_set_mime (share, mime_type (filename)); share_set_hash (share, "SHA1", r->hash->data, AS_HASH_SIZE, FALSE); /* Add meta data. */ if (r->meta) as_meta_foreach_tag (r->meta, (ASMetaForeachFunc)meta_to_gift, share); /* Create the url giFT will pass back to us on download. */ if (!(url = as_source_serialize (r->source))) { AS_ERR_1 ("Couldn't serialize source '%s'", as_source_str (r->source)); share_free (share); return; } /* Assemble username@ip string. */ if (STRING_NULL (r->source->username)) user = stringf_dup ("%s@%s", r->source->username, net_ip_str (r->source->host)); else user = gift_strdup (net_ip_str (r->source->host)); /* Send the result to giFT. */ PROTO->search_result (PROTO, search->udata, user, NULL, url, 1, share); free (user); free (url); share_free (share); }
/** * http_handler - http handler */ void http_handler(struct evhttp_request *req, void *arg) { time_t timep; struct tm *m; struct stat info; struct evbuffer *buf; buf = evbuffer_new(); // 分析URL参数 char *decode_uri = strdup((char*) evhttp_request_uri(req)); decode_uri = evhttp_decode_uri(decode_uri); if (strcmp(decode_uri, "/") == 0) decode_uri = "/index.html"; sprintf(http_req_line.request_uri, "%s", decode_uri); // 返回给浏览器的头信息 evhttp_add_header(req->output_headers, "Server", "logadmin"); evhttp_add_header(req->output_headers, "Connection", "close"); // 取得请求时间 time(&timep); m = localtime(&timep); sprintf(http_req_line.request_time, "%4d-%02d-%02d %02d:%02d:%02d", (1900+m->tm_year), (1+m->tm_mon), m->tm_mday, m->tm_hour, m->tm_min, m->tm_sec); // 获取ACTION的值 struct evkeyvalq params; evhttp_parse_query(decode_uri, ¶ms); char *action = (char*)evhttp_find_header(¶ms, "action"); // 处理不同的ACTION if (action) { char *tmp = ""; evhttp_add_header(req->output_headers, "Content-Type", "text/html; charset=UTF-8"); if (strcmp(action, "loginfo") == 0) { char *loghost = (char*)evhttp_find_header(¶ms, "loghost"); char *logport = (char*)evhttp_find_header(¶ms, "logport"); char *logname = (char*)evhttp_find_header(¶ms, "logname"); char *pagenum = (char*)evhttp_find_header(¶ms, "pagenum"); int pnum = pagenum ? atoi(pagenum) : 1; int port = logport ? atoi(logport) : 8706; if (logname) { int psize = 20; char *logreg = (char*)evhttp_find_header(¶ms, "logreg"); if(!logreg || !strlen(logreg)){ logreg = ".*"; } char *cmd = joinstr("grep '%s' %s -c && grep '%s' %s | tail -n +%d | head -n %d", logreg, logname, logreg, logname, (pnum - 1) * psize, psize); if (!loghost || !strlen(loghost)) { tmp = exec_cmd(cmd); } else { sock_cmd(loghost, port, cmd, (char*)&tmp, MEM_SIZE); } } evbuffer_add_printf(buf, "%s", tmp); evhttp_send_reply(req, HTTP_OK, "OK", buf); } else if(strcmp(action, "loglist") == 0) { char *loghost = (char*)evhttp_find_header(¶ms, "loghost"); char *logport = (char*)evhttp_find_header(¶ms, "logport"); char *dirname = (char*)evhttp_find_header(¶ms, "dirname"); int port = logport ? atoi(logport) : 8706; if (dirname) { char *cmd = joinstr("ls -lF %s | awk '{print $9}'", dirname); if (!loghost || !strlen(loghost)) { tmp = exec_cmd(cmd); } else { sock_cmd(loghost, port, cmd, (char*)&tmp, MEM_SIZE); } } evbuffer_add_printf(buf, "%s", tmp); evhttp_send_reply(req, HTTP_OK, "OK", buf); } else if(strcmp(action, "logconf") == 0) { #ifndef __WIN32 GKeyFile* config = g_key_file_new(); GKeyFileFlags flags = G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS; if (g_key_file_load_from_file(config, "./conf/logadmin.conf", flags, NULL)) { int i; gsize length; gchar** groups = g_key_file_get_groups(config, &length); for (i = 0; i < (int)length; i++) { gchar* host = g_key_file_get_string(config, groups[i], "host", NULL); gchar* path = g_key_file_get_string(config, groups[i], "path", NULL); gchar* port = g_key_file_get_string(config, groups[i], "port", NULL); if(!port || !strlen(port)) port = "8706"; if (host && path) tmp = joinstr("%s%s:%s;%s\n", tmp, host, port, path); } } g_key_file_free(config); #endif evbuffer_add_printf(buf, "%s", tmp); evhttp_send_reply(req, HTTP_OK, "OK", buf); } else { evbuffer_add_printf(buf, "<html><head><title>870 Error Action</title></head><body><h1 align=\"center\">870 Error Action</h1></body></html>"); evhttp_send_reply(req, 870, "Error Action", buf); } evbuffer_free(buf); return; } // 获取请求的服务器文件路径 char *dir_root = getcwd(NULL, 0); char *real_path = joinstr("%s/%s%s", dir_root, DOCUMENT_ROOT, http_req_line.request_uri); if (stat(real_path,&info) == -1) { evhttp_add_header(req->output_headers, "Content-Type", "text/html; charset=UTF-8"); if (errno == ENOENT) { evbuffer_add_printf(buf, "<html><head><title>404 Not Found</title></head><body><h1 align=\"center\">404 Not Found</h1></body></html>"); evhttp_send_reply(req, 404, "Not Found", buf); } else if(access(real_path,R_OK) < 0) { evbuffer_add_printf(buf, "<html><head><title>403 Not Found</title></head><body><h1 align=\"center\">403 Forbidden</h1></body></html>"); evhttp_send_reply(req, 403, "Forbidden", buf); } else { evbuffer_add_printf(buf, "<html><head><title>500 Server Error</title></head><body><h1 align=\"center\">500 Server Error</h1></body></html>"); evhttp_send_reply(req, 500, "Server Error", buf); } } else if(S_ISREG(info.st_mode)) { // 设置HEADER头并输出内容到浏览器 evhttp_add_header(req->output_headers, "Content-Type", joinstr("%s; charset=UTF-8", mime_type(real_path))); evbuffer_add_printf(buf, "%s", read_log(real_path)); evhttp_send_reply(req, HTTP_OK, "OK", buf); } // 内存释放 evbuffer_free(buf); free(dir_root); }