After IIS6 enables gzip, it first compresses the static files to be compressed and stores them in a directory. When requested, it first determines whether Gzip is supported and does not support direct transmission of static files, if yes, you can determine whether the file is modified. If no, the compressed file is directly sent. If yes, the compressed file is regenerated.
According to my observations on multiple websites of the company, the visitor's browser supports more than 99% of gzip files. I just want to save more static files and directly Save the compressed files, it saves both space and processing. If you encounter a visitor of less than 1%, you can relieve the pressure on him. Good! In this case, a special suffix. ghtml is used for the compressed HTML.
Generate ghtml:
First, IIS registers the. ghtml file and submits it to. Net for processing.
Then, use this function to process the aspx file that needs to generate ghtml, that is, to generate a static file, and then compress it further.
/** // <Summary>
/// Generate compressed HTML under the HTML directory
/// </Summary>
/// <Param name = "aspxpath"> dynamic page Request Path </param>
/// <Param name = "urlpath"> Static Page Request Path </param>
Public static void aspxtohtml (string aspxpath, string urlpath)
{
String filepath = httpcontext. Current. server. mappath (urlpath );
If (! Directory. exists (filepath ))
{
Directory. createdirectory (filepath. substring (0, filepath. lastindexof ("")));
}
Using (filestream FS = new filestream (filepath, filemode. Create ))
{
Using (gzipstream Gz = new gzipstream (FS, compressionmode. Compress ))
{
Using (streamwriter Sw = new streamwriter (GZ, encoding. utf8 ))
{
Httpcontext. Current. server. Execute (aspxpath, SW, false );
Sw. Flush ();
}
}
}
}
To process ghtml requests, the browser supports gzip to directly write files; otherwise, extract the content before outputting:
Write an httpmodule and process the. ghtml request in the beginrequest event. The static page simulates the processing of HTML 304.
Public class freemodule: ihttpmodule
{
// The init method is only used to register the method for the expected event
Public void Init (httpapplication context)
{
Context. beginrequest + = new eventhandler (context_beginrequest );
}
Private void setclientcaching (httpresponse response, datetime lastmodified)
{
Response. cache. setetag (lastmodified. ticks. tostring ());
Response. cache. setlastmodified (lastmodified );
Response. cache. setcacheability (httpcacheability. Public );
}
// Actual handling of the beginrequest eventCode
Void context_beginrequest (Object sender, eventargs E)
{
Httpapplication application = (httpapplication) sender;
Httpcontext context = application. context;
Httprequest request = context. request;
Httpresponse response = context. response;
String Path = context. Request. Path. tolower ();
String acceptencoding = request. headers ["Accept-encoding"];
Bool accept =! String. isnullorempty (acceptencoding )? Acceptencoding. tolower (). Contains ("gzip"): false;
If (path. Contains (". ghtml "))
{
String filepath = request. physicalapplicationpath + "/html" + path;
If (! File. exists (filepath ))
{Throw new filenotfoundexception ("file ghtml not found ");}
Datetime writetime = file. getlastwritetimeutc (filepath );
Datetime since;
If (datetime. tryparse (request. headers ["ifmodifiedsince"], out since) & writetime = since. touniversaltime ())
{
Response. statuscode = 304;
Response. statusdescription = "not modified ";
}
Else
{
If (accept)
{
Response. appendheader ("content-encoding", "gzip ");
Response. transmitfile (filepath );
}
Else
{
Response. Write (deziptext (filepath); // decompress the ghtml File
}
Setclientcaching (response, writetime );
Response. End ();
}
}
}
Public void dispose ()
{
}
}
Decompress ghtml:
Finally, there is a ghtml function to be decompressed.
/** // <Summary>
/// Extract the text file and return Str
/// </Summary>
/// <Param name = "textpath"> physical path </param>
/// <Returns> file string </returns>
Public static string deziptext (string textpath)
{
Using (filestream FS = file. openread (textpath ))
{
Gzipstream Gz = new gzipstream (FS, compressionmode. Decompress );
Streamreader sr = new streamreader (GZ );
Return Sr. readtoend ();
}
}