File: jsonrpc_httpserver.cpp

package info (click to toggle)
eiskaltdcpp 2.2.10%2B340%2Bg44d8aea4-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 22,580 kB
  • sloc: cpp: 95,676; ansic: 9,460; perl: 1,897; xml: 1,378; sh: 1,017; php: 661; makefile: 35
file content (146 lines) | stat: -rw-r--r-- 4,249 bytes parent folder | download | duplicates (3)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 *  JsonRpc-Cpp - JSON-RPC implementation.
 *  Copyright (C) 2011-2014 Eugene Petrov <dhamp@ya.ru>
 *  Copyright (C) 2013 Dorian Scholz <DorianScholz@gmx.de>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

/**
 * \file jsonrpc_httpserver.cpp
 * \brief JSON-RPC HTTPServer. Based on JSON-RPC Server.
 * \author Eugene Petrov
 */

#include "jsonrpc_httpserver.h"
#include <string>
#include <cstring>
#include <cstdlib>
#include "mongoose.h"

namespace Json
{

  namespace Rpc
  {
    struct mg_server *server;
    static int exit_flag;

    static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
        int result = MG_FALSE;
        if (ev == MG_AUTH) {
          return MG_TRUE;   // Authorize all requests
        }
        if (ev == MG_REQUEST) {
            if(strcmp(conn->request_method,"OPTIONS") == 0) {
                std::string res = "";
                reinterpret_cast<HTTPServer*>(conn->server_param)->sendResponse(res, conn);
            }
            else if(strcmp(conn->request_method,"POST") == 0) {
                std::string tmp;
                tmp.assign(conn->content, conn->content_len);
                reinterpret_cast<HTTPServer*>(conn->server_param)->onRequest(tmp, conn);
            }
            result = MG_TRUE;
        }
        return result;
    }

    static void *serving_thread_func(void *param) {
      struct mg_server *srv = (struct mg_server *) param;
      while (exit_flag == 0) {
        mg_poll_server(srv, 1000);
      }
      return NULL;
    }

    bool HTTPServer::onRequest(const std::string& str, void* addInfo)
    {
        Json::Value response;
        m_jsonHandler.Process(str, response);
        std::string res = m_jsonHandler.GetString(response);
        sendResponse(res, addInfo);
        return true;
    }

    HTTPServer::HTTPServer(const std::string& address, uint16_t port)
    {
      m_address = address;
      m_port = port;
      exit_flag = 0;
    }

    HTTPServer::~HTTPServer()
    {
    }

    bool HTTPServer::startPolling()
    {
        // Create and configure the server
        if ((server = mg_create_server(this, ev_handler)) == NULL) {
            return false;
        }
        char tmp_port[30];
        sprintf(tmp_port,"%s:%d", GetAddress().c_str(),GetPort());
        mg_set_option(server, "listening_port", tmp_port);
        serving_thread_func(server);
        return true;
    }

    bool HTTPServer::stopPolling()
    {
        exit_flag = 1;
        mg_destroy_server(&server);
        return true;
    }

    std::string HTTPServer::GetAddress() const
    {
      return m_address;
    }

    uint16_t HTTPServer::GetPort() const
    {
      return m_port;
    }

    void HTTPServer::AddMethod(CallbackMethod* method)
    {
      m_jsonHandler.AddMethod(method);
    }

    void HTTPServer::DeleteMethod(const std::string& method)
    {
      m_jsonHandler.DeleteMethod(method);
    }

    bool HTTPServer::sendResponse(std::string & response, void *addInfo)
    {
        struct mg_connection* conn = (struct mg_connection*)addInfo;
        std::string tmp = "HTTP/1.1 200 OK\r\nServer: eidcppd server\r\nAccess-Control-Allow-Origin: *\r\nAccess-Control-Allow-Headers: Content-Type\r\nContent-Type: application/json; charset=utf-8\r\nContent-Length: ";
        char v[16];
        snprintf(v, sizeof(v), "%lu", response.size());
        tmp += v;
        tmp += "\r\n\r\n";
        tmp += response;
        if(mg_write(conn, tmp.c_str(), tmp.size()) > 0) {
            return true;
        } else {
            return false;
        }
    }

  } /* namespace Rpc */

} /* namespace Json */