session.c
来自「this is the file used to browse web」· C语言 代码 · 共 1,618 行 · 第 1/4 页
C
1,618 行
struct frame *frm; nl = mem_alloc(sizeof(struct location) + strlen(l->vs.url) + 1); memcpy(nl, l, sizeof(struct location) + strlen(l->vs.url) + 1); init_list(nl->frames); foreachback(frm, l->frames) { struct frame *nfrm; nfrm = mem_alloc(sizeof(struct frame) + strlen(frm->vs.url) + 1); memcpy(nfrm, frm, sizeof(struct frame) + strlen(frm->vs.url) + 1); add_to_list(nl->frames, nfrm); } add_to_list(new->history, nl); }*/ if (!list_empty(old->history)) { goto_url(new, cur_loc(old)->vs.url); }}void *create_session_info(int cp, unsigned char *url, int *ll){ int l = strlen(url); int *i; *ll = 2 * sizeof(int) + l; i = mem_alloc(2 * sizeof(int) + l); i[0] = cp; i[1] = l; memcpy(i + 2, url, l); return i;}static inline unsigned char hx(int a){ return a >= 10 ? a + 'A' - 10 : a + '0';}static inline int unhx(unsigned char a){ if (a >= '0' && a <= '9') return a - '0'; if (a >= 'A' && a <= 'F') return a - 'A' + 10; if (a >= 'a' && a <= 'f') return a - 'a' + 10; return -1;}unsigned char *encode_url(unsigned char *url){ unsigned char *u = init_str(); int l = 0; for (; *url; url++) { if (is_safe_in_shell(*url) && *url != '+') add_chr_to_str(&u, &l, *url); else add_chr_to_str(&u, &l, '+'), add_chr_to_str(&u, &l, hx(*url >> 4)), add_chr_to_str(&u, &l, hx(*url & 0xf)); } return u;}unsigned char *decode_url(unsigned char *url){ unsigned char *u = init_str(); int l = 0; for (; *url; url++) { if (*url != '+' || unhx(url[1]) == -1 || unhx(url[2]) == -1) add_chr_to_str(&u, &l, *url); else add_chr_to_str(&u, &l, (unhx(url[1]) << 4) + unhx(url[2])), url += 2; } return u;}int read_session_info(int fd, struct session *ses, void *data, int len){ unsigned char *h; int cpfrom, sz; struct session *s; if (len < 2 * (int)sizeof(int)) return -1; cpfrom = *(int *)data; sz = *((int *)data + 1); foreach(s, sessions) if (s->id == cpfrom) { copy_session(s, ses); break; } if (sz) { char *u, *uu; if (len < 2 * (int)sizeof(int) + sz) return 0; if ((unsigned)sz >= MAXINT) overalloc(); u = mem_alloc(sz + 1); memcpy(u, (int *)data + 2, sz); u[sz] = 0; uu = decode_url(u); goto_url(ses, uu); mem_free(u); mem_free(uu); } else if ((h = getenv("WWW_HOME")) && *h) goto_url(ses, h); return 0;}void abort_preloading(struct session *ses){ if (ses->wtd) { change_connection(&ses->loading, NULL, PRI_CANCEL); free_wtd(ses); }}void abort_loading(struct session *ses){ struct location *l = cur_loc(ses); if ((void *)l != &ses->history) { if (l->stat.state >= 0) change_connection(&l->stat, NULL, PRI_CANCEL); abort_files_load(ses); } abort_preloading(ses);}void destroy_session(struct session *ses){ struct f_data_c *fdc; struct download *d; struct location *l; if (!ses) return; foreach(d, downloads) if (d->ses == ses && d->prog) { d = d->prev; abort_download(d->next); } abort_loading(ses); free_files(ses); if (ses->screen) detach_formatted(ses->screen), mem_free(ses->screen); foreach(fdc, ses->scrn_frames) detach_formatted(fdc); free_list(ses->scrn_frames); while ((void *)(l = ses->history.next) != &ses->history) { struct frame *frm; while ((void *)(frm = l->frames.next) != &l->frames) { destroy_vs(&frm->vs); mem_free(frm->name); del_from_list(frm); mem_free(frm); } destroy_vs(&l->vs); del_from_list(l); mem_free(l); } if (ses->loading_url) mem_free(ses->loading_url); if (ses->display_timer != -1) kill_timer(ses->display_timer); if (ses->goto_position) mem_free(ses->goto_position); if (ses->imgmap_href_base) mem_free(ses->imgmap_href_base); if (ses->imgmap_target_base) mem_free(ses->imgmap_target_base); if (ses->tq_ce) ses->tq_ce->refcount--; if (ses->tq_url) { change_connection(&ses->tq, NULL, PRI_CANCEL); mem_free(ses->tq_url); } if (ses->tq_goto_position) mem_free(ses->tq_goto_position); if (ses->tq_prog) mem_free(ses->tq_prog); if (ses->dn_url) mem_free(ses->dn_url); if (ses->search_word) mem_free(ses->search_word); if (ses->last_search_word) mem_free(ses->last_search_word); del_from_list(ses); /*mem_free(ses);*/}void destroy_all_sessions(){ /*while (!list_empty(sessions)) destroy_session(sessions.next);*/}void abort_all_downloads(){ while (!list_empty(downloads)) abort_download(downloads.next);}void reload(struct session *ses, int no_cache){ struct location *l; abort_loading(ses); if (no_cache == -1) no_cache = ++ses->reloadlevel; else ses->reloadlevel = no_cache; if ((void *)(l = ses->history.next) != &ses->history) { struct file_to_load *ftl; l->stat.data = ses; l->stat.end = (void *)doc_end_load; load_url(l->vs.url, &l->stat, PRI_MAIN, no_cache); foreach(ftl, ses->more_files) { if (ftl->req_sent && ftl->stat.state >= 0) continue; ftl->stat.data = ftl; ftl->stat.end = (void *)file_end_load; load_url(ftl->url, &ftl->stat, PRI_FRAME, no_cache); } }}void go_back(struct session *ses){ unsigned char *url; ses->reloadlevel = NC_CACHE; if (ses->wtd) { if (1 || ses->wtd != WTD_BACK) { abort_loading(ses); print_screen_status(ses); reload(ses, NC_CACHE); } return; } if (ses->history.next == &ses->history || ses->history.next == ses->history.prev) return; abort_loading(ses); url = stracpy(((struct location *)ses->history.next)->next->vs.url); ses->redirect_cnt = 0; ses_goto(ses, url, NULL, PRI_MAIN, NC_ALWAYS_CACHE, WTD_BACK, NULL, end_load, 0);}void goto_url_w(struct session *ses, unsigned char *url, unsigned char *target, int wtd){ unsigned char *u; unsigned char *pos; void (*fn)(struct session *, unsigned char *); if ((fn = get_external_protocol_function(url))) { fn(ses, url); return; } ses->reloadlevel = NC_CACHE; /*struct location *l = ses->history.next;*/ if (!(u = translate_url(url, ses->term->cwd))) { struct status stat = { NULL, NULL, NULL, NULL, S_BAD_URL, PRI_CANCEL, 0, NULL, NULL, NULL }; print_error_dialog(ses, &stat, TEXT(T_ERROR)); return; } pos = extract_position(u); if (ses->wtd == wtd) { if (!strcmp(ses->loading_url, u)) { mem_free(u); if (ses->goto_position) mem_free(ses->goto_position); ses->goto_position = pos; return; } } abort_loading(ses); ses->redirect_cnt = 0; ses_goto(ses, u, target, PRI_MAIN, NC_CACHE, wtd, pos, end_load, 0); /*abort_loading(ses);*/}void goto_url_f(struct session *ses, unsigned char *url, unsigned char *target){ goto_url_w(ses, url, target, WTD_FORWARD);}void goto_url(struct session *ses, unsigned char *url){ goto_url_w(ses, url, NULL, WTD_FORWARD);}void goto_imgmap(struct session *ses, unsigned char *url, unsigned char *href, unsigned char *target){ if (ses->imgmap_href_base) mem_free(ses->imgmap_href_base); ses->imgmap_href_base = href; if (ses->imgmap_target_base) mem_free(ses->imgmap_target_base); ses->imgmap_target_base = target; goto_url_w(ses, url, target, WTD_IMGMAP);}struct frame *ses_find_frame(struct session *ses, unsigned char *name){ struct location *l = cur_loc(ses); struct frame *frm; if (list_empty(ses->history)) { internal("ses_request_frame: history empty"); return NULL; } foreachback(frm, l->frames) if (!strcasecmp(frm->name, name)) return frm; /*internal("ses_find_frame: frame not found");*/ return NULL;}struct frame *ses_change_frame_url(struct session *ses, unsigned char *name, unsigned char *url){ struct location *l = cur_loc(ses); struct frame *frm; if (list_empty(ses->history)) { internal("ses_change_frame_url: history empty"); return NULL; } foreachback(frm, l->frames) if (!strcasecmp(frm->name, name)) { if (strlen(url) > strlen(frm->vs.url)) { struct f_data_c *fd; struct frame *nf = frm; nf = mem_realloc(frm, sizeof(struct frame) + strlen(url) + 1); nf->prev->next = nf->next->prev = nf; foreach(fd, ses->scrn_frames) { if (fd->vs == &frm->vs) fd->vs = &nf->vs; } frm = nf; } strcpy(frm->vs.url, url); return frm; } return NULL; }void win_func(struct window *win, struct event *ev, int fw){ struct session *ses = win->data; switch (ev->ev) { case EV_ABORT: if (ses) destroy_session(ses); break; case EV_INIT: if (!(ses = win->data = create_session(win)) || read_session_info(win->term->fdin, ses, (char *)ev->b + sizeof(int), *(int *)ev->b)) { register_bottom_half((void (*)(void *))destroy_terminal, win->term); return; } /*make_request(ses, 0);*/ case EV_RESIZE: html_interpret(ses); draw_formatted(ses); load_frames(ses, ses->screen); process_file_requests(ses); print_screen_status(ses); break; case EV_REDRAW: draw_formatted(ses); print_screen_status(ses); break; case EV_KBD: case EV_MOUSE: send_event(ses, ev); break; default: error("ERROR: unknown event"); }}/* Gets the url being viewed by this session. Writes it into str. A maximum of str_size bytes (including null) will be written.*/ unsigned char *get_current_url(struct session *ses, unsigned char *str, size_t str_size) { unsigned char *here, *end_of_url; size_t url_len = 0; /* Not looking at anything */ if (list_empty(ses->history)) return NULL; here = cur_loc(ses)->vs.url; /* Find the length of the url */ if ((end_of_url = strchr(here, POST_CHAR))) { url_len = (size_t)(end_of_url - (unsigned char *)here); } else { url_len = strlen(here); } /* Ensure that the url size is not greater than str_size */ if (url_len >= str_size) url_len = str_size - 1; strncpy(str, here, url_len); /* Ensure null termination */ str[url_len] = '\0'; return str;}/* Gets the title of the page being viewed by this session. Writes it into str. A maximum of str_size bytes (including null) will be written.*/ unsigned char *get_current_title(struct session *ses, unsigned char *str, size_t str_size) { struct f_data_c *fd; fd = (struct f_data_c *)current_frame(ses); /* Ensure that the title is defined */ if (!fd) return NULL; return safe_strncpy(str, fd->f_data->title, str_size);}/* Gets the url of the link currently selected. Writes it into str. A maximum of str_size bytes (including null) will be written.*/ unsigned char *get_current_link_url(struct session *ses, unsigned char *str, size_t str_size) { struct f_data_c *fd; struct link *l; fd = (struct f_data_c *)current_frame(ses); /* What the hell is an 'fd'? */ if (!fd) return NULL; /* Nothing selected? */ if (fd->vs->current_link == -1) return NULL; l = &fd->f_data->links[fd->vs->current_link]; /* Only write a link */ if (l->type != L_LINK) return NULL; return safe_strncpy(str, l->where ? l->where : l->where_img, str_size);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?