xingzilong
2017-08-18 9e5babf9db52e64bdae60137be7696e56241fca6
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#ifndef nullptr
#define nullptr NULL
#endif
 
#include <unistd.h>
#include <time.h>
#include <errno.h>
 
#include <stdlib.h>
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <cctype>
 
using namespace std;
 
// http://www.fastcgi.com
#include <fcgi_stdio.h>
#include <fcgi_config.h>
 
// http://uriparser.sourceforge.net
#include <uriparser/Uri.h>
 
// http://www.postgresql.org
//#include <pgsql/libpq-fe.h>
#include <libpq-fe.h>
 
// https://github.com/open-source-parsers/jsoncpp
#include <json/json.h>
 
#define SOCK_TCP
#include <cmdhub/client.h>
 
#include "sqlmaker/sqlmaker.h"
 
my_id_t get_my_id()
{
    static my_id_t myid = 0;
    myid++;
    return myid;
}
 
char to_upper(char c)
{
    return toupper((int)c);
}
 
extern char **environ;
static char **g_initial_nv = nullptr;
static size_t g_req_count = 0;
 
stringstream g_log_msg;
stringstream g_http_res;
 
typedef void (*url_process_func_t)();
typedef map<string, url_process_func_t> upf_map_t;
static upf_map_t g_url_processors;
 
typedef map<string, string> str_map_t;
 
static void http_res_200(const char* msg)
{
    FCGI_printf("Status: 20 OK\r\n" "Content-type: text/html\r\n" "\r\n" "%s", msg);
}
static void http_res_400()
{
    FCGI_printf("Status: 400 Bad Request\r\n" "Content-type: text/html\r\n" "\r\n");
}
static void http_res_401()
{
    FCGI_printf("Status: 401 Unauthorized\r\n" "Content-type: text/html\r\n" "\r\n");
}
static void http_res_404()
{
    FCGI_printf("Status: 404 Not Found\r\n" "\r\n");
}
static void http_res_500()
{
    FCGI_printf("Status: 500 Internal Server Error\r\n" "\r\n");
}
static void http_res_500(int problem_code = 0)
{
    FCGI_printf("Status: 500 Internal Server Error\r\n" "Content-type: text/html\r\n" "\r\n" "Problem code = %d", problem_code);
}
static void http_res_500(const char* msg)
{
    FCGI_printf("Status: 500 Internal Server Error\r\n" "Content-type: text/html\r\n" "\r\n" "Message = %s", msg);
}
 
size_t read_shell(const char* cmd, char* buffer, size_t buffer_size, int* exit_status = nullptr, int* exit_code = nullptr)
{
    if (cmd == nullptr)
        return 0;
 
    FCGI_FILE* fcmd = FCGI_popen(cmd, "r");
    if (fcmd == nullptr)
        return 0;
 
    size_t red = FCGI_fread(buffer, sizeof(char), buffer_size - 1, fcmd);
 
    int es = FCGI_pclose(fcmd);
 
    if (exit_status != nullptr)
        *exit_status = es;
    if (exit_code != nullptr)
        *exit_code = WEXITSTATUS(es);
 
    return red;
}
 
void run_in_bash(const char* cmd, int* exit_status = nullptr, int* exit_code = nullptr)
{
#define BASH_WRAPPER "su -s /bin/bash -c '%s' http"
 
    char buffer[512];
    snprintf(buffer, sizeof(buffer) - 1, BASH_WRAPPER, cmd);
 
    int es = system(buffer);
 
    if (exit_status != nullptr)
        *exit_status = es;
    if (exit_code != nullptr)
        *exit_code = WEXITSTATUS(es);
}
 
static void print_env(const char *label, char **envp)
{
    FCGI_printf("%s:<br>\n<pre>\n", label);
    for ( ; *envp != NULL; envp++)
    {
        FCGI_printf("%s\n", *envp);
    }
    FCGI_printf("</pre><p>\n");
}
 
static void hpf_echo()
{
    g_log_msg.flush();
 
    char *contentLength = getenv("CONTENT_LENGTH");
    int len;
 
    FCGI_printf("Content-type: text/html\r\n"
                "\r\n"
                "<title>FastCGI echo</title>"
                "<h1>FastCGI echo</h1>\n"
                "Request number %d,  Process ID: %d<p>\n", g_req_count, getpid());
 
    if (contentLength != NULL)
        len = strtol(contentLength, NULL, 10);
    else
        len = 0;
 
    if (len <= 0)
    {
        FCGI_printf("No data from standard input.<p>\n");
    }
    else
    {
        int i, ch;
 
        FCGI_printf("Standard input:<br>\n<pre>\n");
        for (i = 0; i < len; i++)
        {
            if ((ch = getchar()) < 0)
            {
                FCGI_printf("Error: Not enough bytes received on standard input<p>\n");
                break;
            }
            putchar(ch);
        }
        FCGI_printf("\n</pre><p>\n");
    }
 
    print_env("Request environment", environ);
    print_env("Initial environment", g_initial_nv);
}
 
bool parse_query_items(str_map_t& query_items)
{
    char* req_REQUEST_URI;
    req_REQUEST_URI = getenv("REQUEST_URI");
    if (req_REQUEST_URI == nullptr)
    {
        g_log_msg << "can not get REQUEST_URI" << endl;
        goto error_ret;
    }
 
    char* req_QUERY_STRING;
    req_QUERY_STRING = getenv("QUERY_STRING");
    if (req_QUERY_STRING == nullptr)
    {
        g_log_msg << "can not get QUERY_STRING" << endl;
        goto error_ret;
    }
    else if (req_QUERY_STRING[0] == '\0')
    {
        g_log_msg << "QUERY_STRING is empty" << endl;
        return true;
    }
 
    UriParserStateA state;
    UriUriA uri;
 
    state.uri = &uri;
    if (uriParseUriA(&state, req_REQUEST_URI) != URI_SUCCESS)
    {
        g_log_msg << "uriParseUriA not success" << endl;
        goto error_ret_uri_1;
    }
 
    UriQueryListA * query_list;
    query_list = nullptr;
    int ql_item_count;
    ql_item_count = 0;
    if (uriDissectQueryMallocA(&query_list, &ql_item_count, uri.query.first, uri.query.afterLast) != URI_SUCCESS)
    {
        g_log_msg << "uriDissectQueryMallocA not success" << endl;
        goto error_ret_uri_2;
    }
 
    //debug:
    //FCGI_printf("Content-type: text/json\r\n" "\r\n");
 
    UriQueryListA* iter_query_list;
    iter_query_list = query_list;
    while (iter_query_list != nullptr)
    {
        const char* qi_key = iter_query_list->key;
        const char* qi_value = iter_query_list->value;
 
        if(qi_key == nullptr || qi_value == nullptr)
        {
            g_log_msg << "query_list contains null key or value" << endl;
            goto error_ret_uri_2;
        }
 
        query_items[string(qi_key)] = string(qi_value);
        //debug:
        //FCGI_printf("%s=%s\n", qi_key, qi_value);
        iter_query_list = iter_query_list->next;
    }
 
    uriFreeQueryListA(query_list);
    uriFreeUriMembersA(&uri);
    return true;
 
error_ret_uri_2:
    uriFreeQueryListA(query_list);
error_ret_uri_1:
    uriFreeUriMembersA(&uri);
error_ret:
    return false;
}
 
size_t get_post_content(char* buffer, size_t max_size, size_t offset_content = 0)
{
    if (buffer == NULL || max_size == 0)
        return 0;
 
    const char* contentLength = getenv("CONTENT_LENGTH");
    size_t len = 0;
    if (contentLength != NULL)
        len = strtol(contentLength, NULL, 10);
 
    if (len == 0)
        return 0;
 
    if (offset_content > 0)
        fseek(stdin, offset_content, SEEK_SET);
 
    return fread(buffer, sizeof(char), min(max_size, len), stdin);
}
 
const string* try_get_str_from_map(const str_map_t& str_map, const string& key, const string* default_value = nullptr)
{
    str_map_t::const_iterator iter = str_map.find(key);
    if (iter == str_map.end())
        return default_value;
    else
        return &(iter->second);
}
 
 
 
 
 
static void hpf_fishbowl_fish_count()
{
    g_log_msg.flush();
 
    str_map_t query_items;
    if (! parse_query_items(query_items))
    {
        http_res_500(g_log_msg.str().c_str());
        return;
    }
 
    Json::Value json_root;
 
    bool json_ret = true;
    const string* qi_cmd = try_get_str_from_map(query_items, string("cmd"));
    if (qi_cmd == nullptr)
    {
        json_root["msg"] = "cmd not needed";
    }
    else
    {
        if (strcmp("list", qi_cmd->c_str()) == 0)
        {
            json_ret = hpf_fishbowl_fish_count_list(query_items, json_root);
        }
        else if (strcmp("variate", qi_cmd->c_str()) == 0)
        {
            json_ret = hpf_fishbowl_fish_count_variate(query_items, json_root);
        }
        else if (strcmp("summed", qi_cmd->c_str()) == 0)
        {
            json_ret = hpf_fishbowl_fish_count_summed(query_items, json_root);
        }
        else if (strcmp("graph", qi_cmd->c_str()) == 0)
        {
        }
        else
        {
            json_root["msg"] = "cmd not supported";
        }
    }
 
    if (json_ret)
    {
        printf("Content-type: application/json\r\n" "\r\n");
        printf("%s", json_root.toStyledString().c_str());
    }
}
 
static void hpf_fishbowl_load_v4l2grab()
{
    g_log_msg.flush();
    errno = 0;
    bool ret = false;
    int exit_code = 0;
    const std::string cmd("exec/LOAD_V4L2GRAB");
    std::string result;
 
    CmdhubClientSession session;
    session.recv_retry = 20;
    if (ret = cmdhub_client_init(session))
    {
        if (ret = cmdhub_client_connect(session))
        {
            ret = cmdhub_client_command(session, cmd, result, exit_code);
            cmdhub_client_destory(session);
        }
    }
 
    Json::Value json_root;
    json_root["return"] = ret;
    if (ret)
    {
        json_root["exitcode"] = exit_code;
        json_root["result"] = result;
    }
    else
    {
        json_root["errno"] = errno;
    }
 
    printf("Content-type: application/json\r\n" "\r\n");
    printf("%s", json_root.toStyledString().c_str());
}
 
int main ()
{
    g_initial_nv = environ;
 
    g_url_processors["/echo"] = hpf_echo;
    g_url_processors["/fishbowl-basic-data"] = hpf_fishbowl_basic_data;
    g_url_processors["/fishbowl-bdtype"] = hpf_fishbowl_bdtype;
    g_url_processors["/fishbowl-fish-count"] = hpf_fishbowl_fish_count;
    g_url_processors["/fishbowl-fish-type"] = hpf_fishbowl_fish_type;
    g_url_processors["/fishbowl-fish-diary"] = hpf_fishbowl_fish_diary;
    g_url_processors["/fishbowl-load-v4l2grab"] = hpf_fishbowl_load_v4l2grab;
    //g_url_processors["/fishbowl-reminder"] = hpf_fishbowl_reminder;
 
    if(psql_conn() == nullptr)
    {
        cerr << g_log_msg.rdbuf();
        exit(EXIT_FAILURE);
    }
 
    cerr << "begin accept access" << endl;
 
    int fcgi_accept_status;
 
    while ((fcgi_accept_status = FCGI_Accept()) >= 0)
    {
        g_req_count++;
        g_log_msg.flush();
 
        char* req_PATH_INFO = getenv("PATH_INFO");
        if(req_PATH_INFO == nullptr)
        {
            cerr << "PATH_INFO is null" << endl;
            exit(EXIT_FAILURE);
        }
 
        upf_map_t::iterator iter_upf = g_url_processors.find(req_PATH_INFO);
        if (iter_upf == g_url_processors.end())
        {
            http_res_404();
        }
        else
        {
            url_process_func_t proc = iter_upf->second;
            proc();
        }
    }
 
    PGconn* conn = psql_conn();
    if (conn != nullptr)
        PQfinish(conn);
 
    cerr << "exit gracefully, fcgi_accept_status=" << fcgi_accept_status << endl;
 
    return 0;
}