/* function passRequest(req: Request, worker: Worker): Void */ static EjsVoid *hs_passRequest(Ejs *ejs, EjsHttpServer *server, int argc, EjsAny **argv) { Ejs *nejs; EjsRequest *req, *nreq; EjsWorker *worker; HttpConn *conn; MprEvent *event; req = argv[0]; worker = argv[1]; nejs = worker->pair->ejs; conn = req->conn; conn->ejs = nejs; if ((nreq = ejsCloneRequest(nejs, req, 1)) == 0) { ejsThrowStateError(ejs, "Cannot clone request"); return 0; } httpSetConnContext(conn, nreq); if ((nreq->server = ejsCloneHttpServer(nejs, req->server, 1)) == 0) { ejsThrowStateError(ejs, "Cannot clone request"); return 0; } event = mprCreateEvent(conn->dispatcher, "RequestWorker", 0, receiveRequest, nreq, MPR_EVENT_DONT_QUEUE); httpUseWorker(conn, nejs->dispatcher, event); return 0; }
/* function WebSocket(uri: Uri, protocols = null, options) options = { certificate: Path, verify: Boolean, } */ static EjsWebSocket *wsConstructor(Ejs *ejs, EjsWebSocket *ws, int argc, EjsObj **argv) { EjsAny *certificate; bool verify; assert(ejsIsPot(ejs, ws)); ejsLoadHttpService(ejs); ws->ejs = ejs; verify = 0; ws->uri = httpUriToString(((EjsUri*) argv[0])->uri, 0); if (argc >= 2) { if (ejsIs(ejs, argv[1], Array)) { ws->protocols = sclone((ejsToString(ejs, argv[1]))->value); } else if (ejsIs(ejs, argv[1], String)) { ws->protocols = sclone(((EjsString*) argv[1])->value); } else { ws->protocols = sclone("chat"); } } else { ws->protocols = sclone("chat"); } if (*ws->protocols == '\0') { ejsThrowArgError(ejs, "Bad protocol"); return 0; } if (argc >= 3) { ws->frames = ejsGetPropertyByName(ejs, argv[2], EN("frames")) == ESV(true); verify = ejsGetPropertyByName(ejs, argv[2], EN("verify")) == ESV(true); if ((certificate = ejsGetPropertyByName(ejs, argv[2], EN("certificate"))) != 0) { ws->certFile = ejsToMulti(ejs, argv[0]); } } if ((ws->conn = httpCreateConn(MPR->httpService, NULL, ejs->dispatcher)) == 0) { ejsThrowMemoryError(ejs); return 0; } httpSetAsync(ws->conn, 1); httpPrepClientConn(ws->conn, 0); httpSetConnNotifier(ws->conn, webSocketNotify); httpSetWebSocketProtocols(ws->conn, ws->protocols); httpSetConnContext(ws->conn, ws); if (sstarts(ws->uri, "wss")) { ws->ssl = mprCreateSsl(0); mprVerifySslIssuer(ws->ssl, verify); mprVerifySslPeer(ws->ssl, verify); #if FUTURE if (!hp->caFile) { //MOB - Some define for this. hp->caFile = mprJoinPath(mprGetAppDir(), "http-ca.crt"); } mprSetSslCaFile(hp->ssl, hp->caFile); mprSetSslCaFile(hp->ssl, mprJoinPath(mprGetAppDir(), "http-ca.crt")); #endif } startWebSocketRequest(ejs, ws); return ws; }
/* function close(): Void */ static EjsObj *http_close(Ejs *ejs, EjsHttp *hp, int argc, EjsObj **argv) { if (hp->conn) { httpFinalize(hp->conn); sendHttpCloseEvent(ejs, hp); httpDestroyConn(hp->conn); // TODO OPT - Better to do this on demand. This consumes a conn until GC. hp->conn = httpCreateConn(ejs->http, NULL, ejs->dispatcher); httpPrepClientConn(hp->conn, 0); httpSetConnNotifier(hp->conn, httpEventChange); httpSetConnContext(hp->conn, hp); } return 0; }
static void closeEjsHandler(HttpQueue *q) { EjsRequest *req; HttpConn *conn; conn = q->conn; if ((req = httpGetConnContext(conn)) != 0) { ejsSendRequestCloseEvent(req->ejs, req); req->conn = 0; } httpSetConnContext(conn, 0); if (conn->pool && conn->ejs) { ejsFreePoolVM(conn->pool, conn->ejs); conn->ejs = 0; } }
/* function Http(uri: Uri = null) */ static EjsHttp *httpConstructor(Ejs *ejs, EjsHttp *hp, int argc, EjsObj **argv) { ejsLoadHttpService(ejs); hp->ejs = ejs; if ((hp->conn = httpCreateConn(ejs->http, NULL, ejs->dispatcher)) == 0) { ejsThrowMemoryError(ejs); return 0; } httpPrepClientConn(hp->conn, 0); httpSetConnNotifier(hp->conn, httpEventChange); httpSetConnContext(hp->conn, hp); if (argc == 1 && ejsIs(ejs, argv[0], Null)) { hp->uri = httpUriToString(((EjsUri*) argv[0])->uri, HTTP_COMPLETE_URI); } hp->method = sclone("GET"); hp->requestContent = mprCreateBuf(BIT_MAX_BUFFER, -1); hp->responseContent = mprCreateBuf(BIT_MAX_BUFFER, -1); return hp; }
static EjsRequest *createRequest(EjsHttpServer *sp, HttpConn *conn) { Ejs *ejs; EjsRequest *req; EjsPath *documents; cchar *dir; ejs = sp->ejs; documents = ejsGetProperty(ejs, sp, ES_ejs_web_HttpServer_documents); if (ejsIs(ejs, documents, Path)) { dir = documents->value; } else { /* Safety fall back */ dir = conn->rx->route->home; } req = ejsCreateRequest(ejs, sp, conn, dir); httpSetConnContext(conn, req); #if FUTURE if (sp->pipe) { def = ejsRunFunction(ejs, sp->createPipeline, if ((vp = ejsGetPropertyByName(ejs, def, ejsName(&name, "", "handler"))) != 0) { handler = ejsToMulti(ejs, vp); }