Repository navigation
Expand file tree
/
Copy pathWebSrv.cpp
More file actions
397 lines (372 loc) · 10.1 KB
/
Copy pathWebSrv.cpp
File metadata and controls
397 lines (372 loc) · 10.1 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
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
/*
* WebSrv.cpp
*
* Manage WebServer for WinVetSim
*
* We use PHP for a simple web service. The port can be 80, but can also be configured differently to allow
* operarion with a stanard web server on the same host.
*
* This file is part of the sim-mgr distribution
* TODO: Add link to GitHub for WinVetSim
*
* Copyright (c) 2021 VetSim, Cornell University College of Veterinary Medicine Ithaca, NY
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "vetsim.h"
using namespace std;
int checkURL(string url, string port, string page);
int isServerRunning(void)
{
string host;
string page;
string port;
int sts;
host = PHP_SERVER_ADDR;
port = std::to_string(PHP_SERVER_PORT );
page = "sim-ii/hostCheck.php";
sts = checkURL(host, port, page);
//cout << "URL: " << host << ":" << port << "/" << page << " sts: " << sts << endl;
return (sts);
}
void
stopPHPServer(void)
{
char cmd[200] = "taskkill /FI \"WINDOWTITLE eq WinVetSim PHP\"";
char mbuf[300];
STARTUPINFOA si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
BOOL created = CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
if (!created)
{
DWORD error = GetLastError();
// Log the command line that was attempted
log_message("", cmd);
sprintf_s(mbuf, 300, "CreateProcess failed with error %lu: %s", error, std::system_category().message(error).c_str());
printf("%s\n", mbuf);
log_message("", mbuf);
}
else
{
// We don't need the handles; close them.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
}
char phpPath[FILENAME_MAX];
int
findPhpPath(void)
{
HANDLE hFind;
WIN32_FIND_DATA FindFileData;
// Local copy is preferred
if ((hFind = FindFirstFile(L"./PHP8.0/*.exe", &FindFileData)) != INVALID_HANDLE_VALUE)
{
do {
if (wcscmp(FindFileData.cFileName, L"php.exe") == 0)
{
sprintf_s(phpPath, "%s", "./PHP8.0/");
FindClose(hFind);
return (1);
}
} while (FindNextFile(hFind, &FindFileData));
FindClose(hFind);
}
// Shared 64-Bit
if ((hFind = FindFirstFile(L"C:\\Program Files/WinVetSim/PHP8.0/*.exe", &FindFileData)) != INVALID_HANDLE_VALUE)
{
do {
//printf("Found %S\n", FindFileData.cFileName);
if (wcscmp(FindFileData.cFileName, L"php.exe") == 0)
{
sprintf_s(phpPath, "%s", "C:/Program Files/WinVetSim/PHP8.0");
FindClose(hFind);
return (1);
}
} while (FindNextFile(hFind, &FindFileData));
FindClose(hFind);
}
else
{
printf("No files found in %s\n", "C:/Program Files/WinVetSim/PHP/V8.0");
}
// Shared 32-bit
if ((hFind = FindFirstFile(L"C:/Program Files (x86)/PHP/v8.0/*.exe", &FindFileData)) != INVALID_HANDLE_VALUE)
{
do {
if (wcscmp(FindFileData.cFileName, L"php.exe") == 0)
{
sprintf_s(phpPath, "%s", "C:/Program Files (x86)/PHP/8.0");
FindClose(hFind);
return (1);
}
} while (FindNextFile(hFind, &FindFileData));
FindClose(hFind);
}
// Shared 64-Bit
if ((hFind = FindFirstFile(L"C:/Program Files/PHP/v7.4/*.exe", &FindFileData)) != INVALID_HANDLE_VALUE)
{
do {
if (wcscmp(FindFileData.cFileName, L"php.exe") == 0)
{
sprintf_s(phpPath, "%s", "C:/Program Files/PHP/v7.4");
FindClose(hFind);
return (1);
}
} while (FindNextFile(hFind, &FindFileData));
FindClose(hFind);
}
// Shared 32-Bit
if ((hFind = FindFirstFile(L"C:/Program Files (x86)/PHP/v7.4/*.exe", &FindFileData)) != INVALID_HANDLE_VALUE)
{
do {
if (wcscmp(FindFileData.cFileName, L"php.exe") == 0)
{
sprintf_s(phpPath, "%s", "C:/Program Files (x86)/PHP/v7.4");
FindClose(hFind);
return (1);
}
} while (FindNextFile(hFind, &FindFileData));
FindClose(hFind);
}
// Shared 64-Bit
if ((hFind = FindFirstFile(L"C:/Program Files/PHP/v7.3/*.exe", &FindFileData)) != INVALID_HANDLE_VALUE)
{
do {
if (wcscmp(FindFileData.cFileName, L"php.exe") == 0)
{
sprintf_s(phpPath, "%s", "C:/Program Files/PHP/v7.3");
FindClose(hFind);
return (1);
}
} while (FindNextFile(hFind, &FindFileData));
FindClose(hFind);
}
// Shared 32-Bit
if ((hFind = FindFirstFile(L"C:/Program Files (x86)/PHP/v7.3/*.exe", &FindFileData)) != INVALID_HANDLE_VALUE)
{
do {
if (wcscmp(FindFileData.cFileName, L"php.exe") == 0)
{
sprintf_s(phpPath, "%s", "C:/Program Files (x86)/PHP/v7.3");
FindClose(hFind);
return (1);
}
} while (FindNextFile(hFind, &FindFileData));
FindClose(hFind);
}
// Shared 64-Bit
if ((hFind = FindFirstFile(L"C:/Program Files/PHP/v7.2/*.exe", &FindFileData)) != INVALID_HANDLE_VALUE)
{
do {
if (wcscmp(FindFileData.cFileName, L"php.exe") == 0)
{
sprintf_s(phpPath, "%s", "C:/Program Files/PHP/v7.2");
FindClose(hFind);
return (1);
}
} while (FindNextFile(hFind, &FindFileData));
FindClose(hFind);
}
// Shared 32-Bit
if ((hFind = FindFirstFile(L"C:/Program Files (x86)/PHP/v7.2/*.exe", &FindFileData)) != INVALID_HANDLE_VALUE)
{
do {
if (wcscmp(FindFileData.cFileName, L"php.exe") == 0)
{
sprintf_s(phpPath, "%s", "C:/Program Files (x86)/PHP/v7.2");
FindClose(hFind);
return (1);
}
} while (FindNextFile(hFind, &FindFileData));
FindClose(hFind);
}
return (0);
}
int startPHPServer(void )
{
char commandLine[2048];
int rval = -1;
int checkCount;
char mbuf[300];
if (isServerRunning() == 0)
{
if (findPhpPath() == 0)
{
sprintf_s(mbuf, 300, "%s", "findPhpPath() Fails");
log_message("", mbuf);
printf("Cannot find PHP\n");
return ( rval );
}
printf("Starting PHP Server %s\n", phpPath );
// start [<title>] //d <path>] [program [<parameter>...]]
sprintf_s(commandLine, 2048,
//"cmd /c start \"WinVetSim PHP\" /d \"%s\" /min \"%s/php.exe\" -S %s:%d sim-ii/router.php",
//localConfig.html_path, phpPath, PHP_SERVER_ADDR, PHP_SERVER_PORT );
"\"%s/php.exe\" -S %s:%d sim-ii/router.php",
phpPath, PHP_SERVER_ADDR, PHP_SERVER_PORT );
STARTUPINFOA si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
BOOL created;
DWORD fAtr;
if ( ( strlen(localConfig.html_path) != 0 ) && (fAtr = GetFileAttributesA(localConfig.html_path) ) & FILE_ATTRIBUTE_DIRECTORY )
{
// Good path. At least its a directory
sprintf_s(mbuf, 300, "starting PHP: %s in directory %s", commandLine, localConfig.html_path);
created = CreateProcessA(NULL, commandLine, NULL, NULL, FALSE, 0, NULL, localConfig.html_path, &si, &pi);
}
else
{
// Unknown HTML path. Start in current directory
sprintf_s(mbuf, 300, "starting PHP: %s in current directory", commandLine );
created = CreateProcessA(NULL, commandLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
}
log_message("", mbuf);
if (!created)
{
DWORD error = GetLastError();
// Log the command line that was attempted
log_message("", commandLine);
sprintf_s(mbuf, 300, "CreateProcess failed with error %lu: %s", error, std::system_category().message(error).c_str());
printf("%s\n", mbuf);
log_message("", mbuf);
}
else
{
// We don't need the handles; close them.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
sprintf_s(mbuf, 300, "CreateProcess PHP %s", "Started OK");
log_message("", mbuf);
//
for ( checkCount = 100 ; checkCount > 0; checkCount-- )
{
Sleep(10);
if (isServerRunning() != 0)
{
rval = 0;
break;
}
}
}
else
{
sprintf_s(mbuf, 90, "%s", "isServerRunning() says PHP is Already Running");
log_message("", mbuf);
rval = 0;
}
return (rval);
}
int
checkURL(string host, string port, string page)
{
WSADATA wsaData;
SOCKET Socket;
int lineCount = 0;
int rowCount = 0;
locale local;
char buffer[10000];
int i = 0;
int nDataLength = 0;
string website_HTML;
DWORD dwRetval;
int found = 0;
int sts;
struct addrinfo* result = NULL;
struct addrinfo* ptr = NULL;
struct addrinfo hints;
//HTTP GET
string get_http = "GET /" + page +" HTTP/1.1\r\nHost: " + host + "\r\nConnection: close\r\n\r\n";
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
cout << "WSAStartup failed.\n";
return 0;
}
// host = gethostbyname(url.c_str());
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
cout << host << ":" << port << endl;
dwRetval = getaddrinfo(host.c_str(),
port.c_str(),
&hints,
&result);
if (dwRetval != 0) {
printf("getaddrinfo failed with error: %d\n", dwRetval);
WSACleanup();
return 0;
}
for (ptr = result; ptr != NULL; ptr = ptr->ai_next)
{
switch (ptr->ai_family)
{
case AF_INET:
found = 1;
break;
}
if (found) break;
}
if (found)
{
Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (Socket != -1)
{
sts = connect(Socket, ptr->ai_addr, (int)ptr->ai_addrlen);
if ( sts == 0 )
{
//cout << "Connected" << endl;
// send GET / HTTP
sts = send(Socket, get_http.c_str(),(int) strlen(get_http.c_str()), 0);
//cout << "send returns " << sts << endl;
// recieve html
i = 0;
while ((nDataLength = recv(Socket, buffer, 10000, 0)) > 0) {
//cout << "recv returns " << nDataLength << endl;
while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') {
website_HTML += buffer[i];
i += 1;
}
}
//cout << "recv returns " << i << endl;
// Display HTML source
cout << "HTML: " << website_HTML << endl;
}
else
{
//cout << "Connect failed" << endl;
i = 0;
}
}
else
{
//cout << "socket fails" << endl;
i = 0;
}
closesocket(Socket);
}
freeaddrinfo(result);
WSACleanup();
// Display HTML source
//cout << website_HTML;
// pause
//cout << "\n\nPress ANY key to close.\n\n";
//cin.ignore(); cin.get();
return ((int)website_HTML.length());
}