Example #1
0
void CHTTPSock::ReadLine(const CString& sData) {
	if (m_bGotHeader) {
		return;
	}

	CString sLine = sData;
	sLine.TrimRight("\r\n");

	CString sName = sLine.Token(0);

	if (sName.Equals("GET")) {
		m_bPost = false;
		m_sURI = sLine.Token(1);
		m_bHTTP10Client = sLine.Token(2).Equals("HTTP/1.0");
		ParseURI();
	} else if (sName.Equals("POST")) {
		m_bPost = true;
		m_sURI = sLine.Token(1);
		ParseURI();
	} else if (sName.Equals("Cookie:")) {
		VCString vsNV;

		sLine.Token(1, true).Split(";", vsNV, false, "", "", true, true);

		for (const CString& s : vsNV) {
			m_msRequestCookies[s.Token(0, false, "=").Escape_n(CString::EURL, CString::EASCII)] =
				s.Token(1, true, "=").Escape_n(CString::EURL, CString::EASCII);
		}
	} else if (sName.Equals("Authorization:")) {
		CString sUnhashed;
		sLine.Token(2).Base64Decode(sUnhashed);
		m_sUser = sUnhashed.Token(0, false, ":");
		m_sPass = sUnhashed.Token(1, true, ":");
		m_bBasicAuth = true;
		// Postpone authorization attempt until end of headers, because cookies should be read before that, otherwise session id will be overwritten in GetSession()
	} else if (sName.Equals("Content-Length:")) {
		m_uPostLen = sLine.Token(1).ToULong();
		if (m_uPostLen > MAX_POST_SIZE)
			PrintErrorPage(413, "Request Entity Too Large", "The request you sent was too large.");
	} else if (sName.Equals("X-Forwarded-For:")) {
		// X-Forwarded-For: client, proxy1, proxy2
		if (m_sForwardedIP.empty()) {
			const VCString& vsTrustedProxies = CZNC::Get().GetTrustedProxies();
			CString sIP = GetRemoteIP();

			VCString vsIPs;
			sLine.Token(1, true).Split(",", vsIPs, false, "", "", false, true);

			while (!vsIPs.empty()) {
				// sIP told us that it got connection from vsIPs.back()
				// check if sIP is trusted proxy
				bool bTrusted = false;
				for (const CString& sTrustedProxy : vsTrustedProxies) {
					if (sIP.WildCmp(sTrustedProxy)) {
						bTrusted = true;
						break;
					}
				}
				if (bTrusted) {
					// sIP is trusted proxy, so use vsIPs.back() as new sIP
					sIP = vsIPs.back();
					vsIPs.pop_back();
				} else {
					break;
				}
			}

			// either sIP is not trusted proxy, or it's in the beginning of the X-Forwarded-For list
			// in both cases use it as the endpoind
			m_sForwardedIP = sIP;
		}
	} else if (sName.Equals("If-None-Match:")) {
		// this is for proper client cache support (HTTP 304) on static files:
		m_sIfNoneMatch = sLine.Token(1, true);
	} else if (sName.Equals("Accept-Encoding:") && !m_bHTTP10Client) {
		SCString ssEncodings;
		// trimming whitespace from the tokens is important:
		sLine.Token(1, true).Split(",", ssEncodings, false, "", "", false, true);
		m_bAcceptGzip = (ssEncodings.find("gzip") != ssEncodings.end());
	} else if (sLine.empty()) {
		if (m_bBasicAuth && !m_bLoggedIn) {
			m_bLoggedIn = OnLogin(m_sUser, m_sPass, true);
			// After successful login ReadLine("") will be called again to trigger "else" block
			// Failed login sends error and closes socket, so no infinite loop here
		} else {
			m_bGotHeader = true;

			if (m_bPost) {
				m_sPostData = GetInternalReadBuffer();
				CheckPost();
			} else {
				GetPage();
			}

			DisableReadLine();
		}
	}
}