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

Lines Size Modified Created Owner MIME Types
447 12,352 2010/05/08 18:46:41 2011/06/13 15:35:16 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 "WtUpload.h"
0018
#include "WtUtil.h"
0019
#include "WtTable.h"
0020
#include "WtTableUtil.h"
0021
#include "WtMultiTable.h"
0022
#include "WtClientRequest.h"
0023
0024
/* uploadSettings command */
0025
0026
void WtUploadSettingsUsage(Tcl_Interp *interp, Tcl_Obj *cmd)
0027
{
0028
  char *cmdStr = WtToString(cmd);
0029
  Tcl_AppendResult(interp, wtBadUsagePrefix, "\n",
0030
    cmdStr, " postMax ?size?\n",
0031
    cmdStr, " allowUploads ?flag?\n",
0032
    cmdStr, " tempDir ?path?\n",
0033
    cmdStr, " uploadHandler ?script?\n",
0034
    cmdStr, " uploadHandlerData ?data?\n",
0035
    cmdStr, " info\n",
0036
    NULL);
0037
}
0038
0039
int WtUploadSettingsCmd(ClientData clientData, Tcl_Interp *interp,
0040
    int objc, Tcl_Obj *const objv[])
0041
{
0042
  int ret = TCL_ERROR, intVal;
0043
  Tcl_Obj *interpRes = Tcl_GetObjResult(interp), *tbl;
0044
  const char *subCmd;
0045
  char *str;
0046
  WtContext *w;
0047
0048
  w = (WtContext *)Tcl_GetAssocData(interp, "wt::context", NULL);
0049
0050
  if (objc < 2) {
0051
    WtUploadSettingsUsage(interp, objv[0]);
0052
    return TCL_ERROR;
0053
  }
0054
0055
  subCmd = WtToString(objv[1]);
0056
0057
  if (!strcmp(subCmd, "postMax")) {
0058
0059
    /* postMax command */
0060
0061
    if (objc != 2 && objc != 3) {
0062
      Tcl_AppendStringsToObj(interpRes, wtBadUsagePrefix,
0063
        WtToString(objv[0]), " ", WtToString(objv[1]), " ?size?");
0064
    } else if (objc == 2) {
0065
      Tcl_SetObjResult(interp,
0066
        Tcl_NewIntObj(w->web->clientRequest.apWeb->post_max));
0067
      ret = TCL_OK;
0068
    } else {
0069
      if (Tcl_GetIntFromObj(interp, objv[2], &intVal) == TCL_OK) {
0070
        ApacheRequest_set_post_max(w->web->clientRequest.apWeb, intVal);
0071
        Tcl_SetObjResult(interp,
0072
          Tcl_NewIntObj(w->web->clientRequest.apWeb->post_max));
0073
        ret = TCL_OK;        
0074
      }
0075
    }
0076
0077
  } else if (!strcmp(subCmd, "allowUploads")) {
0078
0079
    /* allowUploads command */
0080
0081
    if (objc != 2 && objc != 3) {
0082
      Tcl_AppendStringsToObj(interpRes, wtBadUsagePrefix,
0083
        WtToString(objv[0]), " ", WtToString(objv[1]), " ?flag?");
0084
    } else if (objc == 2) {
0085
      Tcl_SetObjResult(interp,
0086
        Tcl_NewBooleanObj(!w->web->clientRequest.apWeb->disable_uploads));
0087
      ret = TCL_OK;
0088
    } else {
0089
      if (Tcl_GetBooleanFromObj(interp, objv[2], &intVal) == TCL_OK) {
0090
        w->web->clientRequest.apWeb->disable_uploads = !intVal;
0091
        Tcl_SetObjResult(interp,
0092
          Tcl_NewBooleanObj(!w->web->clientRequest.apWeb->disable_uploads));
0093
        ret = TCL_OK;
0094
      }
0095
    }
0096
0097
  } else if (!strcmp(subCmd, "tempDir")) {
0098
0099
    /* tempDir command */
0100
0101
    if (objc != 2) {
0102
      Tcl_AppendStringsToObj(interpRes, wtBadUsagePrefix,
0103
        WtToString(objv[0]), " ", WtToString(objv[1]), " ?path?");
0104
    } else if (objc == 2) {
0105
      Tcl_SetObjResult(interp, WtNewString(w->web->clientRequest.apWeb->temp_dir));
0106
      ret = TCL_OK;
0107
    } else {
0108
      str = WtToString(objv[2]);
0109
      ApacheRequest_set_temp_dir(w->web->clientRequest.apWeb,
0110
        ap_pstrdup(w->web->apReq->pool, str));
0111
      Tcl_SetObjResult(interp, WtNewString(w->web->clientRequest.apWeb->temp_dir));
0112
      ret = TCL_OK;
0113
    }
0114
0115
  } else if (!strcmp(subCmd, "uploadHandler")) {
0116
0117
    /* uploadHandler command */
0118
0119
    if (objc != 2) {
0120
      Tcl_AppendStringsToObj(interpRes, wtBadUsagePrefix,
0121
        WtToString(objv[0]), " ", WtToString(objv[1]), " ?command?");
0122
    } else if (objc == 2) {
0123
      Tcl_SetObjResult(interp, w->web->clientRequest.uploadHandler);
0124
      ret = TCL_OK;
0125
    } else {
0126
      w->web->clientRequest.uploadHandler = objv[2];
0127
      Tcl_IncrRefCount(w->web->clientRequest.uploadHandler);
0128
      Tcl_SetObjResult(interp, w->web->clientRequest.uploadHandler);
0129
      ret = TCL_OK;
0130
    }
0131
0132
  } else if (!strcmp(subCmd, "uploadHandlerData")) {
0133
0134
    /* uploadHandlerData command */
0135
0136
    if (objc != 2) {
0137
      Tcl_AppendStringsToObj(interpRes, wtBadUsagePrefix,
0138
        WtToString(objv[0]), " ", WtToString(objv[1]), " ?command?");
0139
    } else if (objc == 2) {
0140
      Tcl_SetObjResult(interp,
0141
        w->web->clientRequest.uploadHandlerData ?
0142
        w->web->clientRequest.uploadHandlerData : WtNewString(""));
0143
      ret = TCL_OK;
0144
    } else {
0145
      if (w->web->clientRequest.uploadHandlerData) {
0146
        Tcl_DecrRefCount(w->web->clientRequest.uploadHandlerData);
0147
      }
0148
      w->web->clientRequest.uploadHandlerData = objv[2];
0149
      Tcl_IncrRefCount(w->web->clientRequest.uploadHandlerData);
0150
      Tcl_SetObjResult(interp, w->web->clientRequest.uploadHandlerData);
0151
      ret = TCL_OK;
0152
    }
0153
0154
  } else if (!strcmp(subCmd, "info")) {
0155
0156
    /* info command - Create a table with the settings */
0157
0158
    tbl = WtNewTableObj();
0159
0160
    WtTableSetStrToInt(tbl, "postMax", w->web->clientRequest.apWeb->post_max);
0161
    WtTableSetStrToBool(tbl, "allowUploads",
0162
      !w->web->clientRequest.apWeb->disable_uploads);
0163
    WtTableSetStrToStr(tbl, "tempDir",
0164
      WtSafeStr(w->web->clientRequest.apWeb->temp_dir));
0165
    WtTableSetStrToObj(tbl, "uploadHandler",
0166
      w->web->clientRequest.uploadHandler);
0167
    WtTableSetStrToObj(tbl, "uploadHandlerData",
0168
      w->web->clientRequest.uploadHandlerData ?
0169
      w->web->clientRequest.uploadHandlerData : WtNewString(""));
0170
0171
    Tcl_SetObjResult(interp, tbl);
0172
0173
    ret = TCL_OK;
0174
0175
  } else {
0176
0177
    /* unknown command */
0178
0179
    WtUploadSettingsUsage(interp, objv[0]);
0180
0181
  }
0182
0183
  return ret;
0184
}
0185
0186
/* Create a new upload object */
0187
0188
Tcl_Obj *WtNewUploadObj(ApacheUpload *item, Tcl_Interp *interp)
0189
{
0190
  Tcl_Obj *obj = WtNewTableObj();
0191
0192
  WtTableSetStrToStr(obj, "name", WtSafeStr(item->name));
0193
  WtTableSetStrToStr(obj, "fileName", WtSafeStr(item->filename));
0194
  WtTableSetStrToStr(obj, "path", WtSafeStr(item->tempname));
0195
  WtTableSetStrToStr(obj, "contentType",
0196
    WtSafeStr(ApacheUpload_type(item)));
0197
  WtTableSetStrToInt(obj, "size", ApacheUpload_size(item));
0198
0199
  return obj;
0200
}
0201
0202
Tcl_Obj *WtUploadGetProperty(Tcl_Obj *upload, const char *key,
0203
    Tcl_Interp *interp)
0204
{
0205
  Tcl_Obj *val = WtTableGetObjFromStr(upload, key, NULL);
0206
  if (!val) {
0207
    Tcl_AppendResult(interp, "Unable to read ", key,
0208
      " property of upload object.");
0209
    return NULL;
0210
  }
0211
  return val;
0212
}
0213
0214
int WtUploadSetProperty(Tcl_Obj *upload, const char *key,
0215
    Tcl_Obj *val, Tcl_Interp *interp)
0216
{
0217
  WtTableSetStrToObj(upload, key, val);
0218
  return 1;
0219
}
0220
0221
Tcl_Obj *WtUploadGetName(Tcl_Obj *upload, Tcl_Interp *interp)
0222
{
0223
  return WtUploadGetProperty(upload, "name", interp);
0224
}
0225
0226
int WtUploadSetName(Tcl_Obj *upload, Tcl_Obj *val, Tcl_Interp *interp)
0227
{
0228
  return WtUploadSetProperty(upload, "name", val, interp);
0229
}
0230
0231
Tcl_Obj *WtUploadGetFileName(Tcl_Obj *upload, Tcl_Interp *interp)
0232
{
0233
  return WtUploadGetProperty(upload, "fileName", interp);
0234
}
0235
0236
int WtUploadSetFileName(Tcl_Obj *upload, Tcl_Obj *val, Tcl_Interp *interp)
0237
{
0238
  return WtUploadSetProperty(upload, "fileName", val, interp);
0239
}
0240
0241
Tcl_Obj *WtUploadGetPath(Tcl_Obj *upload, Tcl_Interp *interp)
0242
{
0243
  return WtUploadGetProperty(upload, "path", interp);
0244
}
0245
0246
int WtUploadSetPath(Tcl_Obj *upload, Tcl_Obj *val, Tcl_Interp *interp)
0247
{
0248
  return WtUploadSetProperty(upload, "path", val, interp);
0249
}
0250
0251
Tcl_Obj *WtUploadGetContentType(Tcl_Obj *upload, Tcl_Interp *interp)
0252
{
0253
  return WtUploadGetProperty(upload, "contentType", interp);
0254
}
0255
0256
int WtUploadSetContentType(Tcl_Obj *upload, Tcl_Obj *val, Tcl_Interp *interp)
0257
{
0258
  return WtUploadSetProperty(upload, "contentType", val, interp);
0259
}
0260
0261
Tcl_Obj *WtUploadGetSize(Tcl_Obj *upload, Tcl_Interp *interp)
0262
{
0263
  return WtUploadGetProperty(upload, "size", interp);
0264
}
0265
0266
int WtUploadSetSize(Tcl_Obj *upload, Tcl_Obj *val, Tcl_Interp *interp)
0267
{
0268
  return WtUploadSetProperty(upload, "size", val, interp);
0269
}
0270
0271
Tcl_Channel WtCreateUploadChannel(ApacheUpload *item, Tcl_Interp *interp)
0272
{
0273
  FILE *uploadFile;
0274
  Tcl_Channel uploadChan = NULL;
0275
0276
  if (uploadFile = ApacheUpload_FILE(item)) {
0277
#ifdef WIN32
0278
    uploadChan = Tcl_MakeFileChannel((ClientData)_get_osfhandle(
0279
      _fileno(uploadFile)), TCL_READABLE);
0280
#else
0281
    uploadChan = Tcl_MakeFileChannel((ClientData)_fileno(
0282
      uploadFile), TCL_READABLE);
0283
#endif
0284
0285
    if (!uploadChan) {
0286
      Tcl_AppendResult(interp, "Failed to create file channel.",
0287
        NULL);
0288
    } else {
0289
      Tcl_RegisterChannel(interp, uploadChan);
0290
    }
0291
  }
0292
0293
  return uploadChan;
0294
}
0295
0296
/* uploads command */
0297
0298
int WtUploadsCmd(ClientData clientData, Tcl_Interp *interp,
0299
    int objc, Tcl_Obj *const objv[])
0300
{
0301
  int ret;
0302
  WtContext *w = WtGetAssocContext(interp);
0303
0304
  if (!WtReadRequest(w, interp)) {
0305
    return TCL_ERROR;
0306
  }
0307
0308
  WtMultiTableMethodHelper(clientData, interp, objc, objv, &ret,
0309
    &w->web->clientRequest.uploads, &WtTableType, 0, 1);
0310
0311
  return ret;
0312
}
0313
0314
int WtUploadCmd(ClientData clientData, Tcl_Interp *interp, int objc,
0315
    Tcl_Obj *const objv[])
0316
{
0317
  int ret = TCL_ERROR;
0318
  const char *subCmd;
0319
  WtContext *w;
0320
  Tcl_Obj *upload, *obj;
0321
0322
  w = (WtContext *)Tcl_GetAssocData(interp, "wt::context", NULL);
0323
0324
  if (objc < 2) {
0325
    WtUploadUsage(interp, objv[0]);
0326
    return TCL_ERROR;
0327
  }
0328
0329
  subCmd = WtToString(objv[1]);
0330
0331
  /* Read the POST data if needed */
0332
0333
  if (!WtReadRequest(w, interp)) {
0334
    return TCL_ERROR;
0335
  }
0336
0337
  if (!strcmp(subCmd, "name")) {
0338
0339
    /* name command */
0340
0341
    if (objc != 3) {
0342
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0343
        WtToString(objv[0]), " ", WtToString(objv[1]), " uploadVal");
0344
    } else if (upload = WtUploadCmdGetVal(objv[2], interp)) {
0345
      if (obj = WtUploadGetName(upload, interp)) {
0346
        Tcl_SetObjResult(interp, obj);
0347
        ret = TCL_OK;
0348
      }
0349
    }
0350
0351
  } else if (!strcmp(subCmd, "fileName")) {
0352
0353
    /* fileName command */
0354
0355
    if (objc != 3) {
0356
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0357
        WtToString(objv[0]), " ", WtToString(objv[1]), " uploadVal");
0358
    } else if (upload = WtUploadCmdGetVal(objv[2], interp)) {
0359
      if (obj = WtUploadGetFileName(upload, interp)) {
0360
        Tcl_SetObjResult(interp, obj);
0361
        ret = TCL_OK;
0362
      }
0363
    }
0364
0365
  } else if (!strcmp(subCmd, "path")) {
0366
0367
    /* path command */
0368
0369
    if (objc != 3) {
0370
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0371
        WtToString(objv[0]), " ", WtToString(objv[1]), " uploadVal");
0372
    } else if (upload = WtUploadCmdGetVal(objv[2], interp)) {
0373
      if (obj = WtUploadGetPath(upload, interp)) {
0374
        Tcl_SetObjResult(interp, obj);
0375
        ret = TCL_OK;
0376
      }
0377
    }
0378
0379
  } else if (!strcmp(subCmd, "contentType")) {
0380
0381
    /* contentType command */
0382
0383
    if (objc != 3) {
0384
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0385
        WtToString(objv[0]), " ", WtToString(objv[1]), " upload");
0386
    } else if (upload = WtUploadCmdGetVal(objv[2], interp)) {
0387
      if (obj = WtUploadGetContentType(upload, interp)) {
0388
        Tcl_SetObjResult(interp, obj);
0389
        ret = TCL_OK;
0390
      }
0391
    }
0392
0393
  } else if (!strcmp(subCmd, "size")) {
0394
0395
    /* size command */
0396
0397
    if (objc != 3) {
0398
      Tcl_AppendResult(interp, wtBadUsagePrefix,
0399
        WtToString(objv[0]), " ", WtToString(objv[1]), " upload");
0400
    } else if (upload = WtUploadCmdGetVal(objv[2], interp)) {
0401
      if (obj = WtUploadGetSize(upload, interp)) {
0402
        Tcl_SetObjResult(interp, obj);
0403
        ret = TCL_OK;
0404
      }
0405
    }
0406
0407
  } else {
0408
0409
    /* Unknown command */
0410
0411
    WtUploadUsage(interp, objv[0]);
0412
0413
  }
0414
0415
  return ret;
0416
}
0417
0418
void WtUploadUsage(Tcl_Interp *interp, Tcl_Obj *cmd)
0419
{
0420
  char *cmdStr = WtToString(cmd);
0421
  Tcl_AppendResult(interp, wtBadUsagePrefix, "\n",
0422
    cmdStr, " name upload\n",
0423
    cmdStr, " fileName upload\n",
0424
    cmdStr, " path upload\n",
0425
    cmdStr, " contentType upload\n",
0426
    cmdStr, " size upload\n",
0427
    NULL);
0428
}
0429
0430
void WtInitUploadCommands(Tcl_Interp *interp)
0431
{
0432
  WtContext *w = WtGetAssocContext(interp);
0433
0434
  Tcl_CreateObjCommand(interp, "::wt::request::uploadSettings",
0435
    WtUploadSettingsCmd, NULL, NULL);
0436
0437
  Tcl_CreateObjCommand(interp, "::wt::request::uploads",
0438
    WtUploadsCmd, NULL, NULL);
0439
0440
  Tcl_CreateObjCommand(interp, "::wt::request::upload",
0441
    WtUploadCmd, NULL, NULL);
0442
}
0443
0444
Tcl_Obj *WtUploadCmdGetVal(Tcl_Obj *val, Tcl_Interp *interp)
0445
{
0446
  return WtCmdGetVal(val, &WtTableType, interp);
0447
}