agent/browser/ie/pagetest/PagetestReporting.cpp
/*-----------------------------------------------------------------------------
	Check each text element to make sure it was gzip encoded
-----------------------------------------------------------------------------*/
Check each text element to make sure it was gzip encoded
Check each text element to make sure it was gzip encoded
void CPagetestReporting::CheckGzip()
{
	gzipScore = -1;
	int count = 0;
	int total = 0;
	DWORD totalBytes = 0;
	DWORD targetBytes = 0;
	ATLTRACE(_T("[Pagetest] - CheckGzip\n"));
	POSITION pos = events.GetHeadPosition();
	while( pos )
	{
		CTrackedEvent * e = events.GetNext(pos);
		CWinInetRequest * w = (CWinInetRequest *)e;
		if( e && 
        e->type == CTrackedEvent::etWinInetRequest && 
        (!e->ignore || !w->object.Right(11).CompareNoCase(_T("favicon.ico"))) )
		{
			CString mime = w->response.contentType;
			mime.MakeLower();
			if( w->result == 200
				&& w->fromNet )
			{
				CString enc = w->response.contentEncoding;
				enc.MakeLower();
				w->gzipScore = 0;
				DWORD target = w->in;
				if( enc.Find(_T("gzip")) >= 0 || enc.Find(_T("deflate")) >= 0 )
					w->gzipScore = 100;
				else if( w->in < 1400 )	
					w->gzipScore = -1;
				if( !w->gzipScore )
				{
					LPBYTE body = w->body;
					DWORD bodyLen = w->bodyLen;
          if ((bodyLen > 3 &&             
               body[0] == 0xFF &&
               body[1] == 0xD8 &&
               body[2] == 0xFF) ||
              (bodyLen > 8 &&             
               body[0] == 0x89 &&
               body[1] == 0x50 &&
               body[2] == 0x4E &&
               body[3] == 0x47 &&
               body[4] == 0x0D &&
               body[5] == 0x0A &&
               body[6] == 0x1A &&
               body[7] == 0x0A) ||
              (bodyLen > 6 &&             
               body[0] == 0x47 &&
               body[1] == 0x49 &&
               body[2] == 0x46 &&
               body[3] == 0x38 &&
               body[5] == 0x61)) {
            w->gzipScore = -1;
          } else {
					  DWORD origSize = w->in;
					  DWORD origLen = bodyLen;
					  DWORD headSize = w->inHeaders.GetLength();
					  if( origLen && body )
					  {
						  DWORD len = compressBound(origLen);
						  if( len )
						  {
							  LPBYTE buff = (LPBYTE)malloc(len);
							  if( buff )
							  {
								  if( compress2(buff, &len, body, origLen, 7) == Z_OK )
									  target = len + headSize;
								  free(buff);
							  }
						  }
						  if( target < (origSize * 0.9) && origSize - target > 1400 )
							  w->warning = true;
						  else
						  {
							  target = origSize;
							  w->gzipScore = -1;
						  }
            } else {
						  target = origSize;
						  w->gzipScore = -1;
            }
					}
				}
				if( w->gzipScore != -1 )
				{
					count++;
				  w->gzipTotal = w->in;
				  w->gzipTarget = target;
				  targetBytes += target;
					total += w->gzipScore;
				  totalBytes += w->in;
				}
			}
		}
	}
	gzipTotal = totalBytes;
	gzipTarget = targetBytes;
	if( count && totalBytes )
		gzipScore = targetBytes * 100 / totalBytes;
}
void CPagetestReporting :: CheckGzip ( ) { gzipScore = -1 ; int count = 0 ; int total = 0 ; DWORD totalBytes = 0 ; DWORD targetBytes = 0 ; ATLTRACE ( _T ( " \n " ) ) ; POSITION pos = events . GetHeadPosition ( ) ; while ( pos ) { CTrackedEvent * e = events . GetNext ( pos ) ; CWinInetRequest * w = ( CWinInetRequest * ) e ; if ( e && e -> type == CTrackedEvent :: etWinInetRequest && ( ! e -> ignore || ! w -> object . Right ( 11 ) . CompareNoCase ( _T ( " " ) ) ) ) { CString mime = w -> response . contentType ; mime . MakeLower ( ) ; if ( w -> result == 200 && w -> fromNet ) { CString enc = w -> response . contentEncoding ; enc . MakeLower ( ) ; w -> gzipScore = 0 ; DWORD target = w -> in ; if ( enc . Find ( _T ( " " ) ) >= 0 || enc . Find ( _T ( " " ) ) >= 0 ) w -> gzipScore = 100 ; else if ( w -> in < 1400 ) w -> gzipScore = -1 ; if ( ! w -> gzipScore ) { LPBYTE body = w -> body ; DWORD bodyLen = w -> bodyLen ; if ( ( bodyLen > 3 && body [ 0 ] == 0xFF && body [ 1 ] == 0xD8 && body [ 2 ] == 0xFF ) || ( bodyLen > 8 && body [ 0 ] == 0x89 && body [ 1 ] == 0x50 && body [ 2 ] == 0x4E && body [ 3 ] == 0x47 && body [ 4 ] == 0x0D && body [ 5 ] == 0x0A && body [ 6 ] == 0x1A && body [ 7 ] == 0x0A ) || ( bodyLen > 6 && body [ 0 ] == 0x47 && body [ 1 ] == 0x49 && body [ 2 ] == 0x46 && body [ 3 ] == 0x38 && body [ 5 ] == 0x61 ) ) { w -> gzipScore = -1 ; } else { DWORD origSize = w -> in ; DWORD origLen = bodyLen ; DWORD headSize = w -> inHeaders . GetLength ( ) ; if ( origLen && body ) { DWORD len = compressBound ( origLen ) ; if ( len ) { LPBYTE buff = ( LPBYTE ) malloc ( len ) ; if ( buff ) { if ( compress2 ( buff , & len , body , origLen , 7 ) == Z_OK ) target = len + headSize ; free ( buff ) ; } } if ( target < ( origSize * 0.9 ) && origSize - target > 1400 ) w -> warning = true ; else { target = origSize ; w -> gzipScore = -1 ; } } else { target = origSize ; w -> gzipScore = -1 ; } } } if ( w -> gzipScore != -1 ) { count ++ ; w -> gzipTotal = w -> in ; w -> gzipTarget = target ; targetBytes += target ; total += w -> gzipScore ; totalBytes += w -> in ; } } } } gzipTotal = totalBytes ; gzipTarget = targetBytes ; if ( count && totalBytes ) gzipScore = targetBytes * 100 / totalBytes ; }
Check each text element to make sure it was gzip encoded
Check each text element to make sure it was gzip encoded
// if it's less than 1 packet anyway, give it a pass
 // don't try gzip for known image formats that shouldn't be gzipped
 // JPEG FF D8 FF
 // PNG 89 50 4E 47 0D 0A 1A 0A
 // Gif 47 49 46 38 37(9) 61
 // try gzipping the item to see how much smaller it will be
 // average the gzip scores of all of the objects for the page
