Newer
Older
if(get_req(&session)) {
,session.req.request,session.req.method,session.http_ver);
if((session.http_ver<HTTP_1_0)||parse_headers(&session)) {
if(check_request(&session)) {
respond(&session);
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
}
}
}
}
}
void DLLCALL web_terminate(void)
{
recycle_server=FALSE;
if(server_socket!=INVALID_SOCKET) {
lprintf("%04d Web Terminate: closing socket",server_socket);
close_socket(server_socket);
}
}
static void cleanup(int code)
{
free_cfg(&scfg);
if(server_socket!=INVALID_SOCKET) {
close_socket(server_socket);
server_socket=INVALID_SOCKET;
}
update_clients();
#ifdef _WINSOCKAPI_
if(WSAInitialized && WSACleanup()!=0)
lprintf("0000 !WSACleanup ERROR %d",ERROR_VALUE);
#endif
thread_down();
status("Down");
lprintf("#### Web Server thread terminated (%u threads remain)", thread_count);
if(startup!=NULL && startup->terminated!=NULL)
startup->terminated(code);
}
const char* DLLCALL web_ver(void)
{
static char ver[256];
char compiler[32];
DESCRIBE_COMPILER(compiler);
sscanf("$Revision$" + 11, "%s", revision);
sprintf(ver,"%s %s%s "
"Compiled %s %s with %s"
,server_name
,revision
#ifdef _DEBUG
," Debug"
#else
,""
#endif
,__DATE__, __TIME__, compiler);
return(ver);
}
void DLLCALL web_server(void* arg)
{
int i;
int result;
time_t start;
WORD host_port;
char path[MAX_PATH+1];
char logstr[256];
SOCKADDR_IN server_addr={0};
SOCKADDR_IN client_addr;
socklen_t client_addr_len;
SOCKET client_socket;
SOCKET high_socket_set;
fd_set socket_set;
time_t t;
time_t initialized=0;
char host_ip[32];
char compiler[32];
http_session_t * session;
struct timeval tv;
startup=(web_startup_t*)arg;
if(startup==NULL) {
sbbs_beep(100,500);
fprintf(stderr, "No startup structure passed!\n");
return;
}
if(startup->size!=sizeof(web_startup_t)) { /* verify size */
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
sbbs_beep(100,500);
sbbs_beep(300,500);
sbbs_beep(100,500);
fprintf(stderr, "Invalid startup structure!\n");
return;
}
/* Setup intelligent defaults */
if(startup->port==0) startup->port=IPPORT_HTTP;
if(startup->index_file_name[0]==0) SAFECOPY(startup->index_file_name,"index.html");
startup->recycle_now=FALSE;
recycle_server=TRUE;
do {
thread_up(FALSE /* setuid */);
status("Initializing");
memset(&scfg, 0, sizeof(scfg));
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
,revision
#ifdef _DEBUG
," Debug"
#else
,""
#endif
);
DESCRIBE_COMPILER(compiler);
lprintf("Compiled %s %s with %s", __DATE__, __TIME__, compiler);
srand(time(NULL)); /* Seed random number generator */
sbbs_random(10); /* Throw away first number */
if(!winsock_startup()) {
cleanup(1);
return;
}
t=time(NULL);
lprintf("Initializing on %.24s with options: %lx"
,ctime(&t),startup->options);
/* Initial configuration and load from CNF files */
SAFECOPY(scfg.ctrl_dir,startup->ctrl_dir);
lprintf("Loading configuration files from %s", scfg.ctrl_dir);
scfg.size=sizeof(scfg);
SAFECOPY(logstr,UNKNOWN_LOAD_ERROR);
if(!load_cfg(&scfg, NULL, FALSE, logstr)) {
lprintf("!ERROR %s",logstr);
lprintf("!FAILED to load configuration files");
cleanup(1);
return;
}
scfg_reloaded=TRUE;
if(!read_mime_types()) {
lprintf("!ERROR %d reading mime_types.cfg", ERROR_VALUE);
cleanup(1);
return;
}
if(startup->host_name[0]==0)
SAFECOPY(startup->host_name,scfg.sys_inetaddr);
if(!(scfg.sys_misc&SM_LOCAL_TZ) && !(startup->options&BBS_OPT_LOCAL_TIMEZONE)) {
if(putenv("TZ=UTC0"))
lprintf("!putenv() FAILED");
tzset();
}
/* open a socket and wait for a client */
server_socket = open_socket(SOCK_STREAM);
if(server_socket == INVALID_SOCKET) {
lprintf("!ERROR %d creating HTTP socket", ERROR_VALUE);
cleanup(1);
return;
}
lprintf("HTTP socket %d opened",server_socket);
/*****************************/
/* Listen for incoming calls */
/*****************************/
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_addr.s_addr = htonl(startup->interface_addr);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(startup->port);
if(startup->seteuid!=NULL)
startup->seteuid(FALSE);
result = bind(server_socket,(struct sockaddr *)&server_addr,sizeof(server_addr));
if(startup->seteuid!=NULL)
startup->seteuid(TRUE);
if(result != 0) {
lprintf("!ERROR %d (%d) binding HTTP socket to port %d"
,result, ERROR_VALUE,startup->port);
lprintf(BIND_FAILURE_HELP);
cleanup(1);
return;
}
result = listen(server_socket, 1);
if(result != 0) {
lprintf("!ERROR %d (%d) listening on HTTP socket", result, ERROR_VALUE);
cleanup(1);
return;
}
lprintf("Web Server listening on port %d",startup->port);
status("Listening");
/* signal caller that we've started up successfully */
if(startup->started!=NULL)
startup->started();
lprintf("Web Server thread started.");
while(server_socket!=INVALID_SOCKET) {
/* check for re-cycle semaphores */
if(!(startup->options&BBS_OPT_NO_RECYCLE)) {
sprintf(path,"%swebsrvr.rec",scfg.ctrl_dir);
t=fdate(path);
if(!active_clients && t!=-1 && t>initialized) {
lprintf("0000 Recycle semaphore file (%s) detected",path);
initialized=t;
break;
}
if(!active_clients && startup->recycle_now==TRUE) {
lprintf("0000 Recycle semaphore signaled");
startup->recycle_now=FALSE;
break;
}
}
/* now wait for connection */
FD_ZERO(&socket_set);
FD_SET(server_socket,&socket_set);
high_socket_set=server_socket+1;
tv.tv_sec=2;
tv.tv_usec=0;
if((i=select(high_socket_set,&socket_set,NULL,NULL,&tv))<1) {
if(i==0) {
mswait(1);
continue;
}
if(ERROR_VALUE==EINTR)
lprintf("Web Server listening interrupted");
else if(ERROR_VALUE == ENOTSOCK)
lprintf("Web Server socket closed");
else
lprintf("!ERROR %d selecting socket",ERROR_VALUE);
break;
}
client_addr_len = sizeof(client_addr);
if(FD_ISSET(server_socket,&socket_set))
client_socket = accept(server_socket, (struct sockaddr *)&client_addr
,&client_addr_len);
else {
lprintf("!NO SOCKETS set by select");
continue;
}
if(client_socket == INVALID_SOCKET) {
if(ERROR_VALUE == ENOTSOCK || ERROR_VALUE == EINTR) {
lprintf("HTTP socket closed");
break;
}
lprintf("!ERROR %d accepting connection", ERROR_VALUE);
}
strcpy(host_ip,inet_ntoa(client_addr.sin_addr));
host_port=ntohs(client_addr.sin_port);
lprintf("%04d HTTP connection accepted from: %s port %u"
,client_socket
,host_ip, host_port);
if(startup->socket_open!=NULL)
startup->socket_open(TRUE);
if((session=malloc(sizeof(http_session_t)))==NULL) {
lprintf("%04d !ERROR allocating %u bytes of memory for service_client"
,client_socket, sizeof(http_session_t));
mswait(3000);
close_socket(client_socket);
continue;
}
memset(session, 0, sizeof(http_session_t));
session->addr=client_addr;
session->socket=client_socket;
_beginthread(http_session_thread, 0, session);
}
lprintf("Closing HTTP socket %d", server_socket);
close_socket(server_socket);
server_socket=INVALID_SOCKET;
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
if(http_threads_running) {
lprintf("Waiting for %d connection threads to terminate...", http_threads_running);
start=time(NULL);
while(http_threads_running) {
if(time(NULL)-start>TIMEOUT_THREAD_WAIT) {
lprintf("!TIMEOUT waiting for %d http thread(s) to "
"terminate", http_threads_running);
break;
}
mswait(100);
}
}
cleanup(0);
if(recycle_server) {
lprintf("Recycling server...");
mswait(2000);
}
} while(recycle_server);
}