summaryrefslogtreecommitdiffstats
blob: f0a946a2d29cbfa840d768151440cfac4d1ece32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * Copyright (c) 2011 Thilo Cestonaro
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

/*
 * Copyright (c) 2011-2017 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */


#pragma once

#include <memory>

#include <Swiften/Base/API.h>
#include <Swiften/Base/Error.h>
#include <Swiften/Base/String.h>
#include <Swiften/Base/URL.h>
#include <Swiften/Network/Connection.h>
#include <Swiften/Network/Connector.h>
#include <Swiften/Network/HostAddressPort.h>
#include <Swiften/Session/SessionStream.h>
#include <Swiften/TLS/TLSError.h>

class BOSHConnectionTest;

namespace Swift {
    class XMLParserFactory;
    class TLSContextFactory;
    class TLSLayer;
    class TLSOptions;
    class HighLayer;

    class SWIFTEN_API BOSHError : public SessionStream::SessionStreamError {
        public:
            enum Type {
                BadRequest, HostGone, HostUnknown, ImproperAddressing,
                InternalServerError, ItemNotFound, OtherRequest, PolicyViolation,
                RemoteConnectionFailed, RemoteStreamError, SeeOtherURI, SystemShutdown, UndefinedCondition,
                NoError};

            BOSHError(Type type) : SessionStream::SessionStreamError(SessionStream::SessionStreamError::ConnectionReadError), type(type) {}
            Type getType() const {return type;}
            typedef std::shared_ptr<BOSHError> ref;

        private:
            Type type;
    };

    class SWIFTEN_API BOSHConnection : public std::enable_shared_from_this<BOSHConnection> {
        public:
            typedef std::shared_ptr<BOSHConnection> ref;
            static ref create(const URL& boshURL, Connector::ref connector, XMLParserFactory* parserFactory, TLSContextFactory* tlsContextFactory, const TLSOptions& tlsOptions) {
                return ref(new BOSHConnection(boshURL, connector, parserFactory, tlsContextFactory, tlsOptions));
            }
            virtual ~BOSHConnection();
            virtual void connect();
            virtual void disconnect();
            virtual void write(const SafeByteArray& data);

            const std::string& getSID();
            void setRID(unsigned long long rid);
            void setSID(const std::string& sid);
            void startStream(const std::string& to, unsigned long long rid);
            void terminateStream();
            bool isReadyToSend();
            void restartStream();

            bool setClientCertificate(CertificateWithKey::ref cert);
            Certificate::ref getPeerCertificate() const;
            std::vector<Certificate::ref> getPeerCertificateChain() const;
            CertificateVerificationError::ref getPeerCertificateVerificationError() const;

            boost::signals2::signal<void (bool /* error */)> onConnectFinished;
            boost::signals2::signal<void (bool /* error */)> onDisconnected;
            boost::signals2::signal<void (BOSHError::ref)> onSessionTerminated;
            boost::signals2::signal<void (const std::string& /*sid*/, size_t /*requests*/)> onSessionStarted;
            boost::signals2::signal<void (const SafeByteArray&)> onXMPPDataRead;
            boost::signals2::signal<void (const SafeByteArray&)> onBOSHDataRead;
            boost::signals2::signal<void (const SafeByteArray&)> onBOSHDataWritten;
            boost::signals2::signal<void (const std::string&)> onHTTPError;

        private:
            friend class ::BOSHConnectionTest;

            BOSHConnection(const URL& boshURL, Connector::ref connector, XMLParserFactory* parserFactory, TLSContextFactory* tlsContextFactory, const TLSOptions& tlsOptions);

            static std::pair<SafeByteArray, size_t> createHTTPRequest(const SafeByteArray& data, bool streamRestart, bool terminate, unsigned long long rid, const std::string& sid, const URL& boshURL);
            void handleConnectFinished(Connection::ref);
            void handleDataRead(std::shared_ptr<SafeByteArray> data);
            void handleDisconnected(const boost::optional<Connection::Error>& error);
            void write(const SafeByteArray& data, bool streamRestart, bool terminate); /* FIXME: refactor */
            BOSHError::Type parseTerminationCondition(const std::string& text);
            void cancelConnector();

            void handleTLSConnected();
            void handleTLSApplicationDataRead(const SafeByteArray& data);
            void handleTLSNetowrkDataWriteRequest(const SafeByteArray& data);
            void handleRawDataRead(std::shared_ptr<SafeByteArray> data);
            void handleTLSError(std::shared_ptr<TLSError> error);
            void writeData(const SafeByteArray& data);

            URL boshURL_;
            Connector::ref connector_;
            XMLParserFactory* parserFactory_;
            std::shared_ptr<Connection> connection_;
            std::shared_ptr<HighLayer> dummyLayer_;
            std::shared_ptr<TLSLayer> tlsLayer_;
            std::string sid_;
            bool waitingForStartResponse_;
            unsigned long long rid_;
            SafeByteArray buffer_;
            bool pending_;
            bool connectionReady_;
    };
}