예제 #1
0
        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);
        }
예제 #2
0
        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
            }
        }