int FastCGITransport::getIntHeader(const std::string& name) { try { auto* key = getRawHeaderPtr(name); return (key != nullptr && !key->empty()) ? std::stoi(*key) : 0; } catch (std::invalid_argument&) { return 0; } catch (std::out_of_range&) { return 0; } }
/** * Passed an HTTP header like "Cookie" or "Cache-Control" **/ std::string FastCGITransport::getHeader(const char *name) { auto *header = getRawHeaderPtr(mangleHeader(name)); if (header) { return *header; } if (strcasecmp(name, "Content-Length") == 0) { return getRawHeader(s_contentLength); // No HTTP_ prefix for CONTENT_LENGTH } if (strcasecmp(name, "Content-Type") == 0) { return getRawHeader(s_contentType); // No HTTP_ prefix for CONTENT_TYPE } return ""; }
void FastCGITransport::onHeadersComplete() { m_requestURI = getRawHeader(s_requestURI); m_remoteHost = getRawHeader(s_remoteHost); m_remoteAddr = getRawHeader(s_remoteAddr); m_serverName = getRawHeader(s_serverName); m_serverAddr = getRawHeader(s_serverAddr); m_serverSoftware = getRawHeader(s_serverSoftware); m_extendedMethod = getRawHeader(s_extendedMethod); m_httpVersion = getRawHeader(s_httpVersion); m_serverObject = getRawHeader(s_scriptName); m_scriptFilename = getRawHeader(s_scriptFilename); m_pathTranslated = getRawHeader(s_pathTranslated); if (getRawHeaderPtr(s_pathInfo) != nullptr) { m_pathInfoSet = true; } m_pathInfo = getRawHeader(s_pathInfo); m_documentRoot = getRawHeader(s_documentRoot); if (!m_documentRoot.empty() && m_documentRoot[m_documentRoot.length() - 1] != '/') { m_documentRoot += '/'; } m_serverPort = getIntHeader(s_serverPort); m_requestSize = getIntHeader(s_contentLength); int port = getIntHeader(s_remotePort); if (port < std::numeric_limits<decltype(m_remotePort)>::min() || port > std::numeric_limits<decltype(m_remotePort)>::max()) { port = 0; } m_remotePort = port; auto* value = getRawHeaderPtr(s_https); if (value != nullptr && !value->empty()) { auto lValue = std::string{*value}; for (auto& c : lValue) { c = std::toupper(c); } // IIS sets this value but sets it to off when SSL is off. if (lValue != "OFF") { setSSL(); } } // Treat everything apart from GET and HEAD as a post to be like php-src. if (m_extendedMethod == "GET") { m_method = Method::GET; } else if (m_extendedMethod == "HEAD") { m_method = Method::HEAD; } else { m_method = Method::POST; } if (m_httpVersion.empty()) { // If we didn't receive a version, assume default transport version. // Flushing the request early requires HTTP_VERSION to be 1.1. m_httpVersion = Transport::getHTTPVersion(); } if (m_scriptFilename.empty() || RuntimeOption::ServerFixPathInfo) { // According to php-fpm, some servers don't set SCRIPT_FILENAME. In // this case, it uses PATH_TRANSLATED. // Added runtime option to change m_scriptFilename to s_pathTranslated // which will allow mod_fastcgi and mod_action to work correctly. m_scriptFilename = getRawHeader(s_pathTranslated); } // do a check for mod_proxy_fcgi and remove the extra portions of the string if (m_scriptFilename.find(s_modProxy) == 0) { // remove the proxy:type + :// from the start. int proxyPos = m_scriptFilename.find(s_modProxySearch); if (proxyPos != String::npos) { m_scriptFilename = m_scriptFilename.substr(proxyPos + s_modProxySearch.size()); } // remove everything before the first / which is host:port int slashPos = m_scriptFilename.find(s_slash); if (slashPos != String::npos) { m_scriptFilename = m_scriptFilename.substr(slashPos); } // remove everything after the first ? int questionPos = m_scriptFilename.find(s_questionMark); if (questionPos != String::npos) { m_scriptFilename = m_scriptFilename.substr(0, questionPos); } } // RequestURI needs script_filename and path_translated to not include // the document root if (!m_pathTranslated.empty()) { if (m_pathTranslated.find(m_documentRoot) == 0) { m_pathTranslated = m_pathTranslated.substr(m_documentRoot.length()); } } if (!m_scriptFilename.empty()) { if (m_scriptFilename.find(m_documentRoot) == 0) { m_scriptFilename = m_scriptFilename.substr(m_documentRoot.length()); } else { // if the document root isn't in the url set document root to / m_documentRoot = "/"; } } auto* queryString = getRawHeaderPtr(s_queryString); if (queryString != nullptr && !queryString->empty()) { m_serverObject += "?" + *queryString; } m_connection->handleRequest(m_id); }
/** * Passed a FastCGI mangled header like "HTTP_COOKIE" or "HTTP_CACHE_CONTROL" **/ std::string FastCGITransport::getRawHeader(const std::string& name) { auto header = getRawHeaderPtr(name); return (header == nullptr) ? std::string{""} : *header; }
void FastCGITransport::onHeadersComplete() { m_requestURI = getRawHeader(s_requestURI); m_remoteHost = getRawHeader(s_remoteHost); m_remoteAddr = getRawHeader(s_remoteAddr); m_serverName = getRawHeader(s_serverName); m_serverAddr = getRawHeader(s_serverAddr); m_extendedMethod = getRawHeader(s_extendedMethod); m_httpVersion = getRawHeader(s_httpVersion); m_serverObject = getRawHeader(s_scriptName); m_pathTranslated = getRawHeader(s_pathTranslated); m_documentRoot = getRawHeader(s_documentRoot) + "/"; m_serverPort = getIntHeader(s_serverPort); m_requestSize = getIntHeader(s_contentLength); int port = getIntHeader(s_remotePort); if (port < std::numeric_limits<decltype(m_remotePort)>::min() || port > std::numeric_limits<decltype(m_remotePort)>::max()) { port = 0; } m_remotePort = port; auto* value = getRawHeaderPtr(s_https); if (value != nullptr && !value->empty()) { auto lValue = std::string{*value}; for (auto& c : lValue) { c = std::toupper(c); } // IIS sets this value but sets it to off when SSL is off. if (lValue != "OFF") { setSSL(); } } if (m_extendedMethod == "GET") { m_method = Method::GET; } else if (m_extendedMethod == "POST") { m_method = Method::POST; } else if (m_extendedMethod == "HEAD") { m_method = Method::HEAD; } else { m_method = Method::Unknown; } if (m_pathTranslated.empty()) { // If someone follows http://wiki.nginx.org/HttpFastcgiModule they won't // pass in PATH_TRANSLATED and instead will just send SCRIPT_FILENAME m_pathTranslated = getRawHeader(s_scriptFilename); } // do a check for mod_proxy_cgi and remove the start portion of the string const std::string modProxy = "proxy:fcgi://"; if (m_pathTranslated.find(modProxy) == 0) { m_pathTranslated = m_pathTranslated.substr(modProxy.length()); // remove everything before the first / which is host:port int slashPos = m_pathTranslated.find('/'); if (slashPos != String::npos) { m_pathTranslated = m_pathTranslated.substr(slashPos); } } // RequestURI needs path_translated to not include the document root if (!m_pathTranslated.empty()) { if (m_pathTranslated.find(m_documentRoot) == 0) { m_pathTranslated = m_pathTranslated.substr(m_documentRoot.length()); } else { // if the document root isn't in the url set document root to / m_documentRoot = "/"; } } auto* queryString = getRawHeaderPtr(s_queryString); if (queryString != nullptr && !queryString->empty()) { m_serverObject += "?" + *queryString; } m_connection->handleRequest(m_id); }