Exemplo n.º 1
0
            protected override bool PrepareHttpSend(Message message)
            {
                bool   retValue = base.PrepareHttpSend(message);
                object property;

                bool httpMethodIsHead = string.Compare(this.context.HttpMethod, "HEAD", StringComparison.OrdinalIgnoreCase) == 0;

                if (httpMethodIsHead)
                {
                    retValue = true;
                }

                if (message.Properties.TryGetValue(HttpResponseMessageProperty.Name, out property))
                {
                    HttpResponseMessageProperty responseProperty = (HttpResponseMessageProperty)property;

                    if (responseProperty.SuppressPreamble)
                    {
                        return(retValue || responseProperty.SuppressEntityBody);
                    }

                    this.SetStatusCode(responseProperty.StatusCode);
                    if (responseProperty.StatusDescription != null)
                    {
                        this.SetStatusDescription(responseProperty.StatusDescription);
                    }

                    WebHeaderCollection responseHeaders = responseProperty.Headers;
                    for (int i = 0; i < responseHeaders.Count; i++)
                    {
                        string name  = responseHeaders.Keys[i];
                        string value = responseHeaders[i];
                        if (string.Compare(name, "content-type", StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            this.contentType = value;
                        }
                        else if (string.Compare(name, HttpChannelUtilities.MIMEVersionHeader, StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            this.mimeVersion = value;
                        }
                        else if (string.Compare(name, "content-length", StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            int contentLength = -1;
                            if (httpMethodIsHead &&
                                int.TryParse(value, out contentLength))
                            {
                                this.SetContentLength(contentLength);
                            }
                        }
                        else
                        {
                            this.AddHeader(name, value);
                        }
                    }
                    if (responseProperty.SuppressEntityBody)
                    {
                        contentType = null;
                        retValue    = true;
                    }
                }

                else
                {
                    this.SetStatusCode((HttpStatusCode)statusCode);
                }

                if (contentType != null && contentType.Length != 0)
                {
                    if (this.CanSendCompressedResponses)
                    {
                        string contentEncoding;
                        if (HttpChannelUtilities.GetHttpResponseTypeAndEncodingForCompression(ref contentType, out contentEncoding))
                        {
                            result.SetContentEncoding(contentEncoding);
                        }
                    }

                    this.isSettingContentType = true;
                    this.SetContentType(contentType);
                }

                if (this.mimeVersion != null)
                {
                    this.isSettingMimeHeader = true;
                    this.AddMimeVersion(this.mimeVersion);
                }

                return(retValue);
            }