public void Finish(HttpTransaction transaction)
        {
            if (state == State.InKey)
                throw new HttpException ("Malformed POST data, key found without value.");

            FinishPair (transaction);
        }
Beispiel #2
0
 private void AddRequestToTracker(HttpTransaction httpTranc)
 {
     lock (httpTransactions){
         while (httpTransactions.Count > (isTracking ? MaxBufferSizeWhenTracking : MaxBufferSizeWhenIdle))
         {
             httpTransactions.RemoveFirst();
         }
         httpTransactions.AddLast(httpTranc);
     }
 }
        private void SaveCache(byte[] buffer, int offset, int length, bool isBody)
        {
            if (this.PipesChain.ChainState.ContainsKey(HttpTracerPipe.STATE_KEY) == false)
            {
                return;
            }

            if ((outStreamBody == null || outStreamHeader == null) && this.Configuration is EngineSuProxyConfiguration)
            {
                try
                {
                    ResponseHeaderDumpFilesInfo header = new ResponseHeaderDumpFilesInfo((EngineSuProxyConfiguration)this.Configuration);
                    ResponseBodyDumpFilesInfo   body   = new ResponseBodyDumpFilesInfo((EngineSuProxyConfiguration)this.Configuration);

                    String guid = null;

                    do
                    {
                        guid = Guid.NewGuid().ToString().Replace("-", "").ToLower();
                    } while (header.Exist(guid) || body.Exist(guid));

                    Stream b = body.Open(FileAccess.Write, guid);
                    if (b != null)
                    {
                        outStreamBody = new BinaryWriter(b);
                    }

                    Stream h = header.Open(FileAccess.Write, guid);
                    if (h != null)
                    {
                        outStreamHeader = new BinaryWriter(h);
                    }

                    HttpTransaction httpTranc = (HttpTransaction)this.PipesChain.ChainState[HttpTracerPipe.STATE_KEY];
                    httpTranc.FileGUID = guid;
                }
                catch
                {
                }
            }
            try
            {
                if (isBody && outStreamBody != null)
                {
                    outStreamBody.Write(buffer, offset, length);
                }
                else if (outStreamHeader != null)
                {
                    outStreamHeader.Write(buffer, offset, length);
                }
            }
            catch
            {
            }
        }
Beispiel #4
0
        public static void FlushTracker(DownloadDumpFilesInfo fileInfo, String fromUrl)
        {
            lock (httpTransactions)
            {
                if (isTracking == false)
                {
                    return;
                }

                isTracking = false;

                if (httpTransactions.Count <= 0)
                {
                    return;
                }

                HttpTransaction t = null;

                while (httpTransactions.Count > 0)
                {
                    t = httpTransactions.First.Value;

                    if (t.URL != null && t.URL.Equals(fromUrl))
                    {
                        break;
                    }

                    httpTransactions.RemoveFirst();
                }
                foreach (HttpTransaction tt in httpTransactions)
                {
                    if (URLMasks.ContainsKey(tt.URL))
                    {
                        tt.URL = URLMasks[tt.URL];
                    }
                }
                if (httpTransactions.Count > 0)
                {
                    HttpFlushPipe.AddFlushQue(fileInfo, new LinkedList <HttpTransaction>(httpTransactions));
                }

                httpTransactions.Clear();
            }
        }
Beispiel #5
0
        public override void SendData(byte[] buffer, int offset, int length)
        {
            if (isRequest && this.PipesChain.ChainState.ContainsKey(STATE_KEY) == false && this.PipesChain.ChainState.ContainsKey("REQUEST_URI"))
            {
                HttpTransaction httpTranc = new HttpTransaction();

                httpTranc.URL  = (String)this.PipesChain.ChainState["REQUEST_URI"];
                httpTranc.Mode = HttpMode.SendingRequest;

                httpTranc.ConnectionStartTime     = DateTime.Now;
                httpTranc.SendingRequestStartTime = DateTime.Now;
                httpTranc.TotalSent = length;

                TestEvents.FireProgressEvent(TestEventType.RequestingFile, httpTranc.URL);
                TestEvents.FireProgressEvent(TestEventType.SendingData, length, 0, httpTranc.URL);

                this.PipesChain.ChainState.Add(STATE_KEY, httpTranc);

                AddRequestToTracker(httpTranc);
            }
            else if (isRequest)
            {
                HttpTransaction httpTranc = (HttpTransaction)this.PipesChain.ChainState[STATE_KEY];
                httpTranc.TotalSent += length;
                TestEvents.FireProgressEvent(TestEventType.SendingData, length, 0, httpTranc.URL);
            }
            else if (isRequest == false && this.PipesChain.ChainState.ContainsKey(STATE_KEY))
            {
                HttpTransaction httpTranc = (HttpTransaction)this.PipesChain.ChainState[STATE_KEY];

                if (httpTranc.Mode == HttpMode.WaitingForResponse)
                {
                    httpTranc.Mode = HttpMode.ReceivingResponse;
                    httpTranc.ReceivingResponseStartTime = DateTime.Now;
                }
                httpTranc.TotalReceived += length;

                TestEvents.FireProgressEvent(TestEventType.ReceivingData, length, 0, httpTranc.URL);
            }

            base.SendData(buffer, offset, length);
        }
Beispiel #6
0
        public override void Flush()
        {
            if (this.PipesChain.ChainState.ContainsKey(STATE_KEY))
            {
                HttpTransaction httpTranc = (HttpTransaction)this.PipesChain.ChainState[STATE_KEY];

                if (isRequest)
                {
                    httpTranc.Mode = HttpMode.WaitingForResponse;
                    httpTranc.SendingRequestEndTime = DateTime.Now;
                }
                else
                {
                    TestEvents.FireProgressEvent(TestEventType.ResponseEnded, httpTranc.URL);
                    httpTranc.Mode = HttpMode.Completed;
                    httpTranc.ReceivingResponseEndTime = DateTime.Now;
                    httpTranc.ConnectionEndTime        = DateTime.Now;
                }
            }

            base.Flush();
        }
        public void HandleData(HttpTransaction transaction, ByteBuffer data, int pos, int len)
        {
            string str_data = transaction.Request.ContentEncoding.GetString (data.Bytes, pos, len);

            str_data = HttpUtility.HtmlDecode (str_data);

            pos = 0;
            len = str_data.Length;

            while (pos < len) {
                char c = str_data [pos++];

                if (c == '&') {
                    if (state == State.InKey)
                        throw new InvalidOperationException ("& symbol can not be used in key data.");
                    FinishPair (transaction);
                    state = State.InKey;
                    continue;
                }

                if (c == '=') {
                    if (state == State.InValue)
                        throw new InvalidOperationException ("= symbol can not be used in value data.");
                    state = State.InValue;
                    continue;
                }

                switch (state) {
                case State.InKey:
                    key_buffer.Append (c);
                    break;
                case State.InValue:
                    value_buffer.Append (c);
                    break;
                }
            }
        }
        private void HandleHeader(HttpTransaction transaction)
        {
            string key = encoding.GetString (header_key.ToArray ());
            string value = encoding.GetString (header_value.ToArray ());

            if (key == "Content-Disposition")
                ParseContentDisposition (value);
            else if (key == "Content-Type")
                ParseContentType (value);
        }
        private void FinishFormData(HttpTransaction transaction)
        {
            if (form_data.Count == 0)
                return;

            transaction.Request.PostData.Set (current_name, HttpUtility.UrlDecode (form_data.ToString (), encoding));
            form_data.Clear ();
        }
        private void FinishFileData(HttpTransaction transaction)
        {
            if (uploaded_file == null)
                return;

            // Chop off the \r\n that gets appended before the boundary marker
            uploaded_file.Contents.SetLength (uploaded_file.Contents.Position - 2);

            uploaded_file.Finish ();
            transaction.Request.Files.Add (current_name, uploaded_file);
            uploaded_file = null;
        }
        public void HandleData(HttpTransaction transaction, ByteBuffer data, int pos, int len)
        {
            // string str_data = encoding.GetString (data.Bytes, pos, len);
            byte [] str_data = data.Bytes;

            int begin = pos;
            int end = begin + len;

            pos = begin - 1;

            while (pos < end - 1 && state != State.Finished) {

                byte c = str_data [++pos];

                switch (state) {
                case State.InBoundary:
                    if (index == boundary.Length - 1) {

                        boundary_buffer.Clear ();

                        // Flush any data
                        FinishFormData (transaction);
                        FinishFileData (transaction);

                        state = State.PostBoundary1;
                        index = 0;
                        break;
                    }

                    boundary_buffer.Add (c);

                    if (c != boundary [index]) {
                        // Copy the boundary buffer to the beginning and restart parsing there
                        MemoryStream stream = new MemoryStream ();
                        stream.Write (boundary_buffer.ToArray (), 0, boundary_buffer.Count);
                        stream.Write (str_data, pos + 1, end - pos - 1);
                        str_data = stream.ToArray ();

                        pos = -1;
                        end = str_data.Length;

                        not_boundary = true;
                        boundary_buffer.Clear ();
                        state = previous_state;
                        index = 0;

                        // continue instead of break so not_boundary is not reset
                        continue;
                    }

                    ++index;
                    break;

                case State.PostBoundary1:
                    if (c == '-') {
                        state = State.PostBoundaryComplete;
                        break;
                    }

                    if (c == '\r')
                        break;

                    if (c == '\n') {
                        state = State.InHeaderKey;
                        break;
                    }

                    throw new Exception (String.Format ("Invalid post boundary char '{0}'", c));

                case State.PostBoundaryComplete:
                    if (c != '-')
                        throw new Exception (String.Format ("Invalid char '{0}' in boundary complete.", c));

                    state = State.Finished;
                    break;

                case State.InHeaderKey:
                    if (c == '\n') {
                        state = current_filename == null ? State.InFormData : State.InFileData;
                        break;
                    }

                    if (c == ':') {
                        state = State.InHeaderValue;
                        break;
                    }

                    header_key.Add (c);
                    break;

                case State.InHeaderValue:
                    if (c == '\r') {
                        state = State.PostHeader1;
                        break;
                    }

                    header_value.Add (c);
                    break;

                case State.PostHeader1:
                    if (c != '\n')
                        throw new Exception (String.Format ("Invalid char '{0}' in post header 1.", c));
                    state = State.PostHeader2;
                    break;

                case State.PostHeader2:
                    HandleHeader (transaction);
                    header_key.Clear ();
                    header_value.Clear ();
                    state = State.InHeaderKey;
                    break;

                case State.InFormData:
                    if (CheckStartingBoundary (str_data, pos))
                        break;

                    form_data.Add (c);
                    break;

                case State.InFileData:
                    if (CheckStartingBoundary (str_data, pos))
                        break;;

                    uploaded_file.Contents.WriteByte (c);
                    break;
                default:
                    throw new Exception (String.Format ("Unhandled state: {0}", state));
                }

                not_boundary = false;
            }
        }
 public void Finish(HttpTransaction transaction)
 {
     FinishFormData (transaction);
     FinishFileData (transaction);
 }
        private void FinishPair(HttpTransaction transaction)
        {
            if (key_buffer.Length == 0 || value_buffer.Length == 0)
                throw new HttpException ("zero length www-form data.");

            Encoding e =  transaction.Request.ContentEncoding;
            transaction.Request.PostData.Set (HttpUtility.UrlDecode (key_buffer.ToString (), e),
                    HttpUtility.UrlDecode (value_buffer.ToString (), e));

            key_buffer.Clear ();
            value_buffer.Clear ();
        }