Ejemplo n.º 1
0
 public void Close()
 {
     if (closed)
     {
         return;
     }
     if (WorkerRequest != null)
     {
         WorkerRequest.CloseConnection();
     }
     closed = true;
 }
Ejemplo n.º 2
0
        //
        // This is called from the QueueManager if a request
        // can not be processed (load, no resources, or
        // appdomain unload).
        //
        static internal void FinishUnavailable(HttpWorkerRequest wr)
        {
            wr.SendStatus(503, "Service unavailable");
            wr.SendUnknownResponseHeader("Connection", "close");
            Encoding enc = Encoding.ASCII;

            wr.SendUnknownResponseHeader("Content-Type", "text/html; charset=" + enc.WebName);
            byte [] contentBytes = enc.GetBytes(content503);
            wr.SendUnknownResponseHeader("Content-Length", contentBytes.Length.ToString());
            wr.SendResponseFromMemory(contentBytes, contentBytes.Length);
            wr.FlushResponse(true);
            wr.CloseConnection();
            HttpApplication.requests_total_counter.Increment();
        }
Ejemplo n.º 3
0
		static void Redirect (HttpWorkerRequest wr, string location)
		{
			string host = wr.GetKnownRequestHeader (HttpWorkerRequest.HeaderHost);
			wr.SendStatus (301, "Moved Permanently");
			wr.SendUnknownResponseHeader ("Connection", "close");
			wr.SendUnknownResponseHeader ("Date", DateTime.Now.ToUniversalTime ().ToString ("r"));
			wr.SendUnknownResponseHeader ("Location", String.Format ("{0}://{1}{2}", wr.GetProtocol(), host, location));
			Encoding enc = Encoding.ASCII;
			wr.SendUnknownResponseHeader ("Content-Type", "text/html; charset=" + enc.WebName);
			string content = String.Format (CONTENT301, host, location);
			byte [] contentBytes = enc.GetBytes (content);
			wr.SendUnknownResponseHeader ("Content-Length", contentBytes.Length.ToString ());
			wr.SendResponseFromMemory (contentBytes, contentBytes.Length);
			wr.FlushResponse (true);
			wr.CloseConnection ();
		}
Ejemplo n.º 4
0
        static void FinishWithException(HttpWorkerRequest wr, HttpException e)
        {
            int code = e.GetHttpCode();

            wr.SendStatus(code, HttpWorkerRequest.GetStatusDescription(code));
            wr.SendUnknownResponseHeader("Connection", "close");
            Encoding enc = Encoding.ASCII;

            wr.SendUnknownResponseHeader("Content-Type", "text/html; charset=" + enc.WebName);
            string msg = e.GetHtmlErrorMessage();

            byte [] contentBytes = enc.GetBytes(msg);
            wr.SendUnknownResponseHeader("Content-Length", contentBytes.Length.ToString());
            wr.SendResponseFromMemory(contentBytes, contentBytes.Length);
            wr.FlushResponse(true);
            wr.CloseConnection();
            HttpApplication.requests_total_counter.Increment();
        }
Ejemplo n.º 5
0
        private void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication app     = sender as HttpApplication;
            HttpContext     context = app.Context;


            // We need the HttpWorkerRequest of the current context to
            // process the request data. For more details about HttpWorkerRequest,
            // please follow the Readme file in the root directory.
            IServiceProvider provider = (IServiceProvider)context;

            System.Web.HttpWorkerRequest request =
                (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));

            // Get the content type of the current request.
            string contentType =
                request.GetKnownRequestHeader(
                    System.Web.HttpWorkerRequest.HeaderContentType);

            // If we could not get the content type, then skip out the module
            if (contentType == null)
            {
                return;
            }
            // If the content type is not multipart/form-data,
            //   means that there is no file upload request
            //   then skip out the moudle
            if (contentType.IndexOf("multipart/form-data") == -1)
            {
                return;
            }
            string boundary = contentType.Substring(contentType.IndexOf("boundary=") + 9);
            // Get the content length of the current request
            long contentLength = Convert.ToInt64(
                request.GetKnownRequestHeader(
                    HttpWorkerRequest.HeaderContentLength));

            // Get the data of the portion of the HTTP request body
            // that has currently been read.
            // This is the first step for us to store the upload file.
            byte[] data = request.GetPreloadedEntityBody();

            // Create an instance of the manager class which
            // help to filter the request data.
            FileUploadDataManager storeManager =
                new FileUploadDataManager(boundary);

            // Append the preloaded data.
            storeManager.AppendData(data);

            UploadStatus status = null;

            if (context.Cache[_cacheContainer] == null)
            {
                //Initialize the UploadStatus which used to
                //store the status for the client.
                status = new UploadStatus(
                    context,         // Send the current context to the status
                    // which will be used for the events.
                    contentLength    // Initialize the file length.
                    );
                // Bind a event when update the status.
                status.OnDataChanged +=
                    new UploadStatusEventHandler(status_OnDataChanged);
            }
            else
            {
                status = context.Cache[_cacheContainer] as UploadStatus;
                if (status.IsFinished)
                {
                    return;
                }
            }

            // Set the first read data length to the status class.
            if (data != null)
            {
                status.UpdateLoadedLength(data.Length);
            }

            // Get the length of the left request data.
            long leftdata = status.ContentLength - status.LoadedLength;

            // Define a custom buffer length
            int customBufferLength = Convert.ToInt32(Math.Ceiling((double)contentLength / 16));

            if (customBufferLength < 1024)
            {
                customBufferLength = 1024;
            }
            while (!request.IsEntireEntityBodyIsPreloaded() && leftdata > 0)
            {
                // Check if user abort the upload, then close the connection
                if (status.Aborted)
                {
                    // Delete the cached files.
                    foreach (UploadFile file in storeManager.FilterResult)
                    {
                        file.ClearCache();
                    }
                    request.CloseConnection();
                    return;
                }

                // If the length the remained request data
                // is less than the buffer length,
                // then set the buffer length as the remained data length.
                if (leftdata < customBufferLength)
                {
                    customBufferLength = (int)leftdata;
                }

                // Read a custom buffer length of the request data
                data = new byte[customBufferLength];
                int redlen = request.ReadEntityBody(data, customBufferLength);
                if (customBufferLength > redlen)
                {
                    data = BinaryHelper.SubData(data, 0, redlen);
                }
                // Append the left data.
                storeManager.AppendData(data);

                // Add the buffer length to the status to update the upload status
                status.UpdateLoadedLength(redlen);

                leftdata -= redlen;
            }

            // After all the data has been read,
            // save the uploaded files.
            foreach (UploadFile file in storeManager.FilterResult)
            {
                file.Save(null);
            }
        }
Ejemplo n.º 6
0
		//
		// This is called from the QueueManager if a request
		// can not be processed (load, no resources, or
		// appdomain unload).
		//
		static internal void FinishUnavailable (HttpWorkerRequest wr)
		{
			wr.SendStatus (503, "Service unavailable");
			wr.SendUnknownResponseHeader ("Connection", "close");
			Encoding enc = Encoding.ASCII;
			wr.SendUnknownResponseHeader ("Content-Type", "text/html; charset=" + enc.WebName);
			byte [] contentBytes = enc.GetBytes (content503);
			wr.SendUnknownResponseHeader ("Content-Length", contentBytes.Length.ToString ());
			wr.SendResponseFromMemory (contentBytes, contentBytes.Length);
			wr.FlushResponse (true);
			wr.CloseConnection ();
			HttpApplication.requests_total_counter.Increment ();
		}
Ejemplo n.º 7
0
		static void FinishWithException (HttpWorkerRequest wr, HttpException e)
		{
			int code = e.GetHttpCode ();
			wr.SendStatus (code, HttpWorkerRequest.GetStatusDescription (code));
			wr.SendUnknownResponseHeader ("Connection", "close");
			Encoding enc = Encoding.ASCII;
			wr.SendUnknownResponseHeader ("Content-Type", "text/html; charset=" + enc.WebName);
			string msg = e.GetHtmlErrorMessage ();
			byte [] contentBytes = enc.GetBytes (msg);
			wr.SendUnknownResponseHeader ("Content-Length", contentBytes.Length.ToString ());
			wr.SendResponseFromMemory (contentBytes, contentBytes.Length);
			wr.FlushResponse (true);
			wr.CloseConnection ();
			HttpApplication.requests_total_counter.Increment ();
		}
Ejemplo n.º 8
0
        private void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication app     = sender as HttpApplication;
            HttpContext     context = app.Context;


            // 我们需要HttpWorkerRequest的当前内容来处理请求数据.
            // 要想知道HttpWorkerRequest更多更详细的内容,
            // 请参考根目录下的Readme文件.
            IServiceProvider provider = (IServiceProvider)context;

            System.Web.HttpWorkerRequest request =
                (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));

            // 获取当前请求的内容类型.
            string contentType =
                request.GetKnownRequestHeader(
                    System.Web.HttpWorkerRequest.HeaderContentType);

            // 如果我们不能获取内容类型,跳过这个模块.
            if (contentType == null)
            {
                return;
            }
            //   如果内容类型不是multipart/form-data,
            //   意味着没有上传请求,
            //   就可以跳过这个模块.
            if (contentType.IndexOf("multipart/form-data") == -1)
            {
                return;
            }
            string boundary = contentType.Substring(contentType.IndexOf("boundary=") + 9);
            // 获取当前请求的内容长度.
            long contentLength = Convert.ToInt64(
                request.GetKnownRequestHeader(
                    HttpWorkerRequest.HeaderContentLength));

            // 获取HTTP请求主体的那些
            // 当前已经被读取的数据.
            // 这是我们存储上传文件的第一步.
            byte[] data = request.GetPreloadedEntityBody();

            // 创建一个管理类的实例可以
            // 帮助过滤请求数据.
            FileUploadDataManager storeManager =
                new FileUploadDataManager(boundary);

            // 添加预装载的数据.
            storeManager.AppendData(data);

            UploadStatus status = null;

            if (context.Cache[_cacheContainer] == null)
            {
                //初始化UploadStatus,
                //它被用来存储客户状态.
                status = new UploadStatus(
                    context,         //  把当前内容发送到status被事件使用
                                     //
                    contentLength    // 初始化文件长度.
                    );
                // 当更新状态时绑定事件.
                status.OnDataChanged +=
                    new UploadStatusEventHandler(status_OnDataChanged);
            }
            else
            {
                status = context.Cache[_cacheContainer] as UploadStatus;
                if (status.IsFinished)
                {
                    return;
                }
            }

            // 把首先读到的数据长度设置到status class.
            if (data != null)
            {
                status.UpdateLoadedLength(data.Length);
            }

            // 获取留下的请求数据的长度.
            long leftdata = status.ContentLength - status.LoadedLength;

            // 定义一个自定义的缓存区的长度
            int customBufferLength = Convert.ToInt32(Math.Ceiling((double)contentLength / 16));

            if (customBufferLength < 1024)
            {
                customBufferLength = 1024;
            }
            while (!request.IsEntireEntityBodyIsPreloaded() && leftdata > 0)
            {
                // 检查用户如果终止了上传,关闭连接.
                if (status.Aborted)
                {
                    // 删除缓存文件.
                    foreach (UploadFile file in storeManager.FilterResult)
                    {
                        file.ClearCache();
                    }
                    request.CloseConnection();
                    return;
                }

                // 如果剩下的请求数据小于缓
                // 冲区的长度,把缓冲区的
                // 长度设置成剩余数据的长度.
                if (leftdata < customBufferLength)
                {
                    customBufferLength = (int)leftdata;
                }

                // 读取自定义缓冲区的长度的请求数据
                data = new byte[customBufferLength];
                int redlen = request.ReadEntityBody(data, customBufferLength);
                if (customBufferLength > redlen)
                {
                    data = BinaryHelper.SubData(data, 0, redlen);
                }
                // 添加剩余数据.
                storeManager.AppendData(data);

                // 把缓冲区的长度添加到status来更新上传status.
                status.UpdateLoadedLength(redlen);

                leftdata -= redlen;
            }

            // 当所有的数据都被读取之后,
            // 保存上传文件.
            foreach (UploadFile file in storeManager.FilterResult)
            {
                file.Save(null);
            }
        }