int request_cookieCount(void) { if (req_cookies == NULL) { parseCookies(); } return req_cookiesCount; }
static Form getCookie(int i) { if (req_cookies == NULL) { parseCookies(); } if (0 <= i && i < req_cookiesCount) { return req_cookies[i]; } return NULL; }
cgicc::CgiEnvironment::CgiEnvironment(CgiInput *input) { // Create a local CgiInput object for us to use // In the vast majority of cases, this will be used // For FastCGI applications it won't but the performance hit of // an empty inline constructor is negligible CgiInput local_input; if(0 == input) readEnvironmentVariables(&local_input); else readEnvironmentVariables(input); // On Win32, use binary read to avoid CRLF conversion #ifdef WIN32 # ifdef __BORLANDC__ setmode(_fileno(stdin), O_BINARY); # else _setmode(_fileno(stdin), _O_BINARY); # endif #endif if(stringsAreEqual(fRequestMethod, "post")) { // Don't use auto_ptr, but vector instead // Bug reported by [email protected] std::vector<char> data(fContentLength); if(getenv("CGICC_MAX_CONTENTLENGTH")&&getContentLength()>atoi(getenv("CGICC_MAX_CONTENTLENGTH"))) { exit(1); } else // If input is 0, use the default implementation of CgiInput if ( getContentLength() ) { // If input is 0, use the default implementation of CgiInput if ( input == 0 ) { if ( local_input.read( &data[0], getContentLength() ) != getContentLength() ) throw std::runtime_error("I/O error"); } else if ( input->read( &data[0], getContentLength() ) != getContentLength() ) throw std::runtime_error("I/O error"); fPostData = std::string( &data[0], getContentLength() ); } } fCookies.reserve(10); parseCookies(); }
void cgicc::CgiEnvironment::restore(const std::string& filename) { std::ifstream file( filename.c_str(), std::ios::binary | std::ios::in ); if( ! file ) throw std::runtime_error("I/O error"); file.flags(file.flags() & std::ios::skipws); fContentLength = readLong(file); fServerPort = readLong(file); fUsingHTTPS = (bool) readLong(file); fServerSoftware = readString(file); fServerName = readString(file); fGatewayInterface = readString(file); fServerProtocol = readString(file); fRequestMethod = readString(file); fPathInfo = readString(file); fPathTranslated = readString(file); fScriptName = readString(file); fQueryString = readString(file); fRemoteHost = readString(file); fRemoteAddr = readString(file); fAuthType = readString(file); fRemoteUser = readString(file); fRemoteIdent = readString(file); fContentType = readString(file); fAccept = readString(file); fUserAgent = readString(file); fRedirectRequest = readString(file); fRedirectURL = readString(file); fRedirectStatus = readString(file); fReferrer = readString(file); fCookie = readString(file); if(stringsAreEqual(fRequestMethod, "post")) fPostData = readString(file); file.close(); fCookies.clear(); fCookies.reserve(10); parseCookies(); }
void Parser::parse(RequestImpl *req, char *env[], Logger* logger) { for (int i = 0; NULL != env[i]; ++i) { logger->debug("env[%d] = %s", i, env[i]); Range key, value; Range::fromChars(env[i]).split('=', key, value); if (COOKIE_RANGE == key) { parseCookies(req, value); addHeader(req, key.trimn(HEADER_RANGE.size(), 0), value.trim()); } else if (CONTENT_TYPE_RANGE == key) { addHeader(req, key, value.trim()); } else if (key.startsWith(HEADER_RANGE)) { addHeader(req, key.trimn(HEADER_RANGE.size(), 0), value.trim()); } else { req->vars_[key.toString()] = value.toString(); } } }
cgicc::CgiEnvironment& cgicc::CgiEnvironment::operator= (const CgiEnvironment& env) { fServerPort = env.fServerPort; fContentLength = env.fContentLength; fUsingHTTPS = env.fUsingHTTPS; fServerSoftware = env.fServerSoftware; fServerName = env.fServerName; fGatewayInterface = env.fGatewayInterface; fServerProtocol = env.fServerProtocol; fRequestMethod = env.fRequestMethod; fPathInfo = env.fPathInfo; fPathTranslated = env.fPathTranslated; fScriptName = env.fScriptName; fQueryString = env.fQueryString; fRemoteHost = env.fRemoteHost; fRemoteAddr = env.fRemoteAddr; fAuthType = env.fAuthType; fRemoteUser = env.fRemoteUser; fRemoteIdent = env.fRemoteIdent; fContentType = env.fContentType; fAccept = env.fAccept; fUserAgent = env.fUserAgent; fPostData = env.fPostData; fRedirectRequest = env.fRedirectRequest; fRedirectURL = env.fRedirectURL; fRedirectStatus = env.fRedirectStatus; fReferrer = env.fReferrer; fCookie = env.fCookie; fCookies.clear(); fCookies.reserve(env.fCookies.size()); parseCookies(); return *this; }
Request *requestNew(char *buff) { Request *request = malloc(sizeof(Request)); char *segment, *bs; request->method = UNKNOWN_METHOD; request->path = NULL; request->uri = NULL; request->queryString = NULL; request->postBody = NULL; request->headers = NULL; request->cookies = NULL; request->account = NULL; // METHOD TOK(buff, " \t"); if (strcmp(segment, "OPTIONS") == 0) request->method = OPTIONS; else if (strcmp(segment, "GET") == 0) request->method = GET; else if (strcmp(segment, "HEAD") == 0) request->method = HEAD; else if (strcmp(segment, "POST") == 0) request->method = POST; else if (strcmp(segment, "PUT") == 0) request->method = PUT; else if (strcmp(segment, "DELETE") == 0) request->method = DELETE; else if (strcmp(segment, "TRACE") == 0) request->method = TRACE; else if (strcmp(segment, "CONNECT") == 0) request->method = CONNECT; else goto fail; // PATH TOK(NULL, " \t"); request->path = bsNew(segment); request->uri = bsNew(segment); if (strchr(request->path, '#') != NULL) goto fail; // VERSION TOK(NULL, "\n"); if (strncmp(segment, "HTTP/1.0", 8) != 0 && strncmp(segment, "HTTP/1.1", 8) != 0) goto fail; // HEADERS request->headers = parseHeaders(segment); // BODY bs = kvFindList(request->headers, "Content-Type"); if (bs != NULL && strncmp(bs, "application/x-www-form-urlencoded", 33) == 0) { segment = strtok(NULL, "\0"); if (segment == NULL) goto fail; request->postBody = parseQS(segment); } // QUERYSTRING segment = strchr(request->path, '?'); if (segment != NULL) { request->uri = bsNewLen(request->path, segment - request->path); request->queryString = parseQS(segment + 1); if (request->queryString == NULL) goto fail; } // COOKIES segment = kvFindList(request->headers, "Cookie"); if (segment != NULL) { request->cookies = parseCookies(segment); if (request->cookies == NULL) goto fail; } return request; fail: requestDel(request); return NULL; }
void WEnvironment::init(const WebRequest& request) { Configuration& conf = session_->controller()->configuration(); queryString_ = request.queryString(); parameters_ = request.getParameterMap(); urlScheme_ = request.urlScheme(); referer_ = request.headerValue("Referer"); accept_ = request.headerValue("Accept"); serverSignature_ = request.envValue("SERVER_SIGNATURE"); serverSoftware_ = request.envValue("SERVER_SOFTWARE"); serverAdmin_ = request.envValue("SERVER_ADMIN"); pathInfo_ = request.pathInfo(); #ifndef WT_TARGET_JAVA sslInfo_ = request.sslInfo(); #endif setUserAgent(request.headerValue("User-Agent")); LOG_INFO("UserAgent: " << userAgent_); /* * Determine server host name */ if (conf.behindReverseProxy()) { /* * Take the last entry in X-Forwarded-Host, assuming that we are only * behind 1 proxy */ std::string forwardedHost = request.headerValue("X-Forwarded-Host"); if (!forwardedHost.empty()) { std::string::size_type i = forwardedHost.rfind(','); if (i == std::string::npos) host_ = forwardedHost; else host_ = forwardedHost.substr(i+1); } else host_ = request.headerValue("Host"); } else host_ = request.headerValue("Host"); if (host_.empty()) { /* * HTTP 1.0 doesn't require it: guess from config */ host_ = request.serverName(); if (!request.serverPort().empty()) host_ += ":" + request.serverPort(); } clientAddress_ = getClientAddress(request, conf); std::string cookie = request.headerValue("Cookie"); doesCookies_ = !cookie.empty(); if (doesCookies_) parseCookies(cookie, cookies_); locale_ = request.parseLocale(); }
void WEnvironment::updateUrlScheme(const WebRequest& request) { urlScheme_ = str(request.urlScheme()); Configuration& conf = session_->controller()->configuration(); #ifndef WT_TARGET_JAVA if (conf.behindReverseProxy() || server()->dedicatedSessionProcess()) { #else if (conf.behindReverseProxy()){ #endif std::string forwardedProto = str(request.headerValue("X-Forwarded-Proto")); if (!forwardedProto.empty()) { std::string::size_type i = forwardedProto.rfind(','); if (i == std::string::npos) urlScheme_ = forwardedProto; else urlScheme_ = forwardedProto.substr(i+1); } } } void WEnvironment::init(const WebRequest& request) { Configuration& conf = session_->controller()->configuration(); queryString_ = request.queryString(); parameters_ = request.getParameterMap(); host_ = str(request.headerValue("Host")); referer_ = str(request.headerValue("Referer")); accept_ = str(request.headerValue("Accept")); serverSignature_ = str(request.envValue("SERVER_SIGNATURE")); serverSoftware_ = str(request.envValue("SERVER_SOFTWARE")); serverAdmin_ = str(request.envValue("SERVER_ADMIN")); pathInfo_ = request.pathInfo(); #ifndef WT_TARGET_JAVA if(!str(request.headerValue("Redirect-Secret")).empty()) session_->controller()->redirectSecret_ = str(request.headerValue("Redirect-Secret")); sslInfo_ = request.sslInfo(); if(!sslInfo_ && !str(request.headerValue("SSL-Client-Certificates")).empty()) { parseSSLInfo(str(request.headerValue("SSL-Client-Certificates"))); } #endif setUserAgent(str(request.headerValue("User-Agent"))); updateUrlScheme(request); LOG_INFO("UserAgent: " << userAgent_); /* * If behind a reverse proxy, use external host, schema as communicated using 'X-Forwarded' * headers. */ #ifndef WT_TARGET_JAVA if (conf.behindReverseProxy() || server()->dedicatedSessionProcess()) { #else if (conf.behindReverseProxy()){ #endif std::string forwardedHost = str(request.headerValue("X-Forwarded-Host")); if (!forwardedHost.empty()) { std::string::size_type i = forwardedHost.rfind(','); if (i == std::string::npos) host_ = forwardedHost; else host_ = forwardedHost.substr(i+1); } } if (host_.empty()) { /* * HTTP 1.0 doesn't require it: guess from config */ host_ = request.serverName(); if (!request.serverPort().empty()) host_ += ":" + request.serverPort(); } clientAddress_ = getClientAddress(request, conf); const char *cookie = request.headerValue("Cookie"); doesCookies_ = cookie; if (cookie) parseCookies(cookie, cookies_); locale_ = request.parseLocale(); } #ifndef WT_TARGET_JAVA void WEnvironment::parseSSLInfo(const std::string& json) { #ifdef WT_WITH_SSL Wt::Json::Object obj; Wt::Json::ParseError error; if(!Wt::Json::parse(Wt::Utils::base64Decode(json), obj, error)) { LOG_ERROR("error while parsing client certificates"); return; } std::string clientCertificatePem = obj["client-certificate"]; X509* cert = Wt::Ssl::readFromPem(clientCertificatePem); if(cert) { Wt::WSslCertificate clientCert = Wt::Ssl::x509ToWSslCertificate(cert); X509_free(cert); Wt::Json::Array arr = obj["client-pem-certification-chain"]; std::vector<Wt::WSslCertificate> clientCertChain; for(unsigned int i = 0; i < arr.size(); ++i ) { clientCertChain.push_back(Wt::Ssl::x509ToWSslCertificate(Wt::Ssl::readFromPem(arr[i]))); } Wt::WValidator::State state = static_cast<Wt::WValidator::State>((int)obj["client-verification-result-state"]); Wt::WString message = obj["client-verification-result-message"]; sslInfo_ = new Wt::WSslInfo(clientCert, clientCertChain, Wt::WValidator::Result(state, message)); } #endif // WT_WITH_SSL }