pans
2017-08-30 71c92f101b6c8b4a678a8c3cfe2d8edbf488efa4
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
#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://www.postgresql.org
//#include <pgsql/libpq-fe.h>
#include <libpq-fe.h>
// http://uriparser.sourceforge.net
#include <uriparser/Uri.h>
 
 
// https://github.com/open-source-parsers/jsoncpp
//#include <json/json.h>
 
 
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 test 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);
}
//=======错误提示==end=====
 
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)
{
    return true;
}
 
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()
{
 
}
 
static void hpf_fishbowl_load_v4l2grab()
{
 
}
 
int main ()
{
    g_initial_nv = environ;
 
 
 
    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();
        }
    }
 
 
    cerr << "exit gracefully, fcgi_accept_status=" << fcgi_accept_status << endl;
 
    return 0;
}