summaryrefslogtreecommitdiff
path: root/zen/http.h
blob: 07c3f28cead9e1affc9e61279bd4b297da6a6e6a (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
// *****************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under    *
// * GNU General Public License: https://www.gnu.org/licenses/gpl-3.0          *
// * Copyright (C) Zenju (zenju AT freefilesync DOT org) - All Rights Reserved *
// *****************************************************************************

#ifndef HTTP_H_879083425703425702
#define HTTP_H_879083425703425702

#include <zen/zstring.h>
#include <zen/sys_error.h>
#include <zen/serialize.h>

namespace zen
{
/*
    - thread-safe! (Window/Linux/macOS)
    - Linux/macOS: init OpenSSL before use!
*/
class HttpInputStream
{
public:
    //support zen/serialize.h buffered input stream concept
    size_t read(void* buffer, size_t bytesToRead); //throw SysError, X; return "bytesToRead" bytes unless end of stream!
    std::string readAll();                         //throw SysError, X

    size_t getBlockSize() const;

    class Impl;
    HttpInputStream(std::unique_ptr<Impl>&& pimpl);
    HttpInputStream(HttpInputStream&&) noexcept = default;
    ~HttpInputStream();

private:
    std::unique_ptr<Impl> pimpl_;
};


HttpInputStream sendHttpGet(const Zstring& url,
                            const Zstring& userAgent,
                            const Zstring* caCertFilePath /*optional: enable certificate validation*/,
                            const IoCallback& notifyUnbufferedIO /*throw X*/); //throw SysError, X

HttpInputStream sendHttpPost(const Zstring& url,
                             const std::vector<std::pair<std::string, std::string>>& postParams,
                             const Zstring& userAgent,
                             const Zstring* caCertFilePath /*optional: enable certificate validation*/,
                             const IoCallback& notifyUnbufferedIO /*throw X*/); //throw SysError, X

HttpInputStream sendHttpPost(const Zstring& url,
                             const std::string& postBuf, const std::string& contentType,
                             const Zstring& userAgent,
                             const Zstring* caCertFilePath /*optional: enable certificate validation*/,
                             const IoCallback& notifyUnbufferedIO /*throw X*/); //throw SysError, X

bool internetIsAlive(); //noexcept
std::wstring formatHttpError(int httpStatus);
bool isValidEmail(const std::string& email);
std::string htmlSpecialChars(const std::string_view& str);

std::string xWwwFormUrlEncode(const std::vector<std::pair<std::string, std::string>>& paramPairs);
std::vector<std::pair<std::string, std::string>> xWwwFormUrlDecode(const std::string& str);
}

#endif //HTTP_H_879083425703425702
bgstack15