Example #1
0
void MaClient::setProxy(char *host, int port)
{
	if (proxyHost) {
		mprFree(proxyHost);
	}
	proxyHost = mprStrdup(host);
	proxyPort = port;
}
Example #2
0
static void upload(MaQueue *q)
{
    MaConn      *conn;
    char        *sw;
    char        *newLocation;
    int         responseStatus;

    conn = q->conn;
    newLocation = 0;
    responseStatus = 0;

    sw = (char*) strstr(maGetFormVar(conn, "QUERY_STRING", ""), "SWITCHES=");
    if (sw) {
        sw = mprStrdup(q, sw + 9);
        mprUrlDecode(sw, (int) strlen(sw) + 1, sw);
        if (*sw == '-') {
            if (sw[1] == 'l') {
                newLocation = sw + 3;
            } else if (sw[1] == 's') {
                responseStatus = atoi(sw + 3);
            }
        }
    }

    maSetResponseCode(conn, 200);
    maSetResponseMimeType(conn, "text/html");
    maDontCacheResponse(conn);

    /*
     *  Test writing headers. The Server header overwrote the "Server" header
     *
     *  maSetHeader(conn, "MyCustomHeader: true");
     *  maSetHeader(conn, "Server: private");
     */

    if (maGetCookies(conn) == 0) {
        maSetCookie(conn, "appwebTest", "Testing can be fun", 43200, "/", 0);
    }

    if (newLocation) {
        maRedirect(conn, 302, newLocation);

    } else if (responseStatus) {
        maFailRequest(conn, responseStatus, "Custom Status");

    } else {
        maWrite(q, "<HTML><TITLE>egiProgram: EGI Output</TITLE><BODY>\r\n");

        printRequestHeaders(q);
        printQueryData(q);
        printBodyData(q);

        maWrite(q, "</BODY></HTML>\r\n");
    }
    if (sw) {
        mprFree(sw);
    }
}
Example #3
0
File: dir.cpp Project: OPSF/uClinux
MaDir::MaDir(MaDir *dp, MaAuth *auth)
{
	indexName = mprStrdup(dp->indexName);
	if (auth) {
		inherit(auth);
	}
	path = 0;
	setPath(dp->path);
}
Example #4
0
MaEgiForm::MaEgiForm(char *formName)
{
	name = mprStrdup(formName);
#if WIN
	mprStrLower(name);
#endif
	setKey(name);
	egiHandlerService->insertForm(0, 0, this);
}
Example #5
0
void maSetRequestUser(MaConn *conn, cchar *user)
{
    MaRequest   *req;

    req = conn->request;

    mprFree(req->user);
    req->user = mprStrdup(conn->request, user);
}
Example #6
0
MaEgiForm::MaEgiForm(MaServer *server, MaHost *host, char *formName)
{
	name = mprStrdup(formName);
#if WIN
	mprStrLower(name);
#endif
	setKey(name);
	egiHandlerService->insertForm(server, host, this);
}
Example #7
0
MaSslProvider::MaSslProvider(char *name)
{
	if (sslProvider) {
		mprError(MPR_L, MPR_LOG, "Multiple SSL providers defined\n");
	} else {
		sslProvider = this;
	}
	this->name = mprStrdup(name);
}
Example #8
0
void maSetRequestGroup(MaConn *conn, cchar *group)
{
    MaRequest   *req;

    req = conn->request;

    mprFree(req->group);
    req->group = mprStrdup(conn->request, group);
}
Example #9
0
void maSetAuthRequiredUsers(MaAuth *auth, cchar *users)
{
    if (mprGetParent(auth->requiredUsers) == auth) {
        mprFree(auth->requiredUsers);
    }
    auth->requiredUsers = mprStrdup(auth, users);
    auth->flags |= MA_AUTH_REQUIRED;
    auth->anyValidUser = 0;
}
Example #10
0
void MaHost::setLog(char *path, char *format)
{
	char	*src, *dest;

	mprAssert(path && *path);
	mprAssert(format && *format);

	logPath = mprStrdup(path);
	logFormat = mprStrdup(format);

	for (src = dest = logFormat; *src; src++) {
		if (*src == '\\' && src[1] != '\\') {
			continue;
		}
		*dest++ = *src;
	}
	*dest = '\0';
}
Example #11
0
void MaHost::setName(char *str)
{
	lock();
	if (name) {
		mprFree(name);
	}
	name = mprStrdup(str);
	unlock();
}
Example #12
0
int mprMakeTempFileName(MprCtx ctx, char *buf, int bufsize, const char *tempDir)
{
	MprFile		*file;
	MprTime		now;
	char		*dir;
	int 		seed, i;

	if (tempDir == 0) {
#if WIN
		char	*cp;
		dir = mprStrdup(ctx, getenv("TEMP"));
		for (cp = dir; *cp; cp++) {
			if (*cp == '\\') {
				*cp = '/';
			}
		}
#else
		dir = mprStrdup(ctx, "/tmp");
#endif
	} else {
		dir = mprStrdup(ctx, tempDir);
	}

	mprGetTime(ctx, &now);
	seed = now.msec % 64000;
	file = 0;

	for (i = 0; i < 128; i++) {
		mprSprintf(buf, bufsize, "%s/MPR_%d_%d.tmp", dir, getpid(), seed++);
		file = mprOpen(ctx, buf, O_CREAT | O_EXCL | O_BINARY, 0664);
		if (file) {
			break;
		}
	}

	if (file == 0) {
		return MPR_ERR_CANT_CREATE;
	}

	mprClose(file);
	mprFree(dir);

	return 0;
}
Example #13
0
MprApp *mprInitEx(MprAllocCback cback, void *shell)
{
	MprApp	*app;

	app = (MprApp*) mprAllocInit(cback);

	mprAssert(app);
	if (app == 0) {
		return 0;
	}

	app->name = mprStrdup(app, BLD_PRODUCT);
	app->title = mprStrdup(app, BLD_NAME);
	app->version = mprStrdup(app, BLD_VERSION);

	mprSetShell(app, shell);

	app->table = mprCreateSymbolTable(app, 0);

	if (mprStartFileServices(app) < 0) {
		mprAllocTerm(app);
		return 0;
	}

#if BLD_FEATURE_MULTITHREAD
	mprInitThreads(app);
#endif

	/*
	 *	See if any of the preceeding allocations failed
	 */
	if (mprGetAllocErrors(app) > 0) {
		mprAllocTerm(app);
		return 0;
	}

	/*
 	 *	Mark all blocks allocated so far as required. They will then be 
	 *	omitted from leak reports.
	 */
	mprSetRequiredAlloc(app, 1);

	return app;
}
Example #14
0
static void addFormVars(MprCtx ctx, cchar *buf)
{
    char    *pair, *tok;

    pair = mprStrTok(mprStrdup(ctx, buf), "&", &tok);
    while (pair != 0) {
        mprAddItem(formData, pair);
        pair = mprStrTok(0, "&", &tok);
    }
}
Example #15
0
/*
 *  function load(script: Path, timeout: Number = 0): Void
 */
static EjsVar *workerLoad(Ejs *ejs, EjsWorker *worker, int argc, EjsVar **argv)
{
    int     timeout;

    mprAssert(argc == 0 || ejsIsPath(argv[0]));

    worker->scriptFile = mprStrdup(worker, ((EjsPath*) argv[0])->path);
    timeout = argc == 2 ? ejsGetInt(argv[1]): 0;
    return startWorker(ejs, worker, timeout);
}
Example #16
0
MaDir::MaDir(MaHost *host, MaDir *dp)
{
	this->host = host;
	indexName = mprStrdup(dp->indexName);
	inheritAuth(dp);
	pathLen = 0;
	path = 0;
	setPath(dp->path);
	inherited = true;
}
Example #17
0
/*
 *  Put an environment var
 *
 *  function putenv(key: String, value: String): void
 */
static EjsVar *putEnvVar(Ejs *ejs, EjsObject *app, int argc, EjsVar **argv)
{

#if !WINCE
#if BLD_UNIX_LIKE
    char    *key, *value;

    key = mprStrdup(ejs, ejsGetString(argv[0]));
    value = mprStrdup(ejs, ejsGetString(argv[1]));
    setenv(key, value, 1);
#else
    char   *cmd;

    cmd = mprStrcat(app, -1, ejsGetString(argv[0]), "=", ejsGetString(argv[1]), NULL);
    putenv(cmd);
#endif
#endif
    return 0;
}
Example #18
0
/*
 *  function eval(script: String, timeout: Boolean = -1): String
 */
static EjsVar *workerEval(Ejs *ejs, EjsWorker *worker, int argc, EjsVar **argv)
{
    int     timeout;

    mprAssert(ejsIsString(argv[0]));

    worker->scriptLiteral = mprStrdup(worker, ejsGetString(argv[0]));
    timeout = argc == 2 ? ejsGetInt(argv[1]): MAXINT;
    return startWorker(ejs, worker, timeout);
}
Example #19
0
/*
 *  Interpret from the console or from a literal command
 */
static int interpretCommands(EcCompiler *cp, cchar *cmd)
{
    Ejs         *ejs;
    EjsString   *result;
    char        *tmpArgv[1];
    int         err;

    ejs = cp->ejs;

    if (ecOpenConsoleStream(cp->lexer, (cmd) ? commandGets: consoleGets) < 0) {
        mprError(cp, "Can't open input");
        return EJS_ERR;
    }
    if (cmd) {
        cp->lexer->input->stream->buf = mprStrdup(cp, cmd);
    } else {
        cp->interactive = 1;
    }
    cp->input = cp->lexer->input;
    cp->token = cp->lexer->input->token;

    ecResetInput(cp);
    tmpArgv[0] = EC_INPUT_STREAM;

    while (!cp->lexer->input->stream->eof) {
        err = 0;
        cp->uid = 0;
        if (ecCompile(cp, 1, tmpArgv, 0) < 0) {
            ejs->result = ejs->undefinedValue;
            err++;
        }
        if (cp->run && !err && cp->errorCount == 0) {
            if (ejsRun(ejs) < 0) {
                ejsReportError(ejs, "Error in script");
            }
        }
        if (!ejs->exception && ejs->result != ejs->undefinedValue) {
            if (ejs->result->primitive || ejsIsDate(ejs->result)) {
                if ((result = (EjsString*) ejsToString(ejs, ejs->result)) != 0) {
                    mprPrintf(cp, "%s\n", result->value);
                }
            } else {
                if ((result = (EjsString*) ejsToJson(ejs, ejs->result)) != 0) {
                    mprPrintf(cp, "%s\n", result->value);
                }
            }
        }
        ecResetInput(cp);
        cp->errorCount = 0;
        err = 0;
    }
    ecCloseStream(cp->lexer);

    return 0;
}
Example #20
0
/*
 *  Dynamic module initialization
 */
MprModule *maEjsHandlerInit(MaHttp *http, cchar *path)
{
    MprModule       *module;
    MaStage         *handler;
    EjsWebControl   *control;

    module = mprCreateModule(http, "ejsHandler", BLD_VERSION, 0, 0, 0);
    if (module == 0) {
        return 0;
    }

    handler = maCreateHandler(http, "ejsHandler", 
        MA_STAGE_GET | MA_STAGE_HEAD | MA_STAGE_POST | MA_STAGE_PUT | MA_STAGE_FORM_VARS | MA_STAGE_VIRTUAL);
    if (handler == 0) {
        mprFree(module);
        return 0;
    }
    http->ejsHandler = handler;
    handler->match = matchEjs;
    handler->run = runEjs;
    handler->parse = parseEjs;

    /*
     *  Setup the control block
     */
    handler->stageData = control = mprAllocObjZeroed(handler, EjsWebControl);

    control->defineParams = defineParams;
    control->discardOutput = discardOutput;
    control->error = error;
    control->getHeader = getHeader;
    control->getVar = getVar;
    control->redirect = redirect;
    control->setCookie = setCookie;
    control->setHeader = setHeader;
    control->setHttpCode = setHttpCode;
    control->setMimeType = setMimeType;
    control->write = writeBlock;
    control->modulePath = mprStrdup(control, path);

#if BLD_FEATURE_MULTITHREAD && FUTURE
    /*
     *  This mutex is used very sparingly and must be an application global lock.
     */
    mutex = mprCreateLock(control);
    control->lock = ejsWebLock;
    control->unlock = ejsWebUnlock;
    control->lockData = mutex;
#endif

    if (ejsOpenWebFramework(control, 1) < 0) {
        return 0;
    }
    return module;
}
Example #21
0
static char *getPassword(MprCtx ctx)
{
#if !WINCE
    char    *password;

    password = getpass("Password: "******"no-user-interaction-support";
#endif
    return mprStrdup(ctx, password);
}
Example #22
0
/*
 *  Decode basic authorization details
 */
static void decodeBasicAuth(MaConn *conn, AuthData *ad)
{
    MaRequest   *req;
    char        decodedDetails[64], *cp;

    req = conn->request;
    mprDecode64(decodedDetails, sizeof(decodedDetails), req->authDetails);
    if ((cp = strchr(decodedDetails, ':')) != 0) {
        *cp++ = '\0';
    }
    if (cp) {
        ad->userName = mprStrdup(req, decodedDetails);
        ad->password = mprStrdup(req, cp);

    } else {
        ad->userName = mprStrdup(req, "");
        ad->password = mprStrdup(req, "");
    }
    maSetRequestUser(conn, ad->userName);
}
Example #23
0
/*
 *  Format an authentication response. This is typically a 401 response code.
 */
static void formatAuthResponse(MaConn *conn, MaAuth *auth, int code, char *msg, char *logMsg)
{
    MaRequest       *req;
#if BLD_FEATURE_AUTH_DIGEST
    char            *qopClass, *nonceStr, *etag;
#endif

    req = conn->request;
    if (logMsg == 0) {
        logMsg = msg;
    }

    mprLog(conn, 3, "formatAuthResponse: code %d, %s\n", code, logMsg);

    if (auth->type == MA_AUTH_BASIC) {
        maSetHeader(conn, 0, "WWW-Authenticate", "Basic realm=\"%s\"", auth->requiredRealm);

#if BLD_FEATURE_AUTH_DIGEST
    } else if (auth->type == MA_AUTH_DIGEST) {

        qopClass = auth->qop;

        /*
         *  Use the etag as our opaque string
         */
        etag = conn->response->etag;
        if (etag == 0) {
            etag = "";
        }
        if (etag[0] == '"') {
            etag = mprStrdup(req, etag);
            etag = mprStrTrim(etag, "\"");
        }
        mprCalcDigestNonce(req, &nonceStr, conn->host->secret, etag, auth->requiredRealm);

        if (strcmp(qopClass, "auth") == 0) {
            maSetHeader(conn, 0, "WWW-Authenticate", "Digest realm=\"%s\", domain=\"%s\", "
                "qop=\"auth\", nonce=\"%s\", opaque=\"%s\", algorithm=\"MD5\", stale=\"FALSE\"", 
                auth->requiredRealm, conn->host->name, nonceStr, etag);

        } else if (strcmp(qopClass, "auth-int") == 0) {
            maSetHeader(conn, 0, "WWW-Authenticate", "Digest realm=\"%s\", domain=\"%s\", "
                "qop=\"auth\", nonce=\"%s\", opaque=\"%s\", algorithm=\"MD5\", stale=\"FALSE\"", 
                auth->requiredRealm, conn->host->name, nonceStr, etag);

        } else {
            maSetHeader(conn, 0, "WWW-Authenticate", "Digest realm=\"%s\", nonce=\"%s\"", auth->requiredRealm, nonceStr);
        }
        mprFree(nonceStr);
#endif
    }

    maFailRequest(conn, code, "Authentication Error: %s", msg);
}
Example #24
0
/*
 *  Set the IP address
 */
void mprSetIpAddr(MprCtx ctx, cchar *s)
{
    Mpr     *mpr;

    mpr = mprGetMpr(ctx);
    if (mpr->ipAddr) {
        mprFree(mpr->ipAddr);
    }
    mpr->ipAddr = mprStrdup(mpr, s);
    return;
}
Example #25
0
/*
 *  Set the domain name
 */
void mprSetDomainName(MprCtx ctx, cchar *s)
{
    Mpr     *mpr;

    mpr = mprGetMpr(ctx);
    if (mpr->domainName) {
        mprFree(mpr->domainName);
    }
    mpr->domainName = mprStrdup(mpr, s);
    return;
}
Example #26
0
/*
 *  Full host name with domain. E.g. "server.domain.com"
 */
void mprSetHostName(MprCtx ctx, cchar *s)
{
    Mpr     *mpr;

    mpr = mprGetMpr(ctx);
    mprLock(mpr->mutex);
    mprFree(mpr->hostName);
    mpr->hostName = mprStrdup(mpr, s);
    mprUnlock(mpr->mutex);
    return;
}
Example #27
0
File: rom.cpp Project: OPSF/uClinux
MaRomFileSystem::MaRomFileSystem(MaRomInode *inodeList)
{
	MaRomInode	*ri;
	char		*name;
	int			nchars;

	romInodes = inodeList;
	fileIndex = new MprHashTable(MPR_HTTP_FILES_HASH_SIZE);
	root = mprStrdup("");
	rootLen = strlen(root);

	for (ri = inodeList; ri->path; ri++) {
		name = mprStrdup(ri->path);
		nchars = strlen(name) - 1;
		if (nchars > 0 && (name[nchars] == '/' || name[nchars] == '\\')) {
			name[nchars] = '\0';
		}
		fileIndex->insert(new MaRomHashEntry(name, ri));
		mprFree(name);
	}
}
Example #28
0
/*
 *  WARNING: the inside interpreter owns the exception object. Must fully extract all fields
 */
static void handleError(Ejs *ejs, EjsWorker *worker, EjsVar *exception)
{
    EjsError        *error;
    MprDispatcher   *dispatcher;
    Message         *msg;

    mprAssert(!worker->inside);
    mprAssert(exception);
    mprAssert(ejs == worker->ejs);

    if ((msg = mprAllocObjZeroed(ejs, Message)) == 0) {
        ejsThrowMemoryError(ejs);
        return;
    }
    msg->worker = worker;
    msg->callback = "onerror";
    msg->callbackSlot = ES_ejs_sys_Worker_onerror;
    
    /*
     *  Inside interpreter owns the exception object, so must fully extract all exception. 
     *  Allocate into the outside worker's interpreter.
     */
    if (ejsIsError(exception)) {
        error = (EjsError*) exception;
        msg->message = mprStrdup(ejs, error->message);
        msg->filename = mprStrdup(ejs, error->filename ? error->filename : "script");
        msg->lineNumber = error->lineNumber;
        msg->stack = mprStrdup(ejs, error->stack);

    } else if (ejsIsString(exception)) {
        msg->message = mprStrdup(ejs, ejsGetString(exception));

    } else {
        msg->message = mprStrdup(ejs, ejsGetString(ejsToString(ejs, exception)));
    }
    dispatcher = ejs->dispatcher;
    mprCreateEvent(dispatcher, (MprEventProc) doMessage, 0, MPR_NORMAL_PRIORITY, msg, 0);
    mprSignalCond(dispatcher->cond);
}
Example #29
0
void maSetAccessLog(MaHost *host, cchar *path, cchar *format)
{
    char    *src, *dest;

    mprAssert(host);
    mprAssert(path && *path);
    mprAssert(format);

    mprFree(host->logPath);
    host->logPath = mprStrdup(host, path);

    mprFree(host->logFormat);
    host->logFormat = mprStrdup(host, format);

    for (src = dest = host->logFormat; *src; src++) {
        if (*src == '\\' && src[1] != '\\') {
            continue;
        }
        *dest++ = *src;
    }
    *dest = '\0';
}
Example #30
0
int maAddUserToGroup(MaAuth *auth, MaGroup *gp, cchar *user)
{
    char        *name;
    int         next;

    for (next = 0; (name = mprGetNextItem(gp->users, &next)) != 0; ) {
        if (strcmp(name, user) == 0) {
            return MPR_ERR_ALREADY_EXISTS;
        }
    }
    mprAddItem(gp->users, mprStrdup(gp, user));
    return 0;
}