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

Lines Size Modified Created Owner MIME Types
925 23,481 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 "WtCookie.h"
0018
#include "WtContext.h"
0019
#include "WtUtil.h"
0020
#include "WtTable.h"
0021
#include "WtTableUtil.h"
0022
#include "WtMultiTable.h"
0023
#include "WtCollectionCmds.h"
0024
#include "WtBasicCmds.h"
0025
0026
/* Create a new cookie */
0027
0028
Tcl_Obj *WtNewCookieObj()
0029
{
0030
  Tcl_Obj *obj = WtNewTableObj();
0031
  WtTableSetStrToStr(obj, "name", "");
0032
  WtTableSetStrToObj(obj, "values", Tcl_NewListObj(0, NULL));
0033
  WtTableSetStrToStr(obj, "expires", "");
0034
  WtTableSetStrToStr(obj, "domain", "");
0035
  WtTableSetStrToStr(obj, "path", "");
0036
  WtTableSetStrToObj(obj, "secure", Tcl_NewBooleanObj(0));
0037
  return obj;
0038
}
0039
0040
/* Get the name */
0041
0042
Tcl_Obj *WtCookieGetName(Tcl_Obj *cookie, Tcl_Interp *interp)
0043
{
0044
  Tcl_Obj *name = WtTableGetObjFromStr(cookie, "name", NULL);
0045
  if (!name) {
0046
    Tcl_AppendResult(interp, "Invalid cookie object.", NULL);
0047
  }
0048
  return name;
0049
}
0050
0051
/* Set the name */
0052
0053
int WtCookieSetName(Tcl_Obj *cookie, Tcl_Obj *name, Tcl_Interp *interp)
0054
{
0055
  WtTableSetStrToObj(cookie, "name", name);
0056
  return 1;
0057
}
0058
0059
/* Get the value */
0060
0061
int WtCookieGetValue(Tcl_Obj *cookie, Tcl_Obj **value, Tcl_Interp *interp)
0062
{
0063
  int ret = 0;
0064
  Tcl_Obj *key = WtNewString("values"), *list;
0065
0066
  *value = NULL;
0067
  if (WtTableGetList(cookie, key, &list, interp)) {
0068
    if (!list) {
0069
      Tcl_AppendResult(interp, "Invalid cookie object.", NULL);
0070
    } else if (Tcl_ListObjIndex(interp, list, 0, value) == TCL_OK) {
0071
      ret = 1;
0072
    }
0073
  }
0074
0075
  Tcl_DecrRefCount(key);
0076
  return ret;
0077
}
0078
0079
/* Set the value */
0080
0081
int WtCookieSetValue(Tcl_Obj *cookie, Tcl_Obj *value, Tcl_Interp *interp)
0082
{
0083
  WtTableSetStrToObj(cookie, "values",
0084
    Tcl_NewListObj(1, &value));
0085
  return 1;
0086
}
0087
0088
/* Get all values */
0089
0090
int WtCookieGetAllValues(Tcl_Obj *cookie, Tcl_Obj **values, Tcl_Interp *interp)
0091
{
0092
  int ret = 0;
0093
  Tcl_Obj *key = WtNewString("values");
0094
0095
  if (WtTableGetList(cookie, key, values, interp)) {
0096
    if (!*values) {
0097
      Tcl_AppendResult(interp, "Invalid cookie object.", NULL);
0098
    } else {
0099
      ret = 1;
0100
    }
0101
  }
0102
0103
  Tcl_DecrRefCount(key);
0104
  return ret;
0105
}
0106
0107
/* Set all values */
0108
0109
int WtCookieSetAllValues(Tcl_Obj *cookie, Tcl_Obj *values, Tcl_Interp *interp)
0110
{
0111
  WtTableSetStrToObj(cookie, "values", values);
0112
  return 1;
0113
}
0114
0115
/* Get expiration time */
0116
0117
Tcl_Obj *WtCookieGetExpires(Tcl_Obj *cookie, Tcl_Interp *interp)
0118
{
0119
  Tcl_Obj *expires = WtTableGetObjFromStr(cookie, "expires", NULL);
0120
  if (!expires) {
0121
    Tcl_AppendResult(interp, "Invalid cookie object.", NULL);
0122
  }
0123
  return expires;
0124
}
0125
0126
/* Set the expiration time */
0127
0128
int WtCookieSetExpires(Tcl_Obj *cookie, Tcl_Obj *expires, Tcl_Interp *interp)
0129
{
0130
  int ret = 0;
0131
  const char *str;
0132
  WtContext *w = WtGetAssocContext(interp);
0133
0134
  str = ApacheUtil_expires(w->web->apReq->pool,
0135
    WtToString(expires), EXPIRES_COOKIE);
0136
0137
  if (!str) {
0138
    Tcl_AppendResult(interp, "Expiration time string is invalid.");
0139
  } else {
0140
    WtTableSetStrToStr(cookie, "expires", str);
0141
    ret = 1;
0142
  }
0143
0144
  return ret;
0145
}
0146
0147
int WtCookieMarkExpired(Tcl_Obj *cookie, Tcl_Interp *interp)
0148
{
0149
  int ok = 0;
0150
  Tcl_Obj *epoch = WtNewString(wtCookieEpoch);
0151
  Tcl_IncrRefCount(epoch);
0152
  ok = WtCookieSetExpires(cookie, epoch, interp);
0153
  Tcl_DecrRefCount(epoch);
0154
  return ok;
0155
}
0156
0157
/* Get the domain */
0158
0159
Tcl_Obj *WtCookieGetDomain(Tcl_Obj *cookie, Tcl_Interp *interp)
0160
{
0161
  Tcl_Obj *domain = WtTableGetObjFromStr(cookie, "domain", NULL);
0162
  if (!domain) {
0163
    Tcl_AppendResult(interp, "Invalid cookie object.", NULL);
0164
  }
0165
  return domain;
0166
}
0167
0168
/* Set the domain */
0169
0170
int WtCookieSetDomain(Tcl_Obj *cookie, Tcl_Obj *domain, Tcl_Interp *interp)
0171
{
0172
  WtTableSetStrToObj(cookie, "domain", domain);
0173
  return 1;
0174
}
0175
0176
/* Get the path */
0177
0178
Tcl_Obj *WtCookieGetPath(Tcl_Obj *cookie, Tcl_Interp *interp)
0179
{
0180
  Tcl_Obj *path = WtTableGetObjFromStr(cookie, "path", NULL);
0181
  if (!path) {
0182
    Tcl_AppendResult(interp, "Invalid cookie object.", NULL);
0183
  }
0184
  return path;
0185
}
0186
0187
/* Set the path */
0188
0189
int WtCookieSetPath(Tcl_Obj *cookie, Tcl_Obj *path, Tcl_Interp *interp)
0190
{
0191
  WtTableSetStrToObj(cookie, "path", path);
0192
  return 1;
0193
}
0194
0195
/* Get the secure option */
0196
0197
Tcl_Obj *WtCookieGetSecure(Tcl_Obj *cookie, Tcl_Interp *interp)
0198
{
0199
  Tcl_Obj *secure = WtTableGetObjFromStr(cookie, "secure", NULL);
0200
  if (!secure) {
0201
    Tcl_AppendResult(interp, "Invalid cookie object.", NULL);
0202
  }
0203
  return secure;
0204
}
0205
0206
/* Set the secure option */
0207
0208
int WtCookieSetSecure(Tcl_Obj *cookie, Tcl_Obj *secure, Tcl_Interp *interp)
0209
{
0210
  int intVal; 
0211
  if (Tcl_GetBooleanFromObj(interp, secure, &intVal) != TCL_OK) {
0212
    Tcl_AppendResult(interp, "Secure value must be boolean.", NULL);
0213
    return 0;
0214
  }
0215
  WtTableSetStrToObj(cookie, "secure", Tcl_NewBooleanObj(intVal));
0216
  return 1;
0217
}
0218
0219
/* cookie command */
0220
0221
int WtCookieCmd(ClientData clientData, Tcl_Interp *interp, int objc,
0222
    Tcl_Obj *const objv[])
0223
{
0224
  int ret = TCL_ERROR;
0225
  char *subCmd;
0226
0227
  if (objc < 2) {
0228
    WtCookieUsage(interp, objv[0]);
0229
  } else {
0230
    subCmd = WtToString(objv[1]);
0231
0232
    if (!strcmp(subCmd, "create")) {
0233
      ret = WtCookieCreateCmd(clientData, interp, objc, objv);
0234
    } else if (!strcmp(subCmd, "name")) {
0235
      ret = WtCookieNameCmd(clientData, interp, objc, objv);
0236
    } else if (!strcmp(subCmd, "value")) {
0237
      ret = WtCookieValueCmd(clientData, interp, objc, objv);
0238
    } else if (!strcmp(subCmd, "allValues")) {
0239
      ret = WtCookieAllValuesCmd(clientData, interp, objc, objv);
0240
    } else if (!strcmp(subCmd, "expires")) {
0241
      ret = WtCookieExpiresCmd(clientData, interp, objc, objv);
0242
    } else if (!strcmp(subCmd, "markExpired")) {
0243
      ret = WtCookieMarkExpiredCmd(clientData, interp, objc, objv);
0244
    } else if (!strcmp(subCmd, "domain")) {
0245
      ret = WtCookieDomainCmd(clientData, interp, objc, objv);
0246
    } else if (!strcmp(subCmd, "path")) {
0247
      ret = WtCookiePathCmd(clientData, interp, objc, objv);
0248
    } else if (!strcmp(subCmd, "secure")) {
0249
      ret = WtCookieSecureCmd(clientData, interp, objc, objv);
0250
    } else {
0251
      WtCookieUsage(interp, objv[0]);
0252
    }
0253
  }
0254
0255
  return ret;
0256
}
0257
0258
/* cookie create command */
0259
0260
int WtCookieCreateCmd(ClientData clientData, Tcl_Interp *interp, int objc,
0261
    Tcl_Obj *const objv[])
0262
{
0263
  char *argStr;
0264
  int ret = TCL_ERROR, i, optsDone = 0, intVal, error = 0;
0265
  Tcl_Obj *cookie = WtNewCookieObj(), *boolObj;
0266
0267
  Tcl_IncrRefCount(cookie);
0268
0269
  for (i = 2; i < objc; i++) {
0270
    argStr = WtToString(objv[i]);
0271
    if (!strcmp(argStr, "-expires")) {
0272
      if (i + 1 >= objc) {
0273
        Tcl_AppendResult(interp,
0274
          "The \"-expires\" option requires a value.", NULL);
0275
        error = 1;
0276
        break;
0277
      } else if (!WtCookieSetExpires(cookie, objv[i + 1], interp)) {
0278
        error = 1;
0279
        break;
0280
      } else {
0281
        i++;
0282
      }
0283
    } else if (!strcmp(argStr, "-domain")) {
0284
      if (i + 1 >= objc) {
0285
        Tcl_AppendResult(interp,
0286
          "The \"-domain\" option requires a value.", NULL);
0287
          error = 1;
0288
          break;
0289
      } else {
0290
        WtCookieSetDomain(cookie, objv[i + 1], interp);
0291
        i++;
0292
      }
0293
    } else if (!strcmp(argStr, "-path")) {
0294
      if (i + 1 >= objc) {
0295
        Tcl_AppendResult(interp, "The \"-path\" option requires a value.",
0296
          NULL);
0297
        error = 1;
0298
        break;
0299
      } else {
0300
        WtCookieSetPath(cookie, objv[i + 1], interp);
0301
        i++;
0302
      }
0303
    } else if (!strcmp(argStr, "-secure")) {
0304
      if (i + 1 >= objc) {
0305
        Tcl_AppendResult(interp,
0306
          "The \"-secure\" option requires a value.", NULL);
0307
        error = 1;
0308
        break;
0309
      } else if (Tcl_GetBooleanFromObj(interp, objv[2], &intVal) != TCL_OK) {
0310
        error = 1;
0311
        break;
0312
      } else {
0313
        boolObj = Tcl_NewBooleanObj(0);
0314
        Tcl_IncrRefCount(boolObj);
0315
        if (!WtCookieSetSecure(cookie, boolObj, interp)) {
0316
          error = 1;
0317
        }
0318
        Tcl_DecrRefCount(boolObj);
0319
        if (error) {
0320
          break;
0321
        } else {
0322
          i++;
0323
        }
0324
      }
0325
    } else if (!strcmp(argStr, "--")) {
0326
      optsDone = 1;
0327
      break;
0328
    } else if (argStr && argStr[0] == '-') {
0329
      Tcl_AppendResult(interp, "Option \"", argStr, "\" is invalid. ",
0330
        NULL);
0331
      WtCookieUsage(interp, objv[0]);
0332
      error = 1;
0333
      break;
0334
    } else {
0335
      optsDone = 1;
0336
      break;
0337
    }
0338
  }
0339
0340
  if (!error) {
0341
    if (i + 2 < objc) {
0342
      Tcl_AppendResult(interp, "Extra parameters found at the end. ");
0343
      WtCookieUsage(interp, objv[0]);
0344
      error = 1;
0345
    } else if (i < objc) {
0346
      if (!WtCookieSetName(cookie, objv[i], interp)) {
0347
        error = 1;
0348
      } else if (i + 1 < objc) {
0349
        if (!WtCookieSetValue(cookie, objv[i + 1], interp)) {
0350
          error = 1;
0351
        }
0352
      }
0353
    }
0354
  }
0355
0356
  if (!error) {
0357
    Tcl_SetObjResult(interp, cookie);
0358
    ret = TCL_OK;
0359
  }
0360
0361
  Tcl_DecrRefCount(cookie);
0362
0363
  return ret;
0364
}
0365
0366
/* cookie name command */
0367
0368
int WtCookieNameCmd(ClientData clientData, Tcl_Interp *interp, int objc,
0369
    Tcl_Obj *const objv[])
0370
{
0371
  int ret = TCL_ERROR, created;
0372
  Tcl_Obj *cookie, *name;
0373
0374
  if (objc == 3) {
0375
    if (cookie = WtCookieCmdGetVal(objv[2], interp)) {
0376
      if (name = WtCookieGetName(cookie, interp)) {
0377
        Tcl_SetObjResult(interp, name);
0378
        ret = TCL_OK;
0379
      }
0380
    }
0381
  } else if (objc == 4) {
0382
    if (WtCookieCmdOwnVar(objv[2], &cookie, &created, interp)) {
0383
      if (WtCookieSetName(cookie, objv[3], interp)) {
0384
        if (name = WtCookieGetName(cookie, interp)) {
0385
          if (WtCookieCmdSetVar(objv[2], cookie, interp)) {
0386
            Tcl_SetObjResult(interp, name);
0387
            ret = TCL_OK;
0388
          }
0389
        }
0390
      }
0391
      if (created) {
0392
        Tcl_DecrRefCount(cookie);
0393
      }
0394
      ret = TCL_OK;
0395
    }
0396
  } else {
0397
    Tcl_AppendResult(interp, wtBadUsagePrefix, "\n",
0398
      WtToString(objv[0]), " ", WtToString(objv[1]), " ",
0399
      " cookieVal\n",
0400
      WtToString(objv[0]), " ", WtToString(objv[1]), " ",
0401
      " cookieVar name\n",
0402
      NULL);
0403
  }
0404
0405
  return ret;
0406
}
0407
0408
/* cookie value command */
0409
0410
int WtCookieValueCmd(ClientData clientData, Tcl_Interp *interp, int objc,
0411
    Tcl_Obj *const objv[])
0412
{
0413
  int ret = TCL_ERROR, created;
0414
  Tcl_Obj *cookie, *value;
0415
0416
  if (objc == 3) {
0417
    if (cookie = WtCookieCmdGetVal(objv[2], interp)) {
0418
      if (WtCookieGetValue(cookie, &value, interp)) {
0419
        Tcl_SetObjResult(interp, value ? value : WtNewString(NULL));
0420
        ret = TCL_OK;
0421
      }
0422
    }
0423
  } else if (objc == 4) {
0424
    if (WtCookieCmdOwnVar(objv[2], &cookie, &created, interp)) {
0425
      if (WtCookieSetValue(cookie, objv[3], interp)) {
0426
        if (WtCookieGetValue(cookie, &value, interp)) {
0427
          if (WtCookieCmdSetVar(objv[2], cookie, interp)) {
0428
            Tcl_SetObjResult(interp, value ? value : WtNewString(NULL));
0429
            ret = TCL_OK;
0430
          }
0431
        }
0432
      }
0433
      if (created) {
0434
        Tcl_DecrRefCount(cookie);
0435
      }
0436
    }
0437
  } else {
0438
    Tcl_AppendResult(interp, wtBadUsagePrefix,
0439
      WtToString(objv[0]), " ", WtToString(objv[1]), " ",
0440
      " cookieVal\n",
0441
      WtToString(objv[0]), " ", WtToString(objv[2]), " ",
0442
      " cookieVar value\n",
0443
      NULL);
0444
  }
0445
0446
  return ret;
0447
}
0448
0449
/* cookie allValues command */
0450
0451
int WtCookieAllValuesCmd(ClientData clientData, Tcl_Interp *interp, int objc,
0452
    Tcl_Obj *const objv[])
0453
{
0454
  int ret = TCL_ERROR, created;
0455
  Tcl_Obj *cookie, *values;
0456
0457
  if (objc == 3) {
0458
    if (cookie = WtCookieCmdGetVal(objv[2], interp)) {
0459
      if (WtCookieGetAllValues(cookie, &values, interp)) {
0460
        Tcl_SetObjResult(interp, values);
0461
        ret = TCL_OK;
0462
      }
0463
    }
0464
  } else if (objc == 4) {
0465
    if (WtCookieCmdOwnVar(objv[2], &cookie, &created, interp)) {
0466
      if (WtCookieSetAllValues(cookie, objv[3], interp)) {
0467
        if (WtCookieGetAllValues(cookie, &values, interp)) {
0468
          if (WtCookieCmdSetVar(objv[2], cookie, interp)) {
0469
            Tcl_SetObjResult(interp, values);
0470
            ret = TCL_OK;
0471
          }
0472
        }
0473
      }
0474
      if (created) {
0475
        Tcl_DecrRefCount(cookie);
0476
      }
0477
    }
0478
  } else {
0479
    Tcl_AppendResult(interp, wtBadUsagePrefix, "\n",
0480
      WtToString(objv[0]), " ", WtToString(objv[1]), " ",
0481
      " cookieVal\n",
0482
      WtToString(objv[0]), " ", WtToString(objv[2]), " ",
0483
      " cookieVar values\n",
0484
      NULL);
0485
  }
0486
0487
  return ret;
0488
}
0489
0490
/* cookie expires command */
0491
0492
int WtCookieExpiresCmd(ClientData clientData, Tcl_Interp *interp, int objc,
0493
    Tcl_Obj *const objv[])
0494
{
0495
  int ret = TCL_ERROR, created;
0496
  Tcl_Obj *cookie, *expires;
0497
0498
  if (objc == 3) {
0499
    if (cookie = WtCookieCmdGetVal(objv[2], interp)) {
0500
      if (expires = WtCookieGetExpires(cookie, interp)) {
0501
        Tcl_SetObjResult(interp, expires);
0502
        ret = TCL_OK;
0503
      }
0504
    }
0505
  } else if (objc == 4) {
0506
    if (WtCookieCmdOwnVar(objv[2], &cookie, &created, interp)) {
0507
      if (WtCookieSetExpires(cookie, objv[3], interp)) {
0508
        if (expires = WtCookieGetExpires(cookie, interp)) {
0509
          if (WtCookieCmdSetVar(objv[2], cookie, interp)) {
0510
            Tcl_SetObjResult(interp, expires);
0511
            ret = TCL_OK;
0512
          }
0513
        }
0514
      }
0515
      if (created) {
0516
        Tcl_DecrRefCount(cookie);
0517
      }
0518
    }
0519
  } else {
0520
    Tcl_AppendResult(interp, wtBadUsagePrefix, "\n",
0521
      WtToString(objv[0]), " ", WtToString(objv[1]), " ",
0522
      " cookieVal\n",
0523
      WtToString(objv[0]), " ", WtToString(objv[1]), " ",
0524
      " cookieVar expirationDate\n",
0525
      NULL);
0526
  }
0527
0528
  return ret;
0529
}
0530
0531
/* cookie markExpired command */
0532
0533
int WtCookieMarkExpiredCmd(ClientData clientData, Tcl_Interp *interp, int objc,
0534
    Tcl_Obj *const objv[])
0535
{
0536
  int ret = TCL_ERROR, created;
0537
  Tcl_Obj *cookie, *expires;
0538
0539
  if (objc == 3) {
0540
    if (WtCookieCmdOwnVar(objv[2], &cookie, &created, interp)) {
0541
      if (WtCookieMarkExpired(cookie, interp)) {
0542
        if (expires = WtCookieGetExpires(cookie, interp)) {
0543
          if (WtCookieCmdSetVar(objv[2], cookie, interp)) {
0544
            Tcl_SetObjResult(interp, expires);
0545
            ret = TCL_OK;
0546
          }
0547
        }
0548
      }
0549
      if (created) {
0550
        Tcl_DecrRefCount(cookie);
0551
      }
0552
    }
0553
  } else {
0554
    Tcl_AppendResult(interp, wtBadUsagePrefix,
0555
      WtToString(objv[0]), " ", WtToString(objv[1]), " ",
0556
      " cookieVar",
0557
      NULL);
0558
  }
0559
0560
  return ret;
0561
}
0562
0563
/* cookie domain command */
0564
0565
int WtCookieDomainCmd(ClientData clientData, Tcl_Interp *interp, int objc,
0566
    Tcl_Obj *const objv[])
0567
{
0568
  int ret = TCL_ERROR, created;
0569
  Tcl_Obj *cookie, *domain;
0570
0571
  if (objc == 3) {
0572
    if (cookie = WtCookieCmdGetVal(objv[2], interp)) {
0573
      if (domain = WtCookieGetDomain(cookie, interp)) {
0574
        Tcl_SetObjResult(interp, domain);
0575
        ret = TCL_OK;
0576
      }
0577
    }
0578
  } else if (objc == 4) {
0579
    if (WtCookieCmdOwnVar(objv[2], &cookie, &created, interp)) {
0580
      if (WtCookieSetDomain(cookie, objv[3], interp)) {
0581
        if (domain = WtCookieGetDomain(cookie, interp)) {
0582
          if (WtCookieCmdSetVar(objv[2], cookie, interp)) {
0583
            Tcl_SetObjResult(interp, domain);
0584
            ret = TCL_OK;
0585
          }
0586
        }
0587
      }
0588
      if (created) {
0589
        Tcl_DecrRefCount(cookie);
0590
      }
0591
    }
0592
  } else {
0593
    Tcl_AppendResult(interp, wtBadUsagePrefix, "\n",
0594
      WtToString(objv[0]), " ", WtToString(objv[1]), " ",
0595
      " cookieVal\n",
0596
      WtToString(objv[0]), " ", WtToString(objv[1]), " ",
0597
      " cookieVar domain\n",
0598
      NULL);
0599
  }
0600
0601
  return ret;
0602
}
0603
0604
/* cookie path command */
0605
0606
int WtCookiePathCmd(ClientData clientData, Tcl_Interp *interp, int objc,
0607
    Tcl_Obj *const objv[])
0608
{
0609
  int ret = TCL_ERROR, created;
0610
  Tcl_Obj *cookie, *path;
0611
0612
  if (objc == 3) {
0613
    if (cookie = WtCookieCmdGetVal(objv[2], interp)) {
0614
      if (path = WtCookieGetPath(cookie, interp)) {
0615
        Tcl_SetObjResult(interp, path);
0616
        ret = TCL_OK;
0617
      }
0618
    }
0619
  } else if (objc == 4) {
0620
    if (WtCookieCmdOwnVar(objv[2], &cookie, &created, interp)) {
0621
      if (WtCookieSetPath(cookie, objv[3], interp)) {
0622
        if (path = WtCookieGetPath(cookie, interp)) {
0623
          if (WtCookieCmdSetVar(objv[2], cookie, interp)) {
0624
            Tcl_SetObjResult(interp, path);
0625
            ret = TCL_OK;
0626
          }
0627
        }
0628
      }
0629
      if (created) {
0630
        Tcl_DecrRefCount(cookie);
0631
      }
0632
    }
0633
  } else {
0634
    Tcl_AppendResult(interp, wtBadUsagePrefix, "\n",
0635
      WtToString(objv[0]), " ", WtToString(objv[1]), " ",
0636
      " cookieVal\n",
0637
      WtToString(objv[0]), " ", WtToString(objv[1]), " ",
0638
      " cookieVar path\n",
0639
      NULL);
0640
  }
0641
0642
  return ret;
0643
}
0644
0645
/* cookie secure command */
0646
0647
int WtCookieSecureCmd(ClientData clientData, Tcl_Interp *interp, int objc,
0648
    Tcl_Obj *const objv[])
0649
{
0650
  int ret = TCL_ERROR, created;
0651
  Tcl_Obj *cookie, *secure;
0652
0653
  if (objc == 3) {
0654
    if (cookie = WtCookieCmdGetVal(objv[2], interp)) {
0655
      if (secure = WtCookieGetSecure(cookie, interp)) {
0656
        Tcl_SetObjResult(interp, secure);
0657
        ret = TCL_OK;
0658
      }
0659
    }
0660
  } else if (objc == 4) {
0661
    if (WtCookieCmdOwnVar(objv[2], &cookie, &created, interp)) {
0662
      if (WtCookieSetSecure(cookie, objv[3], interp)) {
0663
        if (secure = WtCookieGetSecure(cookie, interp)) {
0664
          if (WtCookieCmdSetVar(objv[2], cookie, interp)) {
0665
            Tcl_SetObjResult(interp, secure);
0666
            ret = TCL_OK;
0667
          }
0668
        }
0669
      }
0670
      if (created) {
0671
        Tcl_DecrRefCount(cookie);
0672
      }
0673
    }
0674
  } else {
0675
    Tcl_AppendResult(interp, wtBadUsagePrefix, "\n",
0676
      WtToString(objv[0]), " ", WtToString(objv[1]), " ",
0677
      " cookieVal\n",
0678
      WtToString(objv[0]), " ", WtToString(objv[1]), " ",
0679
      " cookieVar secure\n",
0680
      NULL);
0681
  }
0682
0683
  return ret;
0684
}
0685
0686
void WtCookieUsage(Tcl_Interp *interp, Tcl_Obj *cmd)
0687
{
0688
  char *cmdStr = WtToString(cmd);
0689
  Tcl_AppendResult(interp, wtBadUsagePrefix, "\n",
0690
    cmdStr, " create\n",
0691
    cmdStr, " name cookieVal\n",
0692
    cmdStr, " name cookieVar name\n",
0693
    cmdStr, " value cookieVal\n",
0694
    cmdStr, " value cookieVar value\n",
0695
    cmdStr, " allValues cookieVal\n",
0696
    cmdStr, " allValues cookieVar values\n",
0697
    cmdStr, " expires cookieVal\n",
0698
    cmdStr, " expires cookieVar expires\n",
0699
    cmdStr, " domain cookieVal\n",
0700
    cmdStr, " domain cookieVar domain\n",
0701
    cmdStr, " path cookieVal\n",
0702
    cmdStr, " path cookieVar path\n",
0703
    cmdStr, " secure cookieVal\n",
0704
    cmdStr, " secure cookieVar secure\n",
0705
    NULL);
0706
}
0707
0708
Tcl_Obj *WtCookieCmdGetVal(Tcl_Obj *val, Tcl_Interp *interp)
0709
{
0710
  return WtCmdGetVal(val, &WtTableType, interp);
0711
}
0712
0713
int WtCookieCmdGetVar(Tcl_Obj *var, Tcl_Obj **val,
0714
    Tcl_Interp *interp)
0715
{
0716
  return WtCmdGetVar(var, val, &WtTableType, interp);
0717
}
0718
0719
int WtCookieCmdOwnVar(Tcl_Obj *var, Tcl_Obj **val,
0720
  int *created, Tcl_Interp *interp)
0721
{
0722
  int ok = WtCmdOwnVar(var, val, &WtTableType, created, interp);
0723
  if (ok && !*val) {
0724
    *val = WtNewCookieObj();
0725
    Tcl_IncrRefCount(*val);
0726
    *created = 1;
0727
  }
0728
  return ok;
0729
}
0730
0731
Tcl_Obj *WtCookieCmdSetVar(Tcl_Obj *var, Tcl_Obj *val, Tcl_Interp *interp)
0732
{
0733
  return WtCmdSetVar(var, val, interp);
0734
}
0735
0736
/* Cookie collection object */
0737
0738
Tcl_Obj *WtNewCookieClnObj()
0739
{
0740
  Tcl_Obj *obj = WtNewClnObj(&WtCookieClnType, &WtCookieClnItemType);
0741
  return obj;
0742
}
0743
0744
struct Tcl_ObjType WtCookieClnType =
0745
{
0746
  "cookieCollection",
0747
  WtCookieClnFree,
0748
  WtCookieClnDup,
0749
  WtCookieClnUpdateString,
0750
  WtCookieClnSetFromAny
0751
};
0752
0753
void WtCookieClnFree(Tcl_Obj *cln)
0754
{
0755
  WtClnObjFree(cln);
0756
}
0757
0758
void WtCookieClnDup(Tcl_Obj *src, Tcl_Obj *dst)
0759
{
0760
  WtClnObjDup(src, dst);
0761
}
0762
0763
void WtCookieClnUpdateString(Tcl_Obj *cln)
0764
{
0765
  WtClnObjUpdateString(cln);
0766
}
0767
0768
int WtCookieClnSetFromAny(Tcl_Interp *interp, Tcl_Obj *obj)
0769
{
0770
  return WtClnObjSetFromAny(interp, obj, &WtCookieClnType);
0771
}
0772
0773
WtClnItemType WtCookieClnItemType = {
0774
  WtCookieClnCompare,
0775
  WtCookieClnGetKey,
0776
  WtCookieClnGetValue
0777
};
0778
0779
int WtCookieClnCompare(Tcl_Obj *item, Tcl_Obj *key, int *result,
0780
    Tcl_Interp *interp)
0781
{
0782
  Tcl_Obj *name = WtCookieGetName(item, interp);
0783
  if (name) {
0784
    *result = strcmp(WtToString(name), WtToString(key));
0785
    return 1;
0786
  }
0787
  return 0;
0788
}
0789
0790
int WtCookieClnGetKey(Tcl_Obj *item, Tcl_Obj **key,
0791
    Tcl_Interp *interp)
0792
{
0793
  *key = WtCookieGetName(item, interp);
0794
  if (*key) {
0795
    return 1;
0796
  }
0797
  return 0;
0798
}
0799
0800
int WtCookieClnGetValue(Tcl_Obj *item, Tcl_Obj **value,
0801
    Tcl_Interp *interp)
0802
{
0803
  *value = item;
0804
  return 1;
0805
}
0806
0807
/* responseCookies command */
0808
0809
int WtResponseCookiesCmd(ClientData clientData, Tcl_Interp *interp,
0810
    int objc, Tcl_Obj *const objv[])
0811
{
0812
  int ret, ok = 1;
0813
  WtContext *w = WtGetAssocContext(interp);
0814
  Tcl_Obj *cookie, *val;
0815
0816
  int status = WtClnMethodHelper(clientData, interp, objc,
0817
    objv, &ret, &w->web->clientResponse.cookies,
0818
    &WtCookieClnType, 0, 0);
0819
0820
  if (status == WT_CLN_CMD_MISSING) {
0821
0822
    /* No command */
0823
0824
    WtResponseCookiesUsage(interp, objv[0]);
0825
0826
  } else if (status == WT_CLN_CMD_NOT_FOUND) {
0827
0828
    /* deleteFromClient command */
0829
0830
    if (!strcmp(WtToString(objv[1]), "deleteFromClient")) {
0831
      if (objc < 3) {
0832
        Tcl_AppendResult(interp, wtBadUsagePrefix,
0833
          WtToString(objv[0]), " ", WtToString(objv[1]), " ",
0834
          " name", NULL);
0835
      } else if (cookie = WtNewCookieObj()) {
0836
        Tcl_IncrRefCount(cookie);
0837
0838
        if (!WtCookieSetName(cookie, objv[2], interp)) {
0839
          ok = 0;
0840
        }
0841
0842
        if (ok && objc > 3 && !WtCookieSetPath(
0843
            cookie, objv[3], interp)) {
0844
          ok = 0;
0845
        }
0846
0847
        if (ok && objc > 4 && !WtCookieSetDomain(
0848
            cookie, objv[4], interp)) {
0849
          ok = 0;
0850
        }
0851
0852
        if (ok && objc > 5 && !WtCookieSetSecure(
0853
            cookie, objv[5], interp)) {
0854
          ok = 0;
0855
        }
0856
0857
        if (ok) {
0858
          val = WtNewString(NULL);
0859
          Tcl_IncrRefCount(val);
0860
          if (WtCookieSetValue(cookie, val, interp)) {
0861
            if (WtCookieMarkExpired(cookie, interp)) {
0862
              if (WtOwnCookieCln(&w->web->clientResponse.cookies,
0863
                  interp)) {
0864
                if (WtAppendClnItem(w->web->clientResponse.cookies,
0865
                    cookie, interp)) {
0866
                  Tcl_SetObjResult(interp, cookie);
0867
                  ret = TCL_OK;
0868
                }
0869
              }
0870
            }
0871
          }
0872
          Tcl_DecrRefCount(val);
0873
        }
0874
0875
        Tcl_DecrRefCount(cookie);
0876
      }
0877
0878
    } else {
0879
0880
      /* Unknown command */
0881
0882
      WtResponseCookiesUsage(interp, objv[0]);
0883
0884
    }
0885
  }
0886
0887
  return ret;
0888
}
0889
0890
void WtResponseCookiesUsage(Tcl_Interp *interp, Tcl_Obj *cmd)
0891
{
0892
  Tcl_AppendResult(interp, wtBadUsagePrefix2, NULL);
0893
  WtClnMethodAppendUsage(interp, cmd, 0);
0894
  Tcl_AppendResult(interp, WtToString(cmd), " deleteFromClient key\n",
0895
    NULL);
0896
}
0897
0898
/* Initialize cookie commands */
0899
0900
void WtInitCookieCommands(Tcl_Interp *interp)
0901
{
0902
  WtContext *w = Tcl_GetAssocData(interp, "wt::context", NULL);
0903
0904
  WtLog(HERE, APLOG_DEBUG | APLOG_NOERRNO, w,
0905
    "Wtcl: WtInitCookieCommands.");
0906
0907
  Tcl_CreateObjCommand(interp, "::wt::response::cookie",
0908
    WtCookieCmd, NULL, NULL);
0909
0910
  Tcl_CreateObjCommand(interp, "::wt::response::responseCookies",
0911
    WtResponseCookiesCmd, NULL, NULL);
0912
}
0913
0914
int WtConvertToCookieCln(Tcl_Obj *obj, Tcl_Interp *interp)
0915
{
0916
  return WtConvertToClnObj(obj, &WtCookieClnType, interp);
0917
}
0918
0919
int WtOwnCookieCln(Tcl_Obj **objPtr, Tcl_Interp *interp)
0920
{
0921
  WtCopyOnWrite(objPtr);
0922
  return WtConvertToCookieCln(*objPtr, interp);
0923
}
0924
0925
const char *wtCookieEpoch = "Thu, 01-Jan-1970 00:00:01 GMT";