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 / WtBasicCmds.c

Lines Size Modified Created Owner MIME Types
338 8,527 2010/05/08 18:46:41 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 "WtBasicCmds.h"
0021
#include "WtExecute.h"
0022
#include "WtTable.h"
0023
#include "WtTableUtil.h"
0024
#include "WtMultiTable.h"
0025
#include "WtContext.h"
0026
#include "WtUtil.h"
0027
#include "WtHtmlEntities.h"
0028
0029
/* htmlEncode command */
0030
0031
int WtHtmlEncodeCmd(ClientData clientData, Tcl_Interp *interp,
0032
    int objc, Tcl_Obj *const objv[])
0033
{
0034
  int ret = TCL_ERROR, flags = 0;
0035
  Tcl_UniChar uniCh;
0036
  WtContext *w = WtGetAssocContext(interp);
0037
  const char *str, *p, *q;
0038
  Tcl_Obj *result;
0039
0040
  if (objc == 2 || objc == 3) {
0041
    ret = TCL_OK;
0042
0043
    if (objc == 3) {
0044
      for (p = Tcl_GetString(objv[2]); *p; p = q) {
0045
        Tcl_UtfToUniChar(p, &uniCh);
0046
        q = Tcl_UtfNext(p);
0047
        switch (uniCh) {
0048
          case 'd': flags |= WT_ENT_DEFAULT; break;
0049
          case 'a': flags |= WT_ENT_ALL; break;
0050
          case 's': flags |= WT_ENT_SPECIAL_CHARS; break;
0051
          case 'Q': flags |= WT_ENT_DOUBLE_QUOTE; break;
0052
          case 'q': flags |= WT_ENT_SINGLE_QUOTE; break;
0053
          case 'u': flags |= WT_ENT_UNKNOWN_CHARS; break;
0054
          default:
0055
            Tcl_AppendResult(interp, "Unknown flag: ");
0056
            Tcl_AppendToObj(Tcl_GetObjResult(interp), p, q - p);
0057
            ret = TCL_ERROR;
0058
            break;
0059
        }
0060
        if (ret == TCL_ERROR) {
0061
          break;
0062
        }
0063
      }
0064
    } else {
0065
      flags |= WT_ENT_DEFAULT;
0066
    }
0067
0068
    if (!(str = Tcl_GetString(objv[1]))) {
0069
      ret = TCL_ERROR;
0070
    } else {
0071
      result = WtEncodeHtml(str, flags);
0072
      Tcl_SetObjResult(interp, result);
0073
    }
0074
  } else {
0075
    Tcl_AppendResult(interp, wtBadUsagePrefix,
0076
      WtToString(objv[0]), " string flags", NULL);
0077
  }
0078
0079
  return ret;
0080
}
0081
0082
/* apHtmlEncode command */
0083
0084
int WtApHtmlEncodeCmd(ClientData clientData, Tcl_Interp *interp,
0085
    int objc, Tcl_Obj *const objv[])
0086
{
0087
  int ret = TCL_ERROR;
0088
  WtContext *w = WtGetAssocContext(interp);
0089
0090
  if (objc != 2) {
0091
    Tcl_AppendResult(interp, wtBadUsagePrefix,
0092
      WtToString(objv[0]), " string", NULL);
0093
  } else {
0094
    Tcl_SetObjResult(interp,
0095
      WtNewString(WtSafeStr(ap_escape_html(w->web->apReq->pool,
0096
        WtToString(objv[1])))));
0097
    ret = TCL_OK;
0098
  }
0099
0100
  return ret;
0101
}
0102
0103
/* urlEncode command */
0104
0105
int WtUrlEncodeCmd(ClientData clientData, Tcl_Interp *interp,
0106
    int objc, Tcl_Obj *const objv[])
0107
{
0108
  int ret = TCL_ERROR, stringLen, resultLen;
0109
  WtContext *w = WtGetAssocContext(interp);
0110
  const char *str;
0111
  char *result;
0112
  Tcl_Obj *resultObj;
0113
0114
  if (objc == 2) {
0115
    if (str = Tcl_GetStringFromObj(objv[1], &stringLen)) {
0116
      result = WtEncodeUrl(str, stringLen, &resultLen);
0117
      resultObj = Tcl_NewStringObj(result, resultLen);
0118
      ckfree(result);
0119
      Tcl_SetObjResult(interp, resultObj);
0120
      ret = TCL_OK;
0121
    }
0122
  } else {
0123
    Tcl_AppendResult(interp, wtBadUsagePrefix,
0124
      WtToString(objv[0]), " string", NULL);
0125
  }
0126
0127
  return ret;
0128
}
0129
0130
/* urlDecode command */
0131
0132
int WtUrlDecodeCmd(ClientData clientData, Tcl_Interp *interp,
0133
    int objc, Tcl_Obj *const objv[])
0134
{
0135
  int ret = TCL_ERROR, stringLen, resultLen;
0136
  WtContext *w = WtGetAssocContext(interp);
0137
  char *str, *result;
0138
  Tcl_Obj *resultObj;
0139
0140
  if (objc != 2) {
0141
    Tcl_AppendResult(interp, wtBadUsagePrefix,
0142
      WtToString(objv[0]), " url", NULL);
0143
  } else {
0144
    str = Tcl_GetStringFromObj(objv[1], &stringLen);
0145
    if (str) {
0146
      result = ckalloc(stringLen + 1);
0147
      memcpy(result, str, stringLen);
0148
      result[stringLen] = '\0';
0149
      resultLen = WtDecodeUrlInPlace(result, stringLen);
0150
      resultObj = Tcl_NewStringObj(result, resultLen);
0151
      ckfree(result);
0152
      Tcl_SetObjResult(interp, resultObj);
0153
      ret = TCL_OK;
0154
    }
0155
  }
0156
0157
  return ret;
0158
}
0159
0160
/* md5Encode command */
0161
0162
int WtMd5EncodeCmd(ClientData clientData, Tcl_Interp *interp,
0163
    int objc, Tcl_Obj *const objv[])
0164
{
0165
  int ret = TCL_ERROR, len;
0166
  WtContext *w = WtGetAssocContext(interp);
0167
  char *bytes;
0168
  Tcl_Obj *result;
0169
0170
  if (objc != 2) {
0171
    Tcl_AppendResult(interp, wtBadUsagePrefix,
0172
      WtToString(objv[0]), " string", NULL);
0173
  } else {
0174
    bytes = Tcl_GetStringFromObj(objv[1], &len);
0175
    result = Tcl_NewStringObj(
0176
      ap_md5_binary(w->apReq->pool, bytes, len), -1);
0177
    if (result) {
0178
      Tcl_SetObjResult(interp, result);
0179
      ret = TCL_OK;
0180
    }
0181
  }
0182
0183
  return ret;
0184
}
0185
0186
/* type command */
0187
0188
int WtTypeCmd(ClientData clientData, Tcl_Interp *interp,
0189
    int objc, Tcl_Obj *const objv[])
0190
{
0191
  int ret = TCL_ERROR;
0192
  WtContext *w = WtGetAssocContext(interp);
0193
0194
  if (objc != 2) {
0195
    Tcl_AppendResult(interp, wtBadUsagePrefix,
0196
      WtToString(objv[0]), " value", NULL);
0197
  } else {
0198
    Tcl_SetObjResult(interp, WtNewString(
0199
      WtSafeStr(objv[1]->typePtr->name)));
0200
    ret = TCL_OK;
0201
  }
0202
0203
  return ret;
0204
}
0205
0206
/* objectInfo command */
0207
0208
int WtObjectInfoCmd(ClientData clientData, Tcl_Interp *interp,
0209
    int objc, Tcl_Obj *const objv[])
0210
{
0211
  int ret = TCL_ERROR;
0212
  WtContext *w = WtGetAssocContext(interp);
0213
  Tcl_Obj *obj, *list;
0214
0215
  if (objc != 2) {
0216
    Tcl_AppendResult(interp, wtBadUsagePrefix,
0217
      WtToString(objv[0]), " object", NULL);
0218
  } else {
0219
    obj = objv[1];
0220
0221
    list = Tcl_NewListObj(0, NULL);
0222
0223
    Tcl_ListObjAppendElement(interp, list, WtNewString("address"));
0224
0225
    Tcl_ListObjAppendElement(interp, list, Tcl_NewIntObj((int)obj));
0226
0227
    Tcl_ListObjAppendElement(interp, list, WtNewString("type"));
0228
0229
    if (obj->typePtr && obj->typePtr->name) {
0230
      Tcl_ListObjAppendElement(interp, list,
0231
        WtNewString(obj->typePtr->name));
0232
    } else {
0233
      Tcl_ListObjAppendElement(interp, list, WtNewString(""));
0234
    }
0235
0236
    Tcl_ListObjAppendElement(interp, list,
0237
      WtNewString("hasValidString"));
0238
0239
    Tcl_ListObjAppendElement(interp, list,
0240
      Tcl_NewBooleanObj(obj->bytes != NULL));
0241
0242
    Tcl_ListObjAppendElement(interp, list, WtNewString("bytes"));
0243
0244
    Tcl_ListObjAppendElement(interp, list,
0245
      WtNewString(WtSafeStr(obj->bytes)));
0246
0247
    Tcl_ListObjAppendElement(interp, list, WtNewString("length"));
0248
0249
    Tcl_ListObjAppendElement(interp, list,
0250
      Tcl_NewIntObj(obj->length));
0251
0252
    Tcl_ListObjAppendElement(interp, list, WtNewString("string"));
0253
0254
    Tcl_ListObjAppendElement(interp, list,
0255
      WtNewString(WtSafeStr(WtToString(obj))));
0256
0257
    Tcl_ListObjAppendElement(interp, list, WtNewString("refCount"));
0258
0259
    Tcl_ListObjAppendElement(interp, list,
0260
      Tcl_NewIntObj(obj->refCount));
0261
0262
    Tcl_SetObjResult(interp, list);
0263
0264
    ret = TCL_OK;
0265
  }
0266
0267
  return ret;
0268
}
0269
0270
/* winDebug command */
0271
0272
int WtWinDebugCmd(ClientData clientData, Tcl_Interp *interp,
0273
    int objc, Tcl_Obj *const objv[])
0274
{
0275
  int ret = TCL_ERROR;
0276
0277
  if (objc != 1) {
0278
    Tcl_AppendResult(interp, wtBadUsagePrefix,
0279
      WtToString(objv[0]), NULL);
0280
  } else {
0281
    _asm int 3;
0282
    ret = TCL_OK;
0283
  }
0284
0285
  return ret;
0286
}
0287
0288
int WtBadCmd(ClientData clientData, Tcl_Interp *interp,
0289
    int objc, Tcl_Obj *const objv[])
0290
{
0291
  int ret = TCL_ERROR;
0292
0293
  if (objc != 1) {
0294
    Tcl_AppendResult(interp, wtBadUsagePrefix,
0295
      WtToString(objv[0]), NULL);
0296
  } else {
0297
    _asm int 3;
0298
    ret = TCL_OK;
0299
  }
0300
0301
  return ret;
0302
}
0303
0304
/* Initialize commands. */
0305
0306
void WtInitBasicCommands(Tcl_Interp *interp)
0307
{
0308
  WtContext *w = WtGetAssocContext(interp);
0309
0310
  WtLog(HERE, APLOG_DEBUG | APLOG_NOERRNO, w,
0311
    "Wtcl: WtInitBasicCommands.");
0312
0313
  Tcl_CreateObjCommand(interp, "::wt::web::htmlEncode",
0314
    WtHtmlEncodeCmd, NULL, NULL);
0315
0316
  Tcl_CreateObjCommand(interp, "::wt::web::apHtmlEncode",
0317
    WtApHtmlEncodeCmd, NULL, NULL);
0318
0319
  Tcl_CreateObjCommand(interp, "::wt::web::urlEncode",
0320
    WtUrlEncodeCmd, NULL, NULL);
0321
0322
  Tcl_CreateObjCommand(interp, "::wt::web::urlDecode",
0323
    WtUrlDecodeCmd, NULL, NULL);
0324
0325
  Tcl_CreateObjCommand(interp, "::wt::util::md5Encode",
0326
    WtMd5EncodeCmd, NULL, NULL);
0327
0328
  /* Debugging commands */
0329
0330
  Tcl_CreateObjCommand(interp, "::wt::util::objectType", WtTypeCmd,
0331
    NULL, NULL);
0332
0333
  Tcl_CreateObjCommand(interp, "::wt::util::objectInfo", WtObjectInfoCmd,
0334
    NULL, NULL);
0335
0336
  Tcl_CreateObjCommand(interp, "::wt::util::winDebug", WtWinDebugCmd,
0337
    NULL, NULL);
0338
}