private HttpResponseMessage GetResponseMessage()

in RESTProxy/Models/Endpoint.cs [349:398]


        private HttpResponseMessage GetResponseMessage(HttpWebResponse httpResponseFromApi)
        {
            if (httpResponseFromApi == null)
            {
                return new HttpResponseMessage(HttpStatusCode.InternalServerError);
            }

            HttpResponseMessage httpResponseMessage = new HttpResponseMessage(httpResponseFromApi.StatusCode);
            string responseBody = string.Empty;

            using (Stream stream = httpResponseFromApi.GetResponseStream())
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    responseBody = reader.ReadToEnd();
                }
            }

            // Proxy all of the special headers that the API returns
            // (which all begin with "MS-").  One example is "MS-CorrelationId"
            // which is needed by the Windows Store Submission API team when they
            // are investigating bug reports with the API.
            const string MSHeaderPreface = "MS-";
            const string RetryHeader = "retry";
            const string LocationHeader = "Location";
            foreach (string key in httpResponseFromApi.Headers.AllKeys)
            {
                if (key.StartsWith(MSHeaderPreface, StringComparison.InvariantCultureIgnoreCase) ||
                    key.ToLowerInvariant().Contains(RetryHeader.ToLowerInvariant()) ||
                    (key.ToLowerInvariant() == LocationHeader.ToLowerInvariant()))
                {
                    httpResponseMessage.Headers.Add(key, httpResponseFromApi.Headers[key]);
                }
            }

            if (!string.IsNullOrEmpty(responseBody))
            {
                // The contentType string tends to have the character encoding appended to it.
                // We just want the actual contentType since we specify the content encoding separately.
                string contentType = "text/plain";
                if (!string.IsNullOrWhiteSpace(httpResponseFromApi.ContentType))
                {
                    contentType = httpResponseFromApi.ContentType.Split(';')[0];
                }

                httpResponseMessage.Content = new StringContent(responseBody, Encoding.UTF8, contentType);
            }

            return httpResponseMessage;
        }