Example #1
0
        XPathHttpResponse GetResponse(HttpWebResponse httpResponse)
        {
            var response = new XPathHttpResponse {
                Status  = httpResponse.StatusCode,
                Message = httpResponse.StatusDescription,
                Headers = { httpResponse.Headers }
            };

            if (httpResponse.ContentLength > 0)
            {
                string mediaType = "";

                try {
                    var contentType = new ContentType(httpResponse.ContentType);
                    mediaType = contentType.MediaType;
                } catch (FormatException) {
                } catch (ArgumentException) {
                }

                if (MediaTypes.IsMultipart(mediaType))
                {
                    var multipart = new XPathHttpMultipart {
                        MediaType = mediaType
                    };

                    using (Stream responseBody = httpResponse.GetResponseStream()) {
                        multipart.Deserialize(responseBody, this.StatusOnly);
                    }

                    response.Multipart = multipart;
                }
                else
                {
                    var body = new XPathHttpBody {
                        MediaType = mediaType
                    };

                    if (!String.IsNullOrEmpty(httpResponse.CharacterSet))
                    {
                        try {
                            body.Encoding = Encoding.GetEncoding(httpResponse.CharacterSet);
                        } catch (ArgumentException) { }
                    }

                    if (!this.StatusOnly)
                    {
                        using (Stream responseBody = httpResponse.GetResponseStream())
                            body.Deserialize(responseBody, httpResponse.ResponseUri, this.ItemFactory, this.OverrideMediaType);
                    }

                    response.Body = body;
                }
            }

            return(response);
        }
Example #2
0
        static XmlQualifiedName GetMethodFromMediaType(string mediaType, XmlQualifiedName defaultValue)
        {
            if (MediaTypes.Equals(mediaType, MediaTypes.XHtml))
            {
                return(XPathSerializationMethods.XHtml);
            }

            if (MediaTypes.IsXml(mediaType))
            {
                return(XPathSerializationMethods.Xml);
            }

            if (MediaTypes.Equals(mediaType, MediaTypes.Html))
            {
                return(XPathSerializationMethods.Html);
            }

            if (MediaTypes.IsText(mediaType))
            {
                return(XPathSerializationMethods.Text);
            }

            return(defaultValue);
        }
Example #3
0
        void EnsureValidForHttpRequest()
        {
            if (this.Href == null)
            {
                throw new InvalidOperationException("Href cannot be null.");
            }

            if (String.IsNullOrEmpty(this.Method))
            {
                throw new InvalidOperationException("Method cannot be null or empty.");
            }

            if (this.Multipart != null &&
                this.Body != null)
            {
                throw new InvalidOperationException("Multipart and Body properties are mutually exclusive.");
            }

            if (!String.IsNullOrEmpty(this.Username))
            {
                if (String.IsNullOrEmpty(this.Password))
                {
                    throw new InvalidOperationException("Password cannot be null or empty if Username isn't.");
                }

                if (String.IsNullOrEmpty(this.AuthMethod))
                {
                    throw new InvalidOperationException("AuthMethod cannot be null or empty if Username isn't.");
                }
            }
            else if (this.SendAuthorization)
            {
                throw new InvalidOperationException("SendAuthorization cannot be true if Username is null or empty.");
            }

            if (this.Body != null)
            {
                if (String.IsNullOrEmpty(this.Body.MediaType))
                {
                    throw new InvalidOperationException("Body.MediaType cannot be null or empty.");
                }
            }
            else if (this.Multipart != null)
            {
                if (this.Multipart.Items.Count == 0)
                {
                    throw new InvalidOperationException("A multipart request must have at least one part.");
                }

                if (String.IsNullOrEmpty(this.Multipart.MediaType))
                {
                    throw new InvalidOperationException("Multipart.MediaType cannot be null or empty.");
                }

                if (!MediaTypes.IsMultipart(this.Multipart.MediaType))
                {
                    throw new InvalidOperationException("Multipart.MediaType must be set to a multipart media type.");
                }

                for (int i = 0; i < this.Multipart.Items.Count; i++)
                {
                    XPathHttpMultipartItem item = this.Multipart.Items[i];

                    if (item.Body == null)
                    {
                        throw new InvalidOperationException("Multipart.Items[" + i + "].Body cannot be null.");
                    }

                    if (String.IsNullOrEmpty(item.Body.MediaType))
                    {
                        throw new InvalidOperationException("Multipart.Items[" + i + "].MediaType cannot be null or empty.");
                    }

                    if (item.Body.Content == null &&
                        item.Body.Src == null)
                    {
                        throw new InvalidOperationException("All multipart bodies must have content.");
                    }
                }
            }
        }