Пример #1
0
/*
 *  Process one request
 */
static void processRequest() 
{
    char        *appName, *url;
    int         flags;

    appName = mprStrdup(mpr, pathInfo);
    if (*appName == '/') {
        appName++;
    }
    if ((url = strchr(appName, '/')) != 0) {
        *url++ = '\0';
    }
    if (strncmp(url, "web/", 4) == 0) {
        copyFile(url);
        flushOutput(output);
        return;
    }

    /*
     *  Set default response headers
     */
    setHeader(NULL, 0, "Content-Type", "text/html");
    setHeader(NULL, 0, "Last-Modified", currentDate);
    setHeader(NULL, 0, "Cache-Control", "no-cache");

    flags = 0;
    if (ext == 0) {
        flags |= EJS_WEB_FLAG_APP;
    }
    if ((web = ejsCreateWebRequest(mpr, control, NULL, scriptName, pathInfo, documentRoot, flags)) == 0) {
        error(NULL, 0, "Can't create web request");
        return;
    }
    ejs = web->ejs;

    if (ejsRunWebRequest(web) < 0) {
        error(NULL, 0, "%s", web->error);
        return;
    }
    flushOutput(output);
}
Пример #2
0
static int parseUrl(MaConn *conn)
{
    EjsWeb          *web;
    EjsWebControl   *control;
    MaRequest       *req;
    MaResponse      *resp;
    MaAlias         *alias;
    MaLocation      *location;
    char            *baseDir, *cp, *url, *baseUrl;
    int             flags, locFlags;

    resp = conn->response;
    req = conn->request;
    alias = req->alias;
    location = req->location;
    locFlags = location->flags;
    url = req->url;
    
    flags = 0;
    if (locFlags & MA_LOC_APP_DIR) {
        flags |= EJS_WEB_FLAG_APP;
        mprAllocSprintf(resp, &baseDir, -1, "%s%s", alias->filename, &req->url[alias->prefixLen]);
        if ((cp = strchr(&baseDir[strlen(alias->filename) + 1], '/')) != 0) {
            *cp = '\0';
        }
        mprAllocSprintf(resp, &baseUrl, -1, "%s%s", alias->prefix, &req->url[alias->prefixLen]);
        if ((cp = strchr(&baseUrl[alias->prefixLen + 1], '/')) != 0) {
            *cp = '\0';
        }
        if (*url) {
            /* Step over the directory and app name */
            while (*++url != '/') ;
            if (*url) {
                while (*++url != '/') ;
            }
        }
            
    } else {
        if (locFlags & MA_LOC_APP) {
            flags |= EJS_WEB_FLAG_APP;
        }
        baseDir = alias->filename;
        if (alias->prefixLen > 0) {
            /* Step over the application name (same as alias prefix) */
            url = &url[alias->prefixLen];
            if (*url != '/' && url[-1] == '/') {
                url--;
            }
        }
        baseUrl = alias->prefix;
    }
    
    if (location->flags & MA_LOC_BROWSER) {
        flags |= EJS_WEB_FLAG_BROWSER_ERRORS;
    }
    if (location->flags & MA_LOC_AUTO_SESSION) {
        flags |= EJS_WEB_FLAG_SESSION;
    }

    control = conn->http->ejsHandler->stageData;
    web = ejsCreateWebRequest(req, control, conn, baseUrl, url, baseDir, flags);
    if (web == 0) {
        maFailRequest(conn, MPR_HTTP_CODE_INTERNAL_SERVER_ERROR, "Can't create Ejs web object for %s", req->url);
        return EJS_ERR;
    }
    resp->handlerData = web;

    return 0;
}
Пример #3
0
/*
 *  When we come here, we've already matched either a location block or an extension
 */
static int run(request_rec *r) 
{
    EjsDirConfig        *dir;
    EjsServerConfig     *server;
    cchar               *ext;

    MaRequest       *req;
    MaResponse      *resp;
    MaConn          *conn;
    MaAlias         *alias;
    MaLocation      *location;
    EjsWeb          *web;
    cchar           *sep, *prefix;
    char            *urlBase, *dir, *app, *url, *cp;
    int             flags, locFlags;

    if (!r->handler || strcasecmp(r->handler, "ejs") != 0) {
        return DECLINED;
    }

    dir = getDir(r)
    server = getServer(r->XXX);

    //  EjsAlias should probably be creating a directory block. These flags should probably be in a directory
    if (loc->flags & (MA_LOC_APP | MA_LOC_APP_DIR)) {

        /*
         *  Send non-ejs content under web to another handler, typically the file handler.
         */
        if (strncmp(&req->url[loc->prefixLen], "web/", 4) == 0) {
            if (!(ext && strcmp(ext, "ejs") == 0)) {
                return DECLINED;
            }

        } else {
            if (ext && strcmp(ext, "ejs") == 0) {
                maFormatBody(conn, "Bad Request", "Can't serve *.ejs files outside web directory");
                maFailRequest(conn, MPR_HTTP_CODE_BAD_REQUEST, "Can't server *.ejs files outside web directory");
                return HTTP_XXX;
            }
        }
    }

    flags = 0;
    url = 0;

    locFlags = location->flags;
    
    if (locFlags & MA_LOC_APP) {
        app = mprStrTrim(mprStrdup(q, prefix), "/");
        url = &req->url[alias->prefixLen];
        dir = mprStrdup(resp, alias->filename);
        if (*url != '/') {
            url--;
        }
        urlBase = mprStrdup(resp, prefix);
        
    } else if (locFlags & MA_LOC_APP_DIR) {
        url = &req->url[alias->prefixLen];
        app = mprStrdup(resp, url);
        if ((cp = strchr(app, '/')) != 0) {
            url = mprStrdup(resp, cp);
            *cp = '\0';
        }
        sep = prefix[strlen(prefix) - 1] == '/' ? "" : "/";
        dir = mprStrcat(resp, &dir, alias->filename, sep, app, NULL);
        urlBase = mprStrcat(resp, prefix, sep, app, NULL);

    } else {
        app = 0;
        dir = mprStrdup(resp, alias->filename);
        url = &req->url[alias->prefixLen];
        flags |= EJS_WEB_FLAG_SOLO;
        if (*url != '/') {
            url--;
        }        
        urlBase = mprStrdup(resp, prefix);
    }
    mprStrTrim(urlBase, "/");
    mprStrTrim(dir, "/");
    
    if (location->flags & MA_LOC_BROWSER) {
        flags |= EJS_WEB_FLAG_BROWSER_ERRORS;
    }
    if (location->flags & MA_LOC_AUTO_SESSION) {
        flags |= EJS_WEB_FLAG_SESSION;
    }

    /*
     *  Var         Stand-Alone             App                         AppDir
     *  app         0                       carmen                      carmen
     *  dir         /Users/mob/....         /Users/mob/hg/carmen        /Users/mob/hg/carmen
     *  urlBase                             /xg/carmen                  /carmen
     *  url                                 stock                       stock
     */
    web = ejsCreateWebRequest(req, q->stage->stageData, conn, app, dir, urlBase, url, req->cookie, flags);
    if (web == 0) {
        maFailRequest(conn, MPR_HTTP_CODE_INTERNAL_SERVER_ERROR, "Can't create Ejs web object for %s", req->url);
        return;
    }
    q->queueData = web;
    maSetHeader(conn, 0, "Last-Modified", req->host->currentDate);
    maDontCacheResponse(conn);

    if (r->method_number != M_GET) {
        return HTTP_METHOD_NOT_ALLOWED;
    }

    if (ejsProcessWebRequest((EjsWeb*) r, &msg) < 0) {
        if (web->flags & EJS_WEB_FLAG_BROWSER_ERRORS) {
            maFormatBody(conn, "Request Failed", "%s", msg);
        }
        maFailRequest(conn, MPR_HTTP_CODE_BAD_GATEWAY, msg);
        mprFree(msg);
    }

    ap_set_content_type(r, "text/html");
    ap_rputs("<html><title>Hello World!</title><body><p>Hello World</p></body></html>\r\n", r);

#if 0
    if ((err = set_content_length(r, r->finfo.st_stize)) || (err = set_last_modified(r, r->finfo.st_mtime)))
        return err;
    if (r->finof.st_mode == 0) 
        return NOT_FOUND;
    fopen(r->filename, "r");

    if (!r->main) {
        /* Not internal redirect */
        apr_table_set(r->headers_out, "X-ejs", "Under construction");
    }
    register_timeout("send", r);
    send_http_header(r);
    if (!r->header_only)
        send_fd(f, r);
    pfclose(r->pool, f);
#endif

    return OK;
}