-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMygSoapServer.h
479 lines (439 loc) · 12 KB
/
MygSoapServer.h
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#include "soapH.h"
#include "ns.nsmap"
#include "outStackBuffer.h"
#include "MyUtil.h"
#include "MyFileParser.h"
#include "alltests.h"
#define PATH_LEN SOAP_TAGLEN * 2
#ifndef WINCE
#define HTTP_SVR_PORT 18083
LPCSTR pszCfgFile = "/test.ini";
#else
//#define TEST
#define HTTP_SVR_PORT 80
LPCSTR pszCfgFile = "/config.ini";
#endif
LPCSTR pszLogFile = "/soap.log";
LPCSTR pszHomeHtml = "/index.htm";
LPCSTR pszSoapXml = "/ns.winconfig.req.xml";
LPSTR pszCfgFullpath = NULL;
HANDLE hSoapServerThd = NULL;
DWORD soapSvrThdid;
BOOL startSvr = false;
HANDLE WINAPI MyThread(LPTHREAD_START_ROUTINE lpStartAddress,
LPDWORD lpThreadId,
LPVOID lpParameter = NULL,
DWORD dwCreationFlags = 0);
HANDLE MyOpenFile(
LPCTSTR lpszFilename,
DWORD dwDesiredAccess,
DWORD dwShareMode);
HRESULT GenerateConfigTable();
void my_soap_init(struct soap *pSoap);
void SoapErr(struct soap *soap);
void SoapSendMyStack(struct soap *pSoap, poutStackBuffer pStack)
{
if (pStack->index > 0)
{
//printf("%s", pStack->pBuffer);
soap_send_raw(pSoap, pStack->pBuffer, pStack->index);
emptyzeroStack(pStack);
}
}
LPSTR GetConfigFilename(LPCSTR path)
{
LPSTR pszFilename = (LPSTR)strstr(path, "?");
if (!pszFilename)
{
pszFilename = (LPSTR)pszCfgFile;
}
else
{
pszFilename[0] = '/';
}
return pszFilename;
}
LPSTR GetWinCEConfigFilename(LPCSTR path)
{
LPSTR pszFilename = (LPSTR)strstr(path, "=");
if (pszFilename)
{
pszFilename++;
}
return pszFilename;
}
void SaveConfigPath(LPCSTR pszFilename)
{
int l = strlen(pszFilename) + 1;
// 全局变量 在其他地方释放内存;
pszCfgFullpath = new char[l];
ZeroMemory(pszCfgFullpath, l);
strcpy(pszCfgFullpath, pszFilename);
}
void GenerateConfigTableCRT(struct soap *pSoap, poutStackBuffer pStack)
{
#ifdef TEST
WriteLog("Method GenerateConfigTableCRT");
#endif
const int BUFFER_SIZE = 128;
#if defined(DEBUG) || defined(WINCE)
if (pszCfgFullpath)
{
delete[] pszCfgFullpath;
pszCfgFullpath = NULL;
}
LPSTR pszFilename = GetWinCEConfigFilename(pSoap->path);
if (pszFilename)
{
SaveConfigPath(pszFilename);
}
else
{
WriteLog("read default config file.");
char fullpath[PATH_LEN] = {0};
LPSTR pszFilename = GetConfigFilename(pSoap->path);
GetFileFullPath(fullpath, pszFilename);
SaveConfigPath(fullpath);
}
#endif
FILE *pCfgFile = OpenWebFileCRT(pszCfgFullpath);
if (pCfgFile == NULL)
{
WriteLog("configfile not found!");
return;
}
LPSTR lpBuffer = new CHAR[BUFFER_SIZE];
if (lpBuffer)
{
ZeroMemory(lpBuffer, BUFFER_SIZE);
}
LPCSTR table = "<table id='config'>";
pushStack(pStack, table, strlen(table));
DWORD dwBytesRead = 0;
int state = 0;
do {
dwBytesRead = ReadFileBytesCRT(pCfgFile, lpBuffer, BUFFER_SIZE);
int start = 0;
int indexHtml = 0;
for (int i = 0; i < dwBytesRead; i++)
{
switch (PushStackAsStateChanged(pStack, &state, lpBuffer + i))
{
case 1:
case 3:
{
pushStack(pStack, lpBuffer + i);
}
default:
break;
}
}
SoapSendMyStack(pSoap, pStack);
} while (!(dwBytesRead < BUFFER_SIZE));
pushStack(pStack, "</table>", strlen("</table>"));
if (lpBuffer)
{
delete[] lpBuffer;
lpBuffer = NULL;
}
fclose(pCfgFile);
}
void GetHtml(struct soap *pSoap, LPCSTR lpszFilename)
{
#ifdef TEST
WriteLog("Method GetHtml lpszFilename %s", lpszFilename);
#endif
FILE *pfHtml = fopen(lpszFilename, "rt");
if (pfHtml == NULL)
{
WriteLog("open html file error!");
return;
}
const int BUFFER_SIZE = 1024 * 8;
char read_buf[BUFFER_SIZE] = {0};
outStackBuffer bufStack;
initStack(&bufStack, BUFFER_SIZE * 2);
//ShowInfo("get file success!");
DWORD dwBytesRead = 0;
int status = 0;
char lastCh = '\0';
int pushed = -1;
do {
int start = -1;
int end = -1;
ZeroMemory(read_buf, BUFFER_SIZE);
dwBytesRead = ReadFileBytesCRT(pfHtml, read_buf, BUFFER_SIZE);
// search for <% %> 不能嵌套使用
for (int i = 0; i < dwBytesRead; i++)
{
char ch = *(read_buf + i);
switch (ch)
{
case '\t':
case '\r':
case '\n':
break;
case '%':
{
if (status == 0 && lastCh == '<')
{
popStack(&bufStack);
GenerateConfigTableCRT(pSoap, &bufStack);
status++;
}
}
case '>':
{
if (status == 1 && lastCh == '%')
{
status--;
break;
}
}
default:
{
if (status == 0)
{
pushStack(&bufStack, ch);
}
}
}
lastCh = ch;
}
SoapSendMyStack(pSoap, &bufStack);
} while (!(dwBytesRead < BUFFER_SIZE));
deleteStack(&bufStack);
fclose(pfHtml);
}
DWORD WINAPI gSoapServer(LPVOID lpThreadParam)
{
#ifdef TEST
WriteLog("Method gSoapServer Param %s", lpThreadParam);
#endif
startSvr = true;
//ServiceService calc_service;
//calc_service.serve();
// soap_serve(soap_new());
// use the service operation request dispatcher
// open the log file.
int i = 1;
struct soap calc_soap;
int m, s; // master and slave sockets
// soap init
soap_init(&calc_soap);
my_soap_init(&calc_soap);
//
m = soap_bind(&calc_soap,
NULL, // 任何IP地址都可以访问
HTTP_SVR_PORT, // 端口
100); // 请求队列的长度
if (m < 0) //!soap_valid_socket(m)
{
WriteLog("Start Server Error!");
//
MessageBox(0, _T("Start Server Error!\n"), _T("Info"), MB_OK);
}
else
{
WriteLog("Start Server successful........");
//MessageBox(0, _T("Start Server successful........"), _T("Info"), MB_OK);
while (startSvr)
{
s = soap_accept(&calc_soap);
if (s < 0)
{
SoapErr(&calc_soap);
MessageBox(0, _T("soap_accept Error!"), _T("Error"), MB_OK);
break;
}
WriteLog("Thread %d accept socket %d connection from IP %d.%d.%d.%d, request %d",
soapSvrThdid, s, (calc_soap.ip >> 24) & 0xFF,
(calc_soap.ip >> 16) & 0xFF,
(calc_soap.ip >> 8) & 0xFF, calc_soap.ip & 0xFF, i++);
// process RPC request
if (soap_serve(&calc_soap) != SOAP_OK)
{}
WriteLog("request served");
// clean up class instances
// deallocate C++ data of old thread
soap_destroy(&calc_soap);
// clean up everything and close socket
// deallocate data of old thread
soap_end(&calc_soap);
}
}
soap_done(&calc_soap);
MessageBox(0, _T("soap_done!"), _T("Info"), MB_OK);
startSvr = false;
WriteLog("Web Server End........");
return 0;
}
// Implementation of the "add" service operation
int ns__add(struct soap *calc_soap, double a, double b, double &result)
{
result = a + b;
WriteLog("%lf + %lf = %lf", a, b, result);
return SOAP_OK;
}
// Implementation of the "winconfig" service operation
int ns__winconfig(struct soap* pSoap, char *key, char *value, bool &result)
{
result = false;
WriteLog("POST:path = %s %s = %s", pSoap->path, key, value);
if (strlen(key) == 0)
{
result = false;
return -1;
}
if (pszCfgFullpath == NULL)
{
result = false;
WriteLog("path has no the param file=configfilefullpath.");
return -1;
}
//char fullpath[PATH_LEN] = {0};
//LPSTR pszFilename = GetConfigFilename(pSoap->path);
//GetFileFullPath(fullpath, pszFilename);
//int len = strlen(fullpath) + 1;
//wchar_t* szFullpath = new wchar_t[len];
//MbToWc(fullpath, -1, szFullpath, len);
int len = strlen(pszCfgFullpath) + 1;
wchar_t* szFullpath = new wchar_t[len];
MbToWc(pszCfgFullpath, -1, szFullpath, len);
HANDLE hCfgFile = MyOpenFile(
szFullpath,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE);
if (hCfgFile == INVALID_HANDLE_VALUE)
{
result = false;
WriteLog("没有找到要修改的配置文件%s。", pszCfgFullpath);
return HRESULT_FROM_WIN32(GetLastError());
}
delete[] szFullpath;
DWORD FindKeyLite(HANDLE, LPSTR, LPSTR);
DWORD pFile = FindKeyLite(hCfgFile, key, value);
CloseHandle(hCfgFile);
if (pFile == 0)
{
result = false;
}
else
{
result = true;
}
return SOAP_OK;
}
void SoapErr(struct soap *soap)
{
WriteLog(NULL);
soap_print_fault(soap, stderr);
}
VOID MIMEType4FileType(struct soap *soap, FileType ft)
{
soap_response(soap, SOAP_FILE);
switch (ft)
{
case HTML:
// HTTP response header with text/html
soap->http_content = "text/html; charset=gb2312";
break;
case JS:
soap->http_content = "application/x-javascript;";
break;
case CSS:
soap->http_content = "text/css";
break;
default:
soap->http_content = "text/*; charset=UTF-8";
}
}
HANDLE OpenGetFile(LPCSTR lpszFilename)
{
HANDLE hFile;
// GetFullFilePath here.
int len = strlen(lpszFilename) + 1;
wchar_t* szFullpath = new wchar_t[len];
MbToWc(lpszFilename, -1, szFullpath, len);
hFile = OpenWebFile(szFullpath);
delete[] szFullpath;
return hFile;
}
void SendFile(struct soap *soap, LPSTR pszFilename)
{
const int BUFFER_SIZE = 1024 * 16;
DWORD dwBytesRead = 0;
char read_buf[BUFFER_SIZE] = {0};
HANDLE hFile = OpenGetFile(pszFilename);
if (hFile == INVALID_HANDLE_VALUE)
{
HRESULT hr = HRESULT_FROM_WIN32(::GetLastError());
WriteLog("GET file error %d!", hr);
return;
}
do {
int start = -1;
int end = -1;
ReadFileToBuffer(hFile,
read_buf,
BUFFER_SIZE,
&dwBytesRead);
soap_send_raw(soap, read_buf, dwBytesRead);
} while (!(dwBytesRead < BUFFER_SIZE));
CloseHandle(hFile);
}
int MyHttpGet(struct soap *soap)
{
#ifdef TEST
WriteLog("Method MyHttpGet Param %s", soap->id);
#endif
char szFilename[PATH_LEN] = {0};
FileType ft = GetFileFullPath(szFilename, soap->path);
WriteLog(szFilename);
MIMEType4FileType(soap, ft);
if (ft == HTML)
{
GetHtml(soap, szFilename);
}
else
{
SendFile(soap, szFilename);
}
soap_end_send(soap);
WriteLog("MyHttpGet %s", soap->path);
return SOAP_OK;
}
void MyCloseSoap(struct soap *psoap)
{
soap_destroy(psoap);
soap_end(psoap);
soap_done(psoap);
free(psoap);
}
DWORD WINAPI ProcessRequest(LPVOID lpThreadParam)
{
struct soap *psoap = (struct soap *)lpThreadParam;
if (soap_serve(psoap) != SOAP_OK) //
{
MessageBox(0, _T("soap_serve Error!"), _T("Error"), MB_OK);
}
MyCloseSoap(psoap);
return 0;
}
#define BACKLOG (100) // Max. request backlog
#define MAX_THR (3) // Max. threads to serve requests
#define MAX_QUEUE (100) // Max. size of request queue
SOAP_SOCKET queue[MAX_QUEUE]; // The global request of sockets
int head = 0, tail = 0; // Queue head and tail
void my_soap_init(struct soap *pSoap)
{
pSoap->send_timeout = 60; // 60 seconds
pSoap->recv_timeout = 60;
//pSoap->tc
//pSoap->accept_timeout = 0; 无限等待连接请求
//pSoap->max_keep_alive = 100;
//soap_clr_mode(pSoap, SOAP_C_UTFSTRING);
//soap_set_mode(pSoap, SOAP_C_MBSTRING);
//soap_omode(&calc_soap, SOAP_C_MBSTRING);
pSoap->fget = MyHttpGet;//CRT;
}