-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver.cpp
More file actions
319 lines (299 loc) · 10.4 KB
/
server.cpp
File metadata and controls
319 lines (299 loc) · 10.4 KB
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
/** ************************************************************************************************
* Web Server process
* Part of the Wilson project (www.iambobot.com)
* Powered-by Etherjuice
* Fernando R
*
* 1.0.0 - May 2023
* 1.0.1 - read loop based on Content-Length
* 1.0.2 - Sep 2023 Project ONVIF
* - remove debug.h dependencies (trace2file)
*
** ************************************************************************************************
**/
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "cstr.h"
#include "server.h"
//#include "debug.h"
//bool trace_wserver= true;
void web_server(int portno, int (* f)(char*, int, char*, size_t, int ), int tracelevel);
pid_t WEBSERVERCreate( int (*WEBSERVERCallback)(char* , int , char* , size_t, int), int tracelevel )
{
pid_t pid_server= 0;
// Negative Value: creation of a child process was unsuccessful.
// Zero: Returned to the newly created child process.
// Positive value: Returned to parent or caller. The value contains process ID of newly created child process.
if((pid_server = fork())< 0)
{
perror("Fork failed");
return 0;
}
// FORKED PROCESS
if (pid_server == 0)
{
web_server(WEBSERVER_PORT, WEBSERVERCallback, tracelevel);
fprintf(stdout, "\n[FATAL ERROR] ---------------- process WEB SERVER ended");
fflush(stdout);
return 0;
} // (END) FORKED PROCESS
return pid_server;
} // WEBSERVERCreate
/* ----------------------------------------------------------------------------------------------------- */
/* -------------------------------- WEB LIB (candidates) ---------------------------------------------- */
// Sun, 17 Jan 2016 18:03:58 GMT
const char* strwday[]={"Sun", "Mon","Tue","Wed","Thu","Fri","Sat"};
const char* strmonth[]= {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
char *time_record(char *ts, size_t max)
{
time_t t = time(NULL);
struct tm *tm = localtime(&t);
snprintf(ts, max, "%s, %d %s %d %02d:%02d:%02d",
(size_t)tm->tm_wday<(sizeof(strwday)/sizeof(strwday[0])) ? strwday[tm->tm_wday] : "error",
tm->tm_mday,
(size_t)tm->tm_mon<(sizeof(strmonth)/sizeof(strmonth[0]))? strmonth[tm->tm_mon] : "error",
2000+tm->tm_year-100,
tm->tm_hour,
tm->tm_min,
tm->tm_sec);
return ts;
} // time_record()
// ------------------------------------------------------------------------------------------------
// --------------- SERVER -------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
// Test server
// curl -X POST -H "Content-Type: application/json" -d '{"type":"data_request","key":"cacaculo"}' http://192.168.1.100:8001
void web_server(int portno, int (* f)(char*, int, char*, size_t, int), int tracelevel)
{
char httpdata[4096];
char response[512];
char line[128];
char str[128];
int sockfd= -1;
struct sockaddr_in servaddr, client_addr;
int n;
fprintf(stdout, "\nSERVER ver. %s process running. port %d", WEBSERVER_VERSION, WEBSERVER_PORT);
fflush(stdout);
// PROCESS LOOP
while (1)
{
// SOCKET
while(sockfd<0)
{
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
snprintf(line, sizeof(line), "\n[WEB SERVER] *** ERROR *** web_server(): socket() (%d) %s", errno, strerror(errno));
fprintf(stdout,"%s", line);
//trace2file(DEBUGFILE_WEBSERVER, line);
perror("web_server:socket() failed");
fflush(stdout);
}
else
{
int enable = 1;
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0)
{
snprintf(line, sizeof(line), "\n[WEB SERVER] *** ERROR *** web_server(): setsockopt() (%d) %s", errno, strerror(errno));
fprintf(stdout,"%s", line);
//trace2file(DEBUGFILE_WEBSERVER, line);
perror("web_server:setsockopt() failed");
fflush(stdout);
close(sockfd);
sockfd= -1;
}
else
{
bzero((char *) &servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0)
{
snprintf(line, sizeof(line), "\n[WEB SERVER] *** ERROR *** web_server(): bind() (%d) %s", errno, strerror(errno));
fprintf(stdout,"%s", line);
//trace2file(DEBUGFILE_WEBSERVER, line);
perror("web_server:bind() failed");
fflush(stdout);
close(sockfd);
sockfd= -1;
}
}
}
if(sockfd<0)
{
snprintf(line, sizeof(line), "\n[WEB SERVER] waiting sockect (%d) %s", errno, strerror(errno));
fprintf(stdout,"%s", line);
//trace2file(DEBUGFILE_WEBSERVER, line);
fflush(stdout);
sleep(5);
}
}
// (END) SOCKET
// ACCEPT LOOP
while(1)
{
// marks the socket referred to by sockfd as a passive socket, that is, as a socket that will be used to accept incoming
// connection requests using.
// The backlog argument (=5) defines the maximum length to which the queue of pending connections for sockfd may grow
if( listen(sockfd, 5) < 0)
{
snprintf(line, sizeof(line), "\n[WEB SERVER] *** WARNING *** listen() (%d) %s", errno, strerror(errno));
fprintf(stdout,"%s", line);
//trace2file(DEBUGFILE_WEBSERVER, line);
fflush(stdout);
// ... warning and continue
}
// (1) Wait connection request from client
socklen_t clilen = sizeof(client_addr);
int newsockfd = accept(sockfd, (struct sockaddr *) &client_addr, &clilen);
if (newsockfd < 0)
{
snprintf(line, sizeof(line), "\n[WEB SERVER] ERROR accept()\n(%d) %s", errno, strerror(errno));
fprintf(stdout,"%s", line);
fflush(stdout);
//trace2file(DEBUGFILE_WEBSERVER, line);
close(sockfd);
sockfd= -1;
break;
}
fprintf(stdout, "\n[WEB SERVER] Connection to %s: ", inet_ntoa(client_addr.sin_addr));
fflush(stdout);
// Set time-out before connect
// RCV time-out 2 seconds
struct timeval timeout;
timeout.tv_sec = 2; // 2 seconds
timeout.tv_usec = 0;
if (setsockopt (newsockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
{
fprintf(stdout, "\n[WEB SERVER] ERROR web_server() setting timeout");
perror("web_server() setsockopt() failed\n");
fflush(stdout);
//close(sockfd);
//return -1;
}
/*
// SND time-out 3 seconds
timeout.tv_sec = 3;
timeout.tv_usec = 0;
if (setsockopt (newsockfd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
{
fprintf(stdout, "\n[ERROR] wsSocketCreate() ");
perror("setsockopt() failed\n");
fflush(stdout);
close(sockfd);
return -1;
}
*/
fprintf(stdout, "\n[WEB SERVER] Awaiting messsage ... ");
fflush(stdout);
// (2) RECEIVE
bzero(httpdata, sizeof(httpdata));
if ((n = read(newsockfd, httpdata, sizeof(httpdata)-1)) <= 0)
{
snprintf(line, sizeof(line), "\n[WEB SERVER] *** WARNING *** ERROR reading from socket\n(%d) %s", errno, strerror(errno));
fprintf(stdout,"%s", line);
fflush(stdout);
//trace2file(DEBUGFILE_WEBSERVER, line);
}
// Show message
else
{
size_t Content_Length= atoi(HTTPHeader_Entity("Content-Length", httpdata, sizeof(httpdata), str));
fprintf(stdout, "\n[WEB SERVER] RECEIVED Content-Length= %d bytes", Content_Length);
//fprintf(stdout, "\n%s\n", httpdata);
/*
#ifdef _HADEBUG_H_
snprintf(line, sizeof(line),"%s (%d bytes)", inet_ntoa(client_addr.sin_addr), n);
trace2file(DEBUGFILE_WEBSERVER, line);
#endif
*/
//-------------------------------------------------------
// First packet
// Process response
int ixpl= cstr_find(httpdata, "\r\n\r\n", 0, n);
if(ixpl)
{
int left= (Content_Length + ixpl + 4) -n;
if(left>0)
{
// Check if there are packets left
// get packets until getting Content_Length bytes of the payload
int nloop=0;
do
{
fprintf(stdout, "\nleft %d bytes", left);
nloop= read(newsockfd, &httpdata[n], left);
fprintf(stdout, "\nreceived %d bytes", nloop);
if(nloop>0)
{
n += nloop;
left -= nloop;
}
} while(nloop>0 && left>0);
if(nloop<=0) fprintf(stdout, "\n[WEB SERVER] WARNING read. left= %d bytes, should be zero left", nloop);
if(left==0) fprintf(stdout, "\n[WEB SERVER] Ok - no left to receive");
}
}
httpdata[n]='\0';
// Trace
fprintf(stdout, "\n[WEB SERVER] RECEIVED (%d bytes):", n);
//fprintf(stdout, "\n%s\n", httpdata);
//cstr_dump(httpdata, n);
fflush(stdout);
// callback
// -1 no response. dump raw request
if( f(httpdata, (int)n, response, sizeof(response), tracelevel)<0)
{
fprintf(stdout, "\n\n[WEB SERVER] NO RESPONSE\n\n");
cstr_dump(httpdata, n);
fflush(stdout);
}
else
{
char trecord[128];
snprintf(httpdata, sizeof(httpdata),
"HTTP/1.1 200 OK\r\n"
"Date: %s GMT\r\n" // timerecord
"Server: %s\r\n" // WEBSERVER_NAME
//"X-Powered-By: PHP/5.5.9-1ubuntu4.7\r\n"
//"Cache-Control: no-cache\r\n"
//"Cache-Control: no-store\r\n"
//"Access-Control-Allow-Origin: *\r\n"
//"Vary: Accept-Encoding\r\n"
//"Content-Type: application/json\r\n"
"Content-Type: application/soap+xml; charset=utf-8\r\n"
"Content-Length: %d\r\n"
"Connection: close\r\n"
"\r\n"
"%s",
time_record(trecord, sizeof(trecord)),
WEBSERVER_NAME,
strlen(response),
response);
// (2) RESPONSE
if ( (n = write(newsockfd, httpdata, strlen(httpdata))) < 0)
{
snprintf(line, sizeof(line), "\n[*** WARNING ***] WEBSERVER ERROR writing to socket\n(%d) %s", errno, strerror(errno));
fprintf(stdout,"%s", line);
fflush(stdout);
//trace2file(DEBUGFILE_WEBSERVER, line);
}
// Show message
if(tracelevel)
printf("\n[WEB SERVER] RESPONSE:\n%s\n\n", httpdata);
close(newsockfd);
}
}
}
} // While PROCESS
close(sockfd);
} // web_server()
// END OF FILE