Beispiel #1
0
        /// <summary>
        /// Looks for the "start" parameter of the parent's content type and then finds the corresponding
        /// child HttpContent with a matching Content-ID header field.
        /// </summary>
        /// <returns>The matching child or null if none found.</returns>
        private static HttpContent FindRootContent(HttpContent parent, IEnumerable <HttpContent> children)
        {
            Contract.Assert(children != null);

            // Find 'start' parameter from parent content type. The value is used
            // to identify the MIME body with the corresponding Content-ID header value.
            NameValueHeaderValue startNameValue = FindMultipartRelatedParameter(parent, StartParameter);

            if (startNameValue == null)
            {
                // If we didn't find a "start" parameter then take the first child.
                return(children.FirstOrDefault());
            }

            // Look for the child with a Content-ID header that corresponds to the "start" value.
            // If no matching child is found then we return null.
            string startValue = FormattingUtilities.UnquoteToken(startNameValue.Value);

            return(children.FirstOrDefault(
                       content =>
            {
                IEnumerable <string> values;
                if (content.Headers.TryGetValues(ContentID, out values))
                {
                    return String.Equals(
                        FormattingUtilities.UnquoteToken(values.ElementAt(0)),
                        startValue,
                        StringComparison.OrdinalIgnoreCase);
                }

                return false;
            }));
        }
        /// <summary>
        /// Read the non-file contents as form data.
        /// </summary>
        /// <returns>A <see cref="Task"/> representing the post processing.</returns>
        public static async Task ReadFormDataAsync(
            Collection <HttpContent> contents,
            NameValueCollection formData,
            CancellationToken cancellationToken
            )
        {
            // Find instances of HttpContent for which we created a memory stream and read them asynchronously
            // to get the string content and then add that as form data
            foreach (HttpContent content in contents)
            {
                ContentDispositionHeaderValue contentDisposition =
                    content.Headers.ContentDisposition;
                // If FileName is null or empty, the content is form data and will be processed.
                if (String.IsNullOrEmpty(contentDisposition.FileName))
                {
                    // Extract name from Content-Disposition header. We know from earlier that the header is present.
                    string formFieldName =
                        FormattingUtilities.UnquoteToken(contentDisposition.Name) ?? String.Empty;

                    // Read the contents as string data and add to form data
                    cancellationToken.ThrowIfCancellationRequested();
                    string formFieldValue = await content.ReadAsStringAsync();

                    formData.Add(formFieldName, formFieldValue);
                }
            }
        }
 private static bool FirstDispositionName(HttpContent content, string dispositionName)
 {
     Contract.Assert(content != null, "content cannot be null");
     Contract.Assert(dispositionName != null, "dispositionName cannot be null");
     return(content.Headers != null && content.Headers.ContentDisposition != null &&
            String.Equals(
                FormattingUtilities.UnquoteToken(content.Headers.ContentDisposition.Name),
                FormattingUtilities.UnquoteToken(dispositionName),
                StringComparison.OrdinalIgnoreCase));
 }
Beispiel #4
0
        /// <summary>
        /// Validates whether the content contains an HTTP Request or an HTTP Response.
        /// </summary>
        /// <param name="content">The content to validate.</param>
        /// <param name="isRequest">if set to <c>true</c> if the content is either an HTTP Request or an HTTP Response.</param>
        /// <param name="throwOnError">Indicates whether validation failure should result in an <see cref="Exception"/> or not.</param>
        /// <returns><c>true</c> if content is either an HTTP Request or an HTTP Response</returns>
        internal static bool ValidateHttpMessageContent(HttpContent content, bool isRequest, bool throwOnError)
        {
            if (content == null)
            {
                throw Error.ArgumentNull("content");
            }

            MediaTypeHeaderValue contentType = content.Headers.ContentType;

            if (contentType != null)
            {
                if (!contentType.MediaType.Equals(DefaultMediaType, StringComparison.OrdinalIgnoreCase))
                {
                    if (throwOnError)
                    {
                        throw Error.Argument("content", Properties.Resources.HttpMessageInvalidMediaType, FormattingUtilities.HttpContentType.Name,
                                             isRequest ? DefaultRequestMediaType : DefaultResponseMediaType);
                    }
                    else
                    {
                        return(false);
                    }
                }

                foreach (NameValueHeaderValue parameter in contentType.Parameters)
                {
                    if (parameter.Name.Equals(MsgTypeParameter, StringComparison.OrdinalIgnoreCase))
                    {
                        string msgType = FormattingUtilities.UnquoteToken(parameter.Value);
                        if (!msgType.Equals(isRequest ? DefaultRequestMsgType : DefaultResponseMsgType, StringComparison.OrdinalIgnoreCase))
                        {
                            if (throwOnError)
                            {
                                throw Error.Argument("content", Properties.Resources.HttpMessageInvalidMediaType, FormattingUtilities.HttpContentType.Name, isRequest ? DefaultRequestMediaType : DefaultResponseMediaType);
                            }
                            else
                            {
                                return(false);
                            }
                        }

                        return(true);
                    }
                }
            }

            if (throwOnError)
            {
                throw Error.Argument("content", Properties.Resources.HttpMessageInvalidMediaType, FormattingUtilities.HttpContentType.Name, isRequest ? DefaultRequestMediaType : DefaultResponseMediaType);
            }
            else
            {
                return(false);
            }
        }
        private static bool FirstStart(HttpContent content, string start)
        {
            Contract.Assert(content != null, "content cannot be null");
            Contract.Assert(start != null, "start cannot be null");
            if (content.Headers != null)
            {
                IEnumerable <string> values;
                if (content.Headers.TryGetValues(ContentID, out values))
                {
                    return(String.Equals(
                               FormattingUtilities.UnquoteToken(values.ElementAt(0)),
                               FormattingUtilities.UnquoteToken(start),
                               StringComparison.OrdinalIgnoreCase));
                }
            }

            return(false);
        }
Beispiel #6
0
        /// <summary>
        /// Read the non-file contents as form data.
        /// </summary>
        /// <returns></returns>
        public override async Task ExecutePostProcessingAsync()
        {
            // Find instances of HttpContent for which we created a memory stream and read them asynchronously
            // to get the string content and then add that as form data
            for (int index = 0; index < Contents.Count; index++)
            {
                if (_isFormData[index])
                {
                    HttpContent formContent = Contents[index];
                    // Extract name from Content-Disposition header. We know from earlier that the header is present.
                    ContentDispositionHeaderValue contentDisposition = formContent.Headers.ContentDisposition;
                    string formFieldName = FormattingUtilities.UnquoteToken(contentDisposition.Name) ?? String.Empty;

                    // Read the contents as string data and add to form data
                    string formFieldValue = await formContent.ReadAsStringAsync();

                    FormData.Add(formFieldName, formFieldValue);
                }
            }
        }
Beispiel #7
0
        /// <summary>
        /// Read the non-file contents as form data.
        /// </summary>
        /// <returns></returns>
        public override Task ExecutePostProcessingAsync()
        {
            // Find instances of HttpContent for which we created a memory stream and read them asynchronously
            // to get the string content and then add that as form data
            IEnumerable <Task> readTasks = Contents.Where((content, index) => _isFormData[index] == true).Select(
                formContent =>
            {
                // Extract name from Content-Disposition header. We know from earlier that the header is present.
                ContentDispositionHeaderValue contentDisposition = formContent.Headers.ContentDisposition;
                string formFieldName = FormattingUtilities.UnquoteToken(contentDisposition.Name) ?? String.Empty;

                // Read the contents as string data and add to form data
                return(formContent.ReadAsStringAsync().Then(
                           formFieldValue =>
                {
                    FormData.Add(formFieldName, formFieldValue);
                }, runSynchronously: true));
            });

            // Actually execute the read tasks while trying to stay on the same thread
            return(TaskHelpers.Iterate(readTasks));
        }
Beispiel #8
0
        public void UnquoteTokenReturnsUnquotedStrings(string token, string expectedResult)
        {
            string result = FormattingUtilities.UnquoteToken(token);

            Assert.Equal(expectedResult, result);
        }
Beispiel #9
0
        public void UnquoteTokenReturnsSameRefNonQuotedStrings(string test)
        {
            string result = FormattingUtilities.UnquoteToken(test);

            Assert.Equal(test, result);
        }
Beispiel #10
0
        public void UnquoteTokenReturnsSameRefOnEmpty(string empty)
        {
            string result = FormattingUtilities.UnquoteToken(empty);

            Assert.Same(empty, result);
        }