summaryrefslogtreecommitdiffstats
blob: 2eed906d02bcecbdd69f2499ffea8707baa43905 (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
/*
 * Copyright (c) 2010-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swiften/StreamStack/XMPPLayer.h>

#include <Swiften/Elements/ProtocolHeader.h>
#include <Swiften/Parser/XMPPParser.h>
#include <Swiften/Serializer/XMPPSerializer.h>

namespace Swift {

XMPPLayer::XMPPLayer(
        PayloadParserFactoryCollection* payloadParserFactories,
        PayloadSerializerCollection* payloadSerializers,
        XMLParserFactory* xmlParserFactory,
        StreamType streamType,
        bool setExplictNSonTopLevelElements) :
            payloadParserFactories_(payloadParserFactories),
            payloadSerializers_(payloadSerializers),
            xmlParserFactory_(xmlParserFactory),
            setExplictNSonTopLevelElements_(setExplictNSonTopLevelElements),
            resetParserAfterParse_(false),
            inParser_(false) {
    xmppParser_ = new XMPPParser(this, payloadParserFactories_, xmlParserFactory);
    xmppSerializer_ = new XMPPSerializer(payloadSerializers_, streamType, setExplictNSonTopLevelElements);
}

XMPPLayer::~XMPPLayer() {
    delete xmppSerializer_;
    delete xmppParser_;
}

void XMPPLayer::writeHeader(const ProtocolHeader& header) {
    writeDataInternal(createSafeByteArray(xmppSerializer_->serializeHeader(header)));
}

void XMPPLayer::writeFooter() {
    writeDataInternal(createSafeByteArray(xmppSerializer_->serializeFooter()));
}

void XMPPLayer::writeElement(std::shared_ptr<ToplevelElement> element) {
    writeDataInternal(xmppSerializer_->serializeElement(element));
}

void XMPPLayer::writeData(const std::string& data) {
    writeDataInternal(createSafeByteArray(data));
}

void XMPPLayer::writeDataInternal(const SafeByteArray& data) {
    onWriteData(data);
    writeDataToChildLayer(data);
}

void XMPPLayer::handleDataRead(const SafeByteArray& data) {
    onDataRead(data);
    inParser_ = true;
    // FIXME: Converting to unsafe string. Should be ok, since we don't take passwords
    // from the stream in clients. If servers start using this, and require safe storage,
    // we need to fix this.
    if (!xmppParser_->parse(byteArrayToString(ByteArray(data.begin(), data.end())))) {
        inParser_ = false;
        onError();
        return;
    }
    inParser_ = false;
    if (resetParserAfterParse_) {
        doResetParser();
    }
}

void XMPPLayer::doResetParser() {
    delete xmppParser_;
    xmppParser_ = new XMPPParser(this, payloadParserFactories_, xmlParserFactory_);
    resetParserAfterParse_ = false;
}

void XMPPLayer::handleStreamStart(const ProtocolHeader& header) {
    onStreamStart(header);
}

void XMPPLayer::handleElement(std::shared_ptr<ToplevelElement> stanza) {
    onElement(stanza);
}

void XMPPLayer::handleStreamEnd() {
}

void XMPPLayer::resetParser() {
    if (inParser_) {
        resetParserAfterParse_ = true;
    }
    else {
        doResetParser();
    }
}

}