1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-12-04 14:46:47 -05:00
elinks/src/protocol/protocol.cpp

346 lines
9.2 KiB
C++
Raw Normal View History

/* Protocol implementation manager. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#include "elinks.h"
#include "bfu/dialog.h"
#include "document/view.h"
#include "ecmascript/ecmascript.h"
#include "intl/libintl.h"
#include "main/module.h"
#include "network/connection.h"
#include "protocol/protocol.h"
#include "protocol/uri.h"
#include "session/session.h"
#include "terminal/terminal.h"
#include "terminal/window.h"
#include "util/memory.h"
#include "util/string.h"
/* Backends dynamic area: */
#include "protocol/about.h"
2006-05-20 08:49:51 -04:00
#include "protocol/auth/auth.h"
#include "protocol/bittorrent/bittorrent.h"
#include "protocol/bittorrent/connection.h"
#include "protocol/data.h"
2006-01-30 02:56:40 -05:00
#include "protocol/file/cgi.h"
2022-05-18 15:00:12 -04:00
#include "protocol/file/dgi.h"
#include "protocol/file/file.h"
#include "protocol/file/mailcap.h"
#include "protocol/finger/finger.h"
2006-01-16 05:40:13 -05:00
#include "protocol/fsp/fsp.h"
#include "protocol/ftp/ftp.h"
#include "protocol/gemini/gemini.h"
#include "protocol/gopher/gopher.h"
#include "protocol/http/http.h"
#include "protocol/nntp/connection.h"
#include "protocol/nntp/nntp.h"
#include "protocol/rewrite/rewrite.h"
#include "protocol/smb/smb.h"
#include "protocol/user.h"
struct protocol_backend {
2022-01-30 09:50:29 -05:00
const char *name;
int port;
protocol_handler_T *handler;
unsigned int need_slashes:1;
unsigned int need_slash_after_host:1;
unsigned int free_syntax:1;
unsigned int need_ssl:1;
unsigned int keep_double_slashes:1;
};
static const struct protocol_backend protocol_backends[] = {
{ "about", 0, about_protocol_handler, 0, 0, 1, 0, 1 },
{ "bittorrent", 0, bittorrent_protocol_handler, 0, 0, 1, 0, 1 },
Fix blacklist crash in BitTorrent make_bittorrent_peer_connection() used to construct a struct uri on the stack. This was hacky but worked nicely because the struct uri was not really accessed after make_connection() returned. However, since commit a83ff1f565a4a7bc25a4b8353ee26bc1b97410e3, the struct uri is also needed when the connection is being closed. Valgrind shows: Invalid read of size 2 at 0x8100764: get_blacklist_entry (blacklist.c:33) by 0x8100985: del_blacklist_entry (blacklist.c:64) by 0x80DA579: complete_connect_socket (socket.c:448) by 0x80DA84A: connected (socket.c:513) by 0x80D0DDF: select_loop (select.c:297) by 0x80D00C6: main (main.c:353) Address 0xBEC3BFAE is just below the stack ptr. To suppress, use: --workaround-gcc296-bugs=yes To fix this, allocate the struct uri on the heap instead, by constructing a string and giving that to get_uri(). This string cannot use the "bittorrent" URI scheme because parse_uri() does not recognize the host and port fields in that. (The "bittorrent" scheme has protocol_backend.free_syntax = 1 in order to support strings like "bittorrent:http://beta.legaltorrents.com/get/159-noisome-beasts".) Instead, define a new "bittorrent-peer" URI scheme for this purpose. If the user attempts to use this URI scheme, its handler aborts the connection with an error; but when make_bittorrent_peer_connection() uses a bittorrent-peer URI, the handler is not called. This change also lets get_uri() set the ipv6 flag if peer_info->ip is an IPv6 address literal. Reported by Witold Filipczyk.
2008-09-06 23:10:52 -04:00
{ "bittorrent-peer",0,bittorrent_peer_protocol_handler, 1, 1, 0, 0, 1 },
{ "data", 0, data_protocol_handler, 0, 0, 1, 0, 1 },
{ "dgi", 0, dgi_protocol_handler, 0, 0, 0, 0, 0 },
{ "file", 0, file_protocol_handler, 1, 0, 0, 0, 0 },
{ "finger", 79, finger_protocol_handler, 1, 1, 0, 0, 1 },
{ "fsp", 21, fsp_protocol_handler, 1, 1, 0, 0, 1 },
{ "ftp", 21, ftp_protocol_handler, 1, 1, 0, 0, 0 },
{ "gemini", 1965, gemini_protocol_handler, 1, 1, 0, 1, 1 },
{ "gopher", 70, gopher_protocol_handler, 1, 1, 0, 0, 1 },
{ "http", 80, http_protocol_handler, 1, 1, 0, 0, 1 },
{ "https", 443, https_protocol_handler, 1, 1, 0, 1, 1 },
{ "javascript", 0, NULL, 0, 0, 1, 0, 1 },
2010-07-27 04:07:52 -04:00
{ "mailcap", 0, mailcap_protocol_handler, 0, 0, 1, 0, 0 },
{ "news", 0, news_protocol_handler, 0, 0, 1, 0, 1 },
{ "nntp", 119, nntp_protocol_handler, 1, 1, 0, 0, 0 },
{ "nntps", 563, nntp_protocol_handler, 1, 1, 0, 1, 0 },
{ "proxy", 3128, proxy_protocol_handler, 1, 1, 0, 0, 1 },
{ "smb", 139, smb_protocol_handler, 1, 1, 0, 0, 1 },
{ "snews", 0, news_protocol_handler, 0, 0, 1, 0, 1 },
/* Keep these last! */
{ NULL, 0, NULL, 0, 0, 1, 0, 1 },
{ "user", 0, NULL, 0, 0, 0, 0, 1 },
/* Internal protocol for mapping to protocol.user.* handlers. Placed
* last because it's checked first and else should be ignored. */
{ "custom", 0, NULL, 0, 0, 1, 0, 1 },
};
/* This function gets called quite a lot these days. With incremental rendering
* and all I counted 4400 calls alone when loading fm. With the old linear
* comparison this would lead to 30800 comparison against protocol names. The
* binary search used currently reduces it to 4400 (meaning fm only has HTTP
* links). */
2022-01-28 11:19:11 -05:00
protocol_T
2022-02-21 11:13:14 -05:00
get_protocol(const char *name, int namelen)
{
2022-01-28 11:19:11 -05:00
/* These are really protocol_T values but can take on negative
* values and since 0 <= -1 for enum values it's better to use clean
* integer type. */
int start, end;
2022-01-28 11:19:11 -05:00
protocol_T protocol;
/* Almost dichotomic search is used here */
/* Starting at the HTTP entry which is the most common that will make
* file and NNTP the next entries checked and amongst the third checks
* are proxy and FTP. */
start = 0;
end = PROTOCOL_UNKNOWN - 1;
protocol = PROTOCOL_HTTP;
assert(start <= protocol && protocol <= end);
while (start <= end) {
2022-01-30 09:50:29 -05:00
const char *pname = protocol_backends[protocol].name;
int pnamelen = strlen(pname);
int minlen = int_min(pnamelen, namelen);
int compare = c_strncasecmp(pname, name, minlen);
if (compare == 0) {
if (pnamelen == namelen)
return protocol;
/* If the current protocol name is longer than the
* protocol name being searched for move @end else move
* @start. */
compare = pnamelen > namelen ? 1 : -1;
}
if (compare > 0)
end = protocol - 1;
else
start = protocol + 1;
protocol = (start + end) / 2;
}
/* Custom (protocol.user) protocol has higher precedence than builtin
* handlers, but we will check for it when following a link.
* Calling get_user_program for every link is too expensive. --witekfl */
/* TODO: In order to fully give higher precedence to user chosen
* protocols we have to get some terminal to pass along. */
if (get_user_program(NULL, name, namelen))
return PROTOCOL_USER;
return PROTOCOL_UNKNOWN;
}
#define VALID_PROTOCOL(p) (0 <= (p) && (p) < PROTOCOL_BACKENDS)
int
2022-01-28 11:19:11 -05:00
get_protocol_port(protocol_T protocol)
{
assert(VALID_PROTOCOL(protocol));
if_assert_failed return 0;
assert(uri_port_is_valid(protocol_backends[protocol].port));
if_assert_failed return 0;
return protocol_backends[protocol].port;
}
int
2022-01-28 11:19:11 -05:00
get_protocol_need_slashes(protocol_T protocol)
{
assert(VALID_PROTOCOL(protocol));
if_assert_failed return 0;
return protocol_backends[protocol].need_slashes;
}
int
2022-01-28 11:19:11 -05:00
get_protocol_need_slash_after_host(protocol_T protocol)
{
assert(VALID_PROTOCOL(protocol));
if_assert_failed return 0;
return protocol_backends[protocol].need_slash_after_host;
}
int
2022-01-28 11:19:11 -05:00
get_protocol_keep_double_slashes(protocol_T protocol)
{
assert(VALID_PROTOCOL(protocol));
if_assert_failed return 0;
return protocol_backends[protocol].keep_double_slashes;
}
int
2022-01-28 11:19:11 -05:00
get_protocol_free_syntax(protocol_T protocol)
{
assert(VALID_PROTOCOL(protocol));
if_assert_failed return 0;
return protocol_backends[protocol].free_syntax;
}
int
2022-01-28 11:19:11 -05:00
get_protocol_need_ssl(protocol_T protocol)
{
assert(VALID_PROTOCOL(protocol));
if_assert_failed return 0;
return protocol_backends[protocol].need_ssl;
}
protocol_handler_T *
2022-01-28 11:19:11 -05:00
get_protocol_handler(protocol_T protocol)
{
assert(VALID_PROTOCOL(protocol));
if_assert_failed return NULL;
return protocol_backends[protocol].handler;
}
static void
generic_external_protocol_handler(struct session *ses, struct uri *uri)
{
Here is a framework that detects cases where a PO file assigns the same accelerator key to multiple buttons in a dialog box or to multiple items in a menu. ELinks already has some support for this but it requires the translator to run ELinks and manually scan through all menus and dialogs. The attached changes make it possible to quickly detect and list any conflicts, including ones that can only occur on operating systems or configurations that the translator is not currently using. The changes have no immediate effect on the elinks executable or the MO files. PO files become larger, however. The scheme works like this: - Like before, accelerator keys in translatable strings are tagged with the tilde (~) character. - Whenever a C source file defines an accelerator key, it must assign one or more named "contexts" to it. The translations in the PO files inherit these contexts. If multiple strings use the same accelerator (case insensitive) in the same context, that's a conflict and can be detected automatically. - The contexts are defined with "gettext_accelerator_context" comments in source files. These comments delimit regions where all translatable strings containing tildes are given the same contexts. There must be one special comment at the top of the region; it lists the contexts assigned to that region. The region automatically ends at the end of the function (found with regexp /^\}/), but it can also be closed explicitly with another special comment. The comments are formatted like this: /* [gettext_accelerator_context(foo, bar, baz)] begins a region that uses the contexts "foo", "bar", and "baz". The comma is the delimiter; whitespace is optional. [gettext_accelerator_context()] ends the region. */ The scripts don't currently check whether this syntax occurs inside or outside comments. - The names of contexts consist of C identifiers delimited with periods. I typically used the name of a function that sets up a dialog, or the name of an array where the items of a menu are listed. There is a special feature for static functions: if the name begins with a period, then the period will be replaced with the name of the source file and a colon. - If a menu is programmatically generated from multiple parts, of which some are never used together, so that it is safe to use the same accelerators in them, then it is necessary to define multiple contexts for the same menu. link_menu() in src/viewer/text/link.c is the most complex example of this. - During make update-po: - A Perl script (po/gather-accelerator-contexts.pl) reads po/elinks.pot, scans the source files listed in it for "gettext_accelerator_context" comments, and rewrites po/elinks.pot with "accelerator_context" comments that indicate the contexts of each msgid: the union of all contexts of all of its uses in the source files. It also removes any "gettext_accelerator_context" comments that xgettext --add-comments has copied to elinks.pot. - If po/gather-accelerator-contexts.pl does not find any contexts for some use of an msgid that seems to contain an accelerator (because it contains a tilde), it warns. If the tilde refers to e.g. "~/.elinks" and does not actually mark an accelerator, the warning can be silenced by specifying the special context "IGNORE", which the script otherwise ignores. - msgmerge copies the "accelerator_context" comments from po/elinks.pot to po/*.po. Translators do not edit those comments. - During make check-po: - Another Perl script (po/check-accelerator-contexts.pl) reads po/*.po and keeps track of which accelerators have been bound in each context. It warns about any conflicts it finds. This script does not access the C source files; thus it does not matter if the line numbers in "#:" lines are out of date. This implementation is not perfect and I am not proposing to add it to the main source tree at this time. Specifically: - It introduces compile-time dependencies on Perl and Locale::PO. There should be a configure-time or compile-time check so that the new features are skipped if the prerequisites are missing. - When the scripts include msgstr strings in warnings, they should transcode them from the charset of the PO file to the one specified by the user's locale. - It is not adequately documented (well, except perhaps here). - po/check-accelerator-contexts.pl reports the same conflict multiple times if it occurs in multiple contexts. - The warning messages should include line numbers, so that users of Emacs could conveniently edit the conflicting part of the PO file. This is not feasible with the current version of Locale::PO. - Locale::PO does not understand #~ lines and spews warnings about them. There is an ugly hack to hide these warnings. - Jonas Fonseca suggested the script could propose accelerators that are still available. This has not been implemented. There are three files attached: - po/gather-accelerator-contexts.pl: Augments elinks.pot with context information. - po/check-accelerator-contexts.pl: Checks conflicts. - accelerator-contexts.diff: Makes po/Makefile run the scripts, and adds special comments to source files.
2005-12-04 18:38:29 -05:00
/* [gettext_accelerator_context(generic_external_protocol_handler)] */
struct connection_state state;
switch (uri->protocol) {
case PROTOCOL_JAVASCRIPT:
#if defined(CONFIG_ECMASCRIPT_SMJS) || defined(CONFIG_QUICKJS) || defined(CONFIG_MUJS)
ecmascript_protocol_handler(ses, uri);
return;
#else
state = connection_state(S_NO_JAVASCRIPT);
#endif
break;
case PROTOCOL_UNKNOWN:
state = connection_state(S_UNKNOWN_PROTOCOL);
break;
default:
#ifndef CONFIG_SSL
if (get_protocol_need_ssl(uri->protocol)) {
state = connection_state(S_SSL_ERROR);
break;
}
#endif
msg_box(ses->tab->term, NULL, MSGBOX_FREE_TEXT,
N_("Error"), ALIGN_CENTER,
msg_text(ses->tab->term,
N_("This version of ELinks does not contain "
"%s protocol support"),
protocol_backends[uri->protocol].name),
ses, 1,
MSG_BOX_BUTTON(N_("~OK"), NULL, B_ENTER | B_ESC));
return;
}
print_error_dialog(ses, state, uri, PRI_CANCEL);
}
protocol_external_handler_T *
get_protocol_external_handler(struct terminal *term, struct uri *uri)
{
char *prog;
assert(uri && VALID_PROTOCOL(uri->protocol));
if_assert_failed return NULL;
prog = get_user_program(term, struri(uri), uri->protocollen);
if (prog && *prog)
return user_protocol_handler;
if (!protocol_backends[uri->protocol].handler)
return generic_external_protocol_handler;
return NULL;
}
bug 764: Initialize the right member of union option_value INIT_OPTION used to initialize union option_value at compile time by casting the default value to LIST_OF(struct option) *, which is the type of the first member. On sparc64 and other big-endian systems where sizeof(int) < sizeof(struct list_head *), this tended to leave option->value.number as zero, thus messing up OPT_INT and OPT_BOOL at least. OPT_LONG however tended to work right. This would be easy to fix with C99 designated initializers, but doc/hacking.txt says ELinks must be kept C89 compatible. Another solution would be to make register_options() read the value from option->value.tree (the first member), cast it back to the right type, and write it to the appropriate member; but that would still require somewhat dubious conversions between integers, data pointers, and function pointers. So here's a rather more invasive solution. Add struct option_init, which is somewhat similar to struct option but has non-overlapping members for different types of values, to ensure nothing is lost in compile-time conversions. Move unsigned char *path from struct option_info to struct option_init, and replace struct option_info with a union that contains struct option_init and struct option. Now, this union can be initialized with no portability problems, and register_options() then moves the values from struct option_init to their final places in struct option. In my x86 ELinks build with plenty of options configured in, this change bloated the text section by 340 bytes but compressed the data section by 2784 bytes, presumably because union option_info is a pointer smaller than struct option_info was. (cherry picked from elinks-0.12 commit e5f6592ee20780a61f70feeb1f9e17631b9c5835) Conflicts: src/protocol/fsp/fsp.c: All options had been removed in 0.13.GIT. src/protocol/smb/smb2.c: Ditto.
2009-08-15 15:39:07 -04:00
static union option_info protocol_options[] = {
INIT_OPT_TREE("", N_("Protocols"),
"protocol", OPT_SORT,
N_("Protocol specific options.")),
INIT_OPT_STRING("protocol", N_("Default protocol prefix"),
"default_protocol", OPT_ZERO, "https://",
N_("Default protocol prefix when none protocol was entered.")),
INIT_OPT_STRING("protocol", N_("No-proxy domains"),
"no_proxy", OPT_ZERO, "",
N_("Comma separated list of domains for which the proxy "
"(HTTP/FTP) should be disabled. Optionally, a port can be "
"specified for some domains as well. If it's blank, "
"NO_PROXY environment variable is checked as well.")),
NULL_OPTION_INFO,
};
static struct module *protocol_submodules[] = {
2006-05-20 08:49:51 -04:00
&auth_module,
#ifdef CONFIG_BITTORRENT
&bittorrent_protocol_module,
#endif
&file_protocol_module,
2006-01-30 02:56:40 -05:00
#ifdef CONFIG_CGI
&cgi_protocol_module,
#endif
2022-05-18 15:00:12 -04:00
#ifdef CONFIG_DGI
&dgi_protocol_module,
#endif
#ifdef CONFIG_FINGER
&finger_protocol_module,
#endif
2006-01-16 05:40:13 -05:00
#ifdef CONFIG_FSP
&fsp_protocol_module,
#endif
#ifdef CONFIG_FTP
&ftp_protocol_module,
#endif
2021-07-18 06:44:29 -04:00
#ifdef CONFIG_GEMINI
&gemini_protocol_module,
#endif
#ifdef CONFIG_GOPHER
&gopher_protocol_module,
#endif
&http_protocol_module,
#ifdef CONFIG_NNTP
&nntp_protocol_module,
#endif
#ifdef CONFIG_SMB
&smb_protocol_module,
#endif
#ifdef CONFIG_URI_REWRITE
&uri_rewrite_module,
#endif
&user_protocol_module,
NULL,
};
struct module protocol_module = struct_module(
/* name: */ N_("Protocol"),
/* options: */ protocol_options,
/* hooks: */ NULL,
/* submodules: */ protocol_submodules,
/* data: */ NULL,
/* init: */ NULL,
/* done: */ NULL
);