K
kl
Hi,
I've got a strange problem... I have a form that contains an input file
element and that uploads a file to the server.
Then a custom httphandler "intercept" the request and save all the datas
in a directory.
All works fine except when I enable authentication on a subdir.... the
handler reads the first 48k bytes and then hang... it blocks on
"readentitybody"... and I don't know why... here's the sample codes...
I you have any idea .... thanks
Karim
-----> webconfig <--------------
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- root path access ALL -->
<system.web>
<!-- max pos file size 16 Mo 16384-->
<httpRuntime maxRequestLength="300000" executionTimeout="1200"/>
<!-- UPLOAD HANDLER -->
<httpHandlers>
<add verb="POST" path="*"
type="LBC.Upload.UploadHandler,LBC_LIB" />
</httpHandlers>
<compilation defaultLanguage="c#" debug="true" />
<customErrors mode="RemoteOnly" />
<authentication mode="Forms" >
<forms name="PPLIBFORMSAUTH" loginUrl="protect/login.aspx"
protection="All" path="/" ></forms>
</authentication>
<authorization>
<allow users ="*" />
</authorization>
</system.web>
<!-- protect path access SECURE -->
<location path="protect">
<system.web>
<authorization>
<deny users ="?" />
</authorization>
</system.web>
</location>
</configuration>
--> UPloadHandler.cs <---
using System;
using System.Web;
using System.IO;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Collections;
namespace LBC.Upload
{
public class UploadHandler : IHttpHandler
{
private static UpLoadConfig config;
protected UploadManager prog;
public UploadHandler()
{
// Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
}
public bool IsReusable
{
get {return false;}
}
public void ProcessRequest(HttpContext context)
{
bool error=false;
string query="";
try
{
BindingFlags bindingFlags =
BindingFlags.Instance|BindingFlags.Public|BindingFlags.NonPublic;
HttpWorkerRequest request = (HttpWorkerRequest)
context.GetType().GetProperty("WorkerRequest",
bindingFlags).GetValue(context, null);
//Check to see if this is a multipart upload
string sContentType =
context.Request.ContentType.ToLower();
if ( sContentType.IndexOf("multipart/form-data") == -1 ) return;
//Getting Size
int content_length = context.Request.ContentLength;
// Check if a request body is sent by the browser
if(request.HasEntityBody())
{
//
int content_received = 0;
// Get the preloaded buffered data
byte[] body = request.GetPreloadedEntityBody();
content_received += body.Length;
Thread.Sleep(100);
// Write the preloaded data to new file
// Create a file to store the stream
FileStream newFile = new FileStream("c:\requests\test.dat",
FileMode.Create);
newFile.Write(body, 0, body.Length);
// Get all the other data to be written to file if available
byte[] a_buffer = new byte[16384];
int bytes_read = 16384;
if(!request.IsEntireEntityBodyIsPreloaded())
{
int nBytesRemaining = content_length - (int) body.Length;
// Create an input buffer to store the incomming data
while((content_length - content_received) >= bytes_read)
{
// --------> ERROR BELOW <--------------------
bytes_read = request.ReadEntityBody(a_buffer,
a_buffer.Length);
if (bytes_read<=0)
{
error=true;
break;
}
// -----------------------------------------
content_received += bytes_read;
newFile.Write(a_buffer, 0, bytes_read);
Thread.Sleep(0);
}
// Read the last part of the stream
if (!error)
bytes_read = request.ReadEntityBody(a_buffer, (content_length -
content_received));
newFile.Write(a_buffer, 0, bytes_read);
content_received += bytes_read;
Thread.Sleep(0);
}
//prog.Remove(guid);
newFile.Flush();
newFile.Close();
}
}
catch(System.Threading.ThreadAbortException)
{}
catch(Exception ex)
{
string errMessage="";
for( Exception tempException = ex; tempException != null ;
tempException = tempException.InnerException )
{
errMessage += tempException.Message + Environment.NewLine +
Environment.NewLine;
}
throw new Exception(string.Format( "Error : {0}"+
Environment.NewLine, errMessage ));
//context.Response.Redirect(config.ErrorPage+query+"&mess="+context.Server.UrlEncode(errMessage));
}
}
}
}
I've got a strange problem... I have a form that contains an input file
element and that uploads a file to the server.
Then a custom httphandler "intercept" the request and save all the datas
in a directory.
All works fine except when I enable authentication on a subdir.... the
handler reads the first 48k bytes and then hang... it blocks on
"readentitybody"... and I don't know why... here's the sample codes...
I you have any idea .... thanks
Karim
-----> webconfig <--------------
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- root path access ALL -->
<system.web>
<!-- max pos file size 16 Mo 16384-->
<httpRuntime maxRequestLength="300000" executionTimeout="1200"/>
<!-- UPLOAD HANDLER -->
<httpHandlers>
<add verb="POST" path="*"
type="LBC.Upload.UploadHandler,LBC_LIB" />
</httpHandlers>
<compilation defaultLanguage="c#" debug="true" />
<customErrors mode="RemoteOnly" />
<authentication mode="Forms" >
<forms name="PPLIBFORMSAUTH" loginUrl="protect/login.aspx"
protection="All" path="/" ></forms>
</authentication>
<authorization>
<allow users ="*" />
</authorization>
</system.web>
<!-- protect path access SECURE -->
<location path="protect">
<system.web>
<authorization>
<deny users ="?" />
</authorization>
</system.web>
</location>
</configuration>
--> UPloadHandler.cs <---
using System;
using System.Web;
using System.IO;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Collections;
namespace LBC.Upload
{
public class UploadHandler : IHttpHandler
{
private static UpLoadConfig config;
protected UploadManager prog;
public UploadHandler()
{
// Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
}
public bool IsReusable
{
get {return false;}
}
public void ProcessRequest(HttpContext context)
{
bool error=false;
string query="";
try
{
BindingFlags bindingFlags =
BindingFlags.Instance|BindingFlags.Public|BindingFlags.NonPublic;
HttpWorkerRequest request = (HttpWorkerRequest)
context.GetType().GetProperty("WorkerRequest",
bindingFlags).GetValue(context, null);
//Check to see if this is a multipart upload
string sContentType =
context.Request.ContentType.ToLower();
if ( sContentType.IndexOf("multipart/form-data") == -1 ) return;
//Getting Size
int content_length = context.Request.ContentLength;
// Check if a request body is sent by the browser
if(request.HasEntityBody())
{
//
int content_received = 0;
// Get the preloaded buffered data
byte[] body = request.GetPreloadedEntityBody();
content_received += body.Length;
Thread.Sleep(100);
// Write the preloaded data to new file
// Create a file to store the stream
FileStream newFile = new FileStream("c:\requests\test.dat",
FileMode.Create);
newFile.Write(body, 0, body.Length);
// Get all the other data to be written to file if available
byte[] a_buffer = new byte[16384];
int bytes_read = 16384;
if(!request.IsEntireEntityBodyIsPreloaded())
{
int nBytesRemaining = content_length - (int) body.Length;
// Create an input buffer to store the incomming data
while((content_length - content_received) >= bytes_read)
{
// --------> ERROR BELOW <--------------------
bytes_read = request.ReadEntityBody(a_buffer,
a_buffer.Length);
if (bytes_read<=0)
{
error=true;
break;
}
// -----------------------------------------
content_received += bytes_read;
newFile.Write(a_buffer, 0, bytes_read);
Thread.Sleep(0);
}
// Read the last part of the stream
if (!error)
bytes_read = request.ReadEntityBody(a_buffer, (content_length -
content_received));
newFile.Write(a_buffer, 0, bytes_read);
content_received += bytes_read;
Thread.Sleep(0);
}
//prog.Remove(guid);
newFile.Flush();
newFile.Close();
}
}
catch(System.Threading.ThreadAbortException)
{}
catch(Exception ex)
{
string errMessage="";
for( Exception tempException = ex; tempException != null ;
tempException = tempException.InnerException )
{
errMessage += tempException.Message + Environment.NewLine +
Environment.NewLine;
}
throw new Exception(string.Format( "Error : {0}"+
Environment.NewLine, errMessage ));
//context.Response.Redirect(config.ErrorPage+query+"&mess="+context.Server.UrlEncode(errMessage));
}
}
}
}