Mercurial > prosody-hg
comparison plugins/mod_s2s.lua @ 11669:bca75f34d374
mod_s2s: Drop level of indentation by inverting a condition and early return
Nicer to get rid of a conditional that covers such a large portion of a
pretty big function.
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Sun, 11 Jul 2021 12:37:51 +0200 |
| parents | f18fbae6d9fe |
| children | 7f6c816a2c09 |
comparison
equal
deleted
inserted
replaced
| 11668:f18fbae6d9fe | 11669:bca75f34d374 |
|---|---|
| 550 -- reason: stream error to send to the remote server | 550 -- reason: stream error to send to the remote server |
| 551 -- remote_reason: stream error received from the remote server | 551 -- remote_reason: stream error received from the remote server |
| 552 -- bounce_reason: stanza error to pass to bounce_sendq because stream- and stanza errors are different | 552 -- bounce_reason: stanza error to pass to bounce_sendq because stream- and stanza errors are different |
| 553 local function session_close(session, reason, remote_reason, bounce_reason) | 553 local function session_close(session, reason, remote_reason, bounce_reason) |
| 554 local log = session.log or log; | 554 local log = session.log or log; |
| 555 if session.conn then | 555 if not session.conn then |
| 556 local conn = session.conn; | 556 return |
| 557 conn:pause_writes(); -- until :close | 557 end |
| 558 if session.notopen then | 558 |
| 559 if session.direction == "incoming" then | 559 local conn = session.conn; |
| 560 session:open_stream(session.to_host, session.from_host); | 560 conn:pause_writes(); -- until :close |
| 561 else | 561 if session.notopen then |
| 562 session:open_stream(session.from_host, session.to_host); | 562 if session.direction == "incoming" then |
| 563 end | 563 session:open_stream(session.to_host, session.from_host); |
| 564 end | |
| 565 | |
| 566 local this_host = session.direction == "incoming" and session.to_host or session.from_host | |
| 567 | |
| 568 if reason then -- nil == no err, initiated by us, false == initiated by remote | |
| 569 local stream_error; | |
| 570 local condition, text, extra | |
| 571 if type(reason) == "string" then -- assume stream error | |
| 572 condition = reason | |
| 573 elseif type(reason) == "table" and not st.is_stanza(reason) then | |
| 574 condition = reason.condition or "undefined-condition" | |
| 575 text = reason.text | |
| 576 extra = reason.extra | |
| 577 end | |
| 578 if condition then | |
| 579 stream_error = st.stanza("stream:error"):tag(condition, stream_xmlns_attr):up(); | |
| 580 if text then | |
| 581 stream_error:tag("text", stream_xmlns_attr):text(text):up(); | |
| 582 end | |
| 583 if extra then | |
| 584 stream_error:add_child(extra); | |
| 585 end | |
| 586 end | |
| 587 if this_host and condition then | |
| 588 m_closed_connections:with_labels(this_host, session.direction, condition):add(1) | |
| 589 end | |
| 590 if st.is_stanza(stream_error) then | |
| 591 -- to and from are never unknown on outgoing connections | |
| 592 log("debug", "Disconnecting %s->%s[%s], <stream:error> is: %s", | |
| 593 session.from_host or "(unknown host)" or session.ip, session.to_host or "(unknown host)", session.type, reason); | |
| 594 session.sends2s(stream_error); | |
| 595 end | |
| 596 else | 564 else |
| 597 m_closed_connections:with_labels(this_host, session.direction, reason == false and ":remote-choice" or ":local-choice"):add(1) | 565 session:open_stream(session.from_host, session.to_host); |
| 598 end | 566 end |
| 599 | 567 end |
| 600 session.sends2s("</stream:stream>"); | 568 |
| 601 function session.sends2s() return false; end | 569 local this_host = session.direction == "incoming" and session.to_host or session.from_host |
| 602 | 570 |
| 603 -- luacheck: ignore 422/reason | 571 if reason then -- nil == no err, initiated by us, false == initiated by remote |
| 604 -- FIXME reason should be managed in a place common to c2s, s2s, bosh, component etc | 572 local stream_error; |
| 605 local reason = remote_reason or (reason and (reason.text or reason.condition)) or reason; | 573 local condition, text, extra |
| 606 session.log("info", "%s s2s stream %s->%s closed: %s", session.direction:gsub("^.", string.upper), | 574 if type(reason) == "string" then -- assume stream error |
| 607 session.from_host or "(unknown host)", session.to_host or "(unknown host)", reason or "stream closed"); | 575 condition = reason |
| 608 | 576 elseif type(reason) == "table" and not st.is_stanza(reason) then |
| 609 conn:resume_writes(); | 577 condition = reason.condition or "undefined-condition" |
| 610 | 578 text = reason.text |
| 611 -- Authenticated incoming stream may still be sending us stanzas, so wait for </stream:stream> from remote | 579 extra = reason.extra |
| 612 if reason == nil and not session.notopen and session.direction == "incoming" then | 580 end |
| 613 add_task(stream_close_timeout, function () | 581 if condition then |
| 614 if not session.destroyed then | 582 stream_error = st.stanza("stream:error"):tag(condition, stream_xmlns_attr):up(); |
| 615 session.log("warn", "Failed to receive a stream close response, closing connection anyway..."); | 583 if text then |
| 616 s2s_destroy_session(session, reason, bounce_reason); | 584 stream_error:tag("text", stream_xmlns_attr):text(text):up(); |
| 617 conn:close(); | 585 end |
| 618 end | 586 if extra then |
| 619 end); | 587 stream_error:add_child(extra); |
| 620 else | 588 end |
| 621 s2s_destroy_session(session, reason, bounce_reason); | 589 end |
| 622 conn:close(); -- Close immediately, as this is an outgoing connection or is not authed | 590 if this_host and condition then |
| 623 end | 591 m_closed_connections:with_labels(this_host, session.direction, condition):add(1) |
| 592 end | |
| 593 if st.is_stanza(stream_error) then | |
| 594 -- to and from are never unknown on outgoing connections | |
| 595 log("debug", "Disconnecting %s->%s[%s], <stream:error> is: %s", | |
| 596 session.from_host or "(unknown host)" or session.ip, session.to_host or "(unknown host)", session.type, reason); | |
| 597 session.sends2s(stream_error); | |
| 598 end | |
| 599 else | |
| 600 m_closed_connections:with_labels(this_host, session.direction, reason == false and ":remote-choice" or ":local-choice"):add(1) | |
| 601 end | |
| 602 | |
| 603 session.sends2s("</stream:stream>"); | |
| 604 function session.sends2s() return false; end | |
| 605 | |
| 606 -- luacheck: ignore 422/reason 412/reason | |
| 607 -- FIXME reason should be managed in a place common to c2s, s2s, bosh, component etc | |
| 608 local reason = remote_reason or (reason and (reason.text or reason.condition)) or reason; | |
| 609 session.log("info", "%s s2s stream %s->%s closed: %s", session.direction:gsub("^.", string.upper), | |
| 610 session.from_host or "(unknown host)", session.to_host or "(unknown host)", reason or "stream closed"); | |
| 611 | |
| 612 conn:resume_writes(); | |
| 613 | |
| 614 -- Authenticated incoming stream may still be sending us stanzas, so wait for </stream:stream> from remote | |
| 615 if reason == nil and not session.notopen and session.direction == "incoming" then | |
| 616 add_task(stream_close_timeout, function () | |
| 617 if not session.destroyed then | |
| 618 session.log("warn", "Failed to receive a stream close response, closing connection anyway..."); | |
| 619 s2s_destroy_session(session, reason, bounce_reason); | |
| 620 conn:close(); | |
| 621 end | |
| 622 end); | |
| 623 else | |
| 624 s2s_destroy_session(session, reason, bounce_reason); | |
| 625 conn:close(); -- Close immediately, as this is an outgoing connection or is not authed | |
| 624 end | 626 end |
| 625 end | 627 end |
| 626 | 628 |
| 627 function session_stream_attrs(session, from, to, attr) -- luacheck: ignore 212/session | 629 function session_stream_attrs(session, from, to, attr) -- luacheck: ignore 212/session |
| 628 if not from or (hosts[from] and hosts[from].modules.dialback) then | 630 if not from or (hosts[from] and hosts[from].modules.dialback) then |
