public static HttpResponsePacket ToHttpResponsePacket(this HttpResponseMessage response) { var packet = new HttpResponsePacket(); foreach (var hdr in response.Headers) { packet.AddHeader(hdr); } if (response.Content != null) { foreach (var hdr in response.Content.Headers) { packet.AddHeader(hdr); } } packet.Version = response.Version.ToString(); packet.StatusCode = (int)response.StatusCode; packet.StatusDescription = response.ReasonPhrase; if (response.Content != null) { packet.Content = response.Content.ReadAsByteArrayAsync().Result; } return(packet); }
private HttpResponsePacket CreateResponsePacketFromMessage(HttpResponseMessage responseMsg, IRestBusSubscriber subscriber) { //TODO: Confirm that commas in response headers are merged iproperly into packet header var responsePkt = new HttpResponsePacket(responseMsg); //Add/Update Subscriber-Id header responsePkt.Headers[Common.Shared.SUBSCRIBER_ID_HEADER] = new string[] { subscriber == null ? String.Empty : subscriber.Id ?? String.Empty }; return(responsePkt); }
internal static void ReadAndSignalDelivery(ExpectedResponse expected, BasicDeliverEventArgs evt) { try { expected.Response = HttpResponsePacket.Deserialize(evt.Body); } catch (Exception ex) { expected.DeserializationException = ex; } //NOTE: The ManualResetEventSlim.Set() method can be called after the object has been disposed //So no worries about the Timeout disposing the object before the response comes in. expected.ReceivedEvent.Set(); }
///<summary>Adds a header to the response packet. </summary> ///<remarks>NOTE: This method does not fold the headers as expected in WebAPI 2's response stream.</remarks> private static void AddHeader(this HttpResponsePacket packet, KeyValuePair <string, IEnumerable <string> > hdr) { if (packet == null) { throw new ArgumentNullException("packet"); } if (packet.Headers.ContainsKey(hdr.Key)) { ((List <string>)packet.Headers[hdr.Key]).AddRange(hdr.Value); } else { packet.Headers.Add(hdr.Key, new List <string>(hdr.Value)); } }
internal static HttpResponsePacket ToHttpResponsePacket(this ServiceMessage message) { if (message == null) { throw new ArgumentNullException("message"); } var response = new HttpResponsePacket(); var respFeature = message as IHttpResponseFeature; if (respFeature.Headers != null) { foreach (var hdr in respFeature.Headers) { response.Headers.Add(hdr.Key, hdr.Value); } } response.Version = HTTP_RESPONSE_VERSION; response.StatusCode = respFeature.StatusCode; if (String.IsNullOrEmpty(respFeature.ReasonPhrase)) { response.StatusDescription = ReasonPhrases.ToReasonPhrase(response.StatusCode) ?? "Unknown"; } else { response.StatusDescription = respFeature.ReasonPhrase; } if (message.OriginalResponseBody != null && message.OriginalResponseBody.CanRead) { //NOTE: OriginalResponseBody.CanRead will be false if the stream was disposed. response.Content = message.OriginalResponseBody.ToArray(); } //Add/Update Server header response.Headers["Server"] = HTTP_RESPONSE_SERVER_HEADER; return(response); }
private static bool TryGetHttpResponseMessage(HttpResponsePacket packet, out HttpResponseMessage response) { try { response = new HttpResponseMessage { Content = packet.Content == null ? RestBusClient._emptyByteArrayContent : new ByteArrayContent(packet.Content), Version = packet.Version == "1.1" ? VERSION_1_1 : new Version(packet.Version), ReasonPhrase = packet.StatusDescription, StatusCode = (System.Net.HttpStatusCode)packet.StatusCode }; packet.PopulateHeaders(response.Content.Headers, response.Headers); } catch { response = null; return(false); } return(true); }
private static HttpResponsePacket CreateResponsePacketFromWrapper(ResponseWrapper wrapper, IRestBusSubscriber subscriber) { HttpResponsePacket response = new HttpResponsePacket(); //TODO: Note that when implementing this in WebAPI/MVC the "responsewrapper" will most likely split headers into groups seperated by commas string trimmedKey; foreach (string key in wrapper.Headers.AllKeys) { foreach (string value in wrapper.Headers.GetValues(key)) { trimmedKey = key.Trim(); if (trimmedKey != String.Empty) { if (response.Headers.ContainsKey(trimmedKey)) { ((List <string>)response.Headers[trimmedKey]).Add(value); } else { response.Headers.Add(trimmedKey, new List <string> { value }); } } } } //Add/Update Subscriber-Id header response.Headers[Common.Shared.SUBSCRIBER_ID_HEADER] = new string[] { subscriber == null ? String.Empty : subscriber.Id ?? String.Empty }; response.Content = (wrapper.OutputStream as System.IO.MemoryStream).ToArray(); response.StatusCode = wrapper.StatusCode; response.StatusDescription = wrapper.StatusDescription; response.Version = HTTP_RESPONSE_VERSION; return(response); }
public void SendResponse(MessageContext context, HttpResponsePacket response) { if (disposed) { throw new ObjectDisposedException("Subscriber has been disposed"); } if (String.IsNullOrEmpty(context.ReplyToQueue)) { return; } if (conn == null) { //TODO: Log this -- it technically shouldn't happen. Also translate to a HTTP Unreachable because it means StartCallbackQueueConsumer didn't create a connection throw new ApplicationException("This is Bad"); } //TODO: Channel Pool this connection using (IModel channel = conn.CreateModel()) { BasicProperties basicProperties = new BasicProperties { CorrelationId = context.CorrelationId }; try { channel.BasicPublish(String.Empty, context.ReplyToQueue, basicProperties, response.Serialize()); } catch { //TODO: Log execption } } }
private static HttpResponsePacket CreateResponsePacketFromWrapper(ResponseWrapper wrapper, IRestBusSubscriber subscriber) { HttpResponsePacket response = new HttpResponsePacket(); string trimmedKey; foreach (string key in wrapper.Headers.AllKeys) { foreach (string value in wrapper.Headers.GetValues(key)) { trimmedKey = key.Trim(); if (trimmedKey != String.Empty) { if (response.Headers.ContainsKey(trimmedKey)) { ((List <string>)response.Headers[trimmedKey]).Add(value); } else { response.Headers.Add(trimmedKey, new List <string> { value }); } } } } //TODO: Investigate if servicestack V3 produces a Server header, if so add it here and in CreateResponseFromException response.Content = (wrapper.OutputStream as System.IO.MemoryStream).ToArray(); response.StatusCode = wrapper.StatusCode; response.StatusDescription = wrapper.StatusDescription; response.Version = HTTP_RESPONSE_VERSION; return(response); }
public void SendResponse(MessageContext context, HttpResponsePacket response) { if (disposed) { throw new ObjectDisposedException(GetType().FullName); } var dispatch = context.Dispatch as MessageDispatch; if (dispatch != null) { //Ack request if (Settings.AckBehavior != SubscriberAckBehavior.Automatic && dispatch.Consumer.Model.IsOpen) { dispatch.Consumer.Model.BasicAck(dispatch.Delivery.DeliveryTag, false); //NOTE: The call above takes place in different threads silmultaneously //In which case multiple threads will be using the same channel at the same time. //It's okay in this case, because transmissions within a channel are synchronized, as seen in: //https://github.com/rabbitmq/rabbitmq-dotnet-client/blob/f16c093f6409e11d9d77115038cb224eb39468ec/projects/client/RabbitMQ.Client/src/client/impl/ModelBase.cs#L459 //and //https://github.com/rabbitmq/rabbitmq-dotnet-client/blob/f16c093f6409e11d9d77115038cb224eb39468ec/projects/client/RabbitMQ.Client/src/client/impl/SessionBase.cs#L177 } } //Exit method if no replyToQueue was specified. if (String.IsNullOrEmpty(context.ReplyToQueue)) { return; } if (_subscriberPool.Connection == null) { //TODO: Log this -- it technically shouldn't happen. Also translate to a HTTP Unreachable because it means StartCallbackQueueConsumer didn't create a connection throw new InvalidOperationException("This is Bad"); } //Add/Update Subscriber-Id header response.Headers[Common.Shared.SUBSCRIBER_ID_HEADER] = subscriberIdHeader; //Send response var pooler = _subscriberPool; AmqpModelContainer model = null; try { model = pooler.GetModel(ChannelFlags.None); BasicProperties basicProperties = new BasicProperties { CorrelationId = context.CorrelationId }; model.Channel.BasicPublish(String.Empty, context.ReplyToQueue, basicProperties, response.Serialize()); } finally { if (model != null) { model.Close(); } } }
public static HttpResponsePacket ToHttpResponsePacket (this HttpResponseMessage response) { var packet = new HttpResponsePacket(); foreach (var hdr in response.Headers) { packet.AddHeader(hdr); } if (response.Content != null) { foreach (var hdr in response.Content.Headers) { packet.AddHeader(hdr); } } packet.Version = response.Version.ToString(); packet.StatusCode = (int)response.StatusCode; packet.StatusDescription = response.ReasonPhrase; if (response.Content != null) { packet.Content = response.Content.ReadAsByteArrayAsync().Result; } return packet; }
private void ProcessRequest(MessageContext context) { //NOTE: This method is called on a background thread and must be protected by an outer big-try catch var httpReq = new RequestWrapper(context.Request); var httpRes = new ResponseWrapper(); IServiceStackHttpHandler handler = null; string operationName, contentType; //var handler = ServiceStackHttpHandlerFactory.GetHandler(httpReq); var restPath = RestHandler.FindMatchingRestPath(httpReq.HttpMethod, httpReq.PathInfo, out contentType); if (restPath != null) { handler = new RestHandler { RestPath = restPath, RequestName = restPath.RequestType.Name }; httpReq.OperationName = operationName = ((RestHandler)handler).RestPath.RequestType.Name; } else { handler = new NotFoundHttpHandler(); var stream = httpRes.OutputStream; //Bug fix: reading the OutputStream property will cause it to be created if it's null httpReq.OperationName = operationName = null; } HttpResponsePacket resPacket = null; try { handler.ProcessRequest(httpReq, httpRes, operationName); resPacket = CreateResponsePacketFromWrapper(httpRes, subscriber); } catch (Exception exception) { //Send Exception details back to Queue resPacket = CreateResponsePacketFromException(exception); } finally { httpReq.InputStream.Close(); httpRes.Close(); } if (resPacket == null) { //TODO: Not good, Log this //TODO: derive exception from RestBus.Exceptions class resPacket = CreateResponsePacketFromException(new ApplicationException("Unable to get response")); } try { //TODO: Why can't the subscriber append the subscriber id itself from within sendresponse subscriber.SendResponse(context, resPacket); } catch { //TODO: Log SendResponse error } }
public static HttpResponsePacket ToHttpResponsePacket(this HttpResponseMessage response) { var packet = new HttpResponsePacket(); foreach (var hdr in response.Headers) { packet.AddHttpHeader(hdr); } if (response.Content != null) { foreach (var hdr in response.Content.Headers) { packet.AddHttpHeader(hdr); } } packet.Version = response.Version.ToString(); packet.StatusCode = (int)response.StatusCode; packet.StatusDescription = response.ReasonPhrase; if (response.Content != null) { using (MemoryStream ms = new MemoryStream()) { response.Content.CopyToAsync(ms).Wait(); packet.Content = ms.ToArray(); } } return packet; }