Пример #1
0
        public MimeUploadHandler(UploadHttpRequest httpRequest, UploadRequest request)
        {
            //Current = this;

            _httpRequest = httpRequest;
            _request     = request;

            _boundary = ExtractBoundary(_httpRequest.ContentType, _httpRequest.ContentEncoding);

            _requestStream = new RequestStream(_httpRequest.Worker);
        }
Пример #2
0
        public MimeUploadHandler(UploadHttpRequest httpRequest, UploadRequest request)
        {
            //Current = this;

            _httpRequest = httpRequest;
            _request = request;

            _boundary = ExtractBoundary(_httpRequest.ContentType, _httpRequest.ContentEncoding);

            _requestStream = new RequestStream(_httpRequest.Worker);
        }
Пример #3
0
        internal UploadedFile(string fileName, string contentType, string sourceElement, UploadRequest request, UploadHttpRequest httpRequest)
        {
            // TODO: ensure this is cross-platform compatible
            ClientName = GetFileName(fileName);
            //_fileName = fileName;
            ContentType = contentType;
            SourceElement = sourceElement;

            Data = new Dictionary<string, string>();

            UploadRequest = request;
            UploadHttpRequest = httpRequest;
        }
Пример #4
0
        void context_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            HttpContext context = HttpContext.Current;

            //System.Diagnostics.Debug.WriteLine(context.Request.Url.ToString() + " - " + (context.Request.Cookies.Count > 0 ? context.Request.Cookies[0].Value : "null"));

            if (IsUploadRequest(context.Request))
            {
                if (context.Trace != null && context.Trace.IsEnabled)
                {
                    throw new HttpException("Trace must be disabled for SlickUpload to intercept upload requests.");
                }

                if ((DateTime.Now - _lastCleanupDate).TotalSeconds > SlickUploadContext.Config.SessionStorageProvider.StaleTimeout)
                {
                    lock (_cleanupThreadLock)
                    {
                        if ((DateTime.Now - _lastCleanupDate).TotalSeconds > SlickUploadContext.Config.SessionStorageProvider.StaleTimeout)
                        {
                            _lastCleanupDate = DateTime.Now;

                            Thread cleanupThread = new Thread(CleanupThread);

                            cleanupThread.Start();
                        }
                    }
                }

                string uploadSessionId = context.Request.QueryString["uploadSessionId"];

                // Generate an uploadSessionId if none was specified
                if (string.IsNullOrEmpty(uploadSessionId))
                {
                    uploadSessionId = Guid.NewGuid().ToString();
                }

                UploadSession session = SlickUploadContext.SessionStorageProvider.GetSession(uploadSessionId);

                if (session == null)
                {
                    session = new UploadSession(uploadSessionId, context.Request.QueryString["uploadProfile"]);

                    session.State = UploadState.Uploading;

                    SlickUploadContext.SessionStorageProvider.SaveSession(session, true);
                }

                SlickUploadContext.CurrentUploadSessionInternal = session;

                string uploadRequestId = context.Request.QueryString["uploadRequestId"];

                // Generate an uploadRequestId if none was specified
                if (string.IsNullOrEmpty(uploadRequestId))
                {
                    uploadRequestId = Guid.NewGuid().ToString();
                }

                UploadHttpRequest httpRequest = new UploadHttpRequest(context);
                UploadRequest     request     = new UploadRequest(uploadSessionId, uploadRequestId, httpRequest.ContentLength, context.Request.QueryString["uploadProfile"]);

                bool isCalculateSize = (context.Request.QueryString["handlerType"] == "calculatesize");

                try
                {
                    SlickUploadContext.CurrentUploadRequest = request;

                    if (httpRequest.ContentLength > request.UploadProfile.MaxRequestLengthBytes || (httpRequest.ContentLength < 0 && request.UploadProfile.MaxRequestLength > 4097152))
                    {
                        //SimpleLogger.Log("Request too big... aborting");

                        request.ErrorType = UploadErrorType.MaxRequestLengthExceeded;

                        context.ApplicationInstance.CompleteRequest();
                    }

                    if (request.ErrorType == UploadErrorType.None && !(request.UploadFilter == null || request.UploadFilter.ShouldHandleRequest(httpRequest)))
                    {
                        request.ErrorType = UploadErrorType.UploadFilter;
                    }

                    if (request.ErrorType != UploadErrorType.None)
                    {
                        request.State = UploadState.Error;
                    }
                    else if (isCalculateSize)
                    {
                        request.State = UploadState.Initializing;
                    }
                    else
                    {
                        request.State = UploadState.Uploading;
                    }
                }
                catch (Exception ex)
                {
                    request.State     = UploadState.Error;
                    request.ErrorType = UploadErrorType.Other;
                    request.Error     = ex;

                    throw;
                }
                finally
                {
                    bool hasExistingRequest = false;

                    try
                    {
                        hasExistingRequest = (SlickUploadContext.SessionStorageProvider.GetRequest(uploadSessionId, request.UploadRequestId) != null);
                    }
                    catch
                    { }

                    if (!hasExistingRequest)
                    {
                        SlickUploadContext.InsertRequest(request);
                    }
                    else
                    {
                        SlickUploadContext.UpdateRequest(request, true);
                    }
                }

                if (request.ErrorType == UploadErrorType.None)
                {
                    if (isCalculateSize)
                    {
                        httpRequest.Worker.CloseConnection();
                        context.ApplicationInstance.CompleteRequest();

                        return;
                    }

                    context.Server.ScriptTimeout = request.UploadProfile.ExecutionTimeout;

                    MimeUploadHandler handler = null;

                    try
                    {
                        handler = new MimeUploadHandler(httpRequest, request);

                        handler.ProcessRequest();

                        // TODO: should we check the session's state here, and cancel?
                        // TODO: maybe no, because MimeUploadHandler checks on part end and throws cancelled if it should
                        request.State = UploadState.Complete;
                    }
                    catch (Exception ex)
                    {
                        if (handler != null)
                        {
                            try
                            {
                                handler.CancelParse();
                            }
                            catch
                            {
                                // TODO: what do we do with this exception?
                            }
                        }

                        request.State = UploadState.Error;

                        if (ex is UploadDisconnectedException)
                        {
                            request.ErrorType = UploadErrorType.Disconnected;
                        }
                        else if (ex is UploadCancelledException)
                        {
                            request.ErrorType = UploadErrorType.Cancelled;
                        }
                        else
                        {
                            request.ErrorType = UploadErrorType.Other;

                            request.Error = ex;
                        }

                        try
                        {
                            SlickUploadContext.CleanupRequest(request);
                        }
                        catch
                        {
                            // TODO: what do we do with this exception?
                        }

                        // If we were disconnected, let everything pass through. Otherwise, rethrow it.
                        if (ex is UploadDisconnectedException || ex is UploadCancelledException)
                        {
                            httpRequest.Worker.CloseConnection();
                            context.ApplicationInstance.CompleteRequest();
                        }
                        else
                        {
                            throw;
                        }
                    }
                    finally
                    {
                        SlickUploadContext.UpdateRequest(request, true);
                    }
                }
            }
        }
Пример #5
0
        void context_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            HttpContext context = HttpContext.Current;

            //System.Diagnostics.Debug.WriteLine(context.Request.Url.ToString() + " - " + (context.Request.Cookies.Count > 0 ? context.Request.Cookies[0].Value : "null"));

            if (IsUploadRequest(context.Request))
            {
                if (context.Trace != null && context.Trace.IsEnabled)
                    throw new HttpException("Trace must be disabled for SlickUpload to intercept upload requests.");

                if ((DateTime.Now - _lastCleanupDate).TotalSeconds > SlickUploadContext.Config.SessionStorageProvider.StaleTimeout)
                {
                    lock (_cleanupThreadLock)
                    {
                        if ((DateTime.Now - _lastCleanupDate).TotalSeconds > SlickUploadContext.Config.SessionStorageProvider.StaleTimeout)
                        {
                            _lastCleanupDate = DateTime.Now;

                            Thread cleanupThread = new Thread(CleanupThread);

                            cleanupThread.Start();
                        }
                    }
                }

                string uploadSessionId = context.Request.QueryString["uploadSessionId"];

                // Generate an uploadSessionId if none was specified
                if (string.IsNullOrEmpty(uploadSessionId))
                    uploadSessionId = Guid.NewGuid().ToString();

                UploadSession session = SlickUploadContext.SessionStorageProvider.GetSession(uploadSessionId);

                if (session == null)
                {
                    session = new UploadSession(uploadSessionId, context.Request.QueryString["uploadProfile"]);

                    session.State = UploadState.Uploading;

                    SlickUploadContext.SessionStorageProvider.SaveSession(session, true);
                }

                SlickUploadContext.CurrentUploadSessionInternal = session;

                string uploadRequestId = context.Request.QueryString["uploadRequestId"];

                // Generate an uploadRequestId if none was specified
                if (string.IsNullOrEmpty(uploadRequestId))
                    uploadRequestId = Guid.NewGuid().ToString();

                UploadHttpRequest httpRequest = new UploadHttpRequest(context);
                UploadRequest request = new UploadRequest(uploadSessionId, uploadRequestId, httpRequest.ContentLength, context.Request.QueryString["uploadProfile"]);

                bool isCalculateSize = (context.Request.QueryString["handlerType"] == "calculatesize");

                try
                {
                    SlickUploadContext.CurrentUploadRequest = request;

                    if (httpRequest.ContentLength > request.UploadProfile.MaxRequestLengthBytes || (httpRequest.ContentLength < 0 && request.UploadProfile.MaxRequestLength > 2097152))
                    {
                        //SimpleLogger.Log("Request too big... aborting");

                        request.ErrorType = UploadErrorType.MaxRequestLengthExceeded;

                        context.ApplicationInstance.CompleteRequest();
                    }

                    if (request.ErrorType == UploadErrorType.None && !(request.UploadFilter == null || request.UploadFilter.ShouldHandleRequest(httpRequest)))
                    {
                        request.ErrorType = UploadErrorType.UploadFilter;
                    }

                    if (request.ErrorType != UploadErrorType.None)
                        request.State = UploadState.Error;
                    else if (isCalculateSize)
                        request.State = UploadState.Initializing;
                    else
                        request.State = UploadState.Uploading;
                }
                catch (Exception ex)
                {
                    request.State = UploadState.Error;
                    request.ErrorType = UploadErrorType.Other;
                    request.Error = ex;

                    throw;
                }
                finally
                {
                    bool hasExistingRequest = false;

                    try
                    {
                        hasExistingRequest = (SlickUploadContext.SessionStorageProvider.GetRequest(uploadSessionId, request.UploadRequestId) != null);
                    }
                    catch
                    { }

                    if (!hasExistingRequest)
                        SlickUploadContext.InsertRequest(request);
                    else
                        SlickUploadContext.UpdateRequest(request, true);
                }

                if (request.ErrorType == UploadErrorType.None)
                {
                    if (isCalculateSize)
                    {
                        httpRequest.Worker.CloseConnection();
                        context.ApplicationInstance.CompleteRequest();

                        return;
                    }

                    context.Server.ScriptTimeout = request.UploadProfile.ExecutionTimeout;

                    MimeUploadHandler handler = null;

                    try
                    {
                        handler = new MimeUploadHandler(httpRequest, request);

                        handler.ProcessRequest();

                        // TODO: should we check the session's state here, and cancel?
                        // TODO: maybe no, because MimeUploadHandler checks on part end and throws cancelled if it should
                        request.State = UploadState.Complete;
                    }
                    catch (Exception ex)
                    {
                        if (handler != null)
                        {
                            try
                            {
                                handler.CancelParse();
                            }
                            catch
                            {
                                // TODO: what do we do with this exception?
                            }
                        }

                        request.State = UploadState.Error;

                        if (ex is UploadDisconnectedException)
                        {
                            request.ErrorType = UploadErrorType.Disconnected;
                        }
                        else if (ex is UploadCancelledException)
                        {
                            request.ErrorType = UploadErrorType.Cancelled;
                        }
                        else
                        {
                            request.ErrorType = UploadErrorType.Other;

                            request.Error = ex;
                        }

                        try
                        {
                            SlickUploadContext.CleanupRequest(request);
                        }
                        catch
                        {
                            // TODO: what do we do with this exception?
                        }

                        // If we were disconnected, let everything pass through. Otherwise, rethrow it.
                        if (ex is UploadDisconnectedException || ex is UploadCancelledException)
                        {
                            httpRequest.Worker.CloseConnection();
                            context.ApplicationInstance.CompleteRequest();
                        }
                        else
                            throw;
                    }
                    finally
                    {
                        SlickUploadContext.UpdateRequest(request, true);
                    }
                }
            }
        }