Let users retry connection in case of error. Especially SSL error.
[elinks.git] / src / session / session.c
blob76641aa90ea60ba366588bd135dd60d95a249ae8
1 /** Sessions managment - you'll find things here which you wouldn't expect
2 * @file */
4 #ifdef HAVE_CONFIG_H
5 #include "config.h"
6 #endif
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
12 #include "elinks.h"
14 #include "bfu/dialog.h"
15 #include "bookmarks/bookmarks.h"
16 #include "cache/cache.h"
17 #include "config/home.h"
18 #include "config/options.h"
19 #include "dialogs/menu.h"
20 #include "dialogs/status.h"
21 #include "document/document.h"
22 #include "document/html/frames.h"
23 #include "document/refresh.h"
24 #include "document/renderer.h"
25 #include "document/view.h"
26 #include "globhist/globhist.h"
27 #include "intl/gettext/libintl.h"
28 #include "main/event.h"
29 #include "main/object.h"
30 #include "main/timer.h"
31 #include "network/connection.h"
32 #include "network/state.h"
33 #include "osdep/newwin.h"
34 #include "protocol/protocol.h"
35 #include "protocol/uri.h"
36 #ifdef CONFIG_SCRIPTING_SPIDERMONKEY
37 # include "scripting/smjs/smjs.h"
38 #endif
39 #include "session/download.h"
40 #include "session/history.h"
41 #include "session/location.h"
42 #include "session/session.h"
43 #include "session/task.h"
44 #include "terminal/tab.h"
45 #include "terminal/terminal.h"
46 #include "terminal/window.h"
47 #include "util/conv.h"
48 #include "util/error.h"
49 #include "util/memlist.h"
50 #include "util/memory.h"
51 #include "util/string.h"
52 #include "util/time.h"
53 #include "viewer/text/draw.h"
54 #include "viewer/text/form.h"
55 #include "viewer/text/link.h"
56 #include "viewer/text/view.h"
59 struct file_to_load {
60 LIST_HEAD(struct file_to_load);
62 struct session *ses;
63 unsigned int req_sent:1;
64 int pri;
65 struct cache_entry *cached;
66 unsigned char *target_frame;
67 struct uri *uri;
68 struct download download;
71 /** This structure and related functions are used to maintain information
72 * for instances opened in new windows. We store all related session info like
73 * URI and base session to clone from so that when the new instance connects
74 * we can look up this information. In case of failure the session information
75 * has a timeout */
76 struct session_info {
77 LIST_HEAD(struct session_info);
79 int id;
80 timer_id_T timer;
81 struct session *ses;
82 struct uri *uri;
83 struct uri *referrer;
84 enum task_type task;
85 enum cache_mode cache_mode;
88 #define file_to_load_is_active(ftl) ((ftl)->req_sent && is_in_progress_state((ftl)->download.state))
91 INIT_LIST_OF(struct session, sessions);
93 enum remote_session_flags remote_session_flags;
96 static struct file_to_load *request_additional_file(struct session *,
97 unsigned char *,
98 struct uri *, int);
100 static window_handler_T tabwin_func;
103 static INIT_LIST_OF(struct session_info, session_info);
104 static int session_info_id = 1;
106 static struct session_info *
107 get_session_info(int id)
109 struct session_info *info;
111 foreach (info, session_info) {
112 struct session *ses;
114 if (info->id != id) continue;
116 /* Make sure the info->ses session is still with us. */
117 foreach (ses, sessions)
118 if (ses == info->ses)
119 return info;
121 info->ses = NULL;
122 return info;
125 return NULL;
128 static void
129 done_session_info(struct session_info *info)
131 del_from_list(info);
132 kill_timer(&info->timer);
134 if (info->uri) done_uri(info->uri);
135 if (info->referrer) done_uri(info->referrer);
136 mem_free(info);
139 void
140 done_saved_session_info(void)
142 while (!list_empty(session_info))
143 done_session_info(session_info.next);
146 /** Timer callback for session_info.timer. As explained in install_timer(),
147 * this function must erase the expired timer ID from all variables. */
148 static void
149 session_info_timeout(int id)
151 struct session_info *info = get_session_info(id);
153 if (!info) return;
154 info->timer = TIMER_ID_UNDEF;
155 /* The expired timer ID has now been erased. */
156 done_session_info(info);
160 add_session_info(struct session *ses, struct uri *uri, struct uri *referrer,
161 enum cache_mode cache_mode, enum task_type task)
163 struct session_info *info = mem_calloc(1, sizeof(*info));
165 if (!info) return -1;
167 info->id = session_info_id++;
168 /* I don't know what a reasonable start up time for a new instance is
169 * but it won't hurt to have a few seconds atleast. --jonas */
170 install_timer(&info->timer, (milliseconds_T) 10000,
171 (void (*)(void *)) session_info_timeout,
172 (void *) (long) info->id);
174 info->ses = ses;
175 info->task = task;
176 info->cache_mode = cache_mode;
178 if (uri) info->uri = get_uri_reference(uri);
179 if (referrer) info->referrer = get_uri_reference(referrer);
181 add_to_list(session_info, info);
183 return info->id;
186 static struct session *
187 init_saved_session(struct terminal *term, int id)
189 struct session_info *info = get_session_info(id);
190 struct session *ses;
192 if (!info) return NULL;
194 ses = init_session(info->ses, term, info->uri, 0);
196 if (!ses) {
197 done_session_info(info);
198 return ses;
201 /* Only do the ses_goto()-thing for target=_blank. */
202 if (info->uri && info->task != TASK_NONE) {
203 /* The init_session() call would have started the download but
204 * not with the requested cache mode etc. so interrupt that
205 * download . */
206 abort_loading(ses, 1);
208 ses->reloadlevel = info->cache_mode;
209 set_session_referrer(ses, info->referrer);
210 ses_goto(ses, info->uri, NULL, NULL, info->cache_mode,
211 info->task, 0);
214 done_session_info(info);
216 return ses;
219 static struct session *
220 get_master_session(void)
222 struct session *ses;
224 foreach (ses, sessions)
225 if (ses->tab->term->master) {
226 struct window *current_tab = get_current_tab(ses->tab->term);
228 return current_tab ? current_tab->data : NULL;
231 return NULL;
234 /** @relates session */
235 struct download *
236 get_current_download(struct session *ses)
238 struct download *download = NULL;
240 if (!ses) return NULL;
242 if (ses->task.type)
243 download = &ses->loading;
244 else if (have_location(ses))
245 download = &cur_loc(ses)->download;
247 if (download && is_in_state(download->state, S_OK)) {
248 struct file_to_load *ftl;
250 foreach (ftl, ses->more_files)
251 if (file_to_load_is_active(ftl))
252 return &ftl->download;
255 /* Note that @download isn't necessarily NULL here,
256 * if @ses->more_files is empty. -- Miciah */
257 return download;
260 static void
261 retry_connection_without_verification(void *data)
263 struct delayed_open *deo = (struct delayed_open *)data;
265 if (deo) {
266 deo->ses->verify = 0;
267 goto_uri(deo->ses, deo->uri);
268 done_uri(deo->uri);
269 mem_free(deo);
273 void
274 print_error_dialog(struct session *ses, struct connection_state state,
275 struct uri *uri, enum connection_priority priority)
277 struct string msg;
278 unsigned char *uristring;
280 /* Don't show error dialogs for missing CSS stylesheets */
281 if (priority == PRI_CSS
282 || !init_string(&msg))
283 return;
285 uristring = uri ? get_uri_string(uri, URI_PUBLIC) : NULL;
286 if (uristring) {
287 #ifdef CONFIG_UTF8
288 if (ses->tab->term->utf8_cp)
289 decode_uri(uristring);
290 else
291 #endif /* CONFIG_UTF8 */
292 decode_uri_for_display(uristring);
293 add_format_to_string(&msg,
294 _("Unable to retrieve %s", ses->tab->term),
295 uristring);
296 mem_free(uristring);
297 add_to_string(&msg, ":\n\n");
300 add_to_string(&msg, get_state_message(state, ses->tab->term));
302 if (!ses->verify || !uri) {
303 info_box(ses->tab->term, MSGBOX_FREE_TEXT,
304 N_("Error"), ALIGN_CENTER,
305 msg.source);
306 } else {
307 struct delayed_open *deo = mem_calloc(1, sizeof(*deo));
309 if (!deo) return;
311 add_to_string(&msg, "\n\n");
312 add_to_string(&msg, N_("Retry without verification?"));
313 deo->ses = ses;
314 deo->uri = get_uri_reference(uri);
316 msg_box(ses->tab->term, NULL, MSGBOX_FREE_TEXT,
317 N_("Error"), ALIGN_CENTER,
318 msg.source,
319 deo, 2,
320 MSG_BOX_BUTTON(N_("~Yes"), retry_connection_without_verification, B_ENTER),
321 MSG_BOX_BUTTON(N_("~No"), NULL, B_ESC));
324 /* TODO: retry */
327 static void
328 abort_files_load(struct session *ses, int interrupt)
330 while (1) {
331 struct file_to_load *ftl;
332 int more = 0;
334 foreach (ftl, ses->more_files) {
335 if (!file_to_load_is_active(ftl))
336 continue;
338 more = 1;
339 cancel_download(&ftl->download, interrupt);
342 if (!more) break;
346 void
347 free_files(struct session *ses)
349 struct file_to_load *ftl;
351 abort_files_load(ses, 0);
352 foreach (ftl, ses->more_files) {
353 if (ftl->cached) object_unlock(ftl->cached);
354 if (ftl->uri) done_uri(ftl->uri);
355 mem_free_if(ftl->target_frame);
357 free_list(ses->more_files);
363 static void request_frameset(struct session *, struct frameset_desc *, int);
365 static void
366 request_frame(struct session *ses, unsigned char *name,
367 struct uri *uri, int depth)
369 struct location *loc = cur_loc(ses);
370 struct frame *frame;
372 assertm(have_location(ses), "request_frame: no location");
373 if_assert_failed return;
375 foreach (frame, loc->frames) {
376 struct document_view *doc_view;
378 if (c_strcasecmp(frame->name, name))
379 continue;
381 foreach (doc_view, ses->scrn_frames) {
382 if (doc_view->vs == &frame->vs && doc_view->document->frame_desc) {
383 request_frameset(ses, doc_view->document->frame_desc, depth);
384 return;
388 request_additional_file(ses, name, frame->vs.uri, PRI_FRAME);
389 return;
392 frame = mem_calloc(1, sizeof(*frame));
393 if (!frame) return;
395 frame->name = stracpy(name);
396 if (!frame->name) {
397 mem_free(frame);
398 return;
401 init_vs(&frame->vs, uri, -1);
403 add_to_list(loc->frames, frame);
405 request_additional_file(ses, name, frame->vs.uri, PRI_FRAME);
408 static void
409 request_frameset(struct session *ses, struct frameset_desc *frameset_desc, int depth)
411 int i;
413 if (depth > HTML_MAX_FRAME_DEPTH) return;
415 depth++; /* Inheritation counter (recursion brake ;) */
417 for (i = 0; i < frameset_desc->n; i++) {
418 struct frame_desc *frame_desc = &frameset_desc->frame_desc[i];
420 if (frame_desc->subframe) {
421 request_frameset(ses, frame_desc->subframe, depth);
422 } else if (frame_desc->name && frame_desc->uri) {
423 request_frame(ses, frame_desc->name,
424 frame_desc->uri, depth);
429 #ifdef CONFIG_CSS
430 static inline void
431 load_css_imports(struct session *ses, struct document_view *doc_view)
433 struct document *document = doc_view->document;
434 struct uri *uri;
435 int index;
437 if (!document) return;
439 foreach_uri (uri, index, &document->css_imports) {
440 request_additional_file(ses, "", uri, PRI_CSS);
443 #else
444 #define load_css_imports(ses, doc_view)
445 #endif
447 #ifdef CONFIG_ECMASCRIPT
448 static inline void
449 load_ecmascript_imports(struct session *ses, struct document_view *doc_view)
451 struct document *document = doc_view->document;
452 struct uri *uri;
453 int index;
455 if (!document) return;
457 foreach_uri (uri, index, &document->ecmascript_imports) {
458 request_additional_file(ses, "", uri, /* XXX */ PRI_CSS);
461 #else
462 #define load_ecmascript_imports(ses, doc_view)
463 #endif
465 NONSTATIC_INLINE void
466 load_frames(struct session *ses, struct document_view *doc_view)
468 struct document *document = doc_view->document;
470 if (!document || !document->frame_desc) return;
471 request_frameset(ses, document->frame_desc, 0);
473 foreach (doc_view, ses->scrn_frames) {
474 load_css_imports(ses, doc_view);
475 load_ecmascript_imports(ses, doc_view);
479 /** Timer callback for session.display_timer. As explained in install_timer(),
480 * this function must erase the expired timer ID from all variables. */
481 void
482 display_timer(struct session *ses)
484 timeval_T start, stop, duration;
485 milliseconds_T t;
487 timeval_now(&start);
488 draw_formatted(ses, 3);
489 timeval_now(&stop);
490 timeval_sub(&duration, &start, &stop);
492 t = mult_ms(timeval_to_milliseconds(&duration), DISPLAY_TIME);
493 if (t < DISPLAY_TIME_MIN) t = DISPLAY_TIME_MIN;
494 install_timer(&ses->display_timer, t,
495 (void (*)(void *)) display_timer,
496 ses);
497 /* The expired timer ID has now been erased. */
499 load_frames(ses, ses->doc_view);
500 load_css_imports(ses, ses->doc_view);
501 load_ecmascript_imports(ses, ses->doc_view);
502 process_file_requests(ses);
506 struct questions_entry {
507 LIST_HEAD(struct questions_entry);
509 void (*callback)(struct session *, void *);
510 void *data;
513 INIT_LIST_OF(struct questions_entry, questions_queue);
516 void
517 check_questions_queue(struct session *ses)
519 while (!list_empty(questions_queue)) {
520 struct questions_entry *q = questions_queue.next;
522 q->callback(ses, q->data);
523 del_from_list(q);
524 mem_free(q);
528 void
529 add_questions_entry(void (*callback)(struct session *, void *), void *data)
531 struct questions_entry *q = mem_alloc(sizeof(*q));
533 if (!q) return;
535 q->callback = callback;
536 q->data = data;
537 add_to_list(questions_queue, q);
540 #ifdef CONFIG_SCRIPTING
541 static void
542 maybe_pre_format_html(struct cache_entry *cached, struct session *ses)
544 struct fragment *fragment;
545 static int pre_format_html_event = EVENT_NONE;
547 if (!cached || cached->preformatted)
548 return;
550 /* The script called from here may indirectly call
551 * garbage_collection(). If the refcount of the cache entry
552 * were 0, it could then be freed, and the
553 * cached->preformatted assignment at the end of this function
554 * would crash. Normally, the document has a reference to the
555 * cache entry, and that suffices. However, if the cache
556 * entry was loaded to satisfy e.g. USEMAP="imgmap.html#map",
557 * then cached->object.refcount == 0 here, and must be
558 * incremented.
560 * cached->object.refcount == 0 is safe while the cache entry
561 * is being loaded, because garbage_collection() calls
562 * is_entry_used(), which checks whether any connection is
563 * using the cache entry. But loading has ended before this
564 * point. */
565 object_lock(cached);
567 fragment = get_cache_fragment(cached);
568 if (!fragment) goto unlock_and_return;
570 /* We cannot do anything if the data are fragmented. */
571 if (!list_is_singleton(cached->frag)) goto unlock_and_return;
573 set_event_id(pre_format_html_event, "pre-format-html");
574 trigger_event(pre_format_html_event, ses, cached);
576 /* XXX: Keep this after the trigger_event, because hooks might call
577 * normalize_cache_entry()! */
578 cached->preformatted = 1;
580 unlock_and_return:
581 object_unlock(cached);
583 #endif
585 static int
586 check_incomplete_redirects(struct cache_entry *cached)
588 assert(cached);
590 cached = follow_cached_redirects(cached);
591 if (cached && !cached->redirect) {
592 /* XXX: This is not quite true, but does that difference
593 * matter here? */
594 return cached->incomplete;
597 return 0;
601 session_is_loading(struct session *ses)
603 struct download *download = get_current_download(ses);
605 if (!download) return 0;
607 if (!is_in_result_state(download->state))
608 return 1;
610 /* The validness of download->cached (especially the download struct in
611 * ses->loading) is hard to maintain so check before using it.
612 * Related to bug 559. */
613 if (download->cached
614 && cache_entry_is_valid(download->cached)
615 && check_incomplete_redirects(download->cached))
616 return 1;
618 return 0;
621 void
622 doc_loading_callback(struct download *download, struct session *ses)
624 int submit = 0;
626 if (is_in_result_state(download->state)) {
627 #ifdef CONFIG_SCRIPTING
628 maybe_pre_format_html(download->cached, ses);
629 #endif
630 kill_timer(&ses->display_timer);
632 draw_formatted(ses, 1);
634 if (get_cmd_opt_bool("auto-submit")) {
635 if (!list_empty(ses->doc_view->document->forms)) {
636 get_cmd_opt_bool("auto-submit") = 0;
637 submit = 1;
641 load_frames(ses, ses->doc_view);
642 load_css_imports(ses, ses->doc_view);
643 load_ecmascript_imports(ses, ses->doc_view);
644 process_file_requests(ses);
646 start_document_refreshes(ses);
648 if (!is_in_state(download->state, S_OK)) {
649 print_error_dialog(ses, download->state,
650 ses->doc_view->document->uri,
651 download->pri);
654 } else if (is_in_transfering_state(download->state)
655 && ses->display_timer == TIMER_ID_UNDEF) {
656 display_timer(ses);
659 check_questions_queue(ses);
660 print_screen_status(ses);
662 #ifdef CONFIG_GLOBHIST
663 if (download->pri != PRI_CSS) {
664 unsigned char *title = ses->doc_view->document->title;
665 struct uri *uri;
667 if (download->conn)
668 uri = download->conn->proxied_uri;
669 else if (download->cached)
670 uri = download->cached->uri;
671 else
672 uri = NULL;
674 if (uri)
675 add_global_history_item(struri(uri), title, time(NULL));
677 #endif
679 if (submit) auto_submit_form(ses);
682 static void
683 file_loading_callback(struct download *download, struct file_to_load *ftl)
685 if (ftl->download.cached && ftl->cached != ftl->download.cached) {
686 if (ftl->cached) object_unlock(ftl->cached);
687 ftl->cached = ftl->download.cached;
688 object_lock(ftl->cached);
691 /* FIXME: We need to do content-type check here! However, we won't
692 * handle properly the "Choose action" dialog now :(. */
693 if (ftl->cached && !ftl->cached->redirect_get && download->pri != PRI_CSS) {
694 struct session *ses = ftl->ses;
695 struct uri *loading_uri = ses->loading_uri;
696 unsigned char *target_frame = null_or_stracpy(ses->task.target.frame);
698 ses->loading_uri = ftl->uri;
699 mem_free_set(&ses->task.target.frame, null_or_stracpy(ftl->target_frame));
700 setup_download_handler(ses, &ftl->download, ftl->cached, 1);
701 ses->loading_uri = loading_uri;
702 mem_free_set(&ses->task.target.frame, target_frame);
705 doc_loading_callback(download, ftl->ses);
708 static struct file_to_load *
709 request_additional_file(struct session *ses, unsigned char *name, struct uri *uri, int pri)
711 struct file_to_load *ftl;
713 if (uri->protocol == PROTOCOL_UNKNOWN) {
714 return NULL;
717 /* XXX: We cannot run the external handler here, because
718 * request_additional_file() is called many times for a single URL
719 * (normally the foreach() right below catches them all). Anyway,
720 * having <frame src="mailto:foo"> would be just weird, wouldn't it?
721 * --pasky */
722 if (get_protocol_external_handler(ses->tab->term, uri)) {
723 return NULL;
726 foreach (ftl, ses->more_files) {
727 if (compare_uri(ftl->uri, uri, URI_BASE)) {
728 if (ftl->pri > pri) {
729 ftl->pri = pri;
730 move_download(&ftl->download, &ftl->download, pri);
732 return NULL;
736 ftl = mem_calloc(1, sizeof(*ftl));
737 if (!ftl) return NULL;
739 ftl->uri = get_uri_reference(uri);
740 ftl->target_frame = stracpy(name);
741 ftl->download.callback = (download_callback_T *) file_loading_callback;
742 ftl->download.data = ftl;
743 ftl->pri = pri;
744 ftl->ses = ses;
746 add_to_list(ses->more_files, ftl);
748 return ftl;
751 static void
752 load_additional_file(struct file_to_load *ftl, enum cache_mode cache_mode)
754 struct document_view *doc_view = current_frame(ftl->ses);
755 struct uri *referrer = doc_view && doc_view->document
756 ? doc_view->document->uri : NULL;
758 load_uri(ftl->uri, referrer, &ftl->download, ftl->pri, cache_mode, -1);
761 void
762 process_file_requests(struct session *ses)
764 if (ses->status.processing_file_requests) return;
765 ses->status.processing_file_requests = 1;
767 while (1) {
768 struct file_to_load *ftl;
769 int more = 0;
771 foreach (ftl, ses->more_files) {
772 if (ftl->req_sent)
773 continue;
775 ftl->req_sent = 1;
777 load_additional_file(ftl, CACHE_MODE_NORMAL);
778 more = 1;
781 if (!more) break;
784 ses->status.processing_file_requests = 0;
788 static void
789 dialog_goto_url_open(void *data)
791 dialog_goto_url((struct session *) data, NULL);
794 /** @returns 0 if the first session was not properly initialized and
795 * setup_session() should be called on the session as well. */
796 static int
797 setup_first_session(struct session *ses, struct uri *uri)
799 /* [gettext_accelerator_context(setup_first_session)] */
800 struct terminal *term = ses->tab->term;
802 if (!*get_opt_str("protocol.http.user_agent", NULL)) {
803 info_box(term, 0,
804 N_("Warning"), ALIGN_CENTER,
805 N_("You have an empty string in protocol.http.user_agent - "
806 "this was a default value in the past, substituted by "
807 "default ELinks User-Agent string. However, currently "
808 "this means that NO User-Agent HEADER "
809 "WILL BE SENT AT ALL - if this is really what you want, "
810 "set its value to \" \", otherwise please delete the line "
811 "with this setting from your configuration file (if you "
812 "have no idea what I'm talking about, just do this), so "
813 "that the correct default setting will be used. Apologies for "
814 "any inconvience caused."));
817 if (!get_opt_bool("config.saving_style_w", NULL)) {
818 struct option *opt = get_opt_rec(config_options, "config.saving_style_w");
819 opt->value.number = 1;
820 option_changed(ses, opt);
821 if (get_opt_int("config.saving_style", NULL) != 3) {
822 info_box(term, 0,
823 N_("Warning"), ALIGN_CENTER,
824 N_("You have option config.saving_style set to "
825 "a de facto obsolete value. The configuration "
826 "saving algorithms of ELinks were changed from "
827 "the last time you upgraded ELinks. Now, only "
828 "those options which you actually changed are "
829 "saved to the configuration file, instead of "
830 "all the options. This simplifies our "
831 "situation greatly when we see that some option "
832 "has an inappropriate default value or we need to "
833 "change the semantics of some option in a subtle way. "
834 "Thus, we recommend you change the value of "
835 "config.saving_style option to 3 in order to get "
836 "the \"right\" behaviour. Apologies for any "
837 "inconvience caused."));
841 if (first_use) {
842 /* Only open the goto URL dialog if no URI was passed on the
843 * command line. */
844 void *handler = uri ? NULL : dialog_goto_url_open;
846 first_use = 0;
848 msg_box(term, NULL, 0,
849 N_("Welcome"), ALIGN_CENTER,
850 N_("Welcome to ELinks!\n\n"
851 "Press ESC for menu. Documentation is available in "
852 "Help menu."),
853 ses, 1,
854 MSG_BOX_BUTTON(N_("~OK"), handler, B_ENTER | B_ESC));
856 /* If there is no URI the goto dialog will pop up so there is
857 * no need to call setup_session(). */
858 if (!uri) return 1;
860 #ifdef CONFIG_BOOKMARKS
861 } else if (!uri && get_opt_bool("ui.sessions.auto_restore", NULL)) {
862 unsigned char *folder; /* UTF-8 */
864 folder = get_auto_save_bookmark_foldername_utf8();
865 if (folder) {
866 open_bookmark_folder(ses, folder);
867 mem_free(folder);
869 return 1;
870 #endif
873 /* If there's a URI to load we have to call */
874 return 0;
877 /** First load the current URI of the base session. In most cases it will just
878 * be fetched from the cache so that the new tab will not appear ``empty' while
879 * loading the real URI or showing the goto URL dialog. */
880 static void
881 setup_session(struct session *ses, struct uri *uri, struct session *base)
883 if (base && have_location(base)) {
884 ses_load(ses, get_uri_reference(cur_loc(base)->vs.uri),
885 NULL, NULL, CACHE_MODE_ALWAYS, TASK_FORWARD);
886 if (ses->doc_view && ses->doc_view->vs
887 && base->doc_view && base->doc_view->vs) {
888 struct view_state *vs = ses->doc_view->vs;
890 destroy_vs(vs, 1);
891 copy_vs(vs, base->doc_view->vs);
893 ses->doc_view->vs = vs;
897 if (uri) {
898 goto_uri(ses, uri);
900 } else if (!goto_url_home(ses)) {
901 if (get_opt_bool("ui.startup_goto_dialog", NULL)) {
902 dialog_goto_url_open(ses);
907 /** @relates session */
908 struct session *
909 init_session(struct session *base_session, struct terminal *term,
910 struct uri *uri, int in_background)
912 struct session *ses = mem_calloc(1, sizeof(*ses));
914 if (!ses) return NULL;
916 ses->tab = init_tab(term, ses, tabwin_func);
917 if (!ses->tab) {
918 mem_free(ses);
919 return NULL;
922 ses->verify = 1;
924 ses->option = copy_option(config_options,
925 CO_SHALLOW | CO_NO_LISTBOX_ITEM);
926 create_history(&ses->history);
927 init_list(ses->scrn_frames);
928 init_list(ses->more_files);
929 init_list(ses->type_queries);
930 ses->task.type = TASK_NONE;
931 ses->display_timer = TIMER_ID_UNDEF;
933 #ifdef CONFIG_LEDS
934 init_led_panel(&ses->status.leds);
935 ses->status.ssl_led = register_led(ses, 0);
936 ses->status.insert_mode_led = register_led(ses, 1);
937 ses->status.ecmascript_led = register_led(ses, 2);
938 ses->status.popup_led = register_led(ses, 3);
940 ses->status.download_led = register_led(ses, 5);
941 #endif
942 ses->status.force_show_status_bar = -1;
943 ses->status.force_show_title_bar = -1;
945 add_to_list(sessions, ses);
947 /* Update the status -- most importantly the info about whether to the
948 * show the title, tab and status bar -- _before_ loading the URI so
949 * the document cache is not filled with useless documents if the
950 * content is already cached.
952 * For big document it also reduces memory usage quite a bit because
953 * (atleast that is my interpretation --jonas) the old document will
954 * have a chance to be released before rendering a new one. A few
955 * numbers when opening a new tab while viewing debians package list
956 * for unstable:
958 * 9307 jonas 15 0 34252 30m 5088 S 0.0 12.2 0:03.63 elinks-old
959 * 9305 jonas 15 0 17060 13m 5088 S 0.0 5.5 0:02.07 elinks-new
961 update_status();
963 /* Check if the newly inserted session is the only in the list and do
964 * the special setup for the first session, */
965 if (!list_is_singleton(sessions) || !setup_first_session(ses, uri)) {
966 setup_session(ses, uri, base_session);
969 if (!in_background)
970 switch_to_tab(term, get_tab_number(ses->tab), -1);
972 if (!term->main_menu) activate_bfu_technology(ses, -1);
973 return ses;
976 static void
977 init_remote_session(struct session *ses, enum remote_session_flags *remote_ptr,
978 struct uri *uri)
980 enum remote_session_flags remote = *remote_ptr;
982 if (remote & SES_REMOTE_CURRENT_TAB) {
983 goto_uri(ses, uri);
984 /* Mask out the current tab flag */
985 *remote_ptr = remote & ~SES_REMOTE_CURRENT_TAB;
987 /* Remote session was masked out. Open all following URIs in
988 * new tabs, */
989 if (!*remote_ptr)
990 *remote_ptr = SES_REMOTE_NEW_TAB;
992 } else if (remote & SES_REMOTE_NEW_TAB) {
993 /* FIXME: This is not perfect. Doing multiple -remote
994 * with no URLs will make the goto dialogs
995 * inaccessible. Maybe we should not support this kind
996 * of thing or make the window focus detecting code
997 * more intelligent. --jonas */
998 open_uri_in_new_tab(ses, uri, 0, 0);
1000 if (remote & SES_REMOTE_PROMPT_URL) {
1001 dialog_goto_url_open(ses);
1004 } else if (remote & SES_REMOTE_NEW_WINDOW) {
1005 /* FIXME: It is quite rude because we just take the first
1006 * possibility and should maybe make it possible to specify
1007 * new-screen etc via -remote "openURL(..., new-*)" --jonas */
1008 if (!can_open_in_new(ses->tab->term))
1009 return;
1011 open_uri_in_new_window(ses, uri, NULL,
1012 ses->tab->term->environment,
1013 CACHE_MODE_NORMAL, TASK_NONE);
1015 } else if (remote & SES_REMOTE_ADD_BOOKMARK) {
1016 #ifdef CONFIG_BOOKMARKS
1017 int uri_cp;
1019 if (!uri) return;
1020 /** @todo Bug 1066: What is the encoding of struri()?
1021 * This code currently assumes the system charset.
1022 * It might be best to keep URIs in plain ASCII and
1023 * then have a function that reversibly converts them
1024 * to IRIs for display in a given encoding. */
1025 uri_cp = get_cp_index("System");
1026 add_bookmark_cp(NULL, 1, uri_cp, struri(uri), struri(uri));
1027 #endif
1029 } else if (remote & SES_REMOTE_INFO_BOX) {
1030 unsigned char *text;
1032 if (!uri) return;
1034 text = memacpy(uri->data, uri->datalen);
1035 if (!text) return;
1037 info_box(ses->tab->term, MSGBOX_FREE_TEXT,
1038 N_("Error"), ALIGN_CENTER,
1039 text);
1041 } else if (remote & SES_REMOTE_PROMPT_URL) {
1042 dialog_goto_url_open(ses);
1044 } else if (remote & SES_REMOTE_RELOAD) {
1045 reload(ses, CACHE_MODE_FORCE_RELOAD);
1051 struct string *
1052 encode_session_info(struct string *info,
1053 LIST_OF(struct string_list_item) *url_list)
1055 struct string_list_item *url;
1057 if (!init_string(info)) return NULL;
1059 foreach (url, *url_list) {
1060 struct string *str = &url->string;
1062 add_bytes_to_string(info, str->source, str->length + 1);
1065 return info;
1068 /** Older elinks versions (up to and including 0.9.1) sends no magic variable and if
1069 * this is detected we fallback to the old session info format. For this format
1070 * the magic member of terminal_info hold the length of the URI string. The
1071 * old format is handled by the default label in the switch.
1073 * The new session info format supports extraction of multiple URIS from the
1074 * terminal_info data member. The magic variable controls how to interpret
1075 * the fields:
1077 * - INTERLINK_NORMAL_MAGIC means use the terminal_info session_info
1078 * variable as an id for a saved session.
1080 * - INTERLINK_REMOTE_MAGIC means use the terminal_info session_info
1081 * variable as the remote session flags. */
1083 decode_session_info(struct terminal *term, struct terminal_info *info)
1085 int len = info->length;
1086 struct session *base_session = NULL;
1087 enum remote_session_flags remote = 0;
1088 unsigned char *str;
1090 switch (info->magic) {
1091 case INTERLINK_NORMAL_MAGIC:
1092 /* Lookup if there are any saved sessions that should be
1093 * resumed using the session_info as an id. The id is derived
1094 * from the value of -base-session command line option in the
1095 * connecting instance.
1097 * At the moment it is only used when opening instances in new
1098 * window to figure out how to initialize it when the new
1099 * instance connects to the master.
1101 * We don't need to handle it for the old format since new
1102 * instances connecting this way will always be of the same
1103 * origin as the master. */
1104 if (init_saved_session(term, info->session_info))
1105 return 1;
1106 break;
1108 case INTERLINK_REMOTE_MAGIC:
1109 /* This could mean some fatal bug but I am unsure if we can
1110 * actually assert it since people could pour all kinds of crap
1111 * down the socket. */
1112 if (!info->session_info) {
1113 INTERNAL("Remote magic with no remote flags");
1114 return 0;
1117 remote = info->session_info;
1119 /* If processing session info from a -remote instance we want
1120 * to hook up with the master so we can handle request for
1121 * stuff in current tab. */
1122 base_session = get_master_session();
1123 if (!base_session) return 0;
1125 break;
1127 default:
1128 /* The old format calculates the session_info and magic members
1129 * as part of the data that should be decoded so we have to
1130 * substract it to get the size of the actual URI data. */
1131 len -= sizeof(info->session_info) + sizeof(info->magic);
1133 /* Extract URI containing @magic bytes */
1134 if (info->magic <= 0 || info->magic > len)
1135 len = 0;
1136 else
1137 len = info->magic;
1140 if (len <= 0) {
1141 if (!remote)
1142 return !!init_session(base_session, term, NULL, 0);
1144 /* Even though there are no URIs we still have to
1145 * handle remote stuff. */
1146 init_remote_session(base_session, &remote, NULL);
1147 return 0;
1150 str = info->data;
1152 /* Extract multiple (possible) NUL terminated URIs */
1153 while (len > 0) {
1154 unsigned char *end = memchr(str, 0, len);
1155 int urilength = end ? end - str : len;
1156 struct uri *uri = NULL;
1157 unsigned char *uristring = memacpy(str, urilength);
1159 if (uristring) {
1160 uri = get_hooked_uri(uristring, base_session, term->cwd);
1161 mem_free(uristring);
1164 len -= urilength + 1;
1165 str += urilength + 1;
1167 if (remote) {
1168 if (!uri) continue;
1170 init_remote_session(base_session, &remote, uri);
1172 } else {
1173 int backgrounded = !list_empty(term->windows);
1174 int bad_url = !uri;
1175 struct session *ses;
1177 if (!uri)
1178 uri = get_uri("about:blank", 0);
1180 ses = init_session(base_session, term, uri, backgrounded);
1181 if (!ses) {
1182 /* End loop if initialization fails */
1183 len = 0;
1184 } else if (bad_url) {
1185 print_error_dialog(ses, connection_state(S_BAD_URL),
1186 NULL, PRI_MAIN);
1191 if (uri) done_uri(uri);
1194 /* If we only initialized remote sessions or didn't manage to add any
1195 * new tabs return zero so the terminal will be destroyed ASAP. */
1196 return remote ? 0 : !list_empty(term->windows);
1200 void
1201 abort_loading(struct session *ses, int interrupt)
1203 if (have_location(ses)) {
1204 struct location *loc = cur_loc(ses);
1206 cancel_download(&loc->download, interrupt);
1207 abort_files_load(ses, interrupt);
1209 abort_preloading(ses, interrupt);
1212 static void
1213 destroy_session(struct session *ses)
1215 struct document_view *doc_view;
1217 assert(ses);
1218 if_assert_failed return;
1220 #ifdef CONFIG_SCRIPTING_SPIDERMONKEY
1221 smjs_detach_session_object(ses);
1222 #endif
1223 destroy_downloads(ses);
1224 abort_loading(ses, 0);
1225 free_files(ses);
1226 if (ses->doc_view) {
1227 detach_formatted(ses->doc_view);
1228 mem_free(ses->doc_view);
1231 foreach (doc_view, ses->scrn_frames)
1232 detach_formatted(doc_view);
1234 free_list(ses->scrn_frames);
1236 destroy_history(&ses->history);
1237 set_session_referrer(ses, NULL);
1239 if (ses->loading_uri) done_uri(ses->loading_uri);
1241 kill_timer(&ses->display_timer);
1243 while (!list_empty(ses->type_queries))
1244 done_type_query(ses->type_queries.next);
1246 if (ses->download_uri) done_uri(ses->download_uri);
1247 mem_free_if(ses->search_word);
1248 mem_free_if(ses->last_search_word);
1249 mem_free_if(ses->status.last_title);
1250 #ifdef CONFIG_ECMASCRIPT
1251 mem_free_if(ses->status.window_status);
1252 #endif
1253 if (ses->option) {
1254 delete_option(ses->option);
1255 ses->option = NULL;
1257 del_from_list(ses);
1260 void
1261 reload(struct session *ses, enum cache_mode cache_mode)
1263 reload_frame(ses, NULL, cache_mode);
1266 void
1267 reload_frame(struct session *ses, unsigned char *name,
1268 enum cache_mode cache_mode)
1270 abort_loading(ses, 0);
1272 if (cache_mode == CACHE_MODE_INCREMENT) {
1273 cache_mode = CACHE_MODE_NEVER;
1274 if (ses->reloadlevel < CACHE_MODE_NEVER)
1275 cache_mode = ++ses->reloadlevel;
1276 } else {
1277 ses->reloadlevel = cache_mode;
1280 if (have_location(ses)) {
1281 struct location *loc = cur_loc(ses);
1282 struct file_to_load *ftl;
1284 #ifdef CONFIG_ECMASCRIPT
1285 loc->vs.ecmascript_fragile = 1;
1286 #endif
1288 /* FIXME: When reloading use loading_callback and set up a
1289 * session task so that the reloading will work even when the
1290 * reloaded document contains redirects. This is needed atleast
1291 * when reloading HTTP auth document after the user has entered
1292 * credentials. */
1293 loc->download.data = ses;
1294 loc->download.callback = (download_callback_T *) doc_loading_callback;
1296 mem_free_set(&ses->task.target.frame, null_or_stracpy(name));
1298 load_uri(loc->vs.uri, ses->referrer, &loc->download, PRI_MAIN, cache_mode, -1);
1300 foreach (ftl, ses->more_files) {
1301 if (file_to_load_is_active(ftl))
1302 continue;
1304 ftl->download.data = ftl;
1305 ftl->download.callback = (download_callback_T *) file_loading_callback;
1307 load_additional_file(ftl, cache_mode);
1313 struct frame *
1314 ses_find_frame(struct session *ses, unsigned char *name)
1316 struct location *loc = cur_loc(ses);
1317 struct frame *frame;
1319 assertm(have_location(ses), "ses_request_frame: no location yet");
1320 if_assert_failed return NULL;
1322 foreachback (frame, loc->frames)
1323 if (!c_strcasecmp(frame->name, name))
1324 return frame;
1326 return NULL;
1329 void
1330 set_session_referrer(struct session *ses, struct uri *referrer)
1332 if (ses->referrer) done_uri(ses->referrer);
1333 ses->referrer = referrer ? get_uri_reference(referrer) : NULL;
1336 static void
1337 tabwin_func(struct window *tab, struct term_event *ev)
1339 struct session *ses = tab->data;
1341 switch (ev->ev) {
1342 case EVENT_ABORT:
1343 if (ses) destroy_session(ses);
1344 if (!list_empty(sessions)) update_status();
1345 break;
1346 case EVENT_INIT:
1347 case EVENT_RESIZE:
1348 tab->resize = 1;
1349 /* fall-through */
1350 case EVENT_REDRAW:
1351 if (!ses || ses->tab != get_current_tab(ses->tab->term))
1352 break;
1354 draw_formatted(ses, tab->resize);
1355 if (tab->resize) {
1356 load_frames(ses, ses->doc_view);
1357 process_file_requests(ses);
1358 tab->resize = 0;
1360 print_screen_status(ses);
1361 break;
1362 case EVENT_KBD:
1363 case EVENT_MOUSE:
1364 if (!ses) break;
1365 /* This check is related to bug 296 */
1366 assert(ses->tab == get_current_tab(ses->tab->term));
1367 send_event(ses, ev);
1368 break;
1373 * Gets the url being viewed by this session. Writes it into @a str.
1374 * A maximum of @a str_size bytes (including null) will be written.
1375 * @relates session
1377 unsigned char *
1378 get_current_url(struct session *ses, unsigned char *str, size_t str_size)
1380 struct uri *uri;
1381 int length;
1383 assert(str && str_size > 0);
1385 uri = have_location(ses) ? cur_loc(ses)->vs.uri : ses->loading_uri;
1387 /* Not looking or loading anything */
1388 if (!uri) return NULL;
1390 /* Ensure that the url size is not greater than str_size.
1391 * We can't just happily strncpy(str, here, str_size)
1392 * because we have to stop at POST_CHAR, not only at NULL. */
1393 length = int_min(get_real_uri_length(uri), str_size - 1);
1395 return safe_strncpy(str, struri(uri), length + 1);
1399 * Gets the title of the page being viewed by this session. Writes it into
1400 * @a str. A maximum of @a str_size bytes (including null) will be written.
1401 * @relates session
1403 unsigned char *
1404 get_current_title(struct session *ses, unsigned char *str, size_t str_size)
1406 struct document_view *doc_view = current_frame(ses);
1408 assert(str && str_size > 0);
1410 /* Ensure that the title is defined */
1411 /* TODO: Try globhist --jonas */
1412 if (doc_view && doc_view->document->title)
1413 return safe_strncpy(str, doc_view->document->title, str_size);
1415 return NULL;
1419 * Gets the url of the link currently selected. Writes it into @a str.
1420 * A maximum of @a str_size bytes (including null) will be written.
1421 * @relates session
1423 unsigned char *
1424 get_current_link_url(struct session *ses, unsigned char *str, size_t str_size)
1426 struct link *link = get_current_session_link(ses);
1428 assert(str && str_size > 0);
1430 if (!link) return NULL;
1432 return safe_strncpy(str, link->where ? link->where : link->where_img, str_size);
1435 /** get_current_link_name: returns the name of the current link
1436 * (the text between @<A> and @</A>), @a str is a preallocated string,
1437 * @a str_size includes the null char.
1438 * @relates session */
1439 unsigned char *
1440 get_current_link_name(struct session *ses, unsigned char *str, size_t str_size)
1442 struct link *link = get_current_session_link(ses);
1443 unsigned char *where, *name = NULL;
1445 assert(str && str_size > 0);
1447 if (!link) return NULL;
1449 where = link->where ? link->where : link->where_img;
1450 #ifdef CONFIG_GLOBHIST
1452 struct global_history_item *item;
1454 item = get_global_history_item(where);
1455 if (item) name = item->title;
1457 #endif
1458 if (!name) name = get_link_name(link);
1459 if (!name) name = where;
1461 return safe_strncpy(str, name, str_size);
1464 struct link *
1465 get_current_link_in_view(struct document_view *doc_view)
1467 struct link *link = get_current_link(doc_view);
1469 return link && !link_is_form(link) ? link : NULL;
1472 /** @relates session */
1473 struct link *
1474 get_current_session_link(struct session *ses)
1476 return get_current_link_in_view(current_frame(ses));
1479 /** @relates session */
1481 eat_kbd_repeat_count(struct session *ses)
1483 int count = ses->kbdprefix.repeat_count;
1485 set_kbd_repeat_count(ses, 0);
1487 /* Clear status bar when prefix is eaten (bug 930) */
1488 print_screen_status(ses);
1489 return count;
1492 /** @relates session */
1494 set_kbd_repeat_count(struct session *ses, int new_count)
1496 int old_count = ses->kbdprefix.repeat_count;
1498 if (new_count == old_count)
1499 return old_count;
1501 ses->kbdprefix.repeat_count = new_count;
1503 /* Update the status bar. */
1504 print_screen_status(ses);
1506 /* Clear the old link highlighting. */
1507 draw_formatted(ses, 0);
1509 if (new_count != 0) {
1510 struct document_view *doc_view = current_frame(ses);
1512 highlight_links_with_prefixes_that_start_with_n(ses->tab->term,
1513 doc_view,
1514 new_count);
1517 return new_count;