diff options
author | Marvin W <git@larma.de> | 2023-01-31 15:12:39 +0100 |
---|---|---|
committer | Marvin W <git@larma.de> | 2023-02-07 10:50:43 +0100 |
commit | 18321ed15ce782ff5d1f24de9f2fb459d714d125 (patch) | |
tree | baf849afe826691b351110b8899701f698692eb9 /xmpp-vala/src/core | |
parent | 95fefaff51e5506d3f0e5fe8bced14aeb3fbe037 (diff) | |
download | dino-18321ed15ce782ff5d1f24de9f2fb459d714d125.tar.gz dino-18321ed15ce782ff5d1f24de9f2fb459d714d125.zip |
Collapse most stream releated errors into IOError
Diffstat (limited to 'xmpp-vala/src/core')
-rw-r--r-- | xmpp-vala/src/core/direct_tls_xmpp_stream.vala | 6 | ||||
-rw-r--r-- | xmpp-vala/src/core/io_xmpp_stream.vala | 50 | ||||
-rw-r--r-- | xmpp-vala/src/core/namespace_state.vala | 8 | ||||
-rw-r--r-- | xmpp-vala/src/core/stanza_attribute.vala | 4 | ||||
-rw-r--r-- | xmpp-vala/src/core/stanza_node.vala | 2 | ||||
-rw-r--r-- | xmpp-vala/src/core/stanza_reader.vala | 69 | ||||
-rw-r--r-- | xmpp-vala/src/core/stanza_writer.vala | 13 | ||||
-rw-r--r-- | xmpp-vala/src/core/starttls_xmpp_stream.vala | 6 | ||||
-rw-r--r-- | xmpp-vala/src/core/stream_connect.vala | 6 | ||||
-rw-r--r-- | xmpp-vala/src/core/xmpp_stream.vala | 24 |
10 files changed, 80 insertions, 108 deletions
diff --git a/xmpp-vala/src/core/direct_tls_xmpp_stream.vala b/xmpp-vala/src/core/direct_tls_xmpp_stream.vala index aec17acd..26adc90f 100644 --- a/xmpp-vala/src/core/direct_tls_xmpp_stream.vala +++ b/xmpp-vala/src/core/direct_tls_xmpp_stream.vala @@ -13,7 +13,7 @@ public class Xmpp.DirectTlsXmppStream : TlsXmppStream { this.on_invalid_cert = on_invalid_cert; } - public override async void connect() throws IOStreamError { + public override async void connect() throws IOError { SocketClient client = new SocketClient(); try { debug("Connecting to %s:%i (tls)", host, port); @@ -29,8 +29,10 @@ public class Xmpp.DirectTlsXmppStream : TlsXmppStream { yield setup(); attach_negotation_modules(); + } catch (IOError e) { + throw e; } catch (Error e) { - throw new IOStreamError.CONNECT("Failed connecting to %s:%i (tls): %s", host, port, e.message); + throw new IOError.CONNECTION_REFUSED("Failed connecting to %s:%i (tls): %s", host, port, e.message); } } } diff --git a/xmpp-vala/src/core/io_xmpp_stream.vala b/xmpp-vala/src/core/io_xmpp_stream.vala index 02653720..208e8053 100644 --- a/xmpp-vala/src/core/io_xmpp_stream.vala +++ b/xmpp-vala/src/core/io_xmpp_stream.vala @@ -1,7 +1,7 @@ using Gee; public interface Xmpp.WriteNodeFunc : Object { - public abstract async void write_stanza(XmppStream stream, StanzaNode node) throws IOStreamError; + public abstract async void write_stanza(XmppStream stream, StanzaNode node) throws IOError; } public abstract class Xmpp.IoXmppStream : XmppStream { @@ -15,10 +15,10 @@ public abstract class Xmpp.IoXmppStream : XmppStream { base(remote_name); } - public override async void disconnect() throws IOStreamError, XmlError, IOError { + public override async void disconnect() throws IOError { disconnected = true; if (writer == null || reader == null || stream == null) { - throw new IOStreamError.DISCONNECT("trying to disconnect, but no stream open"); + throw new IOError.CLOSED("trying to disconnect, but no stream open"); } log.str("OUT", "</stream:stream>", this); yield writer.write("</stream:stream>"); @@ -35,16 +35,12 @@ public abstract class Xmpp.IoXmppStream : XmppStream { require_setup(); } - public override async StanzaNode read() throws IOStreamError { + public override async StanzaNode read() throws IOError { StanzaReader? reader = this.reader; - if (reader == null) throw new IOStreamError.READ("trying to read, but no stream open"); - try { - StanzaNode node = yield ((!)reader).read_node(); - log.node("IN", node, this); - return node; - } catch (XmlError e) { - throw new IOStreamError.READ(e.message); - } + if (reader == null) throw new IOError.NOT_CONNECTED("trying to read, but no stream open"); + StanzaNode node = yield ((!)reader).read_node(); + log.node("IN", node, this); + return node; } [Version (deprecated = true, deprecated_since = "0.1", replacement = "write_async")] @@ -56,18 +52,14 @@ public abstract class Xmpp.IoXmppStream : XmppStream { }); } - public override async void write_async(StanzaNode node) throws IOStreamError { + public override async void write_async(StanzaNode node) throws IOError { if (write_obj != null) { yield write_obj.write_stanza(this, node); } else { StanzaWriter? writer = this.writer; - if (writer == null) throw new IOStreamError.WRITE("trying to write, but no stream open"); - try { - log.node("OUT", node, this); - yield ((!)writer).write_node(node); - } catch (XmlError e) { - throw new IOStreamError.WRITE(e.message); - } + if (writer == null) throw new IOError.NOT_CONNECTED("trying to write, but no stream open"); + log.node("OUT", node, this); + yield ((!)writer).write_node(node); } } @@ -75,7 +67,7 @@ public abstract class Xmpp.IoXmppStream : XmppStream { return stream; } - public override async void setup() throws IOStreamError { + public override async void setup() throws IOError { StanzaNode outs = new StanzaNode.build("stream", "http://etherx.jabber.org/streams") .put_attribute("to", remote_name.to_string()) .put_attribute("version", "1.0") @@ -89,17 +81,11 @@ public abstract class Xmpp.IoXmppStream : XmppStream { setup_needed = false; } - private async StanzaNode read_root() throws IOStreamError { + private async StanzaNode read_root() throws IOError { StanzaReader? reader = this.reader; - if (reader == null) throw new IOStreamError.READ("trying to read, but no stream open"); - try { - StanzaNode node = yield ((!)reader).read_root_node(); - log.node("IN ROOT", node, this); - return node; - } catch (XmlError.TLS e) { - throw new IOStreamError.TLS(e.message); - } catch (Error e) { - throw new IOStreamError.READ(e.message); - } + if (reader == null) throw new IOError.NOT_CONNECTED("trying to read, but no stream open"); + StanzaNode node = yield ((!)reader).read_root_node(); + log.node("IN ROOT", node, this); + return node; } }
\ No newline at end of file diff --git a/xmpp-vala/src/core/namespace_state.vala b/xmpp-vala/src/core/namespace_state.vala index fe83ad09..306b515e 100644 --- a/xmpp-vala/src/core/namespace_state.vala +++ b/xmpp-vala/src/core/namespace_state.vala @@ -52,18 +52,18 @@ public class NamespaceState { this.current_ns_uri = current_ns_uri; } - public string find_name(string ns_uri) throws XmlError { + public string find_name(string ns_uri) throws IOError { if (uri_to_name.has_key(ns_uri)) { return uri_to_name[ns_uri]; } - throw new XmlError.NS_DICT_ERROR(@"NS URI $ns_uri not found."); + throw new IOError.INVALID_DATA(@"XML: NS URI $ns_uri not found."); } - public string find_uri(string name) throws XmlError { + public string find_uri(string name) throws IOError { if (name_to_uri.has_key(name)) { return name_to_uri[name]; } - throw new XmlError.NS_DICT_ERROR(@"NS name $name not found."); + throw new IOError.INVALID_DATA(@"XML: NS name $name not found."); } public NamespaceState push() { diff --git a/xmpp-vala/src/core/stanza_attribute.vala b/xmpp-vala/src/core/stanza_attribute.vala index c2edeba0..6c6ac579 100644 --- a/xmpp-vala/src/core/stanza_attribute.vala +++ b/xmpp-vala/src/core/stanza_attribute.vala @@ -51,7 +51,7 @@ public class StanzaAttribute : StanzaEntry { } } - public string to_xml(NamespaceState? state_ = null) throws XmlError { + public string to_xml(NamespaceState? state_ = null) { NamespaceState state = state_ ?? new NamespaceState(); if (ns_uri == state.current_ns_uri || (ns_uri == XMLNS_URI && name == "xmlns")) { return printf(ATTRIBUTE_XML_NO_NS_FORMAT, true); @@ -60,7 +60,7 @@ public class StanzaAttribute : StanzaEntry { } } - public string to_ansi_xml(NamespaceState? state_ = null) throws XmlError { + public string to_ansi_xml(NamespaceState? state_ = null) { NamespaceState state = state_ ?? new NamespaceState(); if (ns_uri == state.current_ns_uri || (ns_uri == XMLNS_URI && name == "xmlns")) { return printf(ATTRIBUTE_XML_ANSI_NO_NS_FORMAT, true); diff --git a/xmpp-vala/src/core/stanza_node.vala b/xmpp-vala/src/core/stanza_node.vala index 6ef3f0aa..502717c8 100644 --- a/xmpp-vala/src/core/stanza_node.vala +++ b/xmpp-vala/src/core/stanza_node.vala @@ -377,7 +377,7 @@ public class StanzaNode : StanzaEntry { } } - public string to_xml(NamespaceState? state = null) throws XmlError { + public string to_xml(NamespaceState? state = null) throws IOError { NamespaceState my_state = state ?? new NamespaceState.for_stanza(); if (name == "#text") return val == null ? "" : (!)encoded_val; my_state = my_state.push(); diff --git a/xmpp-vala/src/core/stanza_reader.vala b/xmpp-vala/src/core/stanza_reader.vala index 0c07948d..17f0b7b0 100644 --- a/xmpp-vala/src/core/stanza_reader.vala +++ b/xmpp-vala/src/core/stanza_reader.vala @@ -6,15 +6,6 @@ public const string XMLNS_URI = "http://www.w3.org/2000/xmlns/"; public const string XML_URI = "http://www.w3.org/XML/1998/namespace"; public const string JABBER_URI = "jabber:client"; -public errordomain XmlError { - NS_DICT_ERROR, - UNSUPPORTED, - EOF, - BAD_XML, - IO, - TLS -} - public class StanzaReader { private static int BUFFER_MAX = 4096; @@ -44,27 +35,23 @@ public class StanzaReader { cancellable.cancel(); } - private async void update_buffer() throws XmlError { - try { - InputStream? input = this.input; - if (input == null) throw new XmlError.EOF("No input stream specified and end of buffer reached."); - if (cancellable.is_cancelled()) throw new XmlError.EOF("Input stream is canceled."); - buffer_fill = (int) yield ((!)input).read_async(buffer, GLib.Priority.DEFAULT, cancellable); - if (buffer_fill == 0) throw new XmlError.EOF("End of input stream reached."); - buffer_pos = 0; - } catch (GLib.IOError e) { - throw new XmlError.IO("GLib.IOError: %s".printf(e.message)); - } + private async void update_buffer() throws IOError { + InputStream? input = this.input; + if (input == null) throw new IOError.CLOSED("No input stream specified and end of buffer reached."); + if (cancellable.is_cancelled()) throw new IOError.CANCELLED("Input stream is canceled."); + buffer_fill = (int) yield ((!)input).read_async(buffer, GLib.Priority.DEFAULT, cancellable); + if (buffer_fill == 0) throw new IOError.CLOSED("End of input stream reached."); + buffer_pos = 0; } - private async char read_single() throws XmlError { + private async char read_single() throws IOError { if (buffer_pos >= buffer_fill) { yield update_buffer(); } return (char) buffer[buffer_pos++]; } - private async char peek_single() throws XmlError { + private async char peek_single() throws IOError { if (buffer_pos >= buffer_fill) { yield update_buffer(); } @@ -79,7 +66,7 @@ public class StanzaReader { buffer_pos++; } - private async void skip_until_non_ws() throws XmlError { + private async void skip_until_non_ws() throws IOError { if (buffer_pos >= buffer_fill) { yield update_buffer(); } @@ -91,7 +78,7 @@ public class StanzaReader { } } - private async string read_until_ws() throws XmlError { + private async string read_until_ws() throws IOError { var res = new StringBuilder(); if (buffer_pos >= buffer_fill) { yield update_buffer(); @@ -105,7 +92,7 @@ public class StanzaReader { return res.str; } - private async string read_until_char_or_ws(char x, char y = 0) throws XmlError { + private async string read_until_char_or_ws(char x, char y = 0) throws IOError { var res = new StringBuilder(); if (buffer_pos >= buffer_fill) { yield update_buffer(); @@ -119,7 +106,7 @@ public class StanzaReader { return res.str; } - private async string read_until_char(char x) throws XmlError { + private async string read_until_char(char x) throws IOError { var res = new StringBuilder(); if (buffer_pos >= buffer_fill) { yield update_buffer(); @@ -133,7 +120,7 @@ public class StanzaReader { return res.str; } - private async StanzaAttribute read_attribute() throws XmlError { + private async StanzaAttribute read_attribute() throws IOError { var res = new StanzaAttribute(); res.name = yield read_until_char_or_ws('='); if ((yield read_single()) == '=') { @@ -149,7 +136,7 @@ public class StanzaReader { return res; } - private void handle_entry_ns(StanzaEntry entry, string default_uri = ns_state.current_ns_uri) throws XmlError { + private void handle_entry_ns(StanzaEntry entry, string default_uri = ns_state.current_ns_uri) throws IOError { if (entry.ns_uri != null) return; if (entry.name.contains(":")) { var split = entry.name.split(":"); @@ -160,7 +147,7 @@ public class StanzaReader { } } - private void handle_stanza_ns(StanzaNode res) throws XmlError { + private void handle_stanza_ns(StanzaNode res) throws IOError { foreach (StanzaAttribute attr in res.attributes) { if (attr.name == "xmlns" && attr.val != null) { attr.ns_uri = XMLNS_URI; @@ -180,7 +167,7 @@ public class StanzaReader { } } - public async StanzaNode read_node_start() throws XmlError { + public async StanzaNode read_node_start() throws IOError { var res = new StanzaNode(); res.attributes = new ArrayList<StanzaAttribute>(); var eof = false; @@ -217,7 +204,7 @@ public class StanzaReader { return res; } - public async StanzaNode read_text_node() throws XmlError { + public async StanzaNode read_text_node() throws IOError { var res = new StanzaNode(); res.name = "#text"; res.ns_uri = ns_state.current_ns_uri; @@ -225,7 +212,7 @@ public class StanzaReader { return res; } - public async StanzaNode read_root_node() throws XmlError { + public async StanzaNode read_root_node() throws IOError { yield skip_until_non_ws(); if ((yield peek_single()) == '<') { var res = yield read_node_start(); @@ -234,11 +221,11 @@ public class StanzaReader { } return res; } else { - throw new XmlError.BAD_XML("Content before root node"); + throw new IOError.INVALID_DATA("XML: Content before root node"); } } - public async StanzaNode read_stanza_node() throws XmlError { + public async StanzaNode read_stanza_node() throws IOError { try { ns_state = ns_state.push(); var res = yield read_node_start(); @@ -255,11 +242,11 @@ public class StanzaReader { skip_single(); if (desc.contains(":")) { var split = desc.split(":"); - if (split[0] != ns_state.find_name((!)res.ns_uri)) throw new XmlError.BAD_XML(""); - if (split[1] != res.name) throw new XmlError.BAD_XML(""); + if (split[0] != ns_state.find_name((!)res.ns_uri)) throw new IOError.INVALID_DATA("XML: Closing namespace prefix mismatch"); + if (split[1] != res.name) throw new IOError.INVALID_DATA("XML: Closing element name mismatch"); } else { - if (ns_state.current_ns_uri != res.ns_uri) throw new XmlError.BAD_XML(""); - if (desc != res.name) throw new XmlError.BAD_XML(""); + if (ns_state.current_ns_uri != res.ns_uri) throw new IOError.INVALID_DATA("XML: Closing element namespace mismatch"); + if (desc != res.name) throw new IOError.INVALID_DATA("XML: Closing element name mismatch"); } finish_node_seen = true; } else { @@ -277,15 +264,15 @@ public class StanzaReader { } ns_state = ns_state.pop(); return res; - } catch (XmlError e) { + } catch (IOError.INVALID_DATA e) { uint8[] buffer_cpy = new uint8[buffer.length + 1]; Memory.copy(buffer_cpy, buffer, buffer.length); - warning("XmlError at: %s".printf((string)buffer_cpy) + "\n"); + warning("invalid data at: %s".printf((string)buffer_cpy) + "\n"); throw e; } } - public async StanzaNode read_node() throws XmlError { + public async StanzaNode read_node() throws IOError { yield skip_until_non_ws(); if ((yield peek_single()) == '<') { return yield read_stanza_node(); diff --git a/xmpp-vala/src/core/stanza_writer.vala b/xmpp-vala/src/core/stanza_writer.vala index 62fe022a..5b926a93 100644 --- a/xmpp-vala/src/core/stanza_writer.vala +++ b/xmpp-vala/src/core/stanza_writer.vala @@ -12,11 +12,11 @@ public class StanzaWriter { this.output = output; } - public async void write_node(StanzaNode node) throws XmlError { + public async void write_node(StanzaNode node) throws IOError { yield write_data(node.to_xml().data); } - public async void write_nodes(StanzaNode node1, StanzaNode node2) throws XmlError { + public async void write_nodes(StanzaNode node1, StanzaNode node2) throws IOError { var data1 = node1.to_xml().data; var data2 = node2.to_xml().data; @@ -32,11 +32,11 @@ public class StanzaWriter { yield write_data(concat); } - public async void write(string s) throws XmlError { + public async void write(string s) throws IOError { yield write_data(s.data); } - private async void write_data(owned uint8[] data) throws XmlError { + private async void write_data(owned uint8[] data) throws IOError { if (running) { queue.push_tail(new SourceFuncWrapper(write_data.callback)); yield; @@ -44,9 +44,12 @@ public class StanzaWriter { running = true; try { yield output.write_all_async(data, 0, null, null); + } catch (IOError e) { + cancel(); + throw e; } catch (GLib.Error e) { cancel(); - throw new XmlError.IO(@"IOError in GLib: $(e.message)"); + throw new IOError.FAILED("Error in GLib: %s".printf(e.message)); } finally { SourceFuncWrapper? sfw = queue.pop_head(); if (sfw != null) { diff --git a/xmpp-vala/src/core/starttls_xmpp_stream.vala b/xmpp-vala/src/core/starttls_xmpp_stream.vala index 2334f664..0d4fbc7d 100644 --- a/xmpp-vala/src/core/starttls_xmpp_stream.vala +++ b/xmpp-vala/src/core/starttls_xmpp_stream.vala @@ -13,7 +13,7 @@ public class Xmpp.StartTlsXmppStream : TlsXmppStream { this.on_invalid_cert = on_invalid_cert; } - public override async void connect() throws IOStreamError { + public override async void connect() throws IOError { try { SocketClient client = new SocketClient(); debug("Connecting to %s:%i (starttls)", host, port); @@ -50,8 +50,10 @@ public class Xmpp.StartTlsXmppStream : TlsXmppStream { yield setup(); attach_negotation_modules(); + } catch (IOError e) { + throw e; } catch (Error e) { - throw new IOStreamError.CONNECT("Failed connecting to %s:%i (starttls): %s", host, port, e.message); + throw new IOError.CONNECTION_REFUSED("Failed connecting to %s:%i (starttls): %s", host, port, e.message); } } } diff --git a/xmpp-vala/src/core/stream_connect.vala b/xmpp-vala/src/core/stream_connect.vala index 833c5131..a4c5b82e 100644 --- a/xmpp-vala/src/core/stream_connect.vala +++ b/xmpp-vala/src/core/stream_connect.vala @@ -10,7 +10,7 @@ namespace Xmpp { public class XmppStreamResult { public TlsXmppStream? stream { get; set; } public TlsCertificateFlags? tls_errors { get; set; } - public IOStreamError? io_error { get; set; } + public IOError? io_error { get; set; } } public async XmppStreamResult establish_stream(Jid bare_jid, Gee.List<XmppStreamModule> modules, string? log_options, owned TlsXmppStream.OnInvalidCert on_invalid_cert) { @@ -55,7 +55,7 @@ namespace Xmpp { // Try all connection options from lowest to highest priority TlsXmppStream? stream = null; TlsCertificateFlags? tls_errors = null; - IOStreamError? io_error = null; + IOError? io_error = null; foreach (SrvTargetInfo target in targets) { try { if (target.service == "xmpp-client") { @@ -72,7 +72,7 @@ namespace Xmpp { yield stream.connect(); return new XmppStreamResult() { stream=stream }; - } catch (IOStreamError e) { + } catch (IOError e) { warning("Could not establish XMPP session with %s:%i: %s", target.host, target.port, e.message); if (stream != null) { diff --git a/xmpp-vala/src/core/xmpp_stream.vala b/xmpp-vala/src/core/xmpp_stream.vala index 0f0793e9..6370554f 100644 --- a/xmpp-vala/src/core/xmpp_stream.vala +++ b/xmpp-vala/src/core/xmpp_stream.vala @@ -1,13 +1,5 @@ using Gee; -public errordomain Xmpp.IOStreamError { - READ, - WRITE, - CONNECT, - DISCONNECT, - TLS -} - public abstract class Xmpp.XmppStream { public signal void received_node(XmppStream stream, StanzaNode node); @@ -38,18 +30,18 @@ public abstract class Xmpp.XmppStream { this.remote_name = remote_name; } - public abstract async void connect() throws IOStreamError; + public abstract async void connect() throws IOError; - public abstract async void disconnect() throws IOStreamError, XmlError, IOError; + public abstract async void disconnect() throws IOError; - public abstract async StanzaNode read() throws IOStreamError; + public abstract async StanzaNode read() throws IOError; [Version (deprecated = true, deprecated_since = "0.1", replacement = "write_async")] public abstract void write(StanzaNode node); - public abstract async void write_async(StanzaNode node) throws IOStreamError; + public abstract async void write_async(StanzaNode node) throws IOError; - public abstract async void setup() throws IOStreamError; + public abstract async void setup() throws IOError; public void require_setup() { setup_needed = true; @@ -105,7 +97,7 @@ public abstract class Xmpp.XmppStream { return null; } - public async void loop() throws IOStreamError { + public async void loop() throws IOError { while (true) { if (setup_needed) { yield setup(); @@ -168,7 +160,7 @@ public abstract class Xmpp.XmppStream { return false; } - private bool negotiation_modules_done() throws IOStreamError { + private bool negotiation_modules_done() throws IOError { if (setup_needed) return false; if (is_negotiation_active()) return false; @@ -176,7 +168,7 @@ public abstract class Xmpp.XmppStream { if (module is XmppStreamNegotiationModule) { XmppStreamNegotiationModule negotiation_module = (XmppStreamNegotiationModule) module; if (negotiation_module.mandatory_outstanding(this)) { - throw new IOStreamError.CONNECT("mandatory-to-negotiate feature not negotiated: " + negotiation_module.get_id()); + throw new IOError.FAILED("mandatory-to-negotiate feature not negotiated: " + negotiation_module.get_id()); } } } |