示例#1
0
        /// <summary>
        /// Updates the request with the current values
        /// </summary>
        /// <param name="requestInfo"></param>
        public HttpRequestInfo UpdateRequest(HttpRequestInfo requestInfo)
        {
            if (requestInfo != null)
            {
                bool   isSecure          = requestInfo.IsSecure;
                string updatedRawRequest = UpdateRequest(requestInfo.ToString());

                requestInfo          = new HttpRequestInfo(updatedRawRequest);
                requestInfo.IsSecure = isSecure;
            }
            return(requestInfo);
        }
示例#2
0
        /// <summary>
        /// Checks if the request matches the traps
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        private bool MatchesTrapDefs(HttpRequestInfo info)
        {
            string rawRequest = info.ToString();

            foreach (HttpTrapDef def in _trapDefs)
            {
                if (def.Location == HttpTrapLocation.Request && def.IsMatch(rawRequest))
                {
                    return(true);
                }
            }
            return(false);
        }
示例#3
0
        /// <summary>
        /// Sends the specified request to the server
        /// (Proxy not supported)
        /// </summary>
        /// <param name="parsedRequest"></param>
        /// <param name="host"></param>
        /// <param name="port"></param>
        /// <param name="https"></param>
        public void SendRequest(HttpRequestInfo parsedRequest, string host, int port, bool https)
        {
            _requestCompleteEvent.Reset();

            _dataBuilder = new ByteArrayBuilder();

            if (_connection == null)
            {
                _connection = new HttpClientConnection(host, port, https);
            }

            try
            {
                //connect
                if (_connection.Connect())
                {
                    bool isProxy = _connection.Host != host;

                    //add connection closed header only this is supported at the moment
                    parsedRequest.Headers["Connection"] = "close";

                    if (isProxy)
                    {
                        parsedRequest.Headers["Proxy-Connection"] = "close";
                    }

                    // Turn off accepting of gzip/deflate
                    //parsedRequest.Headers.Remove("Accept-Encoding");

                    //calculate the content length
                    if (parsedRequest.ContentData != null)
                    {
                        parsedRequest.Headers["Content-Length"] = parsedRequest.ContentData.Length.ToString();
                    }

                    parsedRequest.Host     = host;
                    parsedRequest.Port     = port;
                    parsedRequest.IsSecure = https;

                    if (isProxy && https)
                    {
                        //send a connect message to the proxy
                        SendConnect(host, port);
                    }

                    byte[] reqBytes = Constants.DefaultEncoding.GetBytes(parsedRequest.ToString(isProxy && !https));

                    //write to the stream
                    _connection.Stream.Write(reqBytes, 0, reqBytes.Length);

                    //start reading
                    _buffer = new byte[MAX_BUFFER_SIZE];
                    _connection.Stream.BeginRead(_buffer, 0, MAX_BUFFER_SIZE, new AsyncCallback(ReadResponse), _connection.Stream);
                }
                else
                {
                    throw new Exception("Cannot connect to server");
                }
            }
            catch (Exception ex)
            {
                SdkSettings.Instance.Logger.Log(TraceLevel.Error, "HttpClient error sending request {0}", ex.Message);
                //notify the caller that the request was completed with an error
                if (RequestComplete != null)
                {
                    RequestComplete.Invoke(
                        new HttpClientRequestCompleteEventArgs());
                    RequestCompleteEvent.Set();
                }

                _connection.Close();
            }
        }