summaryrefslogtreecommitdiff
path: root/src/tgbot/Http.h
blob: aedcd93e568df73c78f105f84e6fd02a4ecc5f0e (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
120
121
122
123
124
125
/*
 * Copyright (c) 2015 Oleg Morozenkov
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef TGBOT_CPP_HTTP_H
#define TGBOT_CPP_HTTP_H

#include <string>

#include <boost/asio.hpp>
#include <boost/lexical_cast.hpp>

#include "tgbot/Url.h"

namespace TgBot {

class Http {

public:
    typedef std::function<void (const std::string&)> ServerHandler;

    class Argument {

    public:
        template<typename T>
        Argument(const std::string& name, const T& value, bool isFile = false, const std::string& mimeType = "") :
                name(name), value(boost::lexical_cast<std::string>(value)), isFile(isFile), mimeType(mimeType)
        {
        }

        std::string name;
        std::string value;
        bool isFile = false;
        std::string mimeType;
    };

    std::string makeRequest(const Url& url, const std::vector<Argument>& args);
    void startServer(unsigned short port, const ServerHandler& handler);
    void startServer(const std::string& unixSocketPath, const ServerHandler& handler);
    void stopServer();

private:
    template<typename Protocol>
    class Connection {

    public:
        Connection(Http* http, boost::asio::basic_stream_socket<Protocol>* socket) : http(http), socket(socket) {
        }

        ~Connection() {
            delete socket;
        }

        void start() {
            data.reserve(10240);
            socket->async_receive(data, [this]() {
                size_t headerEnd = data.find("\r\n\r\n");
                if (headerEnd == data.npos) {
                    headerEnd = data.find("\n\n");
                }
                if (headerEnd == data.npos) {
                    headerEnd = 0;
                }
                data.erase(0, headerEnd);
                http->_serverHandler(data);
            });
        }

        Http* http;
        boost::asio::basic_stream_socket<Protocol>* socket;
        std::string data;
    };

    template<typename Protocol>
    class Server {

    public:
        Server(Http* http, boost::asio::basic_socket_acceptor<Protocol>* acceptor) : http(http), acceptor(acceptor) {
        }

        ~Server() {
            delete acceptor;
        }

        void start() {
            boost::asio::basic_stream_socket<Protocol>* socket = new boost::asio::basic_stream_socket<Protocol>(acceptor->get_io_service());
            std::shared_ptr<Http::Connection<Protocol>> connection(new Connection<Protocol>(http, socket));
            acceptor->async_accept(*connection->socket, [this, connection]() {
                connection->start();
                start();
            });
        }

        Http* http;
        boost::asio::basic_socket_acceptor<Protocol>* acceptor;
    };


    boost::asio::io_service _ioService;
    Server<boost::asio::ip::tcp>* _tcpServer = nullptr;
    Server<boost::asio::local::stream_protocol>* _unixSocketServer = nullptr;
    ServerHandler _serverHandler;
};

}

#endif //TGBOT_CPP_HTTP_H