public bool CanFormatResponse(ContentType acceptHeaderElement, bool matchCharset, out ContentType contentType) { if (acceptHeaderElement == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("acceptHeaderElement"); } // Scrub the content type so that it is only mediaType and the charset string charset = acceptHeaderElement.CharSet; contentType = new ContentType(acceptHeaderElement.MediaType); contentType.CharSet = this.DefaultContentType.CharSet; string contentTypeStr = contentType.ToString(); if (matchCharset && !string.IsNullOrEmpty(charset) && !string.Equals(charset, this.DefaultContentType.CharSet, StringComparison.OrdinalIgnoreCase)) { return false; } if (this.contentTypeMapper != null && this.contentTypeMapper.GetMessageFormatForContentType(contentType.MediaType) == this.ContentFormat) { return true; } if (this.Encoder.IsContentTypeSupported(contentTypeStr) && (charset == null || contentType.CharSet == this.DefaultContentType.CharSet)) { return true; } contentType = null; return false; }
public static async Task HandleGetFileRequestAsync(HttpRequest request, HttpResponse response, IHostingEnvironment env) { IFileInfo fileInfo = request.Path.GetContentFileInfo(env.WebRootFileProvider); if (fileInfo.Exists) { System.Net.Mime.ContentType contentType = ResponseHelper.ContentTypeFromName(fileInfo.Name); contentType.Name = fileInfo.Name; response.ContentType = contentType.ToString(); response.StatusCode = StatusCodes.Status200OK; await response.SendFileAsync(fileInfo); } else if (fileInfo is GetFileError) { await SendErrorResponseAsync(response, "Error locating resource", env, tw => { tw.WriteString("Unexpected error locating file at "); tw.WriteElementString("code", fileInfo.Name); tw.WriteString("."); }, ((GetFileError)fileInfo).Error); } else { await SendErrorResponseAsync(response, "Error locating resource", env, tw => { tw.WriteString("Unable to locate file at "); tw.WriteElementString("code", fileInfo.Name); tw.WriteString("."); }, ((GetFileError)fileInfo).Error); } }
public override void ExecuteResult(ControllerContext context) { if (context == null) throw new ArgumentNullException("context"); if (Feed == null) throw new InvalidOperationException("FeedFormatter must not be null"); HttpResponseBase response = context.HttpContext.Response; ContentType contentType = new ContentType { MediaType = "application/atom+xml", CharSet = "utf-8" }; response.Clear(); response.StatusCode = (int) HttpStatusCode.OK; response.ContentType = contentType.ToString(); SyndicationFeedFormatter formatter = Feed.GetAtom10Formatter(); using (XmlWriter xmlWriter = XmlWriter.Create(response.Output, new XmlWriterSettings { Encoding = Encoding.UTF8 })) formatter.WriteTo(xmlWriter); }
private static async Task DeregisterNodeAtNeighbour(string hostname, int port) { var uriBuilder = new UriBuilder(Uri.UriSchemeHttp, hostname, port, "/deregisterNode"); var req = WebRequest.Create(uriBuilder.Uri) as HttpWebRequest; req.Method = WebRequestMethods.Http.Post; var contentTypeObj = new ContentType(MediaTypeNames.Text.Plain); contentTypeObj.CharSet = Encoding.ASCII.WebName; req.ContentType = contentTypeObj.ToString(); var reqBodyData = Encoding.ASCII.GetBytes(nodeGuid.ToString()); req.ContentLength = reqBodyData.LongLength; using (var reqStream = await req.GetRequestStreamAsync()) { await reqStream.WriteAsync(reqBodyData, 0, reqBodyData.Length); await reqStream.FlushAsync(); } using (var resp = await req.GetResponseAsync()) { } }
bool Equals (ContentType other) { return other != null && ToString () == other.ToString (); }
public void ToStringTest2 () { ContentType dummy = new ContentType ("text/plain; charset=us-ascii"); Assert.AreEqual ("text/plain; charset=us-ascii", dummy.ToString ()); }
private void SendBodyWithAlternateViews (MailMessage message, string boundary, bool attachmentExists) { AlternateViewCollection alternateViews = message.AlternateViews; string inner_boundary = GenerateBoundary (); ContentType messageContentType = new ContentType (); messageContentType.Boundary = inner_boundary; messageContentType.MediaType = "multipart/alternative"; if (!attachmentExists) { SendHeader (HeaderName.ContentType, messageContentType.ToString ()); SendData (String.Empty); } // body section AlternateView body = null; if (message.Body != null) { body = AlternateView.CreateAlternateViewFromString (message.Body, message.BodyEncoding, message.IsBodyHtml ? "text/html" : "text/plain"); alternateViews.Insert (0, body); StartSection (boundary, messageContentType); } try { // alternate view sections foreach (AlternateView av in alternateViews) { string alt_boundary = null; ContentType contentType; if (av.LinkedResources.Count > 0) { alt_boundary = GenerateBoundary (); contentType = new ContentType ("multipart/related"); contentType.Boundary = alt_boundary; contentType.Parameters ["type"] = av.ContentType.ToString (); StartSection (inner_boundary, contentType); StartSection (alt_boundary, av.ContentType, av.TransferEncoding); } else { contentType = new ContentType (av.ContentType.ToString ()); StartSection (inner_boundary, contentType, av.TransferEncoding); } switch (av.TransferEncoding) { case TransferEncoding.Base64: byte [] content = new byte [av.ContentStream.Length]; av.ContentStream.Read (content, 0, content.Length); #if TARGET_JVM SendData (Convert.ToBase64String (content)); #else SendData (Convert.ToBase64String (content, Base64FormattingOptions.InsertLineBreaks)); #endif break; case TransferEncoding.QuotedPrintable: byte [] bytes = new byte [av.ContentStream.Length]; av.ContentStream.Read (bytes, 0, bytes.Length); SendData (ToQuotedPrintable (bytes)); break; case TransferEncoding.SevenBit: case TransferEncoding.Unknown: content = new byte [av.ContentStream.Length]; av.ContentStream.Read (content, 0, content.Length); SendData (Encoding.ASCII.GetString (content)); break; } if (av.LinkedResources.Count > 0) { SendLinkedResources (message, av.LinkedResources, alt_boundary); EndSection (alt_boundary); } if (!attachmentExists) SendData (string.Empty); } } finally { if (body != null) alternateViews.Remove (body); } EndSection (inner_boundary); }
private void SendWithAttachments (MailMessage message) { string boundary = GenerateBoundary (); // first "multipart/mixed" ContentType messageContentType = new ContentType (); messageContentType.Boundary = boundary; messageContentType.MediaType = "multipart/mixed"; messageContentType.CharSet = null; SendHeader (HeaderName.ContentType, messageContentType.ToString ()); SendData (String.Empty); // body section Attachment body = null; if (message.AlternateViews.Count > 0) SendWithoutAttachments (message, boundary, true); else { body = Attachment.CreateAttachmentFromString (message.Body, null, message.BodyEncoding, message.IsBodyHtml ? "text/html" : "text/plain"); message.Attachments.Insert (0, body); } try { SendAttachments (message, body, boundary); } finally { if (body != null) message.Attachments.Remove (body); } EndSection (boundary); }
private void StartSection (string section, ContentType sectionContentType, TransferEncoding transferEncoding, ContentDisposition contentDisposition) { SendData (String.Format ("--{0}", section)); SendHeader ("content-type", sectionContentType.ToString ()); SendHeader ("content-transfer-encoding", GetTransferEncodingName (transferEncoding)); if (contentDisposition != null) SendHeader ("content-disposition", contentDisposition.ToString ()); SendData (string.Empty); }
bool Equals(ContentType other) { return(other != null && ToString() == other.ToString()); }
private void StartSection (string section, ContentType sectionContentType) { SendData (String.Format ("--{0}", section)); SendHeader ("content-type", sectionContentType.ToString ()); SendData (string.Empty); }
private void StartSection (string section, ContentType sectionContentType, Attachment att, bool sendDisposition) { SendData (String.Format ("--{0}", section)); if (!string.IsNullOrEmpty (att.ContentId)) SendHeader("content-ID", "<" + att.ContentId + ">"); SendHeader ("content-type", sectionContentType.ToString ()); SendHeader ("content-transfer-encoding", GetTransferEncodingName (att.TransferEncoding)); if (sendDisposition) SendHeader ("content-disposition", att.ContentDisposition.ToString ()); SendData (string.Empty); }
public void TestContentType(ContentType contentType, int paramCount) { string fieldText = contentType.ToString(); MimeFieldParameters fieldParams = new MimeFieldParameters(); Assert.DoesNotThrow(() => fieldParams.Deserialize(fieldText)); Assert.True(fieldParams.Count == paramCount); Assert.Equal(contentType.MediaType, fieldParams[0].Value); Assert.Equal<string>(contentType.Name, fieldParams["name"]); Assert.DoesNotThrow(() => Compare(fieldParams, contentType.Parameters)); string fieldTextSerialized = null; Assert.DoesNotThrow(() => fieldTextSerialized = fieldParams.Serialize()); fieldParams.Clear(); Assert.DoesNotThrow(() => fieldParams.Deserialize(fieldTextSerialized)); Assert.True(fieldParams.Count == paramCount); Assert.DoesNotThrow(() => new ContentType(fieldTextSerialized)); }
private static async Task ConnectToNodesContext(HttpListenerContext http_ctx) { var contentTypeObj = new ContentType(MediaTypeNames.Text.Plain); contentTypeObj.CharSet = Encoding.ASCII.WebName; var contentTypeString = contentTypeObj.ToString(); var reqBodyData = Encoding.ASCII.GetBytes(GetNodeSpecString(http_ctx)); var registrationTaksList = new List<Task>(); using (var bodyReader = new StreamReader(http_ctx.Request.InputStream, http_ctx.Request.ContentEncoding)) { var bodyLineTask = bodyReader.ReadLineAsync(); for (var bodyLine = await bodyLineTask; bodyLine != null; bodyLine = await bodyLineTask) { bodyLineTask = bodyReader.ReadLineAsync(); var nodeAddr = bodyLine; var colIdx = nodeAddr.LastIndexOf(':'); UriBuilder uriBuilder; if (colIdx < 0) uriBuilder = new UriBuilder(Uri.UriSchemeHttp, nodeAddr); else uriBuilder = new UriBuilder(Uri.UriSchemeHttp, nodeAddr.Substring(0, colIdx), int.Parse(nodeAddr.Substring(colIdx + 1))); registrationTaksList.Add(ExchangeWithNode(uriBuilder.Uri, reqBodyData, contentTypeString)); } } await Task.WhenAll(registrationTaksList); http_ctx.Response.StatusCode = (int)HttpStatusCode.OK; http_ctx.Response.Close(new byte[0], willBlock: false); }
private void StartSection(string section, ContentType sectionContentType, TransferEncoding transferEncoding, LinkedResource lr) { SendData (String.Format("--{0}", section)); SendHeader ("content-type", sectionContentType.ToString ()); SendHeader ("content-transfer-encoding", GetTransferEncodingName (transferEncoding)); if (lr.ContentId != null && lr.ContentId.Length > 0) SendHeader("content-ID", "<" + lr.ContentId + ">"); SendData (string.Empty); }
public static void SetContentType(this HttpListenerResponse myResponse, ContentType myContentType) { myResponse.ContentType = myContentType.ToString(); }
/// <summary> /// Gets the type of the content. /// </summary> /// <param name="acceptValue">The accept value.</param> /// <param name="resolvedContentType">Type of the resolved content.</param> /// <returns>The response content type</returns> public static string GetContentType(ContentType acceptValue, ContentType resolvedContentType) { switch (acceptValue.MediaType) { case SdmxMedia.ApplicationXml: case SdmxMedia.TextXml: return acceptValue.ToString(); case "text/*": return SdmxMedia.TextXml; default: return resolvedContentType.ToString(); } }