Ejemplo n.º 1
0
static int writeBody(MprHttp *http, MprList *fields, MprList *files)
{
    MprFile     *file;
    char        buf[MPR_HTTP_BUFSIZE], *path, *pair;
    int         bytes, next, count, rc, len;

    rc = 0;

    mprSetSocketBlockingMode(http->sock, 1);
    if (upload) {
        if (mprWriteHttpUploadData(http, files, fields) < 0) {
            mprError(http, "Can't write upload data %s", mprGetHttpError(http));
            mprSetSocketBlockingMode(http->sock, 0);
            return MPR_ERR_CANT_WRITE;
        }
    } else {
        if (fields) {
            count = mprGetListCount(fields);
            for (next = 0; !rc && (pair = mprGetNextItem(fields, &next)) != 0; ) {
                len = (int) strlen(pair);
                if (next < count) {
                    len = (int) strlen(pair);
                    if (mprWriteSocket(http->sock, pair, len) != len || mprWriteSocket(http->sock, "&", 1) != 1) {
                        return MPR_ERR_CANT_WRITE;
                    }
                } else {
                    if (mprWriteSocket(http->sock, pair, len) != len) {
                        return MPR_ERR_CANT_WRITE;
                    }
                }
            }
        }
        if (files) {
            mprAssert(mprGetListCount(files) == 1);
            for (rc = next = 0; !rc && (path = mprGetNextItem(files, &next)) != 0; ) {
                file = mprOpen(http, path, O_RDONLY | O_BINARY, 0);
                if (file == 0) {
                    mprError(http, "Can't open \"%s\"", path);
                    return MPR_ERR_CANT_OPEN;
                }
                if (verbose && upload) {
                    mprPrintf(http, "uploading: %s\n", path);
                }
                while ((bytes = mprRead(file, buf, sizeof(buf))) > 0) {
                    if (mprWriteHttp(http, buf, bytes) != bytes) {
                        mprFree(file);
                        return MPR_ERR_CANT_WRITE;
                    }
                }
                mprFree(file);
            }
        }
        if (mprFinalizeHttpWriting(http) < 0) {
            return MPR_ERR_CANT_WRITE;
        }
    }
    mprSetSocketBlockingMode(http->sock, 0);
    return rc;
}
Ejemplo n.º 2
0
static void copyFile(cchar *url)
{
    MprFile     *file;
    char        path[MPR_MAX_FNAME], buf[MPR_BUFSIZE], *ext;
    int         len;

    ext = strrchr(url, '.');
    if (ext) {
        ++ext;
        if (strcmp(ext, "htm") == 0 || strcmp(ext, "html") == 0) {
            setHeader(NULL, 0, "Content-Type", "text/html");
        } else if (strcmp(ext, "jpg") == 0 || strcmp(ext, "jpeg") == 0) {
            setHeader(NULL, 0, "Content-Type", "image/jpeg");
        } else if (strcmp(ext, "png") == 0) {
            setHeader(NULL, 0, "Content-Type", "image/png");
        } else if (strcmp(ext, "gif") == 0) {
            setHeader(NULL, 0, "Content-Type", "image/gif");
        } else if (strcmp(ext, "tiff") == 0) {
            setHeader(NULL, 0, "Content-Type", "image/tiff");
        } else if (strcmp(ext, "ico") == 0) {
            setHeader(NULL, 0, "Content-Type", "image/x-ico");
        } else if (strcmp(ext, "bmp") == 0) {
            setHeader(NULL, 0, "Content-Type", "image/bmp");
        } else if (strcmp(ext, "pdf") == 0) {
            setHeader(NULL, 0, "Content-Type", "image/pdf");
        } else if (strcmp(ext, "txt") == 0) {
            setHeader(NULL, 0, "Content-Type", "text/plain");
        } else if (strcmp(ext, "css") == 0) {
            setHeader(NULL, 0, "Content-Type", "text/css");
        }
    }
    mprSprintf(path, sizeof(path), "%s/%s", documentRoot, url);
    file = mprOpen(mpr, path, O_RDONLY, 0);
    if (file == 0) {
        error(NULL, 0, "Can't open %s", path);
        return;
    }
    while ((len = mprRead(file, buf, sizeof(buf))) > 0) {
        if (writeBlock(web, buf, len) != len) {
            error(NULL, 0, "Can't write data from %s", path);
            return;
        }
    }
}
Ejemplo n.º 3
0
char *mprGets(MprFile *file, char *buf, uint size)
{
	MprBuf	*bp;
	int		count, len, c;

	mprAssert(file);

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

	if (file->buf == 0) {
		file->buf = mprCreateBuf(file, MPR_DEFAULT_ALLOC, MPR_MAX_STRING);
	}
	bp = file->buf;

	/*
	 *	Must leave room for null
	 */
	count = 0;
	while (--size > 0) {
		if (mprGetBufLength(bp) == 0) {
			mprFlushBuf(bp);
			len = mprRead(file, mprGetBufEnd(bp), 
				mprGetBufLinearSpace(bp));
			if (len <= 0) {
				return 0;
			}
			mprAdjustBufEnd(bp, len);
			mprAddNullToBuf(bp);
		}
		if ((c = mprGetCharFromBuf(bp)) == '\n') {
			buf[count] = '\0';
			return buf;
		}
		buf[count++] = c;
	}
	buf[count] = '\0';
	return buf;
}
Ejemplo n.º 4
0
/* 
 *  Encode the files as C code
 */
static int binToC(Mpr *mpr, MprList *files, char *romName, char *prefix)
{
    MprFileInfo     info;
    MprFile         *file;
    char            buf[512];
    char            *filename, *cp, *sl, *p;
    int             next, j, i, len;

    mprPrintf(mpr, "/*\n *  %s -- Compiled Files\n */\n", romName);

    mprPrintf(mpr, "#include \"mpr.h\"\n\n");
    mprPrintf(mpr, "#if BLD_FEATURE_ROMFS\n");

    /*
     *  Open each input file and compile
     */
    for (next = 0; (filename = mprGetNextItem(files, &next)) != 0; ) {
        if (mprGetFileInfo(mpr, filename, &info) == 0 && info.isDir) {
            mprError(mpr, "Skipping directory %s", filename);
            continue;
        } 
        if ((file = mprOpen(mpr, filename, O_RDONLY | O_BINARY, 0666)) < 0) {
            mprError(mpr, "Can't open file %s\n", filename);
            return -1;
        }
        mprPrintf(mpr, "static uchar _file_%d[] = {\n", next);

        while ((len = mprRead(file, buf, sizeof(buf))) > 0) {
            p = buf;
            for (i = 0; i < len; ) {
                mprPrintf(mpr, "    ");
                for (j = 0; p < &buf[len] && j < 16; j++, p++) {
                    mprPrintf(mpr, "%3d,", (unsigned char) *p);
                }
                i += j;
                mprPrintf(mpr, "\n");
            }
        }
        mprPrintf(mpr, "    0 };\n\n");

        mprFree(file);
    }

    /*
     *  Now output the page index
     */ 
    mprPrintf(mpr, "MprRomInode %s[] = {\n", romName);

    for (next = 0; (filename = mprGetNextItem(files, &next)) != 0; ) {
        /*
         *  Replace the prefix with a leading "/"
         */ 
        if (strncmp(filename, prefix, strlen(prefix)) == 0) {
            cp = &filename[strlen(prefix)];
        } else {
            cp = filename;
        }
        while((sl = strchr(filename, '\\')) != NULL) {
            *sl = '/';
        }
        if (*cp == '/') {
            cp++;
        }

        if (*cp == '.' && cp[1] == '\0') {
            cp++;
        }
        if (mprGetFileInfo(mpr, filename, &info) == 0 && info.isDir) {
            mprPrintf(mpr, "    { \"%s\", 0, 0, 0 },\n", cp);
            continue;
        }
        mprPrintf(mpr, "    { \"%s\", _file_%d, %d, %d },\n", cp, next, (int) info.size, next);
    }
    
    mprPrintf(mpr, "    { 0, 0, 0, 0 },\n");
    mprPrintf(mpr, "};\n");

    mprPrintf(mpr, "#endif /* BLD_FEATURE_ROMFS */\n");

    return 0;
}
Ejemplo n.º 5
0
/*
    If the program has a UNIX style "#!/program" string at the start of the file that program will be selected 
    and the original program will be passed as the first arg to that program with argv[] appended after that. If 
    the program is not found, this routine supports a safe intelligent search for the command. If all else fails, 
    we just return in program the fileName we were passed in. script will be set if we are modifying the program 
    to run and we have extracted the name of the file to run as a script.
 */
static void findExecutable(MaConn *conn, char **program, char **script, char **bangScript, char *fileName)
{
    MaRequest       *req;
    MaResponse      *resp;
    MaLocation      *location;
    MprHash         *hp;
    MprFile         *file;
    cchar           *actionProgram, *ext, *cmdShell;
    char            *tok, buf[MPR_MAX_FNAME + 1], *path;

    req = conn->request;
    resp = conn->response;
    location = req->location;

    *bangScript = 0;
    *script = 0;
    *program = 0;
    path = 0;

    actionProgram = maGetMimeActionProgram(conn->host, req->mimeType);
    ext = resp->extension;

    /*
        If not found, go looking for the fileName with the extensions defined in appweb.conf. 
        NOTE: we don't use PATH deliberately!!!
     */
    if (access(fileName, X_OK) < 0 /* && *ext == '\0' */) {
        for (hp = 0; (hp = mprGetNextHash(location->extensions, hp)) != 0; ) {
            path = mprStrcat(resp, -1, fileName, ".", hp->key, NULL);
            if (access(path, X_OK) == 0) {
                break;
            }
            mprFree(path);
            path = 0;
        }
        if (hp) {
            ext = hp->key;
        } else {
            path = fileName;
        }

    } else {
        path = fileName;
    }
    mprAssert(path && *path);

#if BLD_WIN_LIKE
    if (ext && (strcmp(ext, ".bat") == 0 || strcmp(ext, ".cmd") == 0)) {
        /*
            Let a mime action override COMSPEC
         */
        if (actionProgram) {
            cmdShell = actionProgram;
        } else {
            cmdShell = getenv("COMSPEC");
        }
        if (cmdShell == 0) {
            cmdShell = "cmd.exe";
        }
        *script = mprStrdup(resp, path);
        *program = mprStrdup(resp, cmdShell);
        return;
    }
#endif

    if ((file = mprOpen(resp, path, O_RDONLY, 0)) != 0) {
        if (mprRead(file, buf, MPR_MAX_FNAME) > 0) {
            mprFree(file);
            buf[MPR_MAX_FNAME] = '\0';
            if (buf[0] == '#' && buf[1] == '!') {
                cmdShell = mprStrTok(&buf[2], " \t\r\n", &tok);
                if (cmdShell[0] != '/' && (cmdShell[0] != '\0' && cmdShell[1] != ':')) {
                    /*
                        If we can't access the command shell and the command is not an absolute path, 
                        look in the same directory as the script.
                     */
                    if (mprPathExists(resp, cmdShell, X_OK)) {
                        cmdShell = mprJoinPath(resp, mprGetPathDir(resp, path), cmdShell);
                    }
                }
                if (actionProgram) {
                    *program = mprStrdup(resp, actionProgram);
                } else {
                    *program = mprStrdup(resp, cmdShell);
                }
                *bangScript = mprStrdup(resp, path);
                return;
            }
        } else {
            mprFree(file);
        }
    }

    if (actionProgram) {
        *program = mprStrdup(resp, actionProgram);
        *bangScript = mprStrdup(resp, path);
    } else {
        *program = mprStrdup(resp, path);
    }
    return;
}
Ejemplo n.º 6
0
int ecOpenFileStream(EcLexer *lp, const char *path)
{
    EcFileStream    *fs;
    MprFileInfo     info;
    int             c;

    fs = mprAllocObjZeroed(lp->input, EcFileStream);
    if (fs == 0) {
        return MPR_ERR_NO_MEMORY;
    }

    if ((fs->file = mprOpen(lp, path, O_RDONLY | O_BINARY, 0666)) == 0) {
        mprFree(fs);
        return MPR_ERR_CANT_OPEN;
    }

    if (mprGetFileInfo(fs, path, &info) < 0 || info.size < 0) {
        mprFree(fs);
        return MPR_ERR_CANT_ACCESS;
    }

    /* Sanity check */
    mprAssert(info.size < (10 * 1024 * 1024));
    mprAssert(info.size >= 0);

    fs->stream.buf = (char*) mprAlloc(fs, (int) info.size + 1);
    if (fs->stream.buf == 0) {
        mprFree(fs);
        return MPR_ERR_NO_MEMORY;
    }
    if (mprRead(fs->file, fs->stream.buf, (int) info.size) != (int) info.size) {
        mprFree(fs);
        return MPR_ERR_CANT_READ;
    }

    fs->stream.buf[info.size] = '\0';
    fs->stream.nextChar = fs->stream.buf;
    fs->stream.end = &fs->stream.buf[info.size];
    fs->stream.currentLine = fs->stream.buf;
    fs->stream.lineNumber = 1;
    fs->stream.compiler = lp->compiler;

    fs->stream.name = mprStrdup(lp, path);

    mprFree(lp->input->stream);
    lp->input->stream = (EcStream*) fs;

    lp->input->putBack = 0;
    lp->input->token = 0;
    lp->input->state = 0;
    lp->input->next = 0;

    /*
     *  Initialize the stream line and column data.
     */
    c = getNextChar(&fs->stream);
    putBackChar(&fs->stream, c);



    return 0;
}