📄 types.c
字号:
ext.ext="grb",ext.ct="application/gerber",update_ext(&ext); ext.ext="jpg,jpeg,jpe",ext.ct="image/jpeg",update_ext(&ext); ext.ext="mid,midi",ext.ct="audio/midi",update_ext(&ext); ext.ext="mpeg,mpg,mpe",ext.ct="video/mpeg",update_ext(&ext); ext.ext="pbm",ext.ct="image/x-portable-bitmap",update_ext(&ext); ext.ext="pcb",ext.ct="application/pcb",update_ext(&ext); ext.ext="pdf",ext.ct="application/pdf",update_ext(&ext); ext.ext="pgm",ext.ct="image/x-portable-graymap",update_ext(&ext); ext.ext="pgp",ext.ct="application/pgp-signature",update_ext(&ext); ext.ext="png",ext.ct="image/png",update_ext(&ext); ext.ext="pnm",ext.ct="image/x-portable-anymap",update_ext(&ext); ext.ext="ppm",ext.ct="image/x-portable-pixmap",update_ext(&ext); ext.ext="ppt",ext.ct="application/powerpoint",update_ext(&ext); ext.ext="ps,eps,ai",ext.ct="application/postscript",update_ext(&ext); ext.ext="qt,mov",ext.ct="video/quicktime",update_ext(&ext); ext.ext="ra,rm,ram",ext.ct="audio/x-pn-realaudio",update_ext(&ext); ext.ext="rtf",ext.ct="application/rtf",update_ext(&ext); ext.ext="sch",ext.ct="application/gschem",update_ext(&ext); ext.ext="svg",ext.ct="image/svg",update_ext(&ext); ext.ext="swf",ext.ct="application/x-shockwave-flash",update_ext(&ext); ext.ext="sxw",ext.ct="application/x-openoffice",update_ext(&ext); ext.ext="tga",ext.ct="image/targa",update_ext(&ext); ext.ext="tiff,tif",ext.ct="image/tiff",update_ext(&ext); ext.ext="wav",ext.ct="audio/x-wav",update_ext(&ext); ext.ext="xbm",ext.ct="image/x-xbitmap",update_ext(&ext); ext.ext="xls",ext.ct="application/excel",update_ext(&ext); ext.ext="xpm",ext.ct="image/x-xpixmap",update_ext(&ext);}/* --------------------------- PROG -----------------------------*/struct list_head mailto_prog = { &mailto_prog, &mailto_prog };struct list_head telnet_prog = { &telnet_prog, &telnet_prog };struct list_head tn3270_prog = { &tn3270_prog, &tn3270_prog };struct list_head mms_prog = { &mms_prog, &mms_prog };int is_in_list(unsigned char *list, unsigned char *str, int l){ unsigned char *l2, *l3; if (!l) return 0; rep: while (*list && *list <= ' ') list++; if (!*list) return 0; for (l2 = list; *l2 && *l2 != ','; l2++) ; for (l3 = l2 - 1; l3 >= list && *l3 <= ' '; l3--) ; l3++; if (l3 - list == l && !casecmp(str, list, l)) return 1; list = l2; if (*list == ',') list++; goto rep;}unsigned char *get_content_type_by_extension(unsigned char *url){ struct extension *e; struct assoc *a; unsigned char *ct, *ext, *exxt; int extl, el; ext = NULL, extl = 0; if (!(ct = get_url_data(url))) ct = url; for (; *ct && !end_of_dir(url, *ct); ct++) if (*ct == '.') { if (ext) { if (!casecmp(ct, ".z", 3) && (!ct[2] || end_of_dir(url, ct[2]))) break; if (!casecmp(ct, ".gz", 3) && (!ct[3] || end_of_dir(url, ct[3]))) break; if (!casecmp(ct, ".bz2", 4) && (!ct[4] || end_of_dir(url, ct[4]))) break; } ext = ct + 1; } else if (dir_sep(*ct)) { ext = NULL; } if (ext) while (ext[extl] && ext[extl] != '.' && !dir_sep(ext[extl]) && !end_of_dir(url, ext[extl])) extl++; if ((extl == 3 && !casecmp(ext, "htm", 3)) || (extl == 4 && !casecmp(ext, "html", 4))) return stracpy("text/html"); foreach(e, extensions) { unsigned char *fname = NULL; if (!(ct = get_url_data(url))) ct = url; for (; *ct && !end_of_dir(url, *ct); ct++) if (dir_sep(*ct)) fname = ct + 1; if (!fname) { if (is_in_list(e->ext, ext, extl)) return stracpy(e->ct); } else { int fnlen = 0; int x; while (fname[fnlen] && !end_of_dir(url, fname[fnlen])) fnlen++; for (x = 0; x < fnlen; x++) if (fname[x] == '.') if (is_in_list(e->ext, fname + x + 1, fnlen - x - 1)) return stracpy(e->ct); } } if ((extl == 3 && !casecmp(ext, "jpg", 3)) || (extl == 4 && !casecmp(ext, "pjpg", 4))|| (extl == 4 && !casecmp(ext, "jpeg", 4))|| (extl == 5 && !casecmp(ext, "pjpeg", 5))) return stracpy("image/jpeg"); if ((extl == 3 && !casecmp(ext, "png", 3))) return stracpy("image/png"); if ((extl == 3 && !casecmp(ext, "gif", 3))) return stracpy("image/gif"); if ((extl == 3 && !casecmp(ext, "xbm", 3))) return stracpy("image/x-xbitmap"); if ((extl == 3 && !casecmp(ext, "tif", 3)) || (extl == 4 && !casecmp(ext, "tiff", 4))) return stracpy("image/tiff"); exxt = init_str(); el = 0; add_to_str(&exxt, &el, "application/x-"); add_bytes_to_str(&exxt, &el, ext, extl); foreach(a, assoc) if (is_in_list(a->ct, exxt, el)) return exxt; mem_free(exxt); return NULL;}unsigned char *get_extension_by_content_type(unsigned char *ct){ struct extension *e; unsigned char *x, *y; if (is_html_type(ct)) return stracpy("html"); foreach(e, extensions) { if (!strcasecmp(e->ct, ct)) { x = stracpy(e->ext); if ((y = strchr(x, ','))) *y = 0; return x; } } if (!strcasecmp(ct, "image/jpeg") || !strcasecmp(ct, "image/jpg") || !strcasecmp(ct, "image/jpe") || !strcasecmp(ct, "image/pjpe") || !strcasecmp(ct, "image/pjpeg") || !strcasecmp(ct, "image/pjpg")) return stracpy("jpg"); if (!strcasecmp(ct, "image/png") || !strcasecmp(ct, "image/x-png")) return stracpy("png"); if (!strcasecmp(ct, "image/gif")) return stracpy("gif"); if (!strcasecmp(ct, "image/x-bitmap")) return stracpy("xbm"); if (!strcasecmp(ct, "image/tiff") || !strcasecmp(ct, "image/tif")) return stracpy("tiff"); if (!cmpbeg(ct, "application/x-")) { x = ct + strlen("application/x-"); if (strcasecmp(x, "z") && strcasecmp(x, "gz") && strcasecmp(x, "gzip") && strcasecmp(x, "bz2") && strcasecmp(x, "bzip2") && !strchr(x, '-') && strlen(x) <= 4) { return stracpy(x); } } return NULL;}unsigned char *get_content_type(unsigned char *head, unsigned char *url){ unsigned char *ct; if ((ct = parse_http_header(head, "Content-Type", NULL))) { unsigned char *s; if ((s = strchr(ct, ';'))) *s = 0; while (*ct && ct[strlen(ct) - 1] <= ' ') ct[strlen(ct) - 1] = 0; if (!strcasecmp(ct, "text/plain") || !strcasecmp(ct, "application/octet-stream") || !strcasecmp(ct, "application/octetstream")) { unsigned char *ctt = get_content_type_by_extension(url); if (ctt) { mem_free(ct); return ctt; } } return ct; } ct = get_content_type_by_extension(url); if (ct) return ct; return !force_html ? stracpy("text/plain") : stracpy("text/html");}unsigned char *get_content_encoding(unsigned char *head, unsigned char *url){ unsigned char *ce, *ct, *ext; unsigned char *u; unsigned l; int code; if ((ce = parse_http_header(head, "Content-Encoding", NULL))) return ce; if ((ct = parse_http_header(head, "Content-Type", NULL))) { unsigned char *s; if ((s = strchr(ct, ';'))) *s = 0; ce = NULL; if (!strcasecmp(ct, "application/x-gzip")) ce = "gzip"; if (!strcasecmp(ct, "application/x-bzip2")) ce = "bzip2"; mem_free(ct); if (ce) return stracpy(ce); } if (!get_http_code(head, &code, NULL) && code >= 300) return NULL; if (!(ext = get_url_data(url))) ext = url; for (u = ext; *u; u++) if (end_of_dir(url, *u)) goto skip_ext; l = strlen(ext); if (l >= 3 && !strcasecmp(ext + l - 3, ".gz")) return stracpy("gzip"); if (l >= 4 && !strcasecmp(ext + l - 4, ".bz2")) return stracpy("bzip2"); skip_ext: if ((ext = get_filename_from_header(head))) { ce = NULL; l = strlen(ext); if (l >= 3 && !strcasecmp(ext + l - 3, ".gz")) ce = "gzip"; if (l >= 4 && !strcasecmp(ext + l - 4, ".bz2")) ce = "bzip2"; mem_free(ext); if (ce) return stracpy(ce); } return NULL;}unsigned char *encoding_2_extension(unsigned char *encoding){ if (!strcasecmp(encoding, "gzip") || !strcasecmp(encoding, "x-gzip")) return "gz"; if (!strcasecmp(encoding, "compress") || !strcasecmp(encoding, "x-compress")) return "Z"; if (!strcasecmp(encoding, "bzip2")) return "bz2"; return NULL;}/* returns field with associations */struct assoc *get_type_assoc(struct terminal *term, unsigned char *type, int *n){ struct assoc *vecirek; struct assoc *a; int count=0; foreach(a, assoc) if (a->system == SYSTEM_ID && (term->environment & ENV_XWIN ? a->xwin : a->cons) && is_in_list(a->ct, type, strlen(type))) { if (count == MAXINT) overalloc(); count++; } *n=count; if (!count)return NULL; if ((unsigned)count > MAXINT / sizeof(struct assoc)) overalloc(); vecirek=mem_alloc(count*sizeof(struct assoc)); count=0; foreach(a, assoc) if (a->system == SYSTEM_ID && (term->environment & ENV_XWIN ? a->xwin : a->cons) && is_in_list(a->ct, type, strlen(type))) vecirek[count++]=*a; return vecirek;}void free_types(void){ struct assoc *a; struct extension *e; struct protocol_program *p; foreach(a, assoc) { mem_free(a->ct); mem_free(a->prog); mem_free(a->label); } free_list(assoc); foreach(e, extensions) { mem_free(e->ext); mem_free(e->ct); } free_list(extensions); foreach(p, mailto_prog) mem_free(p->prog); free_list(mailto_prog); foreach(p, telnet_prog) mem_free(p->prog); free_list(telnet_prog); foreach(p, tn3270_prog) mem_free(p->prog); free_list(tn3270_prog); foreach(p, mms_prog) mem_free(p->prog); free_list(mms_prog); free_list(ext_search_history.items); free_list(assoc_search_history.items);}void update_prog(struct list_head *l, unsigned char *p, int s){ struct protocol_program *repl; foreach(repl, *l) if (repl->system == s) { mem_free(repl->prog); goto ss; } repl = mem_alloc(sizeof(struct protocol_program)); add_to_list(*l, repl); repl->system = s; ss: repl->prog = mem_alloc(MAX_STR_LEN); strncpy(repl->prog, p, MAX_STR_LEN); repl->prog[MAX_STR_LEN - 1] = 0;}unsigned char *get_prog(struct list_head *l){ struct protocol_program *repl; foreach(repl, *l) if (repl->system == SYSTEM_ID) return repl->prog; update_prog(l, "", SYSTEM_ID); foreach(repl, *l) if (repl->system == SYSTEM_ID) return repl->prog; return NULL;}int is_html_type(unsigned char *ct){ return !strcasecmp(ct, "text/html") || !strcasecmp(ct, "text/x-server-parsed-html") || !casecmp(ct, "application/xhtml", strlen("application/xhtml"));}unsigned char *get_filename_from_header(unsigned char *head){ unsigned char *ct, *x, *y; if ((ct = parse_http_header(head, "Content-Disposition", NULL))) { x = parse_header_param(ct, "filename"); mem_free(ct); if (x) { if (*x) goto ret_x; mem_free(x); } } if ((ct = parse_http_header(head, "Content-Type", NULL))) { x = parse_header_param(ct, "name"); mem_free(ct); if (x) { if (*x) goto ret_x; mem_free(x); } } return NULL; ret_x: for (y = x; *y; y++) if (dir_sep(*y)#if defined(DOS_FS) || defined(SPAD) || *y == ':'#endif ) *y = '-'; return x;}unsigned char *get_filename_from_url(unsigned char *url, unsigned char *head, int tmp){ unsigned char *u, *s, *e, *f, *x; unsigned char *ct, *want_ext; want_ext = stracpy(""); f = get_filename_from_header(head); if (f) goto no_ct; if (!(u = get_url_data(url))) u = url; for (e = s = u; *e && !end_of_dir(url, *e); e++) { if (dir_sep(*e)) s = e + 1; } f = memacpy(s, e - s); if (!(ct = parse_http_header(head, "Content-Type", NULL))) goto no_ct; mem_free(ct); ct = get_content_type(head, url); if (ct) { x = get_extension_by_content_type(ct); if (x) { add_to_strn(&want_ext, "."); add_to_strn(&want_ext, x); mem_free(x); } mem_free(ct); } no_ct: ct = get_content_encoding(head, url); if (ct) { x = encoding_2_extension(ct); if (!tmp) { if (x) { add_to_strn(&want_ext, "."); add_to_strn(&want_ext, x); } } else { if (strlen(x) + 1 < strlen(f) && f[strlen(f) - strlen(x) - 1] == '.' && !strcasecmp(f + strlen(f) - strlen(x), x)) { f[strlen(f) - strlen(x) - 1] = 0; } } mem_free(ct); } if (strlen(want_ext) > strlen(f) || strcasecmp(want_ext, f + strlen(f) - strlen(want_ext))) { x = strrchr(f, '.'); if (x) *x = 0; add_to_strn(&f, want_ext); } mem_free(want_ext); return f;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -