1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
|
/*
* The contents of this file are subject to the AOLserver Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://aolserver.com/.
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is AOLserver Code and related documentation
* distributed by AOL.
*
* The Initial Developer of the Original Code is America Online,
* Inc. Portions created by AOL are Copyright (C) 1999 America Online,
* Inc. All Rights Reserved.
*
* Alternatively, the contents of this file may be used under the terms
* of the GNU General Public License (the "GPL"), in which case the
* provisions of GPL are applicable instead of those above. If you wish
* to allow use of your version of this file only under the terms of the
* GPL and not to allow others to use your version of this file under the
* License, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the GPL.
* If you do not delete the provisions above, a recipient may use your
* version of this file under either the License or the GPL.
*/
/*
* op.c --
*
* Routines to register, unregister, and run connection request
* routines (previously known as "op procs").
*/
static const char *RCSID = "@(#) $Header: /cvsroot/aolserver/aolserver/nsd/op.c,v 1.15 2005/10/07 00:48:23 dossy Exp $, compiled: " __DATE__ " " __TIME__;
#include "nsd.h"
#define MAX_RECURSION 3 /* Max return direct recursion limit. */
/*
* The following structure defines a request procedure including user
* routine and client data.
*/
typedef struct {
int refcnt;
Ns_OpProc *proc;
Ns_Callback *delete;
void *arg;
unsigned int flags;
} Req;
/*
* Static functions defined in this file.
*/
static void FreeReq(void *arg);
/*
* Static variables defined in this file.
*/
static Ns_Mutex ulock;
static int uid;
/*
*----------------------------------------------------------------------
*
* NsInitRequests --
*
* Initialize the request API.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
void
NsInitRequests(void)
{
uid = Ns_UrlSpecificAlloc();
Ns_MutexInit(&ulock);
Ns_MutexSetName(&ulock, "nsd:requests");
}
/*
*----------------------------------------------------------------------
*
* Ns_RegisterRequest --
*
* Register a new procedure to be called to service matching
* given method and url pattern.
*
* Results:
* None.
*
* Side effects:
* Delete procedure of previously registered request, if any,
* will be called unless NS_OP_NODELETE flag is set.
*
*----------------------------------------------------------------------
*/
void
Ns_RegisterRequest(char *server, char *method, char *url, Ns_OpProc *proc,
Ns_Callback *delete, void *arg, int flags)
{
Req *reqPtr;
reqPtr = ns_malloc(sizeof(Req));
reqPtr->proc = proc;
reqPtr->delete = delete;
reqPtr->arg = arg;
reqPtr->flags = flags;
reqPtr->refcnt = 1;
Ns_MutexLock(&ulock);
Ns_UrlSpecificSet(server, method, url, uid, reqPtr, flags, FreeReq);
Ns_MutexUnlock(&ulock);
}
/*
*----------------------------------------------------------------------
*
* Ns_GetRequest --
*
* Return the procedures and context for a given method and url
* pattern.
*
* Results:
* None.
*
* Side effects:
*
*
*----------------------------------------------------------------------
*/
void
Ns_GetRequest(char *server, char *method, char *url, Ns_OpProc **procPtr,
Ns_Callback **deletePtr, void **argPtr, int *flagsPtr)
{
Req *reqPtr;
Ns_MutexLock(&ulock);
reqPtr = Ns_UrlSpecificGet(server, method, url, uid);
if (reqPtr != NULL) {
*procPtr = reqPtr->proc;
*deletePtr = reqPtr->delete;
*argPtr = reqPtr->arg;
*flagsPtr = reqPtr->flags;
} else {
*procPtr = NULL;
*deletePtr = NULL;
*argPtr = NULL;
*flagsPtr = 0;
}
Ns_MutexUnlock(&ulock);
}
/*
*----------------------------------------------------------------------
*
* Ns_UnRegisterRequest --
*
* Remove the procedure which would run for the given method and
* url pattern.
*
* Results:
* None.
*
* Side effects:
* Requests deleteProc may run.
*
*----------------------------------------------------------------------
*/
void
Ns_UnRegisterRequest(char *server, char *method, char *url, int inherit)
{
Ns_MutexLock(&ulock);
Ns_UrlSpecificDestroy(server, method, url, uid,
inherit ? 0 : NS_OP_NOINHERIT);
Ns_MutexUnlock(&ulock);
}
/*
*----------------------------------------------------------------------
*
* Ns_ConnRunRequest --
*
* Locate and execute the procedure for the given method and
* url pattern.
*
* Results:
* Standard request procedure result, normally NS_OK.
*
* Side effects:
* Depends on request procedure.
*
*----------------------------------------------------------------------
*/
int
Ns_ConnRunRequest(Ns_Conn *conn)
{
Req *reqPtr;
Conn *connPtr = (Conn *) conn;
int status;
char *server = Ns_ConnServer(conn);
/*
* Return a quick unavailable error on overflow.
*/
if (connPtr->flags & NS_CONN_OVERFLOW) {
return Ns_ConnReturnServiceUnavailable(conn);
}
/*
* Prevent infinite internal redirect loops.
*/
if (connPtr->recursionCount > MAX_RECURSION) {
Ns_Log(Error, "return: failed to redirect '%s %s': "
"exceeded recursion limit of %d",
conn->request->method, conn->request->url, MAX_RECURSION);
return Ns_ConnReturnInternalError(conn);
}
Ns_MutexLock(&ulock);
reqPtr = Ns_UrlSpecificGet(server, conn->request->method,
conn->request->url, uid);
if (reqPtr == NULL) {
Ns_MutexUnlock(&ulock);
return Ns_ConnReturnNotFound(conn);
}
++reqPtr->refcnt;
Ns_MutexUnlock(&ulock);
status = (*reqPtr->proc) (reqPtr->arg, conn);
Ns_MutexLock(&ulock);
FreeReq(reqPtr);
Ns_MutexUnlock(&ulock);
return status;
}
/*
*----------------------------------------------------------------------
*
* Ns_ConnRedirect --
*
* Perform an internal redirect by updating the connection's
* request URL and re-authorizing and running the request. This
* Routine is used in FastPath to redirect to directory files
* (e.g., index.html) and in return.c to redirect by HTTP result
* code (e.g., custom not-found handler).
*
* Results:
* Standard request procedure result, normally NS_OK.
*
* Side effects:
* Depends on request procedure.
*
*----------------------------------------------------------------------
*/
int
Ns_ConnRedirect(Ns_Conn *conn, char *url)
{
Conn *connPtr = (Conn *) conn;
int status;
++connPtr->recursionCount;
/*
* Update the request URL.
*/
Ns_SetRequestUrl(conn->request, url);
/*
* Re-authorize and run the request.
*/
status = Ns_AuthorizeRequest(Ns_ConnServer(conn), conn->request->method,
conn->request->url, conn->authUser,
conn->authPasswd, Ns_ConnPeer(conn));
switch (status) {
case NS_OK:
status = Ns_ConnRunRequest(conn);
break;
case NS_FORBIDDEN:
status = Ns_ConnReturnForbidden(conn);
break;
case NS_UNAUTHORIZED:
status = Ns_ConnReturnUnauthorized(conn);
break;
case NS_ERROR:
default:
status = Ns_ConnReturnInternalError(conn);
break;
}
return status;
}
/*
*----------------------------------------------------------------------
*
* Ns_RegisterProxyRequest --
*
* Register a new procedure to be called to proxy matching
* given method and protocol pattern.
*
* Results:
* None.
*
* Side effects:
* Delete procedure of previously registered request, if any.
*
*----------------------------------------------------------------------
*/
void
Ns_RegisterProxyRequest(char *server, char *method, char *protocol,
Ns_OpProc *proc, Ns_Callback *delete, void *arg)
{
NsServer *servPtr;
Req *reqPtr;
Ns_DString ds;
int new;
Tcl_HashEntry *hPtr;
servPtr = NsGetServer(server);
if (servPtr == NULL) {
Ns_Log(Error, "Ns_RegisterProxyRequest: no such server: %s", server);
return;
}
Ns_DStringInit(&ds);
Ns_DStringVarAppend(&ds, method, protocol, NULL);
reqPtr = ns_malloc(sizeof(Req));
reqPtr->refcnt = 1;
reqPtr->proc = proc;
reqPtr->delete = delete;
reqPtr->arg = arg;
reqPtr->flags = 0;
Ns_MutexLock(&servPtr->request.plock);
hPtr = Tcl_CreateHashEntry(&servPtr->request.proxy, ds.string, &new);
if (!new) {
FreeReq(Tcl_GetHashValue(hPtr));
}
Tcl_SetHashValue(hPtr, reqPtr);
Ns_MutexUnlock(&servPtr->request.plock);
Ns_DStringFree(&ds);
}
/*
*----------------------------------------------------------------------
*
* Ns_UnRegisterProxyRequest --
*
* Remove the procedure which would run for the given method and
* protocol.
*
* Results:
* None.
*
* Side effects:
* Request's deleteProc may run.
*
*----------------------------------------------------------------------
*/
void
Ns_UnRegisterProxyRequest(char *server, char *method, char *protocol)
{
NsServer *servPtr;
Ns_DString ds;
Tcl_HashEntry *hPtr;
servPtr = NsGetServer(server);
if (servPtr != NULL) {
Ns_DStringInit(&ds);
Ns_DStringVarAppend(&ds, method, protocol, NULL);
Ns_MutexLock(&servPtr->request.plock);
hPtr = Tcl_FindHashEntry(&servPtr->request.proxy, ds.string);
if (hPtr != NULL) {
FreeReq(Tcl_GetHashValue(hPtr));
Tcl_DeleteHashEntry(hPtr);
}
Ns_MutexUnlock(&servPtr->request.plock);
Ns_DStringFree(&ds);
}
}
/*
*----------------------------------------------------------------------
*
* NsConnRunProxyRequest --
*
* Locate and execute the procedure for the given method and
* protocol pattern.
*
* Results:
* Standard request procedure result, normally NS_OK.
*
* Side effects:
* Depends on request procedure.
*
*----------------------------------------------------------------------
*/
int
NsConnRunProxyRequest(Ns_Conn *conn)
{
Conn *connPtr = (Conn *) conn;
NsServer *servPtr = connPtr->servPtr;
Ns_Request *request = conn->request;
Req *reqPtr = NULL;
int status;
Ns_DString ds;
Tcl_HashEntry *hPtr;
Ns_DStringInit(&ds);
Ns_DStringVarAppend(&ds, request->method, request->protocol, NULL);
Ns_MutexLock(&servPtr->request.plock);
hPtr = Tcl_FindHashEntry(&servPtr->request.proxy, ds.string);
if (hPtr != NULL) {
reqPtr = Tcl_GetHashValue(hPtr);
++reqPtr->refcnt;
}
Ns_MutexUnlock(&servPtr->request.plock);
if (reqPtr == NULL) {
status = Ns_ConnReturnNotFound(conn);
} else {
status = (*reqPtr->proc) (reqPtr->arg, conn);
Ns_MutexLock(&servPtr->request.plock);
FreeReq(reqPtr);
Ns_MutexUnlock(&servPtr->request.plock);
}
Ns_DStringFree(&ds);
return status;
}
/*
*----------------------------------------------------------------------
*
* FreeReq --
*
* URL space callback to delete a request structure.
*
* Results:
* None.
*
* Side effects:
* Depends on request delete procedure.
*
*----------------------------------------------------------------------
*/
static void
FreeReq(void *arg)
{
Req *reqPtr = (Req *) arg;
if (--reqPtr->refcnt == 0) {
if (reqPtr->delete != NULL) {
(*reqPtr->delete) (reqPtr->arg);
}
ns_free(reqPtr);
}
}
|