void GetHeader(PHTTP_FILTER_PREPROC_HEADERS pn, PHTTP_FILTER_CONTEXT pfc, LPSTR lpszName, dynabuf& s, DWORD size=80, bool bRequired=true) throw (bad_alloc, DWORD) { s.erase(); s.reserve(size); size=s.size(); while (!pn->GetHeader(pfc,lpszName,s,&size)) { // Grumble. Check the error. DWORD e=GetLastError(); if (e==ERROR_INSUFFICIENT_BUFFER) s.reserve(size); else break; } if (bRequired && s.empty()) throw ERROR_NO_DATA; }
DWORD PreProcessHeaders(PHTTP_FILTER_CONTEXT pFilterContext, PHTTP_FILTER_PREPROC_HEADERS pHeaderInfo) { char url[INTERNET_MAX_URL_LENGTH]; DWORD dwSize= sizeof(url); if (!pHeaderInfo->GetHeader(pFilterContext, "url", url, &dwSize)) { return SF_STATUS_REQ_ERROR; } LogMe(1, "PreProcessHeaders"); if (url[0]!='\0') { boolean rc; char * resultString; char buf[INTERNET_MAX_URL_LENGTH + 32]; sprintf(buf, "Url is: '%s'", url); LogMe(1, buf); // TODO: Load new initialization for each new vdir ? rc= ApplyRules(url, 0, &resultString); if (rc) { sprintf(buf, "Rewrite Url to: '%s'", resultString); LogMe(1, buf); pHeaderInfo->SetHeader(pFilterContext, "url", resultString); free(resultString); // or not? //return SF_STATUS_REQ_HANDLED_NOTIFICATION; // need this?? } else { LogMe(1, "No Rewrite"); } } return SF_STATUS_REQ_NEXT_NOTIFICATION; }
DWORD CAgentblockerFilter::OnPreprocHeaders(CHttpFilterContext* pCtxt, PHTTP_FILTER_PREPROC_HEADERS pHeaderInfo) { // TODO: React to this notification accordingly and // return the appropriate status code char pUserAgent[ 1024 ]; pUserAgent[0] = 0; DWORD buflen = 1024; pCtxt->GetServerVariable("HTTP_USER_AGENT", pUserAgent, &buflen); char* ptr = pUserAgent; while (*ptr != 0) { if (ptr[0] == ' ') { ptr[0] = '+'; } ptr++; } DWORD dwDebug = 0; DWORD dwBufferSize = sizeof(dwDebug); DWORD KeyType; LONG Result = RegQueryValueEx( m_hkAgents, pUserAgent, NULL, // lpReserved. Reserved; must be NULL. &KeyType, (LPBYTE)dwDebug, // The word value is &dwBufferSize); if (Result == ERROR_SUCCESS) { if (!m_AgentList.IsAgentAllowed(pUserAgent)) { char szBuffer[1024]; wsprintf(szBuffer, "Error: Too many requests have been made during a short time period so you have been blocked."); DWORD dwBuffSize = lstrlen(szBuffer); pCtxt->ServerSupportFunction(SF_REQ_SEND_RESPONSE_HEADER, "503 Service Unavailable", 0, 0); pCtxt->WriteClient(szBuffer, &dwBuffSize, 0); return SF_STATUS_REQ_FINISHED; } } // Get the cookies for the request char pCookies[1024]; pCookies[0] = 0; pCtxt->GetServerVariable("HTTP_COOKIE", pCookies, &buflen); BOOL bCookie = pHeaderInfo->GetHeader(pCtxt->m_pFC, "cookie:", pCookies, &buflen); if (pCookies != NULL && bCookie) { char* pBBCUIDStart = (strstr(pCookies,"BBC-UID=")); if (pBBCUIDStart != NULL) { char pBBCUIDCookie[1024]; memset(pBBCUIDCookie,0,1024); char* pBBCUIDEnd = strchr(pBBCUIDStart,';'); if (pBBCUIDEnd != NULL) { strncpy_s(pBBCUIDCookie,pBBCUIDStart,pBBCUIDEnd-pBBCUIDStart); } else { strcpy_s(pBBCUIDCookie,pBBCUIDStart); } // Check to see if the cookie is allowed LONG Result = RegQueryValueEx( m_hkCookies, pBBCUIDCookie, NULL, // lpReserved. Reserved; must be NULL. &KeyType, (LPBYTE)dwDebug, // The word value is &dwBufferSize); if (Result == ERROR_SUCCESS) { char szBuffer[1024]; wsprintf(szBuffer, "Error: Too many requests have been made during a short time period so you have been blocked."); DWORD dwBuffSize = lstrlen(szBuffer); pCtxt->ServerSupportFunction(SF_REQ_SEND_RESPONSE_HEADER, "403 Forbidden", 0, 0); pCtxt->WriteClient(szBuffer, &dwBuffSize, 0); return SF_STATUS_REQ_FINISHED; } } } return SF_STATUS_REQ_NEXT_NOTIFICATION; }