ftp_server.h 2 KB
Newer Older
1
/*
2
 *  Copyright (C) 2017-2019 Savoir-faire Linux Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 *  Author: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */

#pragma once

23
#include "data_transfer.h"
24 25 26 27 28
#include "peer_connection.h"

#include <vector>
#include <array>
#include <sstream>
29
#include <memory>
30

Adrien Béraud's avatar
Adrien Béraud committed
31
namespace jami {
32 33 34 35

class FtpServer final : public Stream
{
public:
36
    FtpServer(const std::string& account_id, const std::string& peer_uri);
37

38
    bool read(std::vector<uint8_t>& buffer) const override;
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    bool write(const std::vector<uint8_t>& buffer) override;
    DRing::DataTransferId getId() const override;
    void close() noexcept override;

private:
    bool parseStream(const std::vector<uint8_t>&);
    bool parseLine(const std::string&);
    void handleHeader(const std::string&, const std::string&);
    bool startNewFile();
    void closeCurrentFile();

    enum class FtpState {
        PARSE_HEADERS,
        READ_DATA,
    };

55 56
    const std::string accountId_;
    const std::string peerUri_;
57
    IncomingFileInfo out_ {0, nullptr};
58
    std::size_t fileSize_ {0};
59
    std::size_t rx_ {0};
60 61 62
    std::stringstream headerStream_;
    std::string displayName_;
    std::array<char, 1000> line_;
Guillaume Roguez's avatar
Guillaume Roguez committed
63
    mutable bool closed_ {false};
Guillaume Roguez's avatar
Guillaume Roguez committed
64
    mutable bool go_ {false};
65 66 67
    FtpState state_ {FtpState::PARSE_HEADERS};
};

Adrien Béraud's avatar
Adrien Béraud committed
68
} // namespace jami