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;
55 unsigned int keep_double_slashes
:1;
58 static const struct protocol_backend protocol_backends
[] = {
59 { "about", 0, about_protocol_handler
, 0, 0, 1, 0, 1 },
60 { "bittorrent", 0, bittorrent_protocol_handler
, 0, 0, 1, 0, 1 },
61 { "data", 0, data_protocol_handler
, 0, 0, 1, 0, 1 },
62 { "file", 0, file_protocol_handler
, 1, 0, 0, 0, 0 },
63 { "finger", 79, finger_protocol_handler
, 1, 1, 0, 0, 1 },
64 { "fsp", 21, fsp_protocol_handler
, 1, 1, 0, 0, 1 },
65 { "ftp", 21, ftp_protocol_handler
, 1, 1, 0, 0, 0 },
66 { "gopher", 70, gopher_protocol_handler
, 1, 1, 0, 0, 1 },
67 { "http", 80, http_protocol_handler
, 1, 1, 0, 0, 1 },
68 { "https", 443, https_protocol_handler
, 1, 1, 0, 1, 1 },
69 { "javascript", 0, NULL
, 0, 0, 1, 0, 1 },
70 { "news", 0, news_protocol_handler
, 0, 0, 1, 0, 1 },
71 { "nntp", 119, nntp_protocol_handler
, 1, 1, 0, 0, 0 },
72 { "nntps", 563, nntp_protocol_handler
, 1, 1, 0, 1, 0 },
73 { "proxy", 3128, proxy_protocol_handler
, 1, 1, 0, 0, 1 },
74 { "smb", 139, smb_protocol_handler
, 1, 1, 0, 0, 1 },
75 { "snews", 0, news_protocol_handler
, 0, 0, 1, 0, 1 },
77 /* Keep these last! */
78 { NULL
, 0, NULL
, 0, 0, 1, 0, 1 },
80 { "user", 0, NULL
, 0, 0, 0, 0, 1 },
81 /* Internal protocol for mapping to protocol.user.* handlers. Placed
82 * last because it's checked first and else should be ignored. */
83 { "custom", 0, NULL
, 0, 0, 1, 0, 1 },
87 /* This function gets called quite a lot these days. With incremental rendering
88 * and all I counted 4400 calls alone when loading fm. With the old linear
89 * comparison this would lead to 30800 comparison against protocol names. The
90 * binary search used currently reduces it to 4400 (meaning fm only has HTTP
94 get_protocol(unsigned char *name
, int namelen
)
96 /* These are really enum protocol values but can take on negative
97 * values and since 0 <= -1 for enum values it's better to use clean
100 enum protocol protocol
;
102 /* Almost dichotomic search is used here */
103 /* Starting at the HTTP entry which is the most common that will make
104 * file and NNTP the next entries checked and amongst the third checks
105 * are proxy and FTP. */
107 end
= PROTOCOL_UNKNOWN
- 1;
108 protocol
= PROTOCOL_HTTP
;
110 assert(start
<= protocol
&& protocol
<= end
);
112 while (start
<= end
) {
113 unsigned char *pname
= protocol_backends
[protocol
].name
;
114 int pnamelen
= strlen(pname
);
115 int minlen
= int_min(pnamelen
, namelen
);
116 int compare
= strncasecmp(pname
, name
, minlen
);
119 if (pnamelen
== namelen
)
122 /* If the current protocol name is longer than the
123 * protocol name being searched for move @end else move
125 compare
= pnamelen
> namelen
? 1 : -1;
131 start
= protocol
+ 1;
133 protocol
= (start
+ end
) / 2;
135 /* Custom (protocol.user) protocol has higher precedence than builtin
136 * handlers, but we will check for it when following a link.
137 * Calling get_user_program for every link is too expensive. --witekfl */
138 /* TODO: In order to fully give higher precedence to user chosen
139 * protocols we have to get some terminal to pass along. */
141 if (get_user_program(NULL
, name
, namelen
))
142 return PROTOCOL_USER
;
144 return PROTOCOL_UNKNOWN
;
148 #define VALID_PROTOCOL(p) (0 <= (p) && (p) < PROTOCOL_BACKENDS)
151 get_protocol_port(enum protocol protocol
)
153 assert(VALID_PROTOCOL(protocol
));
154 if_assert_failed
return 0;
156 assert(uri_port_is_valid(protocol_backends
[protocol
].port
));
157 if_assert_failed
return 0;
159 return protocol_backends
[protocol
].port
;
163 get_protocol_need_slashes(enum protocol protocol
)
165 assert(VALID_PROTOCOL(protocol
));
166 if_assert_failed
return 0;
167 return protocol_backends
[protocol
].need_slashes
;
171 get_protocol_need_slash_after_host(enum protocol protocol
)
173 assert(VALID_PROTOCOL(protocol
));
174 if_assert_failed
return 0;
175 return protocol_backends
[protocol
].need_slash_after_host
;
179 get_protocol_keep_double_slashes(enum protocol protocol
)
181 assert(VALID_PROTOCOL(protocol
));
182 if_assert_failed
return 0;
183 return protocol_backends
[protocol
].keep_double_slashes
;
187 get_protocol_free_syntax(enum protocol protocol
)
189 assert(VALID_PROTOCOL(protocol
));
190 if_assert_failed
return 0;
191 return protocol_backends
[protocol
].free_syntax
;
195 get_protocol_need_ssl(enum protocol protocol
)
197 assert(VALID_PROTOCOL(protocol
));
198 if_assert_failed
return 0;
199 return protocol_backends
[protocol
].need_ssl
;
203 get_protocol_handler(enum protocol protocol
)
205 assert(VALID_PROTOCOL(protocol
));
206 if_assert_failed
return NULL
;
207 return protocol_backends
[protocol
].handler
;
212 generic_external_protocol_handler(struct session
*ses
, struct uri
*uri
)
214 /* [gettext_accelerator_context(generic_external_protocol_handler)] */
215 enum connection_state state
;
217 switch (uri
->protocol
) {
218 case PROTOCOL_JAVASCRIPT
:
219 #ifdef CONFIG_ECMASCRIPT
220 ecmascript_protocol_handler(ses
, uri
);
223 state
= S_NO_JAVASCRIPT
;
227 case PROTOCOL_UNKNOWN
:
228 state
= S_UNKNOWN_PROTOCOL
;
233 if (get_protocol_need_ssl(uri
->protocol
)) {
238 msg_box(ses
->tab
->term
, NULL
, MSGBOX_FREE_TEXT
,
239 N_("Error"), ALIGN_CENTER
,
240 msg_text(ses
->tab
->term
,
241 N_("This version of ELinks does not contain "
242 "%s protocol support"),
243 protocol_backends
[uri
->protocol
].name
),
245 MSG_BOX_BUTTON(N_("~OK"), NULL
, B_ENTER
| B_ESC
));
249 print_error_dialog(ses
, state
, uri
, PRI_CANCEL
);
252 protocol_external_handler_T
*
253 get_protocol_external_handler(struct terminal
*term
, struct uri
*uri
)
257 assert(uri
&& VALID_PROTOCOL(uri
->protocol
));
258 if_assert_failed
return NULL
;
260 prog
= get_user_program(term
, struri(uri
), uri
->protocollen
);
262 return user_protocol_handler
;
264 if (!protocol_backends
[uri
->protocol
].handler
)
265 return generic_external_protocol_handler
;
271 static struct option_info protocol_options
[] = {
272 INIT_OPT_TREE("", N_("Protocols"),
273 "protocol", OPT_SORT
,
274 N_("Protocol specific options.")),
276 INIT_OPT_STRING("protocol", N_("No-proxy domains"),
278 N_("Comma separated list of domains for which the proxy (HTTP/FTP)\n"
279 "should be disabled. Optionally, a port can be specified for some\n"
280 "domains as well. If it's blank, NO_PROXY environment variable is\n"
281 "checked as well.")),
285 static struct module
*protocol_submodules
[] = {
287 #ifdef CONFIG_BITTORRENT
288 &bittorrent_protocol_module
,
290 &file_protocol_module
,
292 &cgi_protocol_module
,
295 &finger_protocol_module
,
298 &fsp_protocol_module
,
301 &ftp_protocol_module
,
304 &gopher_protocol_module
,
306 &http_protocol_module
,
308 &nntp_protocol_module
,
311 &smb_protocol_module
,
313 #ifdef CONFIG_URI_REWRITE
316 &user_protocol_module
,
320 struct module protocol_module
= struct_module(
321 /* name: */ N_("Protocol"),
322 /* options: */ protocol_options
,
324 /* submodules: */ protocol_submodules
,