| ︙ | | | ︙ | |
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
** This file implements a very simple (and low-performance) HTTP server
** for windows. It also implements a Windows Service which allows the HTTP
** server to be run without any user logged on.
*/
#include "config.h"
#ifdef _WIN32
/* This code is for win32 only */
#include <windows.h>
#include <process.h>
#include "winhttp.h"
/*
** The HttpServer structure holds information about an instance of
** the HTTP server itself.
|
>
>
|
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
** This file implements a very simple (and low-performance) HTTP server
** for windows. It also implements a Windows Service which allows the HTTP
** server to be run without any user logged on.
*/
#include "config.h"
#ifdef _WIN32
/* This code is for win32 only */
#include <ws2tcpip.h>
#include <mstcpip.h>
#include <windows.h>
#include <process.h>
#include "winhttp.h"
/*
** The HttpServer structure holds information about an instance of
** the HTTP server itself.
|
| ︙ | | | ︙ | |
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
** The HttpRequest structure holds information about each incoming
** HTTP request.
*/
typedef struct HttpRequest HttpRequest;
struct HttpRequest {
int id; /* ID counter */
SOCKET s; /* Socket on which to receive data */
SOCKADDR_IN addr; /* Address from which data is coming */
int flags; /* Flags passed to win32_http_server() */
const char *zOptions; /* --baseurl, --notfound, --localauth, --th-trace */
};
/*
** Prefix for a temporary file.
*/
|
|
|
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
** The HttpRequest structure holds information about each incoming
** HTTP request.
*/
typedef struct HttpRequest HttpRequest;
struct HttpRequest {
int id; /* ID counter */
SOCKET s; /* Socket on which to receive data */
SOCKADDR_IN6 addr; /* Address from which data is coming */
int flags; /* Flags passed to win32_http_server() */
const char *zOptions; /* --baseurl, --notfound, --localauth, --th-trace */
};
/*
** Prefix for a temporary file.
*/
|
| ︙ | | | ︙ | |
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
*/
static void win32_http_request(void *pAppData){
HttpRequest *p = (HttpRequest*)pAppData;
FILE *in = 0, *out = 0, *aux = 0;
int amt, got, i;
int wanted = 0;
char *z;
char zCmdFName[MAX_PATH];
char zRequestFName[MAX_PATH];
char zReplyFName[MAX_PATH];
char zCmd[2000]; /* Command-line to process the request */
char zHdr[4000]; /* The HTTP request header */
sqlite3_snprintf(MAX_PATH, zCmdFName,
|
>
>
|
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
*/
static void win32_http_request(void *pAppData){
HttpRequest *p = (HttpRequest*)pAppData;
FILE *in = 0, *out = 0, *aux = 0;
int amt, got, i;
int wanted = 0;
char *z;
char zIp[50];
DWORD nIp = sizeof(zIp);
char zCmdFName[MAX_PATH];
char zRequestFName[MAX_PATH];
char zReplyFName[MAX_PATH];
char zCmd[2000]; /* Command-line to process the request */
char zHdr[4000]; /* The HTTP request header */
sqlite3_snprintf(MAX_PATH, zCmdFName,
|
| ︙ | | | ︙ | |
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
}
/*
** The repository name is only needed if there was no open checkout. This
** is designed to allow the open checkout for the interactive user to work
** with the local Fossil server started via the "ui" command.
*/
if( (p->flags & HTTP_SERVER_HAD_CHECKOUT)==0 ){
assert( g.zRepositoryName && g.zRepositoryName[0] );
sqlite3_snprintf(sizeof(zCmd), zCmd, "%s%s\n%s\n%s\n%s",
get_utf8_bom(0), zRequestFName, zReplyFName, inet_ntoa(p->addr.sin_addr),
g.zRepositoryName
);
}else{
sqlite3_snprintf(sizeof(zCmd), zCmd, "%s%s\n%s\n%s",
get_utf8_bom(0), zRequestFName, zReplyFName, inet_ntoa(p->addr.sin_addr)
);
}
aux = fossil_fopen(zCmdFName, "wb");
if( aux==0 ) goto end_request;
fwrite(zCmd, 1, strlen(zCmd), aux);
sqlite3_snprintf(sizeof(zCmd), zCmd, "\"%s\" http -args \"%s\" --nossl%s",
|
>
>
>
>
|
<
|
|
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
|
}
/*
** The repository name is only needed if there was no open checkout. This
** is designed to allow the open checkout for the interactive user to work
** with the local Fossil server started via the "ui" command.
*/
if( WSAAddressToStringA((SOCKADDR*)&p->addr, sizeof(p->addr),
NULL, zIp, &nIp)!=0 ){
zIp[0] = 0;
}
if( (p->flags & HTTP_SERVER_HAD_CHECKOUT)==0 ){
assert( g.zRepositoryName && g.zRepositoryName[0] );
sqlite3_snprintf(sizeof(zCmd), zCmd, "%s%s\n%s\n%s\n%s",
get_utf8_bom(0), zRequestFName, zReplyFName, zIp, g.zRepositoryName
);
}else{
sqlite3_snprintf(sizeof(zCmd), zCmd, "%s%s\n%s\n%s",
get_utf8_bom(0), zRequestFName, zReplyFName, zIp
);
}
aux = fossil_fopen(zCmdFName, "wb");
if( aux==0 ) goto end_request;
fwrite(zCmd, 1, strlen(zCmd), aux);
sqlite3_snprintf(sizeof(zCmd), zCmd, "\"%s\" http -args \"%s\" --nossl%s",
|
| ︙ | | | ︙ | |
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
** Process a single incoming SCGI request.
*/
static void win32_scgi_request(void *pAppData){
HttpRequest *p = (HttpRequest*)pAppData;
FILE *in = 0, *out = 0;
int amt, got, nHdr, i;
int wanted = 0;
char zRequestFName[MAX_PATH];
char zReplyFName[MAX_PATH];
char zCmd[2000]; /* Command-line to process the request */
char zHdr[4000]; /* The SCGI request header */
sqlite3_snprintf(MAX_PATH, zRequestFName,
"%s_%06d_in.txt", zTempPrefix, p->id);
|
>
>
|
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
** Process a single incoming SCGI request.
*/
static void win32_scgi_request(void *pAppData){
HttpRequest *p = (HttpRequest*)pAppData;
FILE *in = 0, *out = 0;
int amt, got, nHdr, i;
int wanted = 0;
char zIp[50];
DWORD nIp = sizeof(zIp);
char zRequestFName[MAX_PATH];
char zReplyFName[MAX_PATH];
char zCmd[2000]; /* Command-line to process the request */
char zHdr[4000]; /* The SCGI request header */
sqlite3_snprintf(MAX_PATH, zRequestFName,
"%s_%06d_in.txt", zTempPrefix, p->id);
|
| ︙ | | | ︙ | |
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
while( wanted>amt ){
got = recv(p->s, zHdr, wanted<sizeof(zHdr) ? wanted : sizeof(zHdr), 0);
if( got<=0 ) break;
fwrite(zHdr, 1, got, out);
wanted += got;
}
assert( g.zRepositoryName && g.zRepositoryName[0] );
sqlite3_snprintf(sizeof(zCmd), zCmd,
"\"%s\" http \"%s\" \"%s\" %s \"%s\" --scgi --nossl%s",
g.nameOfExe, zRequestFName, zReplyFName, inet_ntoa(p->addr.sin_addr),
g.zRepositoryName, p->zOptions
);
in = fossil_fopen(zReplyFName, "w+b");
fflush(out);
fossil_system(zCmd);
if( in ){
while( (got = fread(zHdr, 1, sizeof(zHdr), in))>0 ){
|
>
>
>
>
|
|
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
while( wanted>amt ){
got = recv(p->s, zHdr, wanted<sizeof(zHdr) ? wanted : sizeof(zHdr), 0);
if( got<=0 ) break;
fwrite(zHdr, 1, got, out);
wanted += got;
}
assert( g.zRepositoryName && g.zRepositoryName[0] );
if (WSAAddressToStringA((SOCKADDR*)&p->addr, sizeof(p->addr),
NULL, zIp, &nIp)!=0){
zIp[0] = 0;
}
sqlite3_snprintf(sizeof(zCmd), zCmd,
"\"%s\" http \"%s\" \"%s\" %s \"%s\" --scgi --nossl%s",
g.nameOfExe, zRequestFName, zReplyFName, zIp,
g.zRepositoryName, p->zOptions
);
in = fossil_fopen(zReplyFName, "w+b");
fflush(out);
fossil_system(zCmd);
if( in ){
while( (got = fread(zHdr, 1, sizeof(zHdr), in))>0 ){
|
| ︙ | | | ︙ | |
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
const char *zFileGlob, /* The --fileglob option, or NULL */
const char *zIpAddr, /* Bind to this IP address, if not NULL */
int flags /* One or more HTTP_SERVER_ flags */
){
HANDLE hStoppedEvent;
WSADATA wd;
SOCKET s = INVALID_SOCKET;
SOCKADDR_IN addr;
int idCnt = 0;
int iPort = mnPort;
Blob options;
wchar_t zTmpPath[MAX_PATH];
const char *zSkin;
#if USE_SEE
const char *zSavedKey = 0;
|
|
|
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
const char *zFileGlob, /* The --fileglob option, or NULL */
const char *zIpAddr, /* Bind to this IP address, if not NULL */
int flags /* One or more HTTP_SERVER_ flags */
){
HANDLE hStoppedEvent;
WSADATA wd;
SOCKET s = INVALID_SOCKET;
SOCKADDR_IN6 addr;
int idCnt = 0;
int iPort = mnPort;
Blob options;
wchar_t zTmpPath[MAX_PATH];
const char *zSkin;
#if USE_SEE
const char *zSavedKey = 0;
|
| ︙ | | | ︙ | |
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
|
zSavedKey, savedKeySize);
}
#endif
if( WSAStartup(MAKEWORD(1,1), &wd) ){
fossil_fatal("unable to initialize winsock");
}
while( iPort<=mxPort ){
s = socket(AF_INET, SOCK_STREAM, 0);
if( s==INVALID_SOCKET ){
fossil_fatal("unable to create a socket");
}
addr.sin_family = AF_INET;
addr.sin_port = htons(iPort);
if( zIpAddr ){
addr.sin_addr.s_addr = inet_addr(zIpAddr);
if( addr.sin_addr.s_addr == (-1) ){
fossil_fatal("not a valid IP address: %s", zIpAddr);
}
}else if( flags & HTTP_SERVER_LOCALHOST ){
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
}else{
addr.sin_addr.s_addr = htonl(INADDR_ANY);
}
if( bind(s, (struct sockaddr*)&addr, sizeof(addr))==SOCKET_ERROR ){
closesocket(s);
iPort++;
continue;
}
if( listen(s, SOMAXCONN)==SOCKET_ERROR ){
|
>
|
|
|
>
>
|
>
>
|
>
>
>
>
>
>
|
|
>
>
>
>
>
>
>
|
>
>
>
|
>
>
|
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
|
zSavedKey, savedKeySize);
}
#endif
if( WSAStartup(MAKEWORD(1,1), &wd) ){
fossil_fatal("unable to initialize winsock");
}
while( iPort<=mxPort ){
DWORD ipv6only = 0;
s = socket(AF_INET6, SOCK_STREAM, 0);
if( s==INVALID_SOCKET ){
fossil_fatal("unable to create a socket");
}
setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&ipv6only,
sizeof(ipv6only));
if( zIpAddr ){
int addrlen = sizeof(addr);
memset(&addr, 0, sizeof(addr));
addr.sin6_family = AF_INET6;
if (WSAStringToAddress((LPSTR)zIpAddr, AF_INET6, NULL,
(struct sockaddr *)&addr, &addrlen) != 0){
SOCKADDR_IN addrv4;
SCOPE_ID scope;
memset(&addrv4, 0, sizeof(addrv4));
addrv4.sin_family = AF_INET;
int addrlen = sizeof(addrv4);
if (WSAStringToAddress((LPSTR)zIpAddr, AF_INET, NULL,
(struct sockaddr *)&addrv4, &addrlen) != 0){
fossil_fatal("not a valid IP address: %s", zIpAddr);
}
memset(&addr, 0, sizeof(addr));
memset(&scope, 0, sizeof(scope));
IN6ADDR_SETV4MAPPED(&addr, &addrv4.sin_addr, scope, htons(iPort));
}else{
((SOCKADDR_IN6*)&addr)->sin6_port = htons(iPort);
}
}else if( flags & HTTP_SERVER_LOCALHOST ){
SCOPE_ID scope;
memset(&addr, 0, sizeof(addr));
memset(&scope, 0, sizeof(scope));
IN6ADDR_SETV4MAPPED(&addr, &in4addr_loopback, scope, htons(iPort));
}else{
memset(&addr, 0, sizeof(addr));
addr.sin6_family = AF_INET6;
addr.sin6_port = htons(iPort);
memcpy(&addr.sin6_addr, &in6addr_any, sizeof(in6addr_any));
}
if( bind(s, (struct sockaddr*)&addr, sizeof(addr))==SOCKET_ERROR ){
closesocket(s);
iPort++;
continue;
}
if( listen(s, SOMAXCONN)==SOCKET_ERROR ){
|
| ︙ | | | ︙ | |
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
_beginthread(win32_server_stopper, 0, (void*)pServer);
}
/* Set the service status to running and pass the listener socket to the
** service handling procedures. */
win32_http_service_running(s);
for(;;){
SOCKET client;
SOCKADDR_IN client_addr;
HttpRequest *pRequest;
int len = sizeof(client_addr);
int wsaError;
client = accept(s, (struct sockaddr*)&client_addr, &len);
if( client==INVALID_SOCKET ){
/* If the service control handler has closed the listener socket,
|
|
|
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
|
_beginthread(win32_server_stopper, 0, (void*)pServer);
}
/* Set the service status to running and pass the listener socket to the
** service handling procedures. */
win32_http_service_running(s);
for(;;){
SOCKET client;
SOCKADDR_IN6 client_addr;
HttpRequest *pRequest;
int len = sizeof(client_addr);
int wsaError;
client = accept(s, (struct sockaddr*)&client_addr, &len);
if( client==INVALID_SOCKET ){
/* If the service control handler has closed the listener socket,
|
| ︙ | | | ︙ | |