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

Lines Size Modified Created Owner MIME Types
367 9,276 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 "WtClientRequest.h"
0018
#include "WtTable.h"
0019
#include "WtTableUtil.h"
0020
#include "WtUpload.h"
0021
#include "WtUtil.h"
0022
#include "WtMultiTable.h"
0023
0024
/* Create ApacheRequest */
0025
0026
void WtInitApacheWeb(WtContext *w)
0027
{
0028
  if (!w->web->clientRequest.apWeb) {
0029
    w->web->clientRequest.apWeb = ApacheRequest_new(w->web->apReq);
0030
  }
0031
}
0032
0033
/* Read the request data */
0034
0035
int WtReadRequest(WtContext *w, Tcl_Interp *interp)
0036
{
0037
  int ok = 1;
0038
  table *tbl;
0039
  ApacheUpload *upload;
0040
  Tcl_Obj *uploadObj;
0041
0042
  if (w->web->clientRequest.readRequestStatus == 1) {
0043
    return 1;
0044
  } else if (w->web->clientRequest.readRequestStatus == 2) {
0045
    Tcl_AppendResult(interp, "Failed to read request.", NULL);
0046
    return 0;
0047
  }
0048
0049
  WtInitApacheWeb(w);
0050
0051
  if (!WtParseRequest(w, interp)) {
0052
    ok = 0;
0053
  }
0054
0055
  if (ok) {
0056
    tbl = ApacheRequest_query_params(w->web->clientRequest.apWeb,
0057
      w->web->apReq->pool);
0058
    if (!WtOwnTable(&w->web->clientRequest.queryParams, interp)) {
0059
      ok = 0;
0060
    } else if (!WtUpdateTableFromAp(w->web->clientRequest.queryParams,
0061
        tbl, interp)) {
0062
      ok = 0;
0063
    }
0064
  }
0065
0066
  if (ok) {
0067
    tbl = ApacheRequest_post_params(w->web->clientRequest.apWeb,
0068
      w->web->apReq->pool);
0069
    if (!WtOwnTable(&w->web->clientRequest.postParams, interp)) {
0070
      ok = 0;
0071
    } else if (!WtUpdateTableFromAp(w->web->clientRequest.postParams,
0072
        tbl, interp)) {
0073
      ok = 0;
0074
    }
0075
  }
0076
0077
  if (ok) {
0078
    if (!WtReadRequestCookies(w, interp)) {
0079
      ok = 0;
0080
    }
0081
  }
0082
0083
  if (ok) {
0084
    upload = ApacheRequest_upload(w->web->clientRequest.apWeb);
0085
  
0086
    for (; upload; upload = upload->next) {
0087
      WtLog(HERE, APLOG_DEBUG | APLOG_NOERRNO, w,
0088
        "Wtcl: Received uploaded file "
0089
        "(upload = 0x%x, name = \"%s\", filename = \"%s\", "
0090
        "content type = \"%s\", size = %d).",
0091
        upload, WtSafeStr(upload->name),
0092
        WtSafeStr(upload->filename),
0093
        WtSafeStr(ApacheUpload_type(upload)),
0094
        ApacheUpload_size(upload),
0095
        NULL);
0096
0097
      if (!(uploadObj = WtNewUploadObj(upload, interp))) {
0098
        ok = 0;
0099
        break;
0100
      }
0101
0102
      if (!WtOwnTable(&w->web->clientRequest.uploads, interp)) {
0103
        ok = 0;
0104
        break;
0105
      }
0106
0107
      if (!WtTableAppendObjToList(w->web->clientRequest.uploads,
0108
          WtSafeStr(upload->name), uploadObj, interp)) {
0109
        ok = 0;
0110
        break;
0111
      }
0112
0113
      WtTableRemoveStr(w->web->clientRequest.postParams,
0114
        WtSafeStr(upload->name));
0115
    }
0116
  }
0117
0118
  if (ok) {
0119
    if (!WtOwnTable(&w->web->clientRequest.params, interp)) {
0120
      ok = 0;
0121
    } else if (!WtUpdateTable(w->web->clientRequest.params,
0122
        w->web->clientRequest.queryParams)) {
0123
      ok = 0;
0124
    } else if (!WtUpdateTable(w->web->clientRequest.params,
0125
        w->web->clientRequest.postParams)) {
0126
      ok = 0;
0127
    }
0128
  }
0129
0130
  if (!ok) {
0131
    if (WtOwnTable(&w->web->clientRequest.queryParams, interp)) {
0132
      WtClearTable(w->web->clientRequest.queryParams);
0133
    }
0134
0135
    if (WtOwnTable(&w->web->clientRequest.postParams, interp)) {
0136
      WtClearTable(w->web->clientRequest.postParams);
0137
    }
0138
0139
    if (WtOwnTable(&w->web->clientRequest.params, interp)) {
0140
      WtClearTable(w->web->clientRequest.params);
0141
    }
0142
0143
    if (WtOwnTable(&w->web->clientRequest.uploads, interp)) {
0144
      WtClearTable(w->web->clientRequest.uploads);
0145
    }
0146
0147
    w->web->clientRequest.readRequestStatus = 2;
0148
  } else {
0149
    w->web->clientRequest.readRequestStatus = 1;
0150
  }
0151
0152
  return ok;
0153
}
0154
0155
int WtParseRequest(WtContext *w, Tcl_Interp *interp)
0156
{
0157
  const char *errMsg;
0158
0159
  WtInitApacheWeb(w);
0160
0161
  if (w->web->clientRequest.parseStatus == 1) {
0162
    return 1;
0163
  } else if (w->web->clientRequest.parseStatus == 2) {
0164
    Tcl_AppendResult(interp, "Failed to parse request.", NULL);
0165
    return 0;
0166
  }
0167
0168
  if (ApacheRequest_parse(w->web->clientRequest.apWeb) != OK) {
0169
    w->web->clientRequest.parseStatus = 2;
0170
    if (interp) {
0171
      errMsg = ap_table_get(w->web->apReq->notes, "error-notes");
0172
      if (errMsg && *errMsg) {
0173
        Tcl_AppendResult(interp, "Failed to read request data: ",
0174
          errMsg, NULL);
0175
      } else {
0176
        Tcl_AppendResult(interp, "Failed to read request data.",
0177
          NULL);
0178
      }
0179
    }
0180
  } else {
0181
    w->web->clientRequest.parseStatus = 1;
0182
  }
0183
0184
  return w->web->clientRequest.parseStatus == 1;
0185
}
0186
0187
/* Read the request cookies */
0188
0189
int WtReadRequestCookies(WtContext *w, Tcl_Interp *interp)
0190
{
0191
  int ok = 1, count, i, nvals, j;
0192
  ApacheCookie *cookie;
0193
  char *name, *val;
0194
0195
  if (w->web->clientRequest.readCookiesStatus != 0) {
0196
    return w->web->clientRequest.readCookiesStatus == 1;
0197
  }
0198
0199
  WtInitApacheWeb(w);
0200
  w->web->clientRequest.apCookies = ApacheCookie_parse(w->web->apReq, NULL);
0201
  if (!w->web->clientRequest.apCookies) {
0202
    ok = 0;
0203
  }
0204
0205
  if (ok) {
0206
    count = ApacheCookieJarItems(w->web->clientRequest.apCookies);
0207
    for (i = 0; i < count && ok; i++) {
0208
      cookie = ApacheCookieJarFetch(w->web->clientRequest.apCookies, i);
0209
      if (cookie) {
0210
        name = cookie->name ? cookie->name : "";
0211
        nvals = ApacheCookieItems(cookie);
0212
        for (j = 0; j < nvals; j++) {
0213
          val = ApacheCookieFetch(cookie, j);
0214
          if (!val) {
0215
            val = "";
0216
          }
0217
0218
          if (!WtOwnTable(&w->web->clientRequest.cookies, interp)) {
0219
            ok = 0;
0220
            break;
0221
          }
0222
0223
          if (!WtTableAppendStrToList(w->web->clientRequest.cookies,
0224
              name, val, interp)) {
0225
            ok = 0;
0226
            break;
0227
          }
0228
        }
0229
      }
0230
    }
0231
  }
0232
0233
  if (!ok) {
0234
    if (WtOwnTable(&w->web->clientRequest.cookies, interp)) {
0235
      WtClearTable(w->web->clientRequest.cookies);
0236
    }
0237
    w->web->clientRequest.readCookiesStatus = 2;
0238
  } else {
0239
    w->web->clientRequest.readCookiesStatus = 1;
0240
  }
0241
0242
  return ok;
0243
}
0244
0245
/* queryParams command */
0246
0247
int WtQueryParamsCmd(ClientData clientData, Tcl_Interp *interp,
0248
    int objc, Tcl_Obj *const objv[])
0249
{
0250
  int ret;
0251
  WtContext *w = WtGetAssocContext(interp);
0252
0253
  if (!WtReadRequest(w, interp)) {
0254
    return TCL_ERROR;
0255
  }
0256
0257
  WtMultiTableMethodHelper(clientData, interp, objc, objv, &ret,
0258
    &w->web->clientRequest.queryParams, &WtTableType, 0, 1);
0259
0260
  return ret;
0261
}
0262
0263
/* postParams command */
0264
0265
int WtPostParamsCmd(ClientData clientData, Tcl_Interp *interp,
0266
    int objc, Tcl_Obj *const objv[])
0267
{
0268
  int ret;
0269
  WtContext *w = WtGetAssocContext(interp);
0270
0271
  if (!WtReadRequest(w, interp)) {
0272
    return TCL_ERROR;
0273
  }
0274
0275
  WtMultiTableMethodHelper(clientData, interp, objc, objv, &ret,
0276
    &w->web->clientRequest.postParams, &WtTableType, 0, 1);
0277
0278
  return ret;
0279
}
0280
0281
/* params command */
0282
0283
int WtParamsCmd(ClientData clientData, Tcl_Interp *interp,
0284
    int objc, Tcl_Obj *const objv[])
0285
{
0286
  int ret;
0287
  WtContext *w = WtGetAssocContext(interp);
0288
0289
  if (!WtReadRequest(w, interp)) {
0290
    return TCL_ERROR;
0291
  }
0292
0293
  /* XXX: Add separate init for combined params set */
0294
0295
  WtMultiTableMethodHelper(clientData, interp, objc, objv, &ret,
0296
    &w->web->clientRequest.params, &WtTableType, 0, 1);
0297
0298
  return ret;
0299
}
0300
0301
/* requestCookies command */
0302
0303
int WtRequestCookiesCmd(ClientData clientData, Tcl_Interp *interp,
0304
    int objc, Tcl_Obj *const objv[])
0305
{
0306
  int ret;
0307
  WtContext *w = WtGetAssocContext(interp);
0308
0309
  if (!WtReadRequest(w, interp)) {
0310
    return TCL_ERROR;
0311
  }
0312
0313
  WtMultiTableMethodHelper(clientData, interp, objc, objv, &ret,
0314
    &w->web->clientRequest.cookies, &WtTableType, 0, 1);
0315
0316
  return ret;
0317
}
0318
0319
/* requestHeaders command */
0320
0321
int WtRequestHeadersCmd(ClientData clientData, Tcl_Interp *interp,
0322
    int objc, Tcl_Obj *const objv[])
0323
{
0324
  int ret = TCL_ERROR, size;
0325
  WtContext *w = WtGetAssocContext(interp);
0326
0327
  if (WtTableObjSize(w->web->clientRequest.headers, &size,
0328
      &WtCaselessTableType, interp)) {
0329
    if (size != 0) {
0330
      ret = TCL_OK;
0331
    } else if (WtOwnCaselessTable(&w->web->clientRequest.headers,
0332
        interp)) {
0333
      if (WtUpdateTableFromAp(w->web->clientRequest.headers,
0334
          w->web->apReq->headers_in, interp)) {
0335
        ret = TCL_OK;
0336
      }
0337
    }
0338
  }
0339
0340
  if (ret == TCL_OK) {
0341
    WtMultiTableMethodHelper(clientData, interp, objc,
0342
      objv, &ret, &w->web->clientRequest.headers,
0343
      &WtCaselessTableType, 0, 1);
0344
  }
0345
0346
  return ret;
0347
}
0348
0349
/* Initialize client request commands */
0350
0351
void WtInitClientRequestCommands(Tcl_Interp *interp)
0352
{
0353
  Tcl_CreateObjCommand(interp, "::wt::request::queryParams",
0354
    WtQueryParamsCmd, NULL, NULL);
0355
0356
  Tcl_CreateObjCommand(interp, "::wt::request::postParams",
0357
    WtPostParamsCmd, NULL, NULL);
0358
0359
  Tcl_CreateObjCommand(interp, "::wt::request::params",
0360
    WtParamsCmd, NULL, NULL);
0361
0362
  Tcl_CreateObjCommand(interp, "::wt::request::cookies",
0363
    WtRequestCookiesCmd, NULL, NULL);
0364
0365
  Tcl_CreateObjCommand(interp, "::wt::request::requestHeaders",
0366
    WtRequestHeadersCmd, NULL, NULL);
0367
}