Randomly request timeout (IIS6.0)

F

felippe

Hi!
I have got IIS 6.0 on Windows 2003 Server. I have installed HTTP
handler (as simple reverse-proxy) - code below. I created virtual
category with this dll and Wilcard application mapping set to
aspnet_isapi.dll

Now everything works fine, but sometimes IIS stops working and return
'Error in /catalog Application. Request timeout...' And than i must
restart IIS (at least application pools).

Thanks for help,
Filip

Code of Handler.cs:

/*
Handler.cs; v 1.1; 2005/10/25; Filip Balicki
Script capture all references to selected in Web.config virtual
directory, and download resources from
selected (also in Web.config) remote host. (so it is work as
proxy)
*/

using System;
using System.Web;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Collections;
using System.Configuration;

namespace Polygon
{

public class Handler : IHttpHandler
{
string host_address
{
get
//Take local address (where this script
is installed) from
web.config
//http://my_domain/virtual_catalog/
{
return
ParseUrl(ConfigurationSettings.AppSettings["local_address"]);
}
}
string dest_adress
{
get
//Take local address (where this script
is installed) from
web.config
//http://my_domain/virtual_catalog/
{
return
ParseUrl(ConfigurationSettings.AppSettings["destination_host"]);
}
}
public void ProcessRequest(HttpContext context)
{

string my_uri = context.Request.Url.ToString();
//construct real uri to remote host
string real_uri = Regex.Replace (my_uri,
host_address,dest_adress,RegexOptions.Multiline |
RegexOptions.IgnoreCase);
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create(real_uri);
request.KeepAlive = false;
request.AllowAutoRedirect = false;
HttpWebResponse response;
try
{
response = (HttpWebResponse)
request.GetResponse();
}
catch(System.Net.WebException we)
{
context.Response.StatusCode = 404;
context.Response.StatusDescription =
"Not Found ("+we.Message+")";
response = (HttpWebResponse)
we.Response;
writeBody(context, response);
return;
}
if (response.StatusCode != HttpStatusCode.OK)
//Found not OK header - probably
redirect
{
context.Response.AddHeader("Location",
response.Headers["Location"]);

context.Response.AddHeader("Content-Type", response.ContentType);
context.Response.StatusCode = (int)
response.StatusCode;
}
else
{

context.Response.AddHeader("Content-Type", response.ContentType);
writeBody (context, response);
}
response.Close();
context.Response.End();

}

public bool IsReusable
{
get
{
return true;
}
}

public string ParseUrl (string url)
{
if ((char)url[url.Length-1]!= '/')
{
url+='/';
}
return url;
}

public void writeBody (HttpContext context,
HttpWebResponse response)
{
Stream receiveStream =
response.GetResponseStream();
if
((response.ContentType.ToLower().IndexOf("html")>=0)

||(response.ContentType.ToLower().IndexOf("javascript")>=0))
//if we get html or javascript use ASCI
mode
{
string body;
StreamReader readStream = new
StreamReader (receiveStream,
Encoding.Default);
body = readStream.ReadToEnd();
context.Response.Write(body);
}
else
{
//the response is not HTML - use binary
mode
byte[] buff = new byte[1024];
int bytes = 0;
while( ( bytes = receiveStream.Read(
buff, 0, 1024 ) ) > 0 )
{
//Write the stream directly to
the client

context.Response.OutputStream.Write (buff, 0, bytes );
}
}
response.Close();
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top