void HandlerSet
::assignHandler(int socketNum, int conditionSet, TaskScheduler::BackgroundHandlerProc* handlerProc, void* clientData) {
  // First, see if there's already a handler for this socket:
  HandlerDescriptor* handler = lookupHandler(socketNum);
  if (handler == NULL) { // No existing handler, so create a new descr:
    handler = new HandlerDescriptor(fHandlers.fNextHandler);
    handler->socketNum = socketNum;
  }

  handler->conditionSet = conditionSet;
  handler->handlerProc = handlerProc;
  handler->clientData = clientData;
}
void HandlerSet::moveHandler(int oldSocketNum, int newSocketNum) {
  HandlerDescriptor* handler = lookupHandler(oldSocketNum);
  if (handler != NULL) {
    handler->socketNum = newSocketNum;
  }
}
void HandlerSet::clearHandler(int socketNum) {
  HandlerDescriptor* handler = lookupHandler(socketNum);
  delete handler;
}
Example #4
0
MaHandler *MaHost::matchHandlers(MaRequest *rq)
{
	MaHandler	*hp, *cloneHp;
	MaLocation	*lp;
	char		*uri;
	int			uriLen, rc;

	mprAssert(rq);

rematch:
	//
	//	Insert all handlers that are specified as match always. This includes
	//	typically the Auth handler
	// 
	hp = (MaHandler*) handlers.getFirst();
	while (hp) {
		if (hp->flags & MPR_HANDLER_ALWAYS) {
			cloneHp = hp->cloneHandler();
			rq->insertHandler(cloneHp);
			if (hp->getFlags() & MPR_HANDLER_TERMINAL) {
				rq->setExtraPath(0, -1);
				return cloneHp;
			}
		}
		hp = (MaHandler*) handlers.getNext(hp);
	}

	//
	//	Then match by URI prefix. A URI may have an extra path segment after
	//	the URI (E.g. /cgi-bin/scriptName/some/Extra/Path. We need to know where
	//	the URI ends. NOTE: ScriptAlias directives are handled here.
	//
	lp = (MaLocation*) locations.getFirst();
	while (lp) {
		uri = rq->getUri();
		if (lp->getHandlerName() != 0) {

			rc = strncmp(lp->getPrefix(), uri, lp->getPrefixLen());
			if (rc == 0) {
					
				hp = lookupHandler(lp->getHandlerName());
				if (hp == 0) {
					mprError(MPR_L, MPR_LOG, "Handler %s has not been added", 
						lp->getHandlerName());
					lp = (MaLocation*) locations.getNext(lp);
					continue;
				}
				/* FUTURE -- match all method types */
				if (rq->getFlags() & MPR_HTTP_PUT_REQUEST && 
						!(hp->getFlags() & MPR_HANDLER_PUT)) {
					continue;
				}
				if (lp->getFlags() & MPR_HTTP_LOC_EXTRA_PATH ||
						(hp->flags & MPR_HANDLER_TERMINAL &&
						 hp->flags & MPR_HANDLER_EXTRA_PATH)) {
					if (rq->setExtraPath(lp->getPrefix(), lp->getPrefixLen()) 
							< 0) {
						rq->requestError(400, "Extra path does not validate");
						rq->finishRequest();
						return 0;
					}
				}
				rq->setLocation(lp);
				cloneHp = hp->cloneHandler();
				rq->insertHandler(cloneHp);
				if (hp->getFlags() & MPR_HANDLER_TERMINAL) {
					return cloneHp;
				}
				break;
			}
		}
		lp = (MaLocation*) locations.getNext(lp);
	}
	rq->setExtraPath(0, -1);

	//
	//	Now match by extension or by any custom handler matching technique
	//	FUTURE - optimize match by extension.
	//
	hp = (MaHandler*) handlers.getFirst();
	while (hp) {
		uri = rq->getUri();
		uriLen = strlen(uri);
		rc = hp->matchRequest(rq, uri, uriLen);
		if (rc < 0) {
			return 0;
		} else if (rc == 1) {
			if (! (hp->getFlags() & MPR_HANDLER_ALWAYS)) {
				cloneHp = hp->cloneHandler();
				rq->insertHandler(cloneHp);
				if (hp->getFlags() & MPR_HANDLER_TERMINAL) {
					return cloneHp;
				}
			}
		}
		if (uri != rq->getUri()) {
			rq->deleteHandlers();
			goto rematch;
		}
		hp = (MaHandler*) handlers.getNext(hp);
	}
	rq->requestError(404, "No handler for URL: %s", rq->getUri());
	rq->finishRequest();
	return 0;
}