Пример #1
0
        private static ServiceMessage CreateResponse(HttpStatusCode status, string reasonPhrase, string body = null)
        {
            var msg = new ServiceMessage();

            ((Microsoft.AspNet.Http.Features.IHttpResponseFeature)msg).StatusCode   = (int)status;
            ((Microsoft.AspNet.Http.Features.IHttpResponseFeature)msg).ReasonPhrase = reasonPhrase;

            if (body != null)
            {
                msg.CreateResponseBody();
                var buffer = System.Text.Encoding.UTF8.GetBytes(body);
                msg.OriginalResponseBody.Write(buffer, 0, buffer.Length);
            }

            return(msg);
        }
Пример #2
0
        internal static bool TryGetServiceMessage(this HttpRequestPacket request, out ServiceMessage message)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            message = new ServiceMessage();

            //Build Request
            IHttpRequestFeature req = message as IHttpRequestFeature;

            Uri uri;

            try
            {
                uri = request.BuildUri(null, null);
            }
            catch
            {
                message = null;
                return(false);
            }
            req.Path = uri.AbsolutePath;

            req.Protocol    = "HTTP/" + request.Version;
            req.QueryString = uri.Query;
            req.Method      = request.Method;

            if (request.Content != null && request.Content.Length > 0)
            {
                message.CreateRequestBody(request.Content);
            }

            //Add Request Headers
            {
                var headers = new HeaderDictionary();

                foreach (var hdr in request.Headers)
                {
                    if (hdr.Key != null && hdr.Key.Trim().ToUpperInvariant() == "CONTENT-LENGTH")
                    {
                        continue;                                                                           // Content-length is calculated based on actual content.
                    }
                    //NOTE: Client already folds Request Headers into RequestPacket, so there's no need to fold it again here.
                    headers.Add(hdr.Key, hdr.Value.ToArray());
                }

                if (message.OriginalRequestBody != null)
                {
                    headers.Add("Content-Length", request.Content.Length.ToString());
                }
                req.Headers = headers;
            }


            //Create Response
            message.CreateResponseBody();
            IHttpResponseFeature resp = message as IHttpResponseFeature;

            resp.StatusCode = 200;

            //Add Response Headers
            {
                var headers = new HeaderDictionary();

                headers.Add("Server", HTTP_RESPONSE_SERVER_HEADER[0]);
                resp.Headers = headers;
            }

            return(true);
        }