-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy pathhttpserver.cpp
175 lines (135 loc) · 4.83 KB
/
httpserver.cpp
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
/*****************************************************************************
* Copyright (C) 2014 Visualink
*
* Authors: Adrien Maglo <adrien@visualink.io>
*
* This file is part of Pastec.
*
* Pastec 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.
*
* Pastec 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 Pastec. If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/
#include <iostream>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <string.h>
#include "httpserver.h"
#include "messages.h"
#include "requesthandler.h"
#define PORT 4213
HTTPServer::HTTPServer(RequestHandler *requestHandler, unsigned i_port)
: daemon(NULL), requestHandler(requestHandler), i_port(i_port)
{
p_cond = new pthread_cond_t();
p_cond_mutex = new pthread_mutex_t();
pthread_cond_init(p_cond, NULL);
pthread_mutex_init(p_cond_mutex, NULL);
}
HTTPServer::~HTTPServer()
{
pthread_cond_destroy(p_cond);
pthread_mutex_destroy(p_cond_mutex);
delete p_cond_mutex;
delete p_cond;
}
int HTTPServer::run()
{
daemon = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION, i_port, NULL, NULL,
&answerToConnection, this,
MHD_OPTION_NOTIFY_COMPLETED, requestCompleted,
NULL, MHD_OPTION_END);
if (daemon == NULL)
return ERROR_GENERIC;
cout << "Ready to accept querries." << endl;
pthread_mutex_lock(p_cond_mutex);
pthread_cond_wait(p_cond, p_cond_mutex);
pthread_mutex_unlock(p_cond_mutex);
MHD_stop_daemon(daemon);
return OK;
}
int HTTPServer::stop()
{
pthread_cond_signal(p_cond);
return OK;
}
int HTTPServer::sendAnswer(struct MHD_Connection *connection, ConnectionInfo &conInfo)
{
int ret;
struct MHD_Response *response;
const char *buffer = conInfo.answerString.c_str();
response = MHD_create_response_from_buffer(strlen(buffer),
(void *)buffer,
MHD_RESPMEM_MUST_COPY);
if (!response)
return MHD_NO;
ret = MHD_queue_response(connection, conInfo.answerCode, response);
MHD_destroy_response(response);
return ret;
}
void HTTPServer::requestCompleted(void *cls, MHD_Connection *connection,
void **conCls, enum MHD_RequestTerminationCode toe)
{
(void)cls, (void)connection; (void)toe;
ConnectionInfo *conInfo = (ConnectionInfo *)*conCls;
if (conInfo == NULL)
return;
if (conInfo->connectionType == POST
&& conInfo->postprocessor != NULL)
MHD_destroy_post_processor(conInfo->postprocessor);
delete conInfo;
*conCls = NULL;
}
int HTTPServer::answerToConnection(void *cls, MHD_Connection *connection,
const char *url, const char *method,
const char *version, const char *upload_data,
size_t *upload_data_size, void **conCls)
{
(void)version;
HTTPServer *s = (HTTPServer *)cls;
ConnectionInfo *conInfo;
if (*conCls == NULL)
{
conInfo = new ConnectionInfo();
if (conInfo == NULL)
return MHD_NO;
if (strcmp(method, "POST") == 0)
conInfo->connectionType = POST;
else if (strcmp(method, "GET") == 0)
conInfo->connectionType = GET;
else if (strcmp(method, "DELETE") == 0)
conInfo->connectionType = DELETE;
else if (strcmp(method, "PUT") == 0)
conInfo->connectionType = PUT;
conInfo->url = string(url);
*conCls = (void *) conInfo;
return MHD_YES;
}
conInfo = (ConnectionInfo *)*conCls;
if (conInfo->connectionType == GET
|| conInfo->connectionType == DELETE)
s->requestHandler->handleRequest(*conInfo);
else if (conInfo->connectionType == POST
|| conInfo->connectionType == PUT)
{
if (*upload_data_size != 0)
{
conInfo->uploadedData.insert(conInfo->uploadedData.end(),
upload_data, upload_data + *upload_data_size);
*upload_data_size = 0;
return MHD_YES;
}
else
s->requestHandler->handleRequest(*conInfo);
}
return sendAnswer(connection, *conInfo);
}