/// <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> /// 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", 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", Resources.HttpMessageInvalidMediaType, FormattingUtilities.HttpContentType.Name, isRequest ? DefaultRequestMediaType : DefaultResponseMediaType); } else { return(false); } } return(true); } } } if (throwOnError) { throw Error.Argument("content", Resources.HttpMessageInvalidMediaType, FormattingUtilities.HttpContentType.Name, isRequest ? DefaultRequestMediaType : DefaultResponseMediaType); } else { 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); } } }