protected override void ThreadFunc(object param)
        {
            try
            {
                // Step 1 : create a stream with header information
                // Step 2 : create a stream from the file
                // Step 3 : create a StreamList
                // Step 4 : create a HTTPResponse object
                // Step 5 : call the Receive function of the response object

                using (FileStream fs = new FileStream(this.CurrentRequest.CurrentUri.LocalPath, FileMode.Open))
                //using (Stream fs = AndroidFileHelper.GetAPKFileStream(this.CurrentRequest.CurrentUri.LocalPath))
                    using (StreamList stream = new StreamList(new System.IO.MemoryStream(), fs))
                    {
                        // This will write to the MemoryStream
                        stream.Write("HTTP/1.1 200 Ok\r\n");
                        stream.Write("Content-Type: application/octet-stream\r\n");
                        stream.Write("Content-Length: " + fs.Length.ToString() + "\r\n");
                        stream.Write("\r\n");

                        stream.Seek(0, System.IO.SeekOrigin.Begin);

                        base.CurrentRequest.Response = new HTTPResponse(base.CurrentRequest, stream, base.CurrentRequest.UseStreaming, false);

                        if (!CurrentRequest.Response.Receive())
                            CurrentRequest.Response = null;
                    }
            }
            catch(Exception ex)
            {
                if (CurrentRequest != null)
                {
                    // Something gone bad, Response must be null!
                    CurrentRequest.Response = null;

                    switch (State)
                    {
                        case HTTPConnectionStates.AbortRequested:
                            CurrentRequest.State = HTTPRequestStates.Aborted;
                            break;
                        case HTTPConnectionStates.TimedOut:
                            CurrentRequest.State = HTTPRequestStates.TimedOut;
                            break;
                        default:
                            CurrentRequest.Exception = ex;
                            CurrentRequest.State = HTTPRequestStates.Error;
                            break;
                    }
                }
            }
            finally
            {
                State = HTTPConnectionStates.Closed;
                if (CurrentRequest.State == HTTPRequestStates.Processing)
                {
                    if (CurrentRequest.Response != null)
                        CurrentRequest.State = HTTPRequestStates.Finished;
                    else
                        CurrentRequest.State = HTTPRequestStates.Error;
                }
            }
        }
Exemplo n.º 2
0
        protected override void ThreadFunc(object param)
        {
            try
            {
                // Step 1 : create a stream with header information
                // Step 2 : create a stream from the file
                // Step 3 : create a StreamList
                // Step 4 : create a HTTPResponse object
                // Step 5 : call the Receive function of the response object

                using (System.IO.Stream fs = HTTPManager.IOService.CreateFileStream(this.CurrentRequest.CurrentUri.LocalPath, FileStreamModes.Open))
                    //using (Stream fs = AndroidFileHelper.GetAPKFileStream(this.CurrentRequest.CurrentUri.LocalPath))
                    using (StreamList stream = new StreamList(new System.IO.MemoryStream(), fs))
                    {
                        // This will write to the MemoryStream
                        stream.Write("HTTP/1.1 200 Ok\r\n");
                        stream.Write("Content-Type: application/octet-stream\r\n");
                        stream.Write("Content-Length: " + fs.Length.ToString() + "\r\n");
                        stream.Write("\r\n");

                        stream.Seek(0, System.IO.SeekOrigin.Begin);

                        base.CurrentRequest.Response = new HTTPResponse(base.CurrentRequest, stream, base.CurrentRequest.UseStreaming, false);

                        if (!CurrentRequest.Response.Receive())
                        {
                            CurrentRequest.Response = null;
                        }
                    }
            }
            catch (Exception ex)
            {
                if (CurrentRequest != null)
                {
                    // Something gone bad, Response must be null!
                    CurrentRequest.Response = null;

                    switch (State)
                    {
                    case HTTPConnectionStates.AbortRequested:
                        CurrentRequest.State = HTTPRequestStates.Aborted;
                        break;

                    case HTTPConnectionStates.TimedOut:
                        CurrentRequest.State = HTTPRequestStates.TimedOut;
                        break;

                    default:
                        CurrentRequest.Exception = ex;
                        CurrentRequest.State     = HTTPRequestStates.Error;
                        break;
                    }
                }
            }
            finally
            {
                State = HTTPConnectionStates.Closed;
                if (CurrentRequest.State == HTTPRequestStates.Processing)
                {
                    if (CurrentRequest.Response != null)
                    {
                        CurrentRequest.State = HTTPRequestStates.Finished;
                    }
                    else
                    {
                        CurrentRequest.State = HTTPRequestStates.Error;
                    }
                }
            }
        }
Exemplo n.º 3
0
        protected override void ThreadFunc(object param)
        {
            try
            {
                using (FileStream stream = new FileStream(base.CurrentRequest.CurrentUri.LocalPath, FileMode.Open, FileAccess.Read))
                {
                    Stream[] streams = new Stream[] { new MemoryStream(), stream };
                    using (StreamList list = new StreamList(streams))
                    {
                        list.Write("HTTP/1.1 200 Ok\r\n");
                        list.Write("Content-Type: application/octet-stream\r\n");
                        list.Write("Content-Length: " + stream.Length.ToString() + "\r\n");
                        list.Write("\r\n");
                        list.Seek(0L, SeekOrigin.Begin);
                        base.CurrentRequest.Response = new HTTPResponse(base.CurrentRequest, list, base.CurrentRequest.UseStreaming, false);
                        if (!base.CurrentRequest.Response.Receive(-1, true))
                        {
                            base.CurrentRequest.Response = null;
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                if (base.CurrentRequest != null)
                {
                    base.CurrentRequest.Response = null;
                    HTTPConnectionStates state = base.State;
                    if (state != HTTPConnectionStates.AbortRequested)
                    {
                        if (state == HTTPConnectionStates.TimedOut)
                        {
                            goto Label_0139;
                        }
                        goto Label_014A;
                    }
                    base.CurrentRequest.State = HTTPRequestStates.Aborted;
                }
                return;

Label_0139:
                base.CurrentRequest.State = HTTPRequestStates.TimedOut;
                return;

Label_014A:
                base.CurrentRequest.Exception = exception;
                base.CurrentRequest.State     = HTTPRequestStates.Error;
            }
            finally
            {
                base.State = HTTPConnectionStates.Closed;
                if (base.CurrentRequest.State == HTTPRequestStates.Processing)
                {
                    if (base.CurrentRequest.Response != null)
                    {
                        base.CurrentRequest.State = HTTPRequestStates.Finished;
                    }
                    else
                    {
                        base.CurrentRequest.State = HTTPRequestStates.Error;
                    }
                }
            }
        }