void SFTPWorkerThread::DoConnect(SFTPThreadRequet* req) { wxString accountName = req->GetAccount().GetAccountName(); clSSH::Ptr_t ssh(new clSSH(req->GetAccount().GetHost(), req->GetAccount().GetUsername(), req->GetAccount().GetPassword(), req->GetAccount().GetPort())); try { wxString message; DoReportStatusBarMessage(wxString() << _("Connecting to ") << accountName); DoReportMessage(accountName, "Connecting...", SFTPThreadMessage::STATUS_NONE); ssh->Connect(); if(!ssh->AuthenticateServer(message)) { ssh->AcceptServerAuthentication(); } ssh->Login(); m_sftp.reset(new clSFTP(ssh)); // associate the account with the connection m_sftp->SetAccount(req->GetAccount().GetAccountName()); m_sftp->Initialize(); wxString msg; msg << "Successfully connected to " << accountName; DoReportMessage(accountName, msg, SFTPThreadMessage::STATUS_OK); } catch(clException& e) { wxString msg; msg << "Connect error. " << e.What(); DoReportMessage(accountName, msg, SFTPThreadMessage::STATUS_ERROR); m_sftp.reset(NULL); } }
char *proobraz(char* input) { input = registr(input); input= ficha(input); for (int i=0;i<strlen(input);i++) { ssin(i,input); ccos(i,input); aasin(i,input); aacos(i,input); aatan(i,input); cceil(i,input); cch(i,input); eexp(i,input); aabs(i,input); ffloor(i,input); lln(i,input); llog(i,input); ssh(i,input); ssqrt(i,input); ttan(i,input); tth(i,input); cctg(i,input); aactg(i,input); ccth(i,input); } return input; }
void HxCPDSpatialGraphWarp::applyRigidDeformationToSliceVanMises( SpatialGraphSelection& slice, HxSpatialGraph* spatialGraph, const ma::CPDLinearAligner& deformation, const McDMatrix<double>& R, const double s, const McDVector<double>& t) { McWatch watch; watch.start(); ma::SliceSelector ssh(spatialGraph, "TransformInfo"); SpatialGraphSelection fullSliceSelection; ssh.getSlice(ssh.getSliceAttributeValueFromIndex(1), fullSliceSelection); for (int i = 0; i < fullSliceSelection.getNumSelectedEdges(); i++) { int edge = fullSliceSelection.getSelectedEdge(i); McDArray<McVec3f> newEdgePoints; for (int j = 0; j < spatialGraph->getNumEdgePoints(edge); j++) { McVec3f edgePoint = spatialGraph->getEdgePoint(edge, j); const McVec3f edgePointWarped = deformation.warpPoint(edgePoint, R, s, t); newEdgePoints.append(edgePointWarped); } spatialGraph->setEdgePoints(edge, newEdgePoints); } for (int i = 0; i < fullSliceSelection.getNumSelectedVertices(); i++) { int curVertex = fullSliceSelection.getSelectedVertex(i); McVec3f curCoord = spatialGraph->getVertexCoords(curVertex); const McVec3f curCoordWarped = deformation.warpPoint(curCoord, R, s, t); spatialGraph->setVertexCoords(curVertex, curCoordWarped); } std::cout << "\n Apply deformation took " << watch.stop() << " seconds."; }
void BaseExecutionContext::onShutdownPostSend() { ServerStats::SetThreadMode(ServerStats::PostProcessing); try { try { ServerStatsHelper ssh("psp", ServerStatsHelper::TRACK_HWINST); if (!m_shutdowns.isNull()) { if (m_shutdowns.exists(PostSend)) { executeFunctions(m_shutdowns[PostSend]); m_shutdowns.remove(PostSend); } if (m_shutdowns.exists(CleanUp)) { executeFunctions(m_shutdowns[CleanUp]); m_shutdowns.remove(CleanUp); } } } catch (const ExitException &e) { // do nothing } catch (const Exception &e) { onFatalError(e); } catch (const Object &e) { onUnhandledException(e); } } catch (...) { Logger::Error("unknown exception was thrown from psp"); } ServerStats::SetThreadMode(ServerStats::Idling); }
void Transport::sendRawLocked(void *data, int size, int code /* = 200 */, bool compressed /* = false */, bool chunked /* = false */, const char *codeInfo /* = "" */ ) { if (!compressed && RuntimeOption::ForceChunkedEncoding) { chunked = true; } if (m_chunkedEncoding) { chunked = true; assert(!compressed); } else if (chunked) { m_chunkedEncoding = true; assert(!compressed); } // I don't think there is any need to send an empty chunk, other than sending // out headers earlier, which seems to be a useless feature. if (chunked && size == 0) { return; } if (!m_headerCallbackDone && !m_headerCallback.isNull()) { // We could use m_headerSent here, however it seems we can still // end up in an infinite loop when: // m_headerCallback calls flush() // flush() triggers php's recursion guard // the recursion guard calls back into m_headerCallback m_headerCallbackDone = true; vm_call_user_func(m_headerCallback, null_array); } // compression handling ServerStatsHelper ssh("send"); String response = prepareResponse(data, size, compressed, !chunked); if (m_responseCode < 0) { m_responseCode = code; m_responseCodeInfo = codeInfo ? codeInfo: ""; } // HTTP header handling if (!m_headerSent) { String orig_response((const char *)data, size, CopyString); prepareHeaders(compressed, chunked, response, orig_response); m_headerSent = true; } m_responseSize += response.size(); ServerStats::SetThreadMode(ServerStats::ThreadMode::Writing); sendImpl(response.data(), response.size(), m_responseCode, chunked); ServerStats::SetThreadMode(ServerStats::ThreadMode::Processing); ServerStats::LogBytes(size); if (RuntimeOption::EnableStats && RuntimeOption::EnableWebStats) { ServerStats::Log("network.uncompressed", size); ServerStats::Log("network.compressed", response.size()); } }
TEST(signal, sigqueue) { ScopedSignalHandler ssh(SIGALRM, SigqueueSignalHandler, SA_SIGINFO); sigval_t sigval; sigval.sival_int = 1; errno = 0; ASSERT_EQ(0, sigqueue(getpid(), SIGALRM, sigval)); ASSERT_EQ(0, errno); ASSERT_EQ(1, g_sigqueue_signal_handler_call_count); }
TEST(spawn, signal_stress) { // Ensure that posix_spawn doesn't restore the caller's signal mask in the // child without first defaulting any caught signals (http://b/68707996). static pid_t parent = getpid(); setpgid(0, 0); signal(SIGRTMIN, SIG_IGN); pid_t pid = fork(); ASSERT_NE(-1, pid); if (pid == 0) { for (size_t i = 0; i < 1024; ++i) { kill(0, SIGRTMIN); usleep(10); } _exit(99); } // We test both with and without attributes, because they used to be // different codepaths. We also test with an empty `sigdefault` set. posix_spawnattr_t attr1; posix_spawnattr_init(&attr1); sigset_t empty_mask = {}; posix_spawnattr_t attr2; posix_spawnattr_init(&attr2); posix_spawnattr_setflags(&attr2, POSIX_SPAWN_SETSIGDEF); posix_spawnattr_setsigdefault(&attr2, &empty_mask); posix_spawnattr_t* attrs[] = { nullptr, &attr1, &attr2 }; // We use a real-time signal because that's a tricky case for LP32 // because our sigset_t was too small. ScopedSignalHandler ssh(SIGRTMIN, [](int) { ASSERT_EQ(getpid(), parent); }); const size_t pid_count = 128; pid_t spawned_pids[pid_count]; ExecTestHelper eth; eth.SetArgs({"true", nullptr}); for (size_t i = 0; i < pid_count; ++i) { pid_t spawned_pid; ASSERT_EQ(0, posix_spawn(&spawned_pid, "true", nullptr, attrs[i % 3], eth.GetArgs(), nullptr)); spawned_pids[i] = spawned_pid; } for (pid_t spawned_pid : spawned_pids) { ASSERT_EQ(spawned_pid, TEMP_FAILURE_RETRY(waitpid(spawned_pid, nullptr, 0))); } AssertChildExited(pid, 99); }
void RPCRequestHandler::handleRequest(Transport *transport) { ExecutionProfiler ep(ThreadInfo::RuntimeFunctions); Logger::OnNewRequest(); m_context->setTransport(transport); transport->enableCompression(); ServerStatsHelper ssh("all", true); Logger::Verbose("receiving %s", transport->getCommand().c_str()); // will clear all extra logging when this function goes out of scope StackTraceNoHeap::ExtraLoggingClearer clearer; StackTraceNoHeap::AddExtraLogging("RPC-URL", transport->getUrl()); // authentication const string &password = m_serverInfo->getPassword(); if (!password.empty() && password != transport->getParam("auth")) { transport->sendString("Unauthorized", 401); transport->onSendEnd(); return; } // resolve virtual host const VirtualHost *vhost = HttpProtocol::GetVirtualHost(transport); ASSERT(vhost); if (vhost->disabled()) { transport->sendString("Virtual host disabled.", 404); transport->onSendEnd(); return; } vhost->setRequestTimeoutSeconds(); // resolve source root string host = transport->getHeader("Host"); SourceRootInfo sourceRootInfo(host.c_str()); // set thread type switch (m_serverInfo->getType()) { case SatelliteServer::KindOfRPCServer: transport->setThreadType(Transport::RpcThread); break; case SatelliteServer::KindOfXboxServer: transport->setThreadType(Transport::XboxThread); break; default: break; } // record request for debugging purpose std::string tmpfile = HttpProtocol::RecordRequest(transport); bool ret = executePHPFunction(transport, sourceRootInfo); HttpProtocol::ClearRecord(ret, tmpfile); }
void Transport::sendRaw(void *data, int size, int code /* = 200 */, bool compressed /* = false */, bool chunked /* = false */, const char *codeInfo /* = "" */ ) { ASSERT(data || size == 0); ASSERT(size >= 0); FiberWriteLock lock(this); if (!compressed && RuntimeOption::ForceChunkedEncoding) { chunked = true; } if (m_chunkedEncoding) { chunked = true; ASSERT(!compressed); } else if (chunked) { m_chunkedEncoding = true; ASSERT(!compressed); } // I don't think there is any need to send an empty chunk, other than sending // out headers earlier, which seems to be a useless feature. if (chunked && size == 0) { return; } // compression handling ServerStatsHelper ssh("send"); String response = prepareResponse(data, size, compressed, !chunked); if (m_responseCode < 0) { m_responseCode = code; m_responseCodeInfo = codeInfo ? codeInfo: ""; } // HTTP header handling if (!m_headerSent) { prepareHeaders(compressed, data, size); m_headerSent = true; } m_responseSize += response.size(); ServerStats::SetThreadMode(ServerStats::Writing); sendImpl(response.data(), response.size(), m_responseCode, chunked); ServerStats::SetThreadMode(ServerStats::Processing); ServerStats::LogBytes(size); if (RuntimeOption::EnableStats && RuntimeOption::EnableWebStats) { ServerStats::Log("network.uncompressed", size); ServerStats::Log("network.compressed", response.size()); } }
TEST(time, timer_create_NULL) { // A NULL sigevent* is equivalent to asking for SIGEV_SIGNAL for SIGALRM. timer_t timer_id; ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, NULL, &timer_id)); ScopedSignalHandler ssh(SIGALRM, timer_create_NULL_signal_handler); ASSERT_EQ(0, timer_create_NULL_signal_handler_invocation_count); SetTime(timer_id, 0, 1, 0, 0); usleep(500000); ASSERT_EQ(1, timer_create_NULL_signal_handler_invocation_count); }
TEST(signal, sigwait) { ScopedSignalHandler ssh(SIGALRM, HandleSIGALRM); sigset_t wait_set; sigemptyset(&wait_set); sigaddset(&wait_set, SIGALRM); alarm(1); int received_signal; errno = 0; ASSERT_EQ(0, sigwait(&wait_set, &received_signal)); ASSERT_EQ(0, errno); ASSERT_EQ(SIGALRM, received_signal); }
bool ServerConfigurationDialog::testSshConnection(ServerConfiguration const& configuration) { bool okay(false); try { QString hostAddress(configuration.value(ServerConfiguration::HostAddress)); QString userName(configuration.value(ServerConfiguration::UserName)); Network::Connection::AuthenticationT authentication(configuration.authentication()); int port(configuration.port()); Network::SshConnection ssh(hostAddress, port); ssh.open(); ssh.authenticate(authentication, userName); QLOG_TRACE() << "Authentication successful"; QEventLoop loop; Network::Reply* reply(ssh.execute("ls")); QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); loop.exec(); okay = (reply->status() == Network::Reply::Finished); if (okay) { QLOG_DEBUG() << "----------------------------"; QLOG_DEBUG() << reply->message(); QLOG_DEBUG() << "----------------------------"; }else { QString msg("Connection failed:\n"); msg += reply->message(); QMsgBox::warning(this, "IQmol", msg); } delete reply; }catch (Network::AuthenticationCancelled& err) { // don't do anything }catch (Network::AuthenticationError& err) { QMsgBox::warning(0, "IQmol", "Invalid username or password"); }catch (Exception& err) { okay = false; QMsgBox::warning(0, "IQmol", err.what()); } return okay; }
void HxCPDSpatialGraphWarp::applyNLDeformationToSlice( SpatialGraphSelection& slice, HxSpatialGraph* spatialGraph, const McDArray<McVec3f>& origCoords, const McDArray<McVec3f>& shiftedCoords) { McWatch watch; watch.start(); MovingLeastSquares mls; mls.setAlpha(portAlphaForMLS.getValue()); mls.setLandmarks(origCoords, shiftedCoords); ma::SliceSelector ssh(spatialGraph, "TransformInfo"); SpatialGraphSelection fullSliceSelection; ssh.getSlice(ssh.getSliceAttributeValueFromIndex(1), fullSliceSelection); for (int i = 0; i < fullSliceSelection.getNumSelectedEdges(); i++) { int edge = fullSliceSelection.getSelectedEdge(i); McDArray<McVec3f> newEdgePoints; for (int j = 0; j < spatialGraph->getNumEdgePoints(edge); j++) { McVec3f edgePoint = spatialGraph->getEdgePoint(edge, j); warpPoint(edgePoint, edgePoint, mls); newEdgePoints.append(edgePoint); } spatialGraph->setEdgePoints(edge, newEdgePoints); } for (int i = 0; i < fullSliceSelection.getNumSelectedVertices(); i++) { int curVertex = fullSliceSelection.getSelectedVertex(i); McVec3f curCoord = spatialGraph->getVertexCoords(curVertex); McVec3f curCoordWarped; warpPoint(curCoord, curCoordWarped, mls); spatialGraph->setVertexCoords(curVertex, curCoordWarped); // Add new segments that indicate shift. if (slice.isSelectedVertex(curVertex)) { int newVertex = spatialGraph->addVertex(curCoord); McDArray<McVec3f> edgePoints(2); edgePoints[0] = curCoord; edgePoints[1] = curCoordWarped; spatialGraph->addEdge(newVertex, curVertex, edgePoints); } } std::cout << "\n Apply deformation took " << watch.stop() << " seconds."; }
void HxRotateSpatialGraphStackSliceAndCDP::rotateSlice(HxSpatialGraph* graph, const double angle) { mtalign::SliceSelector ssh(graph, "TransformInfo"); SpatialGraphSelection fullSliceSelection; ssh.getSlice(ssh.getSliceAttributeValueFromIndex(1), fullSliceSelection); McMat3f rotMat = McMat3f::IDENTITY; rotMat[0][0] = cos(angle); rotMat[0][1] = -sin(angle); rotMat[1][0] = sin(angle); rotMat[1][1] = cos(angle); // compute barycenter McVec3f barycenter(0, 0, 0); for (int i = 0; i < fullSliceSelection.getNumSelectedVertices(); i++) { barycenter += graph->getVertexCoords(fullSliceSelection.getSelectedVertex(i)); } barycenter /= fullSliceSelection.getNumSelectedVertices(); McVec3f rotatedBarycenter; rotMat.multMatrixVec(barycenter, rotatedBarycenter); McVec3f translation = barycenter - rotatedBarycenter; for (int i = 0; i < fullSliceSelection.getNumSelectedEdges(); i++) { int edge = fullSliceSelection.getSelectedEdge(i); McDArray<McVec3f> newEdgePoints; for (int j = 0; j < graph->getNumEdgePoints(edge); j++) { McVec3f edgePoint = graph->getEdgePoint(edge, j); McVec3f rotatedEdgePoint; rotMat.multMatrixVec(edgePoint, rotatedEdgePoint); rotatedEdgePoint += translation; newEdgePoints.append(rotatedEdgePoint); } graph->setEdgePoints(edge, newEdgePoints); } for (int i = 0; i < fullSliceSelection.getNumSelectedVertices(); i++) { int curVertex = fullSliceSelection.getSelectedVertex(i); McVec3f curCoord = graph->getVertexCoords(curVertex); McVec3f rotatedVertex; rotMat.multMatrixVec(curCoord, rotatedVertex); rotatedVertex += translation; graph->setVertexCoords(curVertex, rotatedVertex); } }
void SFTPTreeView::DoOpenSession() { DoCloseSession(); wxString accountName = m_choiceAccount->GetStringSelection(); if(accountName.IsEmpty()) { return; } SFTPSettings settings; settings.Load(); m_account = SSHAccountInfo(); if(!settings.GetAccount(accountName, m_account)) { ::wxMessageBox(wxString() << _("Could not find account: ") << accountName, "codelite", wxICON_ERROR | wxOK); return; } clSSH::Ptr_t ssh( new clSSH(m_account.GetHost(), m_account.GetUsername(), m_account.GetPassword(), m_account.GetPort())); try { wxString message; m_plugin->GetManager()->SetStatusMessage(wxString() << _("Connecting to: ") << accountName << "..."); ssh->Connect(); if(!ssh->AuthenticateServer(message)) { if(::wxMessageBox(message, "SSH", wxYES_NO | wxCENTER | wxICON_QUESTION) == wxYES) { ssh->AcceptServerAuthentication(); } } ssh->Login(); m_sftp.reset(new clSFTP(ssh)); m_sftp->Initialize(); m_sftp->SetAccount(m_account.GetAccountName()); m_plugin->GetManager()->SetStatusMessage(wxString() << _("Done!")); DoBuildTree("/"); } catch(clException& e) { ::wxMessageBox(e.What(), "codelite", wxICON_ERROR | wxOK); DoCloseSession(); } }
TEST(signal, sigsuspend_sigpending) { // Block SIGALRM. sigset_t just_SIGALRM; sigemptyset(&just_SIGALRM); sigaddset(&just_SIGALRM, SIGALRM); sigset_t original_set; ASSERT_EQ(0, sigprocmask(SIG_BLOCK, &just_SIGALRM, &original_set)); ScopedSignalHandler ssh(SIGALRM, SigSuspendTestHelper); // There should be no pending signals. sigset_t pending; sigemptyset(&pending); ASSERT_EQ(0, sigpending(&pending)); for (size_t i = SIGNAL_MIN(); i <= SIGNAL_MAX(); ++i) { EXPECT_FALSE(sigismember(&pending, i)) << i; } // Raise SIGALRM and check our signal handler wasn't called. raise(SIGALRM); ASSERT_EQ(0, g_sigsuspend_test_helper_call_count); // We should now have a pending SIGALRM but nothing else. sigemptyset(&pending); ASSERT_EQ(0, sigpending(&pending)); for (size_t i = SIGNAL_MIN(); i <= SIGNAL_MAX(); ++i) { EXPECT_EQ((i == SIGALRM), sigismember(&pending, i)); } // Use sigsuspend to block everything except SIGALRM... sigset_t not_SIGALRM; sigfillset(¬_SIGALRM); sigdelset(¬_SIGALRM, SIGALRM); ASSERT_EQ(-1, sigsuspend(¬_SIGALRM)); ASSERT_EQ(EINTR, errno); // ...and check that we now receive our pending SIGALRM. ASSERT_EQ(1, g_sigsuspend_test_helper_call_count); // Restore the original set. ASSERT_EQ(0, sigprocmask(SIG_SETMASK, &original_set, NULL)); }
TEST(time, timer_create_SIGEV_SIGNAL) { sigevent_t se; memset(&se, 0, sizeof(se)); se.sigev_notify = SIGEV_SIGNAL; se.sigev_signo = SIGUSR1; timer_t timer_id; ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id)); ScopedSignalHandler ssh(SIGUSR1, timer_create_SIGEV_SIGNAL_signal_handler); ASSERT_EQ(0, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count); itimerspec ts; ts.it_value.tv_sec = 0; ts.it_value.tv_nsec = 1; ts.it_interval.tv_sec = 0; ts.it_interval.tv_nsec = 0; ASSERT_EQ(0, timer_settime(timer_id, TIMER_ABSTIME, &ts, NULL)); usleep(500000); ASSERT_EQ(1, timer_create_SIGEV_SIGNAL_signal_handler_invocation_count); }
bool RPCRequestHandler::executePHPFunction(Transport *transport, SourceRootInfo &sourceRootInfo) { // reset timeout counter ThreadInfo::s_threadInfo->m_reqInjectionData.started = time(0); string rpcFunc = transport->getCommand(); { ServerStatsHelper ssh("input"); RequestURI reqURI(rpcFunc); HttpProtocol::PrepareSystemVariables(transport, reqURI, sourceRootInfo); } bool isFile = rpcFunc.rfind('.') != string::npos; string rpcFile; bool error = false; Array params; string sparams = transport->getParam("params"); if (!sparams.empty()) { Variant jparams = f_json_decode(String(sparams), true); if (jparams.isArray()) { params = jparams.toArray(); } else { error = true; } } else { vector<string> sparams; transport->getArrayParam("p", sparams); if (!sparams.empty()) { for (unsigned int i = 0; i < sparams.size(); i++) { Variant jparams = f_json_decode(String(sparams[i]), true); if (same(jparams, false)) { error = true; break; } params.append(jparams); } } else { // single string parameter, used by xbox to avoid any en/decoding int size; const void *data = transport->getPostData(size); if (data && size) { params.append(String((char*)data, size, AttachLiteral)); } } } if (transport->getIntParam("reset") == 1) { m_reset = true; } int output = transport->getIntParam("output"); int code; if (!error) { Variant funcRet; string errorMsg = "Internal Server Error"; string warmupDoc, reqInitFunc, reqInitDoc; if (m_serverInfo) { warmupDoc = m_serverInfo->getWarmupDoc(); reqInitFunc = m_serverInfo->getReqInitFunc(); reqInitDoc = m_serverInfo->getReqInitDoc(); } if (!warmupDoc.empty()) warmupDoc = canonicalize_path(warmupDoc, "", 0); if (!warmupDoc.empty()) { warmupDoc = getSourceFilename(warmupDoc, sourceRootInfo); } if (!reqInitDoc.empty()) reqInitDoc = canonicalize_path(reqInitDoc, "", 0); if (!reqInitDoc.empty()) { reqInitDoc = getSourceFilename(reqInitDoc, sourceRootInfo); } bool runOnce = false; bool ret = true; if (isFile) { rpcFile = rpcFunc; rpcFunc.clear(); } else { rpcFile = transport->getParam("include"); if (rpcFile.empty()) { rpcFile = transport->getParam("include_once"); runOnce = true; } } if (!rpcFile.empty()) { // invoking a file through rpc bool forbidden = false; if (!RuntimeOption::ForbiddenFileExtensions.empty()) { const char *ext = rpcFile.c_str() + rpcFile.rfind('.') + 1; if (RuntimeOption::ForbiddenFileExtensions.find(ext) != RuntimeOption::ForbiddenFileExtensions.end()) { forbidden = true; } } if (!forbidden) { rpcFile = canonicalize_path(rpcFile, "", 0); rpcFile = getSourceFilename(rpcFile, sourceRootInfo); ret = hphp_invoke(m_context, rpcFile, false, Array(), null, warmupDoc, reqInitFunc, reqInitDoc, error, errorMsg, runOnce); } // no need to do the initialization for a second time warmupDoc.clear(); reqInitFunc.clear(); reqInitDoc.clear(); } if (ret && !rpcFunc.empty()) { ret = hphp_invoke(m_context, rpcFunc, true, params, ref(funcRet), warmupDoc, reqInitFunc, reqInitDoc, error, errorMsg); } if (ret) { String response; switch (output) { case 0: response = f_json_encode(funcRet); break; case 1: response = m_context->obDetachContents(); break; case 2: response = f_json_encode(CREATE_MAP2("output", m_context->obDetachContents(), "return", f_json_encode(funcRet))); break; } code = 200; transport->sendRaw((void*)response.data(), response.size()); } else if (error) { code = 500; transport->sendString(errorMsg, 500); m_reset = true; } else { code = 404; transport->sendString("Not Found", 404); } } else { code = 400; transport->sendString("Bad Request", 400); } params.reset(); sourceRootInfo.clear(); transport->onSendEnd(); ServerStats::LogPage(isFile ? rpcFile : rpcFunc, code); m_context->onShutdownPostSend(); m_context->obClean(); // in case postsend/cleanup output something m_context->restoreSession(); return !error; }
void HttpRequestHandler::handleRequest(Transport *transport) { ExecutionProfiler ep(ThreadInfo::RuntimeFunctions); Logger::OnNewRequest(); GetAccessLog().onNewRequest(); transport->enableCompression(); ServerStatsHelper ssh("all", true); Logger::Verbose("receiving %s", transport->getCommand().c_str()); // will clear all extra logging when this function goes out of scope StackTraceNoHeap::ExtraLoggingClearer clearer; StackTraceNoHeap::AddExtraLogging("URL", transport->getUrl()); // resolve virtual host const VirtualHost *vhost = HttpProtocol::GetVirtualHost(transport); ASSERT(vhost); if (vhost->disabled() || vhost->isBlocking(transport->getCommand(), transport->getRemoteHost())) { transport->sendString("Not Found", 404); return; } ServerStats::StartRequest(transport->getCommand().c_str(), transport->getRemoteHost(), vhost->getName().c_str()); // resolve source root string host = transport->getHeader("Host"); SourceRootInfo sourceRootInfo(host.c_str()); if (sourceRootInfo.error()) { sourceRootInfo.handleError(transport); return; } // request URI string pathTranslation = m_pathTranslation ? vhost->getPathTranslation().c_str() : ""; RequestURI reqURI(vhost, transport, sourceRootInfo.path(), pathTranslation); if (reqURI.done()) { return; // already handled with redirection or 404 } string path = reqURI.path().data(); string absPath = reqURI.absolutePath().data(); // determine whether we should compress response bool compressed = transport->decideCompression(); const char *data; int len; size_t pos = path.rfind('.'); const char *ext = (pos != string::npos) && path.find('/', pos) == string::npos // no extention in ./foo or ../bar ? (path.c_str() + pos + 1) : NULL; bool cachableDynamicContent = (!RuntimeOption::StaticFileGenerators.empty() && RuntimeOption::StaticFileGenerators.find(path) != RuntimeOption::StaticFileGenerators.end()); // If this is not a php file, check the static and dynamic content caches if (ext && strcasecmp(ext, "php") != 0) { if (RuntimeOption::EnableStaticContentCache) { bool original = compressed; // check against static content cache if (StaticContentCache::TheCache.find(path, data, len, compressed)) { struct stat st; st.st_mtime = 0; String str; // (qigao) not calling stat at this point because the timestamp of // local cache file is not valuable, maybe misleading. This way // the Last-Modified header will not show in response. // stat(RuntimeOption::FileCache.c_str(), &st); if (!original && compressed) { data = gzdecode(data, len); if (data == NULL) { throw FatalErrorException("cannot unzip compressed data"); } compressed = false; str = NEW(StringData)(data, len, AttachString); } sendStaticContent(transport, data, len, st.st_mtime, compressed, path); StaticContentCache::TheFileCache->adviseOutMemory(); ServerStats::LogPage(path, 200); return; } } if (RuntimeOption::EnableStaticContentFromDisk) { String translated = File::TranslatePath(String(absPath)); if (!translated.empty()) { StringBuffer sb(translated.data()); if (sb.valid()) { struct stat st; st.st_mtime = 0; stat(translated.data(), &st); sendStaticContent(transport, sb.data(), sb.size(), st.st_mtime, false, path); ServerStats::LogPage(path, 200); return; } } } // check static contents that were generated by dynamic pages if (cachableDynamicContent) { // check against dynamic content cache ASSERT(transport->getUrl()); string key = path + transport->getUrl(); if (DynamicContentCache::TheCache.find(key, data, len, compressed)) { sendStaticContent(transport, data, len, 0, compressed, path); ServerStats::LogPage(path, 200); return; } } } // proxy any URLs that not specified in ServeURLs if (!RuntimeOption::ProxyOrigin.empty() && ((RuntimeOption::UseServeURLs && RuntimeOption::ServeURLs.find(path) == RuntimeOption::ServeURLs.end()) || (RuntimeOption::UseProxyURLs && (RuntimeOption::ProxyURLs.find(path) != RuntimeOption::ProxyURLs.end() || MatchAnyPattern(path, RuntimeOption::ProxyPatterns) || (abs(rand()) % 100) < RuntimeOption::ProxyPercentage)))) { for (int i = 0; i < RuntimeOption::ProxyRetry; i++) { bool force = (i == RuntimeOption::ProxyRetry - 1); // last one if (handleProxyRequest(transport, force)) break; } return; } // record request for debugging purpose std::string tmpfile = HttpProtocol::RecordRequest(transport); // main body hphp_session_init(); vhost->setRequestTimeoutSeconds(); bool ret = false; try { ret = executePHPRequest(transport, reqURI, sourceRootInfo, cachableDynamicContent); } catch (const Eval::DebuggerException &e) { transport->sendString(e.what(), 200); transport->onSendEnd(); hphp_context_exit(g_context.getNoCheck(), true, true, transport->getUrl()); } catch (...) { Logger::Error("Unhandled exception in HPHP server engine."); } GetAccessLog().log(transport, vhost); hphp_session_exit(); HttpProtocol::ClearRecord(ret, tmpfile); }
bool HttpRequestHandler::executePHPRequest(Transport *transport, RequestURI &reqURI, SourceRootInfo &sourceRootInfo, bool cachableDynamicContent) { ExecutionContext *context = hphp_context_init(); context->setTransport(transport); string file = reqURI.absolutePath().c_str(); { ServerStatsHelper ssh("input"); HttpProtocol::PrepareSystemVariables(transport, reqURI, sourceRootInfo); reqURI.clear(); sourceRootInfo.clear(); } bool error = false; std::string errorMsg = "Internal Server Error"; bool ret = hphp_invoke(context, file, false, Array(), null, RuntimeOption::WarmupDocument, RuntimeOption::RequestInitFunction, error, errorMsg); int code; if (ret) { std::string content = context->getContents(); if (cachableDynamicContent && !content.empty()) { ASSERT(transport->getUrl()); string key = file + transport->getUrl(); DynamicContentCache::TheCache.store(key, content.data(), content.size()); } code = 200; transport->sendRaw((void*)content.data(), content.size()); } else if (error) { code = 500; string errorPage = context->getErrorPage(); if (errorPage.empty()) { errorPage = RuntimeOption::ErrorDocument500; } if (!errorPage.empty()) { context->obEndAll(); context->obStart(); context->obProtect(true); ret = hphp_invoke(context, errorPage, false, Array(), null, RuntimeOption::WarmupDocument, RuntimeOption::RequestInitFunction, error, errorMsg); if (ret) { std::string content = context->getContents(); transport->sendRaw((void*)content.data(), content.size()); } else { errorPage.clear(); // so we fall back to 500 return } } if (errorPage.empty()) { if (RuntimeOption::ServerErrorMessage) { transport->sendString(errorMsg, 500); } else { transport->sendString(RuntimeOption::FatalErrorMessage, 500); } } } else { code = 404; transport->sendString("Not Found", 404); } transport->onSendEnd(); ServerStats::LogPage(file, code); hphp_context_exit(context, true); return ret; }
void HttpRequestHandler::handleRequest(Transport *transport) { Logger::OnNewRequest(); GetAccessLog().onNewRequest(); transport->enableCompression(); Logger::Verbose("receiving %s", transport->getCommand().c_str()); ServerStatsHelper ssh("all", true); // resolve virtual host const VirtualHost *vhost = HttpProtocol::GetVirtualHost(transport); ASSERT(vhost); if (vhost->disabled() || vhost->isBlocking(transport->getCommand(), transport->getRemoteHost())) { transport->sendString("Not Found", 404); return; } ServerStats::StartRequest(transport->getCommand().c_str(), transport->getRemoteHost(), vhost->getName().c_str()); // resolve source root string host = transport->getHeader("Host"); SourceRootInfo sourceRootInfo(host.c_str()); if (sourceRootInfo.error()) { sourceRootInfo.handleError(transport); return; } // request URI string pathTranslation = m_pathTranslation ? vhost->getPathTranslation().c_str() : ""; RequestURI reqURI(vhost, transport, sourceRootInfo.path(), pathTranslation); if (reqURI.done()) { return; // already handled with redirection or 404 } string path = reqURI.path().data(); string absPath = reqURI.absolutePath().data(); bool compressed = transport->acceptEncoding("gzip"); const char *data; int len; size_t pos = path.rfind('.'); const char *ext = (pos != string::npos) ? (path.c_str() + pos + 1) : NULL; bool cachableDynamicContent = (!RuntimeOption::StaticFileGenerators.empty() && RuntimeOption::StaticFileGenerators.find(path) != RuntimeOption::StaticFileGenerators.end()); // If this is not a php file, check the static and dynamic content caches if (ext && strcasecmp(ext, "php") != 0) { if (RuntimeOption::EnableStaticContentCache) { // check against static content cache if (StaticContentCache::TheCache.find(path, data, len, compressed)) { struct stat st; st.st_mtime = 0; // (qigao) not calling stat at this point because the timestamp of // local cache file is not valuable, maybe misleading. This way // the Last-Modified header will not show in response. // stat(RuntimeOption::FileCache.c_str(), &st); sendStaticContent(transport, data, len, st.st_mtime, compressed, path); ServerStats::LogPage(path, 200); return; } } if (RuntimeOption::EnableStaticContentFromDisk && RuntimeOption::StaticFileExtensions.find(ext) != RuntimeOption::StaticFileExtensions.end()) { String translated = File::TranslatePath(String(absPath)); if (!translated.empty()) { StringBuffer sb(translated.data()); if (sb.valid()) { struct stat st; st.st_mtime = 0; stat(translated.data(), &st); sendStaticContent(transport, sb.data(), sb.size(), st.st_mtime, false, path); ServerStats::LogPage(path, 200); return; } } } // check static contents that were generated by dynamic pages if (cachableDynamicContent) { // check against dynamic content cache ASSERT(transport->getUrl()); string key = path + transport->getUrl(); if (DynamicContentCache::TheCache.find(key, data, len, compressed)) { sendStaticContent(transport, data, len, 0, compressed, path); ServerStats::LogPage(path, 200); return; } } } // proxy any URLs that not specified in ServeURLs if (!RuntimeOption::ProxyOrigin.empty() && ((RuntimeOption::UseServeURLs && RuntimeOption::ServeURLs.find(path) == RuntimeOption::ServeURLs.end()) || (RuntimeOption::UseProxyURLs && (RuntimeOption::ProxyURLs.find(path) != RuntimeOption::ProxyURLs.end() || MatchAnyPattern(path, RuntimeOption::ProxyPatterns) || (abs(rand()) % 100) < RuntimeOption::ProxyPercentage)))) { for (int i = 0; i < RuntimeOption::ProxyRetry; i++) { bool force = (i == RuntimeOption::ProxyRetry - 1); // last one if (handleProxyRequest(transport, force)) break; } return; } // record request for debugging purpose std::string tmpfile = HttpProtocol::RecordRequest(transport); // main body hphp_session_init(); bool ret = false; try { ret = executePHPRequest(transport, reqURI, sourceRootInfo, cachableDynamicContent); } catch (...) { Logger::Error("Unhandled exception in HPHP server engine."); } GetAccessLog().log(transport); hphp_session_exit(); HttpProtocol::ClearRecord(ret, tmpfile); }
TEST(signal, raise_in_signal_handler) { ScopedSignalHandler ssh(SIGALRM, raise_in_signal_handler_helper); raise(SIGALRM); }
void SFTPTreeView::DoOpenSession() { DoCloseSession(); wxString accountName = m_choiceAccount->GetStringSelection(); if(accountName.IsEmpty()) { return; } SFTPSettings settings; settings.Load(); m_account = SSHAccountInfo(); if(!settings.GetAccount(accountName, m_account)) { ::wxMessageBox(wxString() << _("Could not find account: ") << accountName, "codelite", wxICON_ERROR | wxOK); return; } wxString message; #ifndef _WIN64 wxProgressDialog dlg(_("SFTP"), wxString(' ', 100) + "\n\n", 10); dlg.Show(); dlg.Update(1, wxString() << _("Connecting to: ") << accountName << "..." << _("\n(this may take a few seconds)")); #else wxBusyCursor bc; clGetManager()->SetStatusMessage(wxString() << _("Connecting to: ") << accountName); #endif // We know that there is a bug that libssh succeeded on connecting only on the second attempt.. // to workaround it, we issue first connect with 1 second timeout, and then re-open the connection try { clSSH::Ptr_t ssh( new clSSH(m_account.GetHost(), m_account.GetUsername(), m_account.GetPassword(), m_account.GetPort())); ssh->Connect(wxNOT_FOUND); } catch(...) { } try { clSSH::Ptr_t ssh( new clSSH(m_account.GetHost(), m_account.GetUsername(), m_account.GetPassword(), m_account.GetPort())); ssh->Connect(5); #ifndef _WIN64 dlg.Update(5, _("Connected!")); dlg.Update(6, _("Authenticating server...")); #endif if(!ssh->AuthenticateServer(message)) { if(::wxMessageBox(message, "SSH", wxYES_NO | wxCENTER | wxICON_QUESTION) == wxYES) { #ifndef _WIN64 dlg.Update(7, _("Accepting server authentication server...")); #endif ssh->AcceptServerAuthentication(); } } else { #ifndef _WIN64 dlg.Update(7, _("Server authenticated")); #endif } #ifndef _WIN64 dlg.Update(8, _("Logging...")); #endif ssh->Login(); m_sftp.reset(new clSFTP(ssh)); m_sftp->Initialize(); m_sftp->SetAccount(m_account.GetAccountName()); m_plugin->GetManager()->SetStatusMessage(wxString() << _("Done!")); #ifndef _WIN64 dlg.Update(9, _("Fetching directory list...")); #endif DoBuildTree(m_account.GetDefaultFolder().IsEmpty() ? "/" : m_account.GetDefaultFolder()); #ifndef _WIN64 dlg.Update(10, _("Done")); #endif } catch(clException& e) { ::wxMessageBox(e.What(), "codelite", wxICON_ERROR | wxOK); DoCloseSession(); } }
bool RPCRequestHandler::executePHPFunction(Transport *transport, SourceRootInfo &sourceRootInfo) { // reset timeout counter ThreadInfo::s_threadInfo->m_reqInjectionData.started = time(0); std::string rpcFunc = transport->getCommand(); { ServerStatsHelper ssh("input"); RequestURI reqURI(rpcFunc); HttpProtocol::PrepareSystemVariables(transport, reqURI, sourceRootInfo); sourceRootInfo.clear(); } bool error = false; Array params; std::string sparams = transport->getParam("params"); if (!sparams.empty()) { Variant jparams = f_json_decode(String(sparams), true); if (jparams.isArray()) { params = jparams.toArray(); } else { error = true; } } else { vector<string> sparams; transport->getArrayParam("p", sparams); if (!sparams.empty()) { for (unsigned int i = 0; i < sparams.size(); i++) { Variant jparams = f_json_decode(String(sparams[i]), true); if (same(jparams, false)) { error = true; break; } params.append(jparams); } } else { // single string parameter, used by xbox to avoid any en/decoding int size; const void *data = transport->getPostData(size); if (data && size) { params.append(String((char*)data, size, AttachLiteral)); } } } if (transport->getIntParam("reset") == 1) { m_reset = true; } int output = transport->getIntParam("output"); int code; if (!error) { Variant funcRet; std::string errorMsg = "Internal Server Error"; string warmupDoc, reqInitFunc, reqInitDoc; if (m_serverInfo) { warmupDoc = m_serverInfo->getWarmupDoc(); reqInitFunc = m_serverInfo->getReqInitFunc(); reqInitDoc = m_serverInfo->getReqInitDoc(); } if (!warmupDoc.empty()) warmupDoc = canonicalize_path(warmupDoc, "", 0); if (!warmupDoc.empty()) warmupDoc = get_source_filename(warmupDoc.c_str()); bool ret = hphp_invoke(m_context, rpcFunc, true, params, ref(funcRet), warmupDoc, reqInitFunc, reqInitDoc, error, errorMsg); if (ret) { String response; switch (output) { case 0: response = f_json_encode(funcRet); break; case 1: response = m_context->obDetachContents(); break; case 2: response = f_json_encode(CREATE_MAP2("output", m_context->obDetachContents(), "return", f_json_encode(funcRet))); break; } code = 200; transport->sendRaw((void*)response.data(), response.size()); } else if (error) { code = 500; transport->sendString(errorMsg, 500); m_reset = true; } else { code = 404; transport->sendString("Not Found", 404); } } else { code = 400; transport->sendString("Bad Request", 400); } params.reset(); transport->onSendEnd(); ServerStats::LogPage(rpcFunc, code); m_context->onShutdownPostSend(); m_context->restoreSession(); return !error; }
void RPCRequestHandler::handleRequest(Transport *transport) { ExecutionProfiler ep(ThreadInfo::RuntimeFunctions); Logger::OnNewRequest(); HttpRequestHandler::GetAccessLog().onNewRequest(); m_context->setTransport(transport); transport->enableCompression(); ServerStatsHelper ssh("all", ServerStatsHelper::TRACK_MEMORY); Logger::Verbose("receiving %s", transport->getCommand().c_str()); // will clear all extra logging when this function goes out of scope StackTraceNoHeap::ExtraLoggingClearer clearer; StackTraceNoHeap::AddExtraLogging("RPC-URL", transport->getUrl()); // authentication const set<string> &passwords = m_serverInfo->getPasswords(); if (!passwords.empty()) { set<string>::const_iterator iter = passwords.find(transport->getParam("auth")); if (iter == passwords.end()) { transport->sendString("Unauthorized", 401); transport->onSendEnd(); HttpRequestHandler::GetAccessLog().log(transport, nullptr); /* * HPHP logs may need to access data in ServerStats, so we have to * clear the hashtable after writing the log entry. */ ServerStats::Reset(); return; } } else { const string &password = m_serverInfo->getPassword(); if (!password.empty() && password != transport->getParam("auth")) { transport->sendString("Unauthorized", 401); transport->onSendEnd(); HttpRequestHandler::GetAccessLog().log(transport, nullptr); /* * HPHP logs may need to access data in ServerStats, so we have to * clear the hashtable after writing the log entry. */ ServerStats::Reset(); return; } } // return encoding type if (transport->getParam("return") == "serialize") { setReturnEncodeType(Serialize); } // resolve virtual host const VirtualHost *vhost = HttpProtocol::GetVirtualHost(transport); assert(vhost); if (vhost->disabled()) { transport->sendString("Virtual host disabled.", 404); transport->onSendEnd(); HttpRequestHandler::GetAccessLog().log(transport, vhost); return; } vhost->setRequestTimeoutSeconds(); // resolve source root string host = transport->getHeader("Host"); SourceRootInfo sourceRootInfo(host.c_str()); // set thread type switch (m_serverInfo->getType()) { case SatelliteServer::KindOfRPCServer: transport->setThreadType(Transport::RpcThread); break; case SatelliteServer::KindOfXboxServer: transport->setThreadType(Transport::XboxThread); break; default: break; } // record request for debugging purpose std::string tmpfile = HttpProtocol::RecordRequest(transport); bool ret = executePHPFunction(transport, sourceRootInfo); HttpRequestHandler::GetAccessLog().log(transport, vhost); /* * HPHP logs may need to access data in ServerStats, so we have to * clear the hashtable after writing the log entry. */ ServerStats::Reset(); HttpProtocol::ClearRecord(ret, tmpfile); }
bool HttpRequestHandler::executePHPRequest(Transport *transport, RequestURI &reqURI, SourceRootInfo &sourceRootInfo, bool cachableDynamicContent) { ExecutionContext *context = hphp_context_init(); if (RuntimeOption::ImplicitFlush) { context->obSetImplicitFlush(true); } if (RuntimeOption::EnableOutputBuffering) { if (RuntimeOption::OutputHandler.empty()) { context->obStart(); } else { context->obStart(String(RuntimeOption::OutputHandler)); } } context->setTransport(transport); string file = reqURI.absolutePath().c_str(); { ServerStatsHelper ssh("input"); HttpProtocol::PrepareSystemVariables(transport, reqURI, sourceRootInfo); if (RuntimeOption::EnableDebugger) { Eval::DSandboxInfo sInfo = sourceRootInfo.getSandboxInfo(); Eval::Debugger::RegisterSandbox(sInfo); context->setSandboxId(sInfo.id()); } reqURI.clear(); sourceRootInfo.clear(); } int code; bool ret = true; if (RuntimeOption::EnableDebugger) { Eval::Debugger::InterruptRequestStarted(transport->getUrl()); } bool error = false; std::string errorMsg = "Internal Server Error"; ret = hphp_invoke(context, file, false, Array(), uninit_null(), RuntimeOption::RequestInitFunction, RuntimeOption::RequestInitDocument, error, errorMsg); if (ret) { String content = context->obDetachContents(); if (cachableDynamicContent && !content.empty()) { assert(transport->getUrl()); string key = file + transport->getUrl(); DynamicContentCache::TheCache.store(key, content.data(), content.size()); } transport->sendRaw((void*)content.data(), content.size()); code = transport->getResponseCode(); } else if (error) { code = 500; string errorPage = context->getErrorPage().data(); if (errorPage.empty()) { errorPage = RuntimeOption::ErrorDocument500; } if (!errorPage.empty()) { context->obProtect(false); context->obEndAll(); context->obStart(); context->obProtect(true); ret = hphp_invoke(context, errorPage, false, Array(), uninit_null(), RuntimeOption::RequestInitFunction, RuntimeOption::RequestInitDocument, error, errorMsg); if (ret) { String content = context->obDetachContents(); transport->sendRaw((void*)content.data(), content.size()); code = transport->getResponseCode(); } else { Logger::Error("Unable to invoke error page %s", errorPage.c_str()); errorPage.clear(); // so we fall back to 500 return } } if (errorPage.empty()) { if (RuntimeOption::ServerErrorMessage) { transport->sendString(errorMsg, 500, false, false, "hphp_invoke"); } else { transport->sendString(RuntimeOption::FatalErrorMessage, 500, false, false, "hphp_invoke"); } } } else { code = 404; transport->sendString("Not Found", 404); } if (RuntimeOption::EnableDebugger) { Eval::Debugger::InterruptRequestEnded(transport->getUrl()); } transport->onSendEnd(); hphp_context_exit(context, true, true, transport->getUrl()); ServerStats::LogPage(file, code); return ret; }
void HttpRequestHandler::handleRequest(Transport *transport) { ExecutionProfiler ep(ThreadInfo::RuntimeFunctions); Logger::OnNewRequest(); GetAccessLog().onNewRequest(); transport->enableCompression(); ServerStatsHelper ssh("all", ServerStatsHelper::TRACK_MEMORY); Logger::Verbose("receiving %s", transport->getCommand().c_str()); // will clear all extra logging when this function goes out of scope StackTraceNoHeap::ExtraLoggingClearer clearer; StackTraceNoHeap::AddExtraLogging("URL", transport->getUrl()); // resolve virtual host const VirtualHost *vhost = HttpProtocol::GetVirtualHost(transport); assert(vhost); if (vhost->disabled() || vhost->isBlocking(transport->getCommand(), transport->getRemoteHost())) { transport->sendString("Not Found", 404); return; } // don't serve the request if it's been sitting in queue for longer than our // allowed request timeout. int requestTimeoutSeconds = vhost->getRequestTimeoutSeconds(getDefaultTimeout()); if (requestTimeoutSeconds > 0) { timespec now; Timer::GetMonotonicTime(now); const timespec& queueTime = transport->getQueueTime(); if (gettime_diff_us(queueTime, now) > requestTimeoutSeconds * 1000000) { transport->sendString("Service Unavailable", 503); m_requestTimedOutOnQueue->addValue(1); return; } } ServerStats::StartRequest(transport->getCommand().c_str(), transport->getRemoteHost(), vhost->getName().c_str()); // resolve source root string host = transport->getHeader("Host"); SourceRootInfo sourceRootInfo(host.c_str()); if (sourceRootInfo.error()) { sourceRootInfo.handleError(transport); return; } // request URI string pathTranslation = m_pathTranslation ? vhost->getPathTranslation().c_str() : ""; RequestURI reqURI(vhost, transport, sourceRootInfo.path(), pathTranslation); if (reqURI.done()) { return; // already handled with redirection or 404 } string path = reqURI.path().data(); string absPath = reqURI.absolutePath().data(); // determine whether we should compress response bool compressed = transport->decideCompression(); const char *data; int len; const char *ext = reqURI.ext(); if (reqURI.forbidden()) { transport->sendString("Forbidden", 403); return; } bool cachableDynamicContent = (!RuntimeOption::StaticFileGenerators.empty() && RuntimeOption::StaticFileGenerators.find(path) != RuntimeOption::StaticFileGenerators.end()); // If this is not a php file, check the static and dynamic content caches if (ext && strcasecmp(ext, "php") != 0) { if (RuntimeOption::EnableStaticContentCache) { bool original = compressed; // check against static content cache if (StaticContentCache::TheCache.find(path, data, len, compressed)) { Util::ScopedMem decompressed_data; // (qigao) not calling stat at this point because the timestamp of // local cache file is not valuable, maybe misleading. This way // the Last-Modified header will not show in response. // stat(RuntimeOption::FileCache.c_str(), &st); if (!original && compressed) { data = gzdecode(data, len); if (data == nullptr) { throw FatalErrorException("cannot unzip compressed data"); } decompressed_data = const_cast<char*>(data); compressed = false; } sendStaticContent(transport, data, len, 0, compressed, path, ext); StaticContentCache::TheFileCache->adviseOutMemory(); ServerStats::LogPage(path, 200); GetAccessLog().log(transport, vhost); return; } } if (RuntimeOption::EnableStaticContentFromDisk) { String translated = File::TranslatePath(String(absPath)); if (!translated.empty()) { CstrBuffer sb(translated.data()); if (sb.valid()) { struct stat st; st.st_mtime = 0; stat(translated.data(), &st); sendStaticContent(transport, sb.data(), sb.size(), st.st_mtime, false, path, ext); ServerStats::LogPage(path, 200); GetAccessLog().log(transport, vhost); return; } } } // check static contents that were generated by dynamic pages if (cachableDynamicContent) { // check against dynamic content cache assert(transport->getUrl()); string key = path + transport->getUrl(); if (DynamicContentCache::TheCache.find(key, data, len, compressed)) { sendStaticContent(transport, data, len, 0, compressed, path, ext); ServerStats::LogPage(path, 200); GetAccessLog().log(transport, vhost); return; } } } // proxy any URLs that not specified in ServeURLs if (!RuntimeOption::ProxyOrigin.empty() && ((RuntimeOption::UseServeURLs && RuntimeOption::ServeURLs.find(path) == RuntimeOption::ServeURLs.end()) || (RuntimeOption::UseProxyURLs && (RuntimeOption::ProxyURLs.find(path) != RuntimeOption::ProxyURLs.end() || MatchAnyPattern(path, RuntimeOption::ProxyPatterns) || (abs(rand()) % 100) < RuntimeOption::ProxyPercentage)))) { for (int i = 0; i < RuntimeOption::ProxyRetry; i++) { bool force = (i == RuntimeOption::ProxyRetry - 1); // last one if (handleProxyRequest(transport, force)) break; } return; } // record request for debugging purpose std::string tmpfile = HttpProtocol::RecordRequest(transport); // main body hphp_session_init(); ThreadInfo::s_threadInfo->m_reqInjectionData. setTimeout(requestTimeoutSeconds); bool ret = false; try { ret = executePHPRequest(transport, reqURI, sourceRootInfo, cachableDynamicContent); } catch (const Eval::DebuggerException &e) { transport->sendString(e.what(), 200); transport->onSendEnd(); hphp_context_exit(g_context.getNoCheck(), true, true, transport->getUrl()); } catch (...) { Logger::Error("Unhandled exception in HPHP server engine."); } GetAccessLog().log(transport, vhost); /* * HPHP logs may need to access data in ServerStats, so we have to * clear the hashtable after writing the log entry. */ ServerStats::Reset(); hphp_session_exit(); HttpProtocol::ClearRecord(ret, tmpfile); }
bool RPCRequestHandler::executePHPFunction(Transport *transport, SourceRootInfo &sourceRootInfo, ReturnEncodeType returnEncodeType) { string rpcFunc = transport->getCommand(); { ServerStatsHelper ssh("input"); RequestURI reqURI(rpcFunc); HttpProtocol::PrepareSystemVariables(transport, reqURI, sourceRootInfo); GlobalVariables *g = get_global_variables(); g->getRef(s__ENV).set(s_HPHP_RPC, 1); } bool isFile = rpcFunc.rfind('.') != string::npos; string rpcFile; bool error = false; Array params; string sparams = transport->getParam("params"); if (!sparams.empty()) { Variant jparams = f_json_decode(String(sparams), true); if (jparams.isArray()) { params = jparams.toArray(); } else { error = true; } } else { vector<string> sparams; transport->getArrayParam("p", sparams); if (!sparams.empty()) { for (unsigned int i = 0; i < sparams.size(); i++) { Variant jparams = f_json_decode(String(sparams[i]), true); if (same(jparams, false)) { error = true; break; } params.append(jparams); } } else { // single string parameter, used by xbox to avoid any en/decoding int size; const void *data = transport->getPostData(size); if (data && size) { params.append(String((char*)data, size, CopyString)); } } } if (transport->getIntParam("reset") == 1) { m_reset = true; } int output = transport->getIntParam("output"); int code; if (!error) { Variant funcRet; string errorMsg = "Internal Server Error"; string reqInitFunc, reqInitDoc; reqInitDoc = transport->getHeader("ReqInitDoc"); if (reqInitDoc.empty() && m_serverInfo) { reqInitFunc = m_serverInfo->getReqInitFunc(); reqInitDoc = m_serverInfo->getReqInitDoc(); } if (!reqInitDoc.empty()) { reqInitDoc = (std::string)canonicalize_path(reqInitDoc, "", 0); } if (!reqInitDoc.empty()) { reqInitDoc = getSourceFilename(reqInitDoc, sourceRootInfo); } bool runOnce = false; bool ret = true; if (isFile) { rpcFile = rpcFunc; rpcFunc.clear(); } else { rpcFile = transport->getParam("include"); if (rpcFile.empty()) { rpcFile = transport->getParam("include_once"); runOnce = true; } } if (!rpcFile.empty()) { // invoking a file through rpc bool forbidden = false; if (!RuntimeOption::ForbiddenFileExtensions.empty()) { const char *ext = rpcFile.c_str() + rpcFile.rfind('.') + 1; if (RuntimeOption::ForbiddenFileExtensions.find(ext) != RuntimeOption::ForbiddenFileExtensions.end()) { forbidden = true; } } if (!forbidden) { rpcFile = (std::string) canonicalize_path(rpcFile, "", 0); rpcFile = getSourceFilename(rpcFile, sourceRootInfo); ret = hphp_invoke(m_context, rpcFile, false, Array(), uninit_null(), reqInitFunc, reqInitDoc, error, errorMsg, runOnce); } // no need to do the initialization for a second time reqInitFunc.clear(); reqInitDoc.clear(); } if (ret && !rpcFunc.empty()) { ret = hphp_invoke(m_context, rpcFunc, true, params, ref(funcRet), reqInitFunc, reqInitDoc, error, errorMsg); } if (ret) { bool serializeFailed = false; String response; switch (output) { case 0: { assert(returnEncodeType == ReturnEncodeType::Json || returnEncodeType == ReturnEncodeType::Serialize); try { response = (returnEncodeType == ReturnEncodeType::Json) ? f_json_encode(funcRet) : f_serialize(funcRet); } catch (...) { serializeFailed = true; } break; } case 1: response = m_context->obDetachContents(); break; case 2: response = f_json_encode( make_map_array(s_output, m_context->obDetachContents(), s_return, f_json_encode(funcRet))); break; case 3: response = f_serialize(funcRet); break; } if (serializeFailed) { code = 500; transport->sendString( "Serialization of the return value failed", 500); m_reset = true; } else { transport->sendRaw((void*)response.data(), response.size()); code = transport->getResponseCode(); } } else if (error) { code = 500; transport->sendString(errorMsg, 500); m_reset = true; } else { code = 404; transport->sendString("Not Found", 404); } } else { code = 400; transport->sendString("Bad Request", 400); } params.reset(); sourceRootInfo.clear(); transport->onSendEnd(); ServerStats::LogPage(isFile ? rpcFile : rpcFunc, code); m_context->onShutdownPostSend(); m_context->obClean(); // in case postsend/cleanup output something m_context->restoreSession(); return !error; }
TEST(pthread, pthread_kill__in_signal_handler) { ScopedSignalHandler ssh(SIGALRM, pthread_kill__in_signal_handler_helper); ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM)); }
JSObject * GlobalObject::initFunctionAndObjectClasses(JSContext *cx) { Rooted<GlobalObject*> self(cx, this); JS_THREADSAFE_ASSERT(cx->compartment != cx->runtime->atomsCompartment); JS_ASSERT(isNative()); /* * Calling a function from a cleared global triggers this (yeah, I know). * Uncomment this once bug 470510 is fixed (if that bug doesn't remove * isCleared entirely). */ // JS_ASSERT(!isCleared()); /* If cx has no global object, make this the global object. */ if (!cx->globalObject) JS_SetGlobalObject(cx, self); RootedObject objectProto(cx); /* * Create |Object.prototype| first, mirroring CreateBlankProto but for the * prototype of the created object. */ objectProto = NewObjectWithGivenProto(cx, &ObjectClass, NULL, self); if (!objectProto || !objectProto->setSingletonType(cx)) return NULL; /* * The default 'new' type of Object.prototype is required by type inference * to have unknown properties, to simplify handling of e.g. heterogenous * objects in JSON and script literals. */ if (!objectProto->setNewTypeUnknown(cx)) return NULL; /* Create |Function.prototype| next so we can create other functions. */ RootedFunction functionProto(cx); { JSObject *functionProto_ = NewObjectWithGivenProto(cx, &FunctionClass, objectProto, self); if (!functionProto_) return NULL; functionProto = functionProto_->toFunction(); /* * Bizarrely, |Function.prototype| must be an interpreted function, so * give it the guts to be one. */ JSObject *proto = js_NewFunction(cx, functionProto, NULL, 0, JSFUN_INTERPRETED, self, NULL); if (!proto) return NULL; JS_ASSERT(proto == functionProto); functionProto->flags |= JSFUN_PROTOTYPE; const char *rawSource = "() {\n}"; size_t sourceLen = strlen(rawSource); jschar *source = InflateString(cx, rawSource, &sourceLen); if (!source) return NULL; ScriptSource *ss = cx->new_<ScriptSource>(); if (!ss) { cx->free_(source); return NULL; } ScriptSourceHolder ssh(cx->runtime, ss); ss->setSource(source, sourceLen); CompileOptions options(cx); options.setNoScriptRval(true) .setVersion(JSVERSION_DEFAULT); Rooted<JSScript*> script(cx, JSScript::Create(cx, /* enclosingScope = */ NullPtr(), /* savedCallerFun = */ false, options, /* staticLevel = */ 0, ss, 0, ss->length())); if (!script || !JSScript::fullyInitTrivial(cx, script)) return NULL; functionProto->initScript(script); functionProto->getType(cx)->interpretedFunction = functionProto; script->setFunction(functionProto); if (!functionProto->setSingletonType(cx)) return NULL; /* * The default 'new' type of Function.prototype is required by type * inference to have unknown properties, to simplify handling of e.g. * CloneFunctionObject. */ if (!functionProto->setNewTypeUnknown(cx)) return NULL; } /* Create the Object function now that we have a [[Prototype]] for it. */ RootedFunction objectCtor(cx); { JSObject *ctor = NewObjectWithGivenProto(cx, &FunctionClass, functionProto, self); if (!ctor) return NULL; objectCtor = js_NewFunction(cx, ctor, js_Object, 1, JSFUN_CONSTRUCTOR, self, CLASS_NAME(cx, Object)); if (!objectCtor) return NULL; } /* * Install |Object| and |Object.prototype| for the benefit of subsequent * code that looks for them. */ self->setObjectClassDetails(objectCtor, objectProto); /* Create |Function| so it and |Function.prototype| can be installed. */ RootedFunction functionCtor(cx); { // Note that ctor is rooted purely for the JS_ASSERT at the end RootedObject ctor(cx, NewObjectWithGivenProto(cx, &FunctionClass, functionProto, self)); if (!ctor) return NULL; functionCtor = js_NewFunction(cx, ctor, Function, 1, JSFUN_CONSTRUCTOR, self, CLASS_NAME(cx, Function)); if (!functionCtor) return NULL; JS_ASSERT(ctor == functionCtor); } /* * Install |Function| and |Function.prototype| so that we can freely create * functions and objects without special effort. */ self->setFunctionClassDetails(functionCtor, functionProto); /* * The hard part's done: now go back and add all the properties these * primordial values have. */ if (!LinkConstructorAndPrototype(cx, objectCtor, objectProto) || !DefinePropertiesAndBrand(cx, objectProto, NULL, object_methods)) { return NULL; } /* * Add an Object.prototype.__proto__ accessor property to implement that * extension (if it's actually enabled). Cache the getter for this * function so that cross-compartment [[Prototype]]-getting is implemented * in one place. */ Rooted<JSFunction*> getter(cx, js_NewFunction(cx, NULL, ProtoGetter, 0, 0, self, NULL)); if (!getter) return NULL; #if JS_HAS_OBJ_PROTO_PROP Rooted<JSFunction*> setter(cx, js_NewFunction(cx, NULL, ProtoSetter, 0, 0, self, NULL)); if (!setter) return NULL; RootedValue undefinedValue(cx, UndefinedValue()); if (!objectProto->defineProperty(cx, cx->runtime->atomState.protoAtom, undefinedValue, JS_DATA_TO_FUNC_PTR(PropertyOp, getter.get()), JS_DATA_TO_FUNC_PTR(StrictPropertyOp, setter.get()), JSPROP_GETTER | JSPROP_SETTER | JSPROP_SHARED)) { return NULL; } #endif /* JS_HAS_OBJ_PROTO_PROP */ self->setProtoGetter(getter); if (!DefinePropertiesAndBrand(cx, objectCtor, NULL, object_static_methods) || !LinkConstructorAndPrototype(cx, functionCtor, functionProto) || !DefinePropertiesAndBrand(cx, functionProto, NULL, function_methods) || !DefinePropertiesAndBrand(cx, functionCtor, NULL, NULL)) { return NULL; } /* Add the global Function and Object properties now. */ jsid objectId = NameToId(CLASS_NAME(cx, Object)); if (!self->addDataProperty(cx, objectId, JSProto_Object + JSProto_LIMIT * 2, 0)) return NULL; jsid functionId = NameToId(CLASS_NAME(cx, Function)); if (!self->addDataProperty(cx, functionId, JSProto_Function + JSProto_LIMIT * 2, 0)) return NULL; /* Heavy lifting done, but lingering tasks remain. */ /* ES5 15.1.2.1. */ RootedId id(cx, NameToId(cx->runtime->atomState.evalAtom)); JSObject *evalobj = js_DefineFunction(cx, self, id, IndirectEval, 1, JSFUN_STUB_GSOPS); if (!evalobj) return NULL; self->setOriginalEval(evalobj); /* ES5 13.2.3: Construct the unique [[ThrowTypeError]] function object. */ RootedFunction throwTypeError(cx, js_NewFunction(cx, NULL, ThrowTypeError, 0, 0, self, NULL)); if (!throwTypeError) return NULL; if (!throwTypeError->preventExtensions(cx)) return NULL; self->setThrowTypeError(throwTypeError); RootedObject intrinsicsHolder(cx, JS_NewObject(cx, NULL, NULL, self)); if (!intrinsicsHolder) return NULL; self->setIntrinsicsHolder(intrinsicsHolder); if (!JS_DefineFunctions(cx, intrinsicsHolder, intrinsic_functions)) return NULL; /* * The global object should have |Object.prototype| as its [[Prototype]]. * Eventually we'd like to have standard classes be there from the start, * and thus we would know we were always setting what had previously been a * null [[Prototype]], but right now some code assumes it can set the * [[Prototype]] before standard classes have been initialized. For now, * only set the [[Prototype]] if it hasn't already been set. */ if (self->shouldSplicePrototype(cx) && !self->splicePrototype(cx, objectProto)) return NULL; /* * Notify any debuggers about the creation of the script for * |Function.prototype| -- after all initialization, for simplicity. */ js_CallNewScriptHook(cx, functionProto->script(), functionProto); return functionProto; }