Example #1
0
        private void SavePendingRequest(httpRequestType incomingRequest)
        {
            SqlCommand cmd = s_connection.CreateCommand();
            cmd.CommandText = "sp_AddOrUpdatePendingRequest";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Transaction = s_transaction;
            cmd.Parameters.AddWithValue("@ConversationHandle", s_currentConversation.Handle);
            cmd.Parameters.AddWithValue("@RetriesUsed", ++m_numRetriesUsed);

            if (s_msgReceived.Type == x_httpRequestMessageType)
            {
                MemoryStream stream = new MemoryStream();
                XmlSerializer xs = new XmlSerializer(typeof(httpRequestType));
                xs.Serialize(stream, incomingRequest);
                cmd.Parameters.AddWithValue("@RequestBody", stream.ToArray());
            }
            else
            {
                cmd.Parameters.Add("@RequestBody", SqlDbType.VarBinary).Value = DBNull.Value;
            }

            if (m_lastError == null)
                cmd.Parameters.AddWithValue("@Status", DBNull.Value);
            else
                cmd.Parameters.AddWithValue("@Status", m_lastError);

            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (SqlException e)
            {
                SqlContext.Pipe.Send(e.Message);
            }
        }
Example #2
0
        public void OnHttpRequest(
            Message msgReceived,
            SqlConnection connection,
            SqlTransaction transaction)
        {
            s_connection = connection;
            s_transaction = transaction;
            s_currentConversation = msgReceived.Conversation;
            s_msgReceived = msgReceived;

            try
            {
                XmlSerializer xs = new XmlSerializer(typeof(httpRequestType));
                httpRequestType request = (httpRequestType)xs.Deserialize(msgReceived.Body);
                ServiceRequest(request);
            }
            catch (Exception e)
            {
                SqlContext.Pipe.Send(e.StackTrace);

                if (connection.State == ConnectionState.Open)
                {
                    msgReceived.Conversation.EndWithError(1, e.Message + "\n" + e.StackTrace, connection, transaction);
                }
            }
        }
Example #3
0
        private RequestFilterAction MatchRequestFilter(httpRequestType incomingRequest)
        {
            SqlCommand cmd = s_connection.CreateCommand();
            cmd.CommandText = "sp_MatchRequestFilter";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Transaction = s_transaction;

            SqlParameter prmMethod = cmd.Parameters.AddWithValue("@Method", incomingRequest.method);
            SqlParameter prmUrl = cmd.Parameters.AddWithValue("@Url", incomingRequest.url);
            SqlDataReader reader = cmd.ExecuteReader();

            if (!reader.Read())
            {
                reader.Close();
                return RequestFilterAction.Deny;
            }

            RequestFilterAction action = (RequestFilterAction)reader.GetByte(0);

            if (!reader.IsDBNull(1))
                m_timeout = TimeSpan.FromSeconds(reader.GetInt32(1));

            if (!reader.IsDBNull(2))
                m_numRetries = (int) reader.GetByte(2);

            if (!reader.IsDBNull(3))
                m_retryDelay = reader.GetInt32(3);

            if (!reader.IsDBNull(4))
                m_backoffFactor = reader.GetFloat(4);

            reader.Close();

            return action;
        }
Example #4
0
        private void ServiceRequest(httpRequestType incomingRequest)
        {
            RequestFilterAction reqAction = MatchRequestFilter(incomingRequest);

            switch (reqAction)
            {
                case RequestFilterAction.Deny:
                    ErrorConversation(2, "Proxy does not accept this type of request to given URL.");
                    return;
            }

            HttpWebRequest outgoingRequest = CreateWebRequest(incomingRequest);

            if (outgoingRequest == null)
            {
                SavePendingRequest(incomingRequest);
                BeginTimer();
                return;
            }

            HttpWebResponse incomingResponse = null;
            ResponseFilterAction respAction = TryWebRequest(outgoingRequest, out incomingResponse);
            SqlContext.Pipe.Send(respAction.ToString());

            switch (respAction)
            {
                case ResponseFilterAction.Respond:
                    httpResponseType outgoingResponse = CreateBrokerResponse(incomingResponse);
                    SendResponse(outgoingResponse);
                    EndConversation();

                    // Deletes the pending request, if there was one stored...
                    DeletePendingRequest();
                    break;

                case ResponseFilterAction.Retry:
                    if (m_numRetries == m_numRetriesUsed)
                    {
                        ErrorConversation(5, m_lastError);
                    }
                    else
                    {
                        SavePendingRequest(incomingRequest);
                        BeginTimer();
                    }
                    break;

                case ResponseFilterAction.Error:
                    if (incomingResponse != null)
                    {
                        ErrorConversation(4, m_lastError);
                    }
                    break;
            }
        }
Example #5
0
        public void OnTimer(
            Message msgReceived,
            SqlConnection connection,
            SqlTransaction transaction)
        {
            s_connection = connection;
            s_transaction = transaction;
            s_currentConversation = msgReceived.Conversation;
            s_msgReceived = msgReceived;

            httpRequestType pendingRequest = GetPendingRequest();

            if (pendingRequest == null)
            {
                ErrorConversation(6, "Your pending request was mysteriously lost.");
                return;
            }

            SqlContext.Pipe.Send("retrieved: " + pendingRequest.url);
            SqlContext.Pipe.Send("num used: " + m_numRetriesUsed);
            ServiceRequest(pendingRequest);
        }
Example #6
0
        private httpRequestType GetPendingRequest()
        {
            SqlCommand cmd = s_connection.CreateCommand();
            cmd.CommandText = @"SELECT RequestBody, RetriesUsed FROM PendingRequest WHERE ConversationHandle = @ConversationHandle";
            cmd.Transaction = s_transaction;
            cmd.Parameters.AddWithValue("@ConversationHandle", s_currentConversation.Handle);
            SqlDataReader reader = cmd.ExecuteReader();

            if (!reader.Read())
            {
                reader.Close();
                return null;
            }

            SqlBytes requestBytes = reader.GetSqlBytes(0);
            XmlSerializer xs = new XmlSerializer(typeof(httpRequestType));
            httpRequestType pendingRequest = xs.Deserialize(requestBytes.Stream) as httpRequestType;

            m_numRetriesUsed = (int) reader.GetByte(1);
            reader.Close();

            return pendingRequest;
        }
Example #7
0
        private HttpWebRequest CreateWebRequest(httpRequestType incomingRequest)
        {
            HttpWebRequest outgoingRequest;

            outgoingRequest = (HttpWebRequest)HttpWebRequest.Create(incomingRequest.url);
            outgoingRequest.Method = incomingRequest.method;
            outgoingRequest.Timeout = (int) m_timeout.TotalMilliseconds;

            if (incomingRequest.protocolVersion != null)
            {
                try
                {
                    string[] s = incomingRequest.protocolVersion.Split(new char[] { '/' });

                    if (s.Length > 1)
                    {
                        outgoingRequest.ProtocolVersion = new Version(s[1]);
                    }
                }
                catch
                { }
            }

            outgoingRequest.ContentLength = incomingRequest.body.Length;

            if (incomingRequest.headers != null)
            {
                foreach (headerType h in incomingRequest.headers)
                {
                    SqlContext.Pipe.Send(h.name + ": " + h.value);

                    switch (h.name.ToLowerInvariant())
                    {
                        case "host":
                        case "date":
                        case "range":
                            break;

                        case "accept":
                            outgoingRequest.Accept = h.value;
                            break;

                        case "connection":
                            outgoingRequest.Connection = h.value;
                            break;

                        //case "content-length":
                        //    outgoingRequest.ContentLength = Int32.Parse(h.value);
                        //    break;

                        case "content-type":
                            outgoingRequest.ContentType = h.value;
                            break;

                        case "expect":
                            outgoingRequest.Expect = h.value;
                            break;

                        case "if-modified-since":
                            outgoingRequest.IfModifiedSince = DateTime.Parse(h.value);
                            break;

                        case "referer":
                            outgoingRequest.Referer = h.value;
                            break;

                        case "transfer-encoding":
                            outgoingRequest.TransferEncoding = h.value;
                            break;

                        case "user-agent":
                            outgoingRequest.UserAgent = h.value;
                            break;

                        default:
                            outgoingRequest.Headers[h.name] = h.value;
                            break;
                    }
                }
            }

            byte[] buffer = incomingRequest.body;

            if (buffer != null && buffer.Length > 0)
            {
                try
                {
                    Stream body = outgoingRequest.GetRequestStream();
                    body.Write(buffer, 0, buffer.Length);
                    body.Close();
                }
                catch (WebException we)
                {
                    // The web service isn't available
                    m_lastError = we.Message;

                    return null;
                }
            }

            return outgoingRequest;
        }