Python: Give goto_url_hook only one argument, like follow_url_hook.
[elinks.git] / src / protocol / protocol.c
blob3ffca54c59921b2b1642b37c310284f38d4b472a
1 /* Protocol implementation manager. */
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
7 #include <stdlib.h>
8 #include <string.h>
10 #include "elinks.h"
12 #include "bfu/dialog.h"
13 #include "document/view.h"
14 #include "ecmascript/ecmascript.h"
15 #include "intl/gettext/libintl.h"
16 #include "main/module.h"
17 #include "network/connection.h"
18 #include "protocol/protocol.h"
19 #include "protocol/uri.h"
20 #include "session/session.h"
21 #include "terminal/terminal.h"
22 #include "terminal/window.h"
23 #include "util/memory.h"
24 #include "util/string.h"
26 /* Backends dynamic area: */
28 #include "protocol/about.h"
29 #include "protocol/auth/auth.h"
30 #include "protocol/bittorrent/bittorrent.h"
31 #include "protocol/bittorrent/connection.h"
32 #include "protocol/data.h"
33 #include "protocol/file/cgi.h"
34 #include "protocol/file/file.h"
35 #include "protocol/finger/finger.h"
36 #include "protocol/fsp/fsp.h"
37 #include "protocol/ftp/ftp.h"
38 #include "protocol/gopher/gopher.h"
39 #include "protocol/http/http.h"
40 #include "protocol/nntp/connection.h"
41 #include "protocol/nntp/nntp.h"
42 #include "protocol/rewrite/rewrite.h"
43 #include "protocol/smb/smb.h"
44 #include "protocol/user.h"
47 struct protocol_backend {
48 unsigned char *name;
49 int port;
50 protocol_handler_T *handler;
51 unsigned int need_slashes:1;
52 unsigned int need_slash_after_host:1;
53 unsigned int free_syntax:1;
54 unsigned int need_ssl:1;
57 static const struct protocol_backend protocol_backends[] = {
58 { "about", 0, about_protocol_handler, 0, 0, 1, 0 },
59 { "bittorrent", 0, bittorrent_protocol_handler, 0, 0, 1, 0 },
60 { "data", 0, data_protocol_handler, 0, 0, 1, 0 },
61 { "file", 0, file_protocol_handler, 1, 0, 0, 0 },
62 { "finger", 79, finger_protocol_handler, 1, 1, 0, 0 },
63 { "fsp", 21, fsp_protocol_handler, 1, 1, 0, 0 },
64 { "ftp", 21, ftp_protocol_handler, 1, 1, 0, 0 },
65 { "gopher", 70, gopher_protocol_handler, 1, 1, 0, 0 },
66 { "http", 80, http_protocol_handler, 1, 1, 0, 0 },
67 { "https", 443, https_protocol_handler, 1, 1, 0, 1 },
68 { "javascript", 0, NULL, 0, 0, 1, 0 },
69 { "news", 0, news_protocol_handler, 0, 0, 1, 0 },
70 { "nntp", 119, nntp_protocol_handler, 1, 1, 0, 0 },
71 { "nntps", 563, nntp_protocol_handler, 1, 1, 0, 1 },
72 { "proxy", 3128, proxy_protocol_handler, 1, 1, 0, 0 },
73 { "smb", 139, smb_protocol_handler, 1, 1, 0, 0 },
74 { "snews", 0, news_protocol_handler, 0, 0, 1, 0 },
76 /* Keep these last! */
77 { NULL, 0, NULL, 0, 0, 1, 0 },
79 { "user", 0, NULL, 0, 0, 0, 0 },
80 /* Internal protocol for mapping to protocol.user.* handlers. Placed
81 * last because it's checked first and else should be ignored. */
82 { "custom", 0, NULL, 0, 0, 1, 0 },
86 /* This function gets called quite a lot these days. With incremental rendering
87 * and all I counted 4400 calls alone when loading fm. With the old linear
88 * comparison this would lead to 30800 comparison against protocol names. The
89 * binary search used currently reduces it to 4400 (meaning fm only has HTTP
90 * links). */
92 enum protocol
93 get_protocol(unsigned char *name, int namelen)
95 /* These are really enum protocol values but can take on negative
96 * values and since 0 <= -1 for enum values it's better to use clean
97 * integer type. */
98 int start, end;
99 enum protocol protocol;
101 /* Almost dichotomic search is used here */
102 /* Starting at the HTTP entry which is the most common that will make
103 * file and NNTP the next entries checked and amongst the third checks
104 * are proxy and FTP. */
105 start = 0;
106 end = PROTOCOL_UNKNOWN - 1;
107 protocol = PROTOCOL_HTTP;
109 assert(start <= protocol && protocol <= end);
111 while (start <= end) {
112 unsigned char *pname = protocol_backends[protocol].name;
113 int pnamelen = strlen(pname);
114 int minlen = int_min(pnamelen, namelen);
115 int compare = strncasecmp(pname, name, minlen);
117 if (compare == 0) {
118 if (pnamelen == namelen)
119 return protocol;
121 /* If the current protocol name is longer than the
122 * protocol name being searched for move @end else move
123 * @start. */
124 compare = pnamelen > namelen ? 1 : -1;
127 if (compare > 0)
128 end = protocol - 1;
129 else
130 start = protocol + 1;
132 protocol = (start + end) / 2;
134 /* Custom (protocol.user) protocol has higher precedence than builtin
135 * handlers, but we will check for it when following a link.
136 * Calling get_user_program for every link is too expensive. --witekfl */
137 /* TODO: In order to fully give higher precedence to user chosen
138 * protocols we have to get some terminal to pass along. */
140 if (get_user_program(NULL, name, namelen))
141 return PROTOCOL_USER;
143 return PROTOCOL_UNKNOWN;
147 #define VALID_PROTOCOL(p) (0 <= (p) && (p) < PROTOCOL_BACKENDS)
150 get_protocol_port(enum protocol protocol)
152 assert(VALID_PROTOCOL(protocol));
153 if_assert_failed return 0;
155 assert(uri_port_is_valid(protocol_backends[protocol].port));
156 if_assert_failed return 0;
158 return protocol_backends[protocol].port;
162 get_protocol_need_slashes(enum protocol protocol)
164 assert(VALID_PROTOCOL(protocol));
165 if_assert_failed return 0;
166 return protocol_backends[protocol].need_slashes;
170 get_protocol_need_slash_after_host(enum protocol protocol)
172 assert(VALID_PROTOCOL(protocol));
173 if_assert_failed return 0;
174 return protocol_backends[protocol].need_slash_after_host;
178 get_protocol_free_syntax(enum protocol protocol)
180 assert(VALID_PROTOCOL(protocol));
181 if_assert_failed return 0;
182 return protocol_backends[protocol].free_syntax;
186 get_protocol_need_ssl(enum protocol protocol)
188 assert(VALID_PROTOCOL(protocol));
189 if_assert_failed return 0;
190 return protocol_backends[protocol].need_ssl;
193 protocol_handler_T *
194 get_protocol_handler(enum protocol protocol)
196 assert(VALID_PROTOCOL(protocol));
197 if_assert_failed return NULL;
198 return protocol_backends[protocol].handler;
202 static void
203 generic_external_protocol_handler(struct session *ses, struct uri *uri)
205 /* [gettext_accelerator_context(generic_external_protocol_handler)] */
206 enum connection_state state;
208 switch (uri->protocol) {
209 case PROTOCOL_JAVASCRIPT:
210 #ifdef CONFIG_ECMASCRIPT
211 ecmascript_protocol_handler(ses, uri);
212 return;
213 #else
214 state = S_NO_JAVASCRIPT;
215 #endif
216 break;
218 case PROTOCOL_UNKNOWN:
219 state = S_UNKNOWN_PROTOCOL;
220 break;
222 default:
223 #ifndef CONFIG_SSL
224 if (get_protocol_need_ssl(uri->protocol)) {
225 state = S_SSL_ERROR;
226 break;
228 #endif
229 msg_box(ses->tab->term, NULL, MSGBOX_FREE_TEXT,
230 N_("Error"), ALIGN_CENTER,
231 msg_text(ses->tab->term,
232 N_("This version of ELinks does not contain "
233 "%s protocol support"),
234 protocol_backends[uri->protocol].name),
235 ses, 1,
236 N_("~OK"), NULL, B_ENTER | B_ESC);
237 return;
240 print_error_dialog(ses, state, uri, PRI_CANCEL);
243 protocol_external_handler_T *
244 get_protocol_external_handler(struct terminal *term, struct uri *uri)
246 unsigned char *prog;
248 assert(uri && VALID_PROTOCOL(uri->protocol));
249 if_assert_failed return NULL;
251 prog = get_user_program(term, struri(uri), uri->protocollen);
252 if (prog && *prog)
253 return user_protocol_handler;
255 if (!protocol_backends[uri->protocol].handler)
256 return generic_external_protocol_handler;
258 return NULL;
262 static struct option_info protocol_options[] = {
263 INIT_OPT_TREE("", N_("Protocols"),
264 "protocol", OPT_SORT,
265 N_("Protocol specific options.")),
267 INIT_OPT_STRING("protocol", N_("No-proxy domains"),
268 "no_proxy", 0, "",
269 N_("Comma separated list of domains for which the proxy (HTTP/FTP)\n"
270 "should be disabled. Optionally, a port can be specified for some\n"
271 "domains as well. If it's blank, NO_PROXY environment variable is\n"
272 "checked as well.")),
274 NULL_OPTION_INFO,
276 static struct module *protocol_submodules[] = {
277 &auth_module,
278 #ifdef CONFIG_BITTORRENT
279 &bittorrent_protocol_module,
280 #endif
281 &file_protocol_module,
282 #ifdef CONFIG_CGI
283 &cgi_protocol_module,
284 #endif
285 #ifdef CONFIG_FINGER
286 &finger_protocol_module,
287 #endif
288 #ifdef CONFIG_FSP
289 &fsp_protocol_module,
290 #endif
291 #ifdef CONFIG_FTP
292 &ftp_protocol_module,
293 #endif
294 #ifdef CONFIG_GOPHER
295 &gopher_protocol_module,
296 #endif
297 &http_protocol_module,
298 #ifdef CONFIG_NNTP
299 &nntp_protocol_module,
300 #endif
301 #ifdef CONFIG_SMB
302 &smb_protocol_module,
303 #endif
304 #ifdef CONFIG_URI_REWRITE
305 &uri_rewrite_module,
306 #endif
307 &user_protocol_module,
308 NULL,
311 struct module protocol_module = struct_module(
312 /* name: */ N_("Protocol"),
313 /* options: */ protocol_options,
314 /* hooks: */ NULL,
315 /* submodules: */ protocol_submodules,
316 /* data: */ NULL,
317 /* init: */ NULL,
318 /* done: */ NULL