View Source

[modwtcl]
apreq.h
dist/
libapreq1/
mod_wtcl.c
mod_wtcl.h
setupExclude.txt
util/
win.def
win.mak
wt1.1/
WtAppTable.c
WtAppTable.h
WtBasicCmds.c
WtBasicCmds.h
WtClientRequest.c
WtClientRequest.h
WtCollection.c
WtCollection.h
WtCollectionCmds.c
WtCollectionCmds.h
WtContext.c
WtContext.h
WtContextEvents.c
WtContextEvents.h
WtCookie.c
WtCookie.h
WtDbSession.c
WtDbSession.h
WtExecute.c
WtExecute.h
WtHtmlEntities.c
WtHtmlEntities.h
WtInitCmds.c
WtInitCmds.h
WtMtTable.c
WtMtTable.h
WtMultiTable.c
WtMultiTable.h
WtOS.h
WtProcSession.c
WtProcSession.h
WtResponse.c
WtResponse.h
WtServerCmds.c
WtServerCmds.h
WtSession.c
WtSession.h
WtSettings.c
WtSettings.h
WtTable.c
WtTable.h
WtTableCmds.c
WtTableCmds.h
WtTableUtil.c
WtTableUtil.h
WtUpload.c
WtUpload.h
WtUtil.c
WtUtil.h
WtWebErrors.c
WtWebErrors.h
WtWindows.h
File: / archive / modwtcl / WtServerCmds.c

Lines Size Modified Created Owner MIME Types
520 13,737 2010/06/13 00:48:32 2011/06/13 15:35:15 BUILTIN\Administrators text/x-csrc

0001
/*
0002
 * Copyright 2001 Alexander Boverman and the original authors.
0003
 * 
0004
 * Licensed under the Apache License, Version 2.0 (the "License");
0005
 * you may not use this file except in compliance with the License.
0006
 * You may obtain a copy of the License at
0007
 * 
0008
 *      http://www.apache.org/licenses/LICENSE-2.0
0009
 * 
0010
 * Unless required by applicable law or agreed to in writing, software
0011
 * distributed under the License is distributed on an "AS IS" BASIS,
0012
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013
 * See the License for the specific language governing permissions and
0014
 * limitations under the License.
0015
 */
0016
0017
#include <httpd.h>
0018
#include <http_request.h>
0019
#include <util_md5.h>
0020
#include "WtOS.h"
0021
#include "WtBasicCmds.h"
0022
#include "WtExecute.h"
0023
#include "WtTable.h"
0024
#include "WtTableUtil.h"
0025
#include "WtMultiTable.h"
0026
#include "WtContext.h"
0027
#include "WtUtil.h"
0028
#include "WtContextEvents.h"
0029
0030
/* serverVars command */
0031
0032
int WtServerVarsCmd(ClientData clientData, Tcl_Interp *interp,
0033
    int objc, Tcl_Obj *const objv[])
0034
{
0035
  int ret = TCL_ERROR;
0036
  WtContext *w = WtGetAssocContext(interp);
0037
0038
  if (w->web->readEnvStatus == 0) {
0039
    w->web->readEnvStatus = 2;
0040
0041
    ap_add_common_vars(w->web->apReq);
0042
    ap_add_cgi_vars(w->web->apReq);
0043
0044
    if (WtOwnTable(&w->web->serverVars, interp)) {
0045
      if (WtUpdateTableFromApNoMulti(w->web->serverVars,
0046
          w->web->apReq->subprocess_env, interp)) {
0047
        ret = TCL_OK;
0048
        w->web->readEnvStatus = 1;
0049
      }
0050
    }
0051
  } else if (w->web->readEnvStatus == 2) {
0052
    Tcl_AppendResult(interp, "Failed to retrieve server variables.");
0053
  }
0054
0055
  if (w->web->readEnvStatus == 1) {
0056
    WtTableMethodHelper(clientData, interp, objc, objv, &ret,
0057
      &w->web->serverVars, &WtTableType, 0, 1);
0058
  }
0059
0060
  return ret;
0061
}
0062
0063
/* taskSettings command */
0064
0065
int WtTaskSettingsCmd(ClientData clientData, Tcl_Interp *interp,
0066
    int objc, Tcl_Obj *const objv[])
0067
{
0068
  int ret = TCL_ERROR;
0069
  WtContext *w = WtGetAssocContext(interp);
0070
0071
  WtTableMethodHelper(clientData, interp, objc, objv, &ret,
0072
    &w->web->taskSettings, &WtTableType, 0, 1);
0073
0074
  return ret;
0075
}
0076
0077
/* requestTime */
0078
0079
int WtRequestTimeCmd(ClientData clientData, Tcl_Interp *interp,
0080
    int objc, Tcl_Obj *const objv[])
0081
{
0082
  int ret = TCL_ERROR;
0083
  WtContext *w = WtGetAssocContext(interp);
0084
  Tcl_Time *tm = &w->web->requestStartTime;
0085
0086
  if (objc != 1) {
0087
    Tcl_AppendResult(interp, wtBadUsagePrefix, WtToString(objv[0]), NULL);
0088
  } else {
0089
    Tcl_SetObjResult(interp,
0090
      Tcl_NewLongObj((long)(tm->sec * 1000 + tm->usec / 1000)));
0091
    ret = TCL_OK;
0092
  }
0093
0094
  return ret;
0095
}
0096
0097
/* elapsedTime */
0098
0099
int WtElapsedTimeCmd(ClientData clientData, Tcl_Interp *interp,
0100
    int objc, Tcl_Obj *const objv[])
0101
{
0102
  int ret = TCL_ERROR;
0103
  WtContext *w = WtGetAssocContext(interp);
0104
  Tcl_Time *start, now;
0105
  double duration;
0106
0107
  if (objc != 1) {
0108
    Tcl_AppendResult(interp, wtBadUsagePrefix, WtToString(objv[0]), NULL);
0109
  } else {
0110
    start = &w->web->requestStartTime;
0111
    Tcl_GetTime(&now);
0112
    duration = (now.sec * 1000.0 + now.usec / 1000.0) -
0113
      (start->sec * 1000.0 + start->usec / 1000.0);
0114
    Tcl_SetObjResult(interp, Tcl_NewDoubleObj(duration));
0115
    ret = TCL_OK;
0116
  }
0117
0118
  return ret;
0119
}
0120
0121
/* unixRequestTime */
0122
0123
int WtUnixRequestTimeCmd(ClientData clientData, Tcl_Interp *interp,
0124
    int objc, Tcl_Obj *const objv[])
0125
{
0126
  int ret = TCL_ERROR;
0127
  WtContext *w = WtGetAssocContext(interp);
0128
0129
  if (objc != 1) {
0130
    Tcl_AppendResult(interp, wtBadUsagePrefix, WtToString(objv[0]), NULL);
0131
  } else {
0132
    Tcl_SetObjResult(interp, Tcl_NewLongObj((long)w->apReq->request_time));
0133
    ret = TCL_OK;
0134
  }
0135
0136
  return ret;
0137
}
0138
0139
/* workerInfo command */
0140
0141
int WtWorkerInfoCmd(ClientData clientData, Tcl_Interp *interp,
0142
    int objc, Tcl_Obj *const objv[])
0143
{
0144
  int ret = TCL_ERROR;
0145
  WtContext *w;
0146
  WtWebContext *web;
0147
  Tcl_Obj *tbl;
0148
0149
  if (objc != 1) {
0150
    Tcl_AppendResult(interp, wtBadUsagePrefix, WtToString(objv[0]), NULL);
0151
  } else {
0152
    w = WtGetAssocContext(interp);
0153
    web = w->web;
0154
    tbl = WtNewTableObj();
0155
    WtTableSetStrToBool(tbl, "isPersistent", web->isPersistent);
0156
    WtTableSetStrToInt(tbl, "requestCount", w->requestCount);
0157
    WtTableSetStrToInt(tbl, "ctxRequestCount", web->ctxRequestCount);
0158
    WtTableSetStrToInt(tbl, "interpRequestCount", web->interpRequestCount);
0159
    WtTableSetStrToLong(tbl, "interpCreationTime",
0160
      (long)web->interpCreationTime);
0161
    WtTableSetStrToBool(tbl, "packageLoadDone", web->packageLoadDone);
0162
    WtTableSetStrToInt(tbl, "pid", os_getpid());
0163
    WtTableSetStrToInt(tbl, "tid", gettid());
0164
    Tcl_SetObjResult(interp, tbl);
0165
    ret = TCL_OK;
0166
  }
0167
0168
  return ret;
0169
}
0170
0171
/* taskInfo command */
0172
0173
int WtTaskInfoCmd(ClientData clientData, Tcl_Interp *interp,
0174
    int objc, Tcl_Obj *const objv[])
0175
{
0176
  int ret = TCL_ERROR;
0177
  WtContext *w;
0178
  WtWebContext *web;
0179
  Tcl_Obj *tbl;
0180
  const char *hs;
0181
0182
  if (objc != 1) {
0183
    Tcl_AppendResult(interp, wtBadUsagePrefix, WtToString(objv[0]), NULL);
0184
  } else {
0185
    w = WtGetAssocContext(interp);
0186
    web = w->web;
0187
    tbl = WtNewTableObj();
0188
    WtTableSetStrToBool(tbl, "errorState", web->errorState);
0189
    WtTableSetStrToObj(tbl, "taskNamespace", web->taskNamespace);
0190
    WtTableSetStrToBool(tbl, "importCommands", web->importCommands);
0191
    WtTableSetStrToBool(tbl, "readEnvStatus", web->readEnvStatus);
0192
    WtTableSetStrToInt(tbl, "parseStatus", web->clientRequest.parseStatus);
0193
    WtTableSetStrToInt(tbl, "readRequestStatus", web->clientRequest.readRequestStatus);
0194
    WtTableSetStrToInt(tbl, "readCookiesStatus", web->clientRequest.readCookiesStatus);
0195
0196
    if (web->clientResponse.headersStatus == WT_STATUS_NONE) {
0197
      hs = "none";
0198
    } else if (web->clientResponse.headersStatus == WT_STATUS_OK) {
0199
      hs = "sent";
0200
    } else if (web->clientResponse.headersStatus == WT_STATUS_ERROR) {
0201
      hs = "error";
0202
    }
0203
    WtTableSetStrToStr(tbl, "headersStatus", hs);
0204
0205
    WtTableSetStrToInt(tbl, "bytesSent", w->apReq->bytes_sent);
0206
    WtTableSetStrToBool(tbl, "responseCode", web->clientResponse.status);
0207
    WtTableSetStrToBool(tbl, "deleteInterp", web->deleteInterp);
0208
0209
    Tcl_SetObjResult(interp, tbl);
0210
    ret = TCL_OK;
0211
  }
0212
0213
  return ret;
0214
}
0215
0216
/* wtVersion command */
0217
0218
int WtVersionCmd(ClientData clientData, Tcl_Interp *interp,
0219
    int objc, Tcl_Obj *const objv[])
0220
{
0221
  int ret = TCL_ERROR;
0222
0223
  if (objc != 1) {
0224
    Tcl_AppendResult(interp, wtBadUsagePrefix,
0225
      Tcl_GetString(objv[0]), NULL);
0226
  } else {
0227
    Tcl_SetResult(interp, (char *)wtVersion, NULL);
0228
    ret = TCL_OK;
0229
  }
0230
0231
  return ret;
0232
}
0233
0234
/* virtual command */
0235
0236
int WtVirtualCmd(ClientData clientData, Tcl_Interp *interp,
0237
    int objc, Tcl_Obj *const objv[])
0238
{
0239
  int ret = TCL_ERROR;
0240
  WtContext *w = WtGetAssocContext(interp);
0241
  request_rec *rr = NULL;
0242
  const char *uri;
0243
  Tcl_Obj *statusObj;
0244
0245
  if (objc != 2) {
0246
    Tcl_AppendResult(interp, wtBadUsagePrefix,
0247
      Tcl_GetString(objv[0]), " ?path?", NULL);
0248
  } else {
0249
    uri = Tcl_GetString(objv[1]);
0250
    rr = ap_sub_req_lookup_uri(uri, w->web->apReq);
0251
    if (!rr) {
0252
      Tcl_AppendResult(interp, "Failed to initialize URI \"",
0253
        uri, "\".");
0254
    } else if (rr->status != 200) {
0255
      statusObj = Tcl_NewIntObj(rr->status);
0256
      Tcl_IncrRefCount(statusObj);
0257
      Tcl_AppendResult(interp, "Failed to find URI \"", uri, "\": Status code: ",
0258
        Tcl_GetString(statusObj), ".");          
0259
      Tcl_DecrRefCount(statusObj);
0260
    } else if (ap_run_sub_req(rr)) {
0261
      statusObj = Tcl_NewIntObj(rr->status);
0262
      Tcl_IncrRefCount(statusObj);
0263
      Tcl_AppendResult(interp, "Failed to run URI \"", uri, "\": Status code: ",
0264
        Tcl_GetString(statusObj), ".");
0265
      Tcl_DecrRefCount(statusObj);
0266
    } else {
0267
      ret = TCL_OK;
0268
    }
0269
  }
0270
0271
  if (rr) {
0272
    ap_destroy_sub_req(rr);
0273
  }
0274
0275
  return ret;
0276
}
0277
0278
/* taskNotices command */
0279
0280
int WtTaskNoticesCmd(ClientData clientData, Tcl_Interp *interp,
0281
    int objc, Tcl_Obj *const objv[])
0282
{
0283
  int ret = TCL_ERROR;
0284
  WtContext *w = WtGetAssocContext(interp);
0285
0286
  if (objc != 1) {
0287
    Tcl_AppendResult(interp, wtBadUsagePrefix, WtToString(objv[0]), NULL);
0288
  } else {
0289
    Tcl_SetObjResult(interp, w->web->notices);
0290
    ret = TCL_OK;
0291
  }
0292
0293
  return ret;
0294
}
0295
0296
/* serverLog command */
0297
0298
int WtParseLogLevel(const char *levelStr)
0299
{
0300
  int level = -1;
0301
0302
  if (!strcmp(levelStr, "emerg")) {
0303
    level = APLOG_EMERG;
0304
  } else if (!strcmp(levelStr, "alert")) {
0305
    level = APLOG_ALERT;
0306
  } else if (!strcmp(levelStr, "crit")) {
0307
    level = APLOG_CRIT;
0308
  } else if (!strcmp(levelStr, "error")) {
0309
    level = APLOG_ERR;
0310
  } else if (!strcmp(levelStr, "warning")) {
0311
    level = APLOG_WARNING;
0312
  } else if (!strcmp(levelStr, "notice")) {
0313
    level = APLOG_NOTICE;
0314
  } else if (!strcmp(levelStr, "info")) {
0315
    level = APLOG_INFO;
0316
  } else if (!strcmp(levelStr, "debug")) {
0317
    level = APLOG_DEBUG;
0318
  }
0319
0320
  return level;
0321
}
0322
0323
char *WtLogLevelString(int level)
0324
{
0325
  char *str = NULL;
0326
0327
  switch (level) {
0328
    case APLOG_EMERG:
0329
      str = "emerg";
0330
      break;
0331
    case APLOG_ALERT:
0332
      str = "alert";
0333
      break;
0334
    case APLOG_CRIT:
0335
      str = "crit";
0336
      break;
0337
    case APLOG_ERR:
0338
      str = "error";
0339
      break;
0340
    case APLOG_WARNING:
0341
      str = "warning";
0342
      break;
0343
    case APLOG_NOTICE:
0344
      str = "notice";
0345
      break;
0346
    case APLOG_INFO:
0347
      str = "info";
0348
      break;
0349
    case APLOG_DEBUG:
0350
      str = "debug";
0351
      break;
0352
  }
0353
0354
  return str;
0355
}
0356
0357
int WtServerLogCmd(ClientData clientData, Tcl_Interp *interp,
0358
    int objc, Tcl_Obj *const objv[])
0359
{
0360
  int ret = TCL_ERROR, level, usage = 0;
0361
  WtContext *w = WtGetAssocContext(interp);
0362
  char *path;
0363
0364
  if (objc < 2) {
0365
    Tcl_AppendResult(interp, wtBadUsagePrefix,
0366
      NULL);
0367
    usage = 1;
0368
  } else {
0369
    path = w->web->apReq->filename ? w->web->apReq->filename : "";
0370
0371
    if (objc == 2) {
0372
      WtLog(path, -1, APLOG_INFO | APLOG_NOERRNO, w,
0373
        "Wtcl: %s", WtToString(objv[1]));
0374
      ret = TCL_OK;
0375
    } else if (objc == 3) {
0376
      level = WtParseLogLevel(WtToString(objv[1]));
0377
      if (level == -1) {
0378
        Tcl_AppendResult(interp, "Unknown log level specified. Usage: ", NULL);
0379
        usage = 1;
0380
      } else {
0381
        WtLog(path, -1, level, w,
0382
          "Wtcl: %s", WtToString(objv[2]));
0383
        ret = TCL_OK;
0384
      }
0385
    }
0386
  }
0387
0388
  if (usage) {
0389
    Tcl_AppendResult(interp, WtToString(objv[0]),
0390
      " [emerg | alert | crit | error | warning | notice | info | ",
0391
      "debug | none] message", NULL);
0392
  }
0393
0394
  return ret;
0395
}
0396
0397
int WtLogLevelCmd(ClientData clientData, Tcl_Interp *interp,
0398
    int objc, Tcl_Obj *const objv[])
0399
{
0400
  int ret = TCL_ERROR, code, usage = 0;
0401
  char *name;
0402
  WtContext *w = WtGetAssocContext(interp);
0403
0404
  if (objc < 2) {
0405
    usage = 1;
0406
  } else if (!strcmp(WtToString(objv[1]), "code")) {
0407
    if (objc < 3) {
0408
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0409
        WtToString(objv[0]), " ", WtToString(objv[1]),
0410
        " name", NULL);
0411
    } else {
0412
      code = WtParseLogLevel(WtToString(objv[2]));
0413
      if (code == -1) {
0414
        Tcl_AppendResult(interp, "Unknown log level name: \"",
0415
          WtToString(objv[2]), "\".", NULL);
0416
      } else {
0417
        Tcl_SetObjResult(interp, Tcl_NewIntObj(code));
0418
        ret = TCL_OK;
0419
      }
0420
    }
0421
  } else if (!strcmp(WtToString(objv[1]), "name")) {
0422
    if (objc < 3) {
0423
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0424
        WtToString(objv[0]), " ", WtToString(objv[1]),
0425
        " code", NULL);
0426
    } else if (Tcl_GetIntFromObj(interp, objv[2], &code)) {
0427
      name = WtLogLevelString(code);
0428
      if (!name) {
0429
        Tcl_AppendResult(interp, "Unknown log level code: ",
0430
          WtToString(objv[2]), ".", NULL);
0431
      } else {
0432
        Tcl_SetObjResult(interp, WtNewString(name));
0433
        ret = TCL_OK;
0434
      }
0435
    }
0436
  } else {
0437
    usage = 1;
0438
  }
0439
0440
  if (usage) {
0441
    Tcl_AppendResult(interp, wtBadUsagePrefix2,
0442
      WtToString(objv[0]), " name code\n",
0443
      WtToString(objv[0]), " code name\n",
0444
      NULL);
0445
  }
0446
0447
  return ret;
0448
}
0449
0450
/* sleep command */
0451
0452
int WtSleepCmd(ClientData clientData, Tcl_Interp *interp,
0453
    int objc, Tcl_Obj *const objv[])
0454
{
0455
  int ret = TCL_ERROR, msec;
0456
0457
  if (objc != 2) {
0458
    Tcl_AppendResult(interp, wtBadUsagePrefix, " msec", NULL);
0459
  } else if (Tcl_GetIntFromObj(interp, objv[1], &msec) == TCL_OK) {
0460
    Tcl_Sleep(msec);
0461
    ret = TCL_OK;
0462
  }
0463
0464
  return ret;
0465
}
0466
0467
/* Initialize commands. */
0468
0469
void WtInitServerCommands(Tcl_Interp *interp)
0470
{
0471
  WtContext *w = WtGetAssocContext(interp);
0472
0473
  WtLog(HERE, APLOG_DEBUG | APLOG_NOERRNO, w,
0474
    "Wtcl: WtInitServerCommands.");
0475
0476
  Tcl_CreateObjCommand(interp, "::wt::server::serverVars",
0477
    WtServerVarsCmd, NULL, NULL);
0478
0479
  Tcl_CreateObjCommand(interp, "::wt::server::taskSettings",
0480
    WtTaskSettingsCmd, NULL, NULL);
0481
0482
  Tcl_CreateObjCommand(interp, "::wt::server::wtVersion",
0483
    WtVersionCmd, NULL, NULL);
0484
0485
  Tcl_CreateObjCommand(interp, "::wt::server::virtual",
0486
    WtVirtualCmd, NULL, NULL);
0487
0488
  Tcl_CreateObjCommand(interp, "::wt::server::requestTime",
0489
    WtRequestTimeCmd, NULL, NULL);
0490
0491
  Tcl_CreateObjCommand(interp, "::wt::server::elapsedTime",
0492
    WtElapsedTimeCmd, NULL, NULL);
0493
0494
  Tcl_CreateObjCommand(interp, "::wt::server::unixRequestTime",
0495
    WtUnixRequestTimeCmd, NULL, NULL);
0496
0497
  Tcl_CreateObjCommand(interp, "::wt::server::workerInfo",
0498
    WtWorkerInfoCmd, NULL, NULL);
0499
0500
  Tcl_CreateObjCommand(interp, "::wt::server::taskInfo",
0501
    WtTaskInfoCmd, NULL, NULL);
0502
0503
  Tcl_CreateObjCommand(interp, "::wt::server::taskNotices",
0504
    WtTaskNoticesCmd, NULL, NULL);
0505
0506
  Tcl_CreateObjCommand(interp, "::wt::server::serverLog",
0507
    WtServerLogCmd, NULL, NULL);
0508
0509
  Tcl_CreateObjCommand(interp, "::wt::server::logLevel",
0510
    WtLogLevelCmd, NULL, NULL);
0511
0512
  Tcl_CreateObjCommand(interp, "::wt::server::sleep",
0513
    WtSleepCmd, NULL, NULL);
0514
0515
  Tcl_CreateObjCommand(interp, "::wt::server::events",
0516
    WtContextEventsCmd, NULL, NULL);
0517
0518
  Tcl_CreateObjCommand(interp, "::wt::server::atEnd",
0519
    WtAtEndCmd, NULL, NULL);
0520
}