1 /* Protocol implementation manager. */
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
{
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
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
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. */
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
);
118 if (pnamelen
== namelen
)
121 /* If the current protocol name is longer than the
122 * protocol name being searched for move @end else move
124 compare
= pnamelen
> namelen
? 1 : -1;
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
;
194 get_protocol_handler(enum protocol protocol
)
196 assert(VALID_PROTOCOL(protocol
));
197 if_assert_failed
return NULL
;
198 return protocol_backends
[protocol
].handler
;
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
);
214 state
= S_NO_JAVASCRIPT
;
218 case PROTOCOL_UNKNOWN
:
219 state
= S_UNKNOWN_PROTOCOL
;
224 if (get_protocol_need_ssl(uri
->protocol
)) {
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
),
236 N_("~OK"), NULL
, B_ENTER
| B_ESC
);
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
)
248 assert(uri
&& VALID_PROTOCOL(uri
->protocol
));
249 if_assert_failed
return NULL
;
251 prog
= get_user_program(term
, struri(uri
), uri
->protocollen
);
253 return user_protocol_handler
;
255 if (!protocol_backends
[uri
->protocol
].handler
)
256 return generic_external_protocol_handler
;
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"),
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.")),
276 static struct module
*protocol_submodules
[] = {
278 #ifdef CONFIG_BITTORRENT
279 &bittorrent_protocol_module
,
281 &file_protocol_module
,
283 &cgi_protocol_module
,
286 &finger_protocol_module
,
289 &fsp_protocol_module
,
292 &ftp_protocol_module
,
295 &gopher_protocol_module
,
297 &http_protocol_module
,
299 &nntp_protocol_module
,
302 &smb_protocol_module
,
304 #ifdef CONFIG_URI_REWRITE
307 &user_protocol_module
,
311 struct module protocol_module
= struct_module(
312 /* name: */ N_("Protocol"),
313 /* options: */ protocol_options
,
315 /* submodules: */ protocol_submodules
,