Example #1
0
        public FileUploader(FileUploadPolicy fileUploadPolicy)
        {
            if (fileUploadPolicy == null)
            {
                throw new ArgumentNullException("fileUploadPolicy");
            }

            FileUploadPolicy = fileUploadPolicy;
        }
Example #2
0
        public void Dispose()
        {
            if (m_FormData != null)
            {
                m_FormData.Dispose();
                m_FormData = null;
            }

            if (m_CurrentFileStream != null)
            {
                FileUploadPolicy.DisposeFileStream(m_CurrentFileSize, m_CurrentFileStream);

                m_CurrentFileStream = null;
            }
        }
Example #3
0
        private void ParseData(byte[] buffer, int startIndex, int length, byte[] boundary)
        {
ParseDataStart:
            if (startIndex == buffer.Length - 1)
            {
                return;
            }

            byte[] formBuffer = null;

            if (m_Status != LOOK_FOR_FILE_CONTENT)
            {
                m_FormData.Write(buffer, startIndex, length);

                formBuffer = m_FormData.GetBuffer();
            }

            int formDataLength = 0;

            switch (m_Status)
            {
            case LOOK_FOR_BOUNDARY:

                formDataLength = (int)m_FormData.Position;

                m_LastBoundaryIndex = SearchByteArray(formBuffer, boundary, m_SearchStartIndex, formDataLength - m_SearchStartIndex);

                if (m_LastBoundaryIndex >= 0)
                {
                    m_Status = LOOK_FOR_DOUBLE_NEWLINE;

                    m_SearchStartIndex = m_LastBoundaryIndex + boundary.Length;

                    goto LOOK_FOR_DOUBLE_NEWLINE;
                }

                break;

            case LOOK_FOR_DOUBLE_NEWLINE:

LOOK_FOR_DOUBLE_NEWLINE:

                formDataLength = (int)m_FormData.Position;

                if (formDataLength - m_SearchStartIndex >= 4)
                {
                    int doubleNewLineIndex = SearchDoubleNewLine(formBuffer, m_SearchStartIndex, formDataLength - m_SearchStartIndex);

                    if (doubleNewLineIndex >= 0)
                    {
                        string str = Request.ContentEncoding.GetString(formBuffer, m_SearchStartIndex, doubleNewLineIndex - m_SearchStartIndex);

                        bool isFile = false;

                        int index = str.IndexOf("Content-Disposition:", StringComparison.OrdinalIgnoreCase);

                        if (index >= 0)
                        {
                            index = index + 20;

                            m_CurrentFileFormName = ExtractValueFromContentDispositionHeader(str, index, "name");
                            m_CurrentFileRealName = ExtractValueFromContentDispositionHeader(str, index, "filename");

                            isFile = m_CurrentFileRealName != null;
                        }

                        if (isFile)
                        {
                            m_Status = LOOK_FOR_FILE_CONTENT;

                            m_FormData.Position = m_LastBoundaryIndex;

                            m_SearchStartIndex = m_LastBoundaryIndex;

                            if (m_CurrentFileStream != null)
                            {
                                m_CurrentFileSize = 0;

                                FileUploadPolicy.DisposeFileStream(m_CurrentFileSize, m_CurrentFileStream);
                            }

                            m_CurrentFileStream = FileUploadPolicy.CreateFileStream(m_CurrentFileRealName);
                        }
                        else
                        {
                            m_Status = LOOK_FOR_BOUNDARY;

                            m_FormData.Position = doubleNewLineIndex + 4;

                            m_SearchStartIndex = doubleNewLineIndex + 4;
                        }

                        if (formDataLength - doubleNewLineIndex - 4 > 0)
                        {
                            /* 以下改为非递归
                             * ParseData(formBuffer, doubleNewLineIndex + 4, formDataLength - doubleNewLineIndex - 4, boundary);
                             */

                            buffer     = formBuffer;
                            startIndex = doubleNewLineIndex + 4;
                            length     = formDataLength - doubleNewLineIndex - 4;

                            goto ParseDataStart;
                        }
                    }
                    else
                    {
                        m_SearchStartIndex += 1;

                        goto LOOK_FOR_DOUBLE_NEWLINE;
                    }
                }

                break;

            case LOOK_FOR_FILE_CONTENT:

                formDataLength = (int)m_FormData.Position;

                int newLineIndex = Array.IndexOf <byte>(buffer, 0x0D, startIndex, length);

                if (newLineIndex >= 0)
                {
                    m_Status = LOOK_FOR_FILE_END;

                    int temp = newLineIndex - startIndex;

                    if (temp > 0)
                    {
                        m_CurrentFileSize += temp;

                        m_CurrentFileStream.Write(buffer, startIndex, temp);
                    }

                    m_SearchStartIndex = (int)m_FormData.Position;

                    /* 以下改为非递归
                     * ParseData(buffer, newLineIndex, length - temp, boundary);
                     */

                    startIndex = newLineIndex;
                    length     = length - temp;

                    goto ParseDataStart;
                }
                else
                {
                    m_CurrentFileSize += length;

                    m_CurrentFileStream.Write(buffer, startIndex, length);
                }

                break;

            case LOOK_FOR_FILE_END:

                formDataLength = (int)m_FormData.Position;

                if (formDataLength - m_SearchStartIndex >= boundary.Length + 2)
                {
                    int fileEndIndex = -1;

                    if (formBuffer[m_SearchStartIndex + 1] == 0x0A)
                    {
                        fileEndIndex = SearchByteArray(formBuffer, boundary, m_SearchStartIndex + 2, formDataLength - m_SearchStartIndex - 2);
                    }

                    if (fileEndIndex >= 0)
                    {
                        m_FormData.Position = m_SearchStartIndex;

                        if (fileEndIndex > m_SearchStartIndex + 2)
                        {
                            int size = fileEndIndex - 2 - m_SearchStartIndex;

                            m_CurrentFileSize += size;

                            m_CurrentFileStream.Write(formBuffer, m_SearchStartIndex, size);
                        }

                        FileUploadPolicy.DisposeFileStream(m_CurrentFileSize, m_CurrentFileStream);

                        m_CurrentFileStream = null;

                        m_Status = LOOK_FOR_DOUBLE_NEWLINE;

                        m_LastBoundaryIndex = fileEndIndex;

                        m_SearchStartIndex = fileEndIndex + boundary.Length;

                        /* 以下改为非递归
                         * ParseData(formBuffer, fileEndIndex, formDataLength - fileEndIndex, boundary);
                         */

                        buffer     = formBuffer;
                        startIndex = fileEndIndex;
                        length     = formDataLength - fileEndIndex;

                        goto ParseDataStart;
                    }
                    else
                    {
                        m_Status = LOOK_FOR_FILE_CONTENT;

                        m_CurrentFileSize += 1;

                        m_CurrentFileStream.Write(formBuffer, m_SearchStartIndex, 1);

                        m_FormData.Position = m_LastBoundaryIndex;

                        /* 以下改为非递归
                         * ParseData(formBuffer, m_SearchStartIndex + 1, formDataLength - m_SearchStartIndex - 1, boundary);
                         */

                        buffer     = formBuffer;
                        startIndex = m_SearchStartIndex + 1;
                        length     = formDataLength - m_SearchStartIndex - 1;

                        goto ParseDataStart;
                    }
                }

                break;
            }
        }
Example #4
0
        public void BeginUpload()
        {
            if (false == StringStartsWithIgnoreCase(Request.ContentType, "multipart/form-data"))
            {
                FileUploadPolicy.InvalidContentType(Request.ContentType);
                return;
            }

            m_FormData = new MemoryStream(1024);

            byte[] boundary = GetMultipartBoundary(Request.ContentType);

            byte[] preloaded = WorkerRequest.GetPreloadedEntityBody();

            if (preloaded != null)
            {
                ParseData(preloaded, 0, preloaded.Length, boundary);

                TotalReadedLength += preloaded.Length;
            }

            int loopCounter = 0;

            if (!WorkerRequest.IsEntireEntityBodyIsPreloaded())
            {
                byte[] buffer = new byte[10240];

                while (WorkerRequest.IsClientConnected() && TotalReadedLength < Request.ContentLength)
                {
                    int readedLength = WorkerRequest.ReadEntityBody(buffer, buffer.Length);

                    if (readedLength <= 0)
                    {
                        break;
                    }

                    TotalReadedLength += readedLength;

                    ParseData(buffer, 0, readedLength, boundary);

                    if (loopCounter == 5)
                    {
                        loopCounter = 0;
                        Thread.Sleep(1);
                    }
                    else
                    {
                        loopCounter += 1;
                    }
                }
            }

            if (FileUploadPolicy.UseFormDataInjection)
            {
                byte[] formData = new byte[m_FormData.Position];

                Buffer.BlockCopy(m_FormData.GetBuffer(), 0, formData, 0, formData.Length);

                FileUploadPolicy.InjectFormData(formData);
            }
        }
Example #5
0
        public FileUploader(FileUploadPolicy fileUploadPolicy)
        {
            if (fileUploadPolicy == null)
                throw new ArgumentNullException("fileUploadPolicy");

            FileUploadPolicy = fileUploadPolicy;
        }