void ParseForm() { if (HttpMethod == "POST") { //if contentType string contentType = ContentType; if (string.IsNullOrEmpty(contentType) || contentType.StartsWith("application/x-www-form-urlencoded", System.StringComparison.OrdinalIgnoreCase)) { try { byte[] buffer = ReadFormData(); string text = ContentEncoding.GetString(buffer); System.Text.Encoding encoding = GetCharset(contentType); _form = ParseNameValues(text, encoding == null ? _contentEncoding : encoding); } catch { } } else if (contentType.StartsWith("multipart/form-data;", System.StringComparison.OrdinalIgnoreCase)) { try { byte[] buffer = ReadFormData(); System.Text.Encoding encoding = GetCharset(contentType); ParseMultiPart(contentType, buffer, encoding == null ? _contentEncoding : encoding); } catch { } } } if (_form == null) { _form = new System.Collections.Specialized.NameValueCollection(); } }
public virtual string Serialize(object instance, Type type) { string result; using (var stream = new MemoryStream()) { using (var writer = XmlWriter.Create(stream, _settings)) { var serializer = CacheOrGetSerializerFor(type); if (_namespaces != null) { serializer.Serialize(writer, instance, _namespaces); } else { serializer.Serialize(writer, instance); } } #if !Smartphone && !NETCF result = ContentEncoding.GetString(stream.ToArray()); #else result = ContentEncoding.GetString(stream.ToArray(), 0, (int)stream.Length); #endif } return(result); }
void LoadMultiPart() { string boundary = GetParameter(ContentType, "; boundary="); if (boundary == null) { return; } Stream input = GetSubStream(InputStream); HttpMultipart multi_part = new HttpMultipart(input, boundary, ContentEncoding); HttpMultipart.Element e; while ((e = multi_part.ReadNextElement()) != null) { if (e.Filename == null) { byte[] copy = new byte[e.Length]; input.Position = e.Start; input.Read(copy, 0, (int)e.Length); form.Add(e.Name, ContentEncoding.GetString(copy)); } else { // // We use a substream, as in 2.x we will support large uploads streamed to disk, // HttpPostedFile sub = new HttpPostedFile(e.Filename, e.ContentType, input, e.Start, e.Length); files.AddFile(e.Name, sub); } } EndSubStream(input); }
public override void Write(byte[] buffer, int offset, int count) { string contentInBuffer = ContentEncoding.GetString(buffer, offset, count); contentInBuffer = Regex.Replace(contentInBuffer, m => m.Groups[1].Value + ImgSrc + m.Groups[3].Value); WriteToOutputStream(contentInBuffer); }
public IObservable <string> GetBody(int maxContentLength = 500000) { var bufferSize = Math.Min(maxContentLength, ContentLength); var buffer = new byte[bufferSize]; return(Observable.FromAsyncPattern <byte[], int, int, int>(InputStream.BeginRead, InputStream.EndRead)(buffer, 0, bufferSize) .Select(bytesRead => ContentEncoding.GetString(buffer, 0, bytesRead))); }
void LoadMultiPart() { string boundary = GetParameter(ContentType, "; boundary="); if (boundary == null) { return; } var input = GetSubStream(InputStream); //DB: 30/01/11 - Hack to get around non-seekable stream and received HTTP request //Not ending with \r\n? var ms = new MemoryStream(32 * 1024); input.CopyTo(ms); input = ms; ms.WriteByte((byte)'\r'); ms.WriteByte((byte)'\n'); input.Position = 0; //Uncomment to debug //var content = new StreamReader(ms).ReadToEnd(); //Console.WriteLine(boundary + "::" + content); //input.Position = 0; var multi_part = new HttpMultipart(input, boundary, ContentEncoding); HttpMultipart.Element e; while ((e = multi_part.ReadNextElement()) != null) { if (e.Filename == null) { byte[] copy = new byte[e.Length]; input.Position = e.Start; input.Read(copy, 0, (int)e.Length); form.Add(e.Name, ContentEncoding.GetString(copy)); } else { // // We use a substream, as in 2.x we will support large uploads streamed to disk, // HttpPostedFile sub = new HttpPostedFile(e.Filename, e.ContentType, input, e.Start, e.Length); files.AddFile(e.Name, sub); } } EndSubStream(input); }
public virtual string Serialize(object instance, Type type) { string result; using (var stream = new MemoryStream()) { var serializer = CacheOrGetSerializerFor(type); serializer.WriteObject(stream, instance); var data = stream.ToArray(); result = ContentEncoding.GetString(data, 0, data.Length); } return(result); }
private string GetLine() { int lineStart = parsePos; int lfIndex = Array.IndexOf(buffer, (byte)'\n', parsePos, writePos - parsePos); if (lfIndex < 0) { return(null); } parsePos = lfIndex + 1; if (lfIndex > 0 && buffer[lfIndex - 1] == '\r') { lfIndex--; } return(ContentEncoding.GetString(buffer, lineStart, lfIndex - lineStart)); }
public override void Write(byte[] buffer, int offset, int count) { string contentInBuffer = ContentEncoding.GetString(buffer); if (BodyEnd.IsMatch(contentInBuffer)) { string bodyCloseWithScript = BodyEnd.Replace(contentInBuffer, HtmlSnippet); byte[] outputBuffer = ContentEncoding.GetBytes(bodyCloseWithScript); OutputStream.Write(outputBuffer, 0, outputBuffer.Length); } else { Logger.Warn("Unable to locate '</body>' with content encoding '{0}'. Response may be compressed.", ContentEncoding.EncodingName); OutputStream.Write(buffer, offset, count); } }
public virtual string Serialize(object instance, Type type) { string result; using (var stream = new MemoryStream()) { using (var writer = XmlWriter.Create(stream, _settings)) { var serializer = CacheOrGetSerializerFor(type); writer.WriteStartDocument(); serializer.WriteObject(writer, instance); writer.Flush(); } var data = stream.ToArray(); result = ContentEncoding.GetString(data, 0, data.Length); } return(result); }
public override void Write(byte[] buffer, int offset, int count) { // There are different cases we need to deal with // Normally you would expect the contentInBuffer to contain the complete HTML code to return, but this is not always true because it is possible that // the content that will be send back is larger than the buffer foreseen by ASP.NET (currently the buffer seems to be a little bit less than 16K) // and in that case this method will be called multiple times, which might result in false positives being written to the logs for not finding a </body> // in the current chunk. // So we need to be able to deal with the following cases without writing those false positives // 1 - the </body> tag is found // 2 - the </body> tag was not found because // 2.1 - the </body> tag will be available in one of the next calls because the total length of the output is larger than 16K // 2.2 - the </body> tag is split up between this buffer and the next e.g.: "</bo" en "dy>" // 2.3 - the </body> tag will never be available (is missing) // 2.4 - Multiple </body> tags are available of which some might be part of a Javascript string or the markup is badly formatted // The easiest way to deal with this is to look for the last match for the </body> tag and if it is found we write everything before it to the // output stream and keep that </body> tag and everything that follows it (normally only a </html> tag but it can also be a 2.4 case) for the next call. // In case there is no match for the </body> tag, then we write everything to the output stream except for the last 10 characters (normally the last 6 would suffice, but we take a little margin to reassure us somehow ;-)) which we keep until the next call. // If there is a next call, then we first prepend the characters we kept from the previous call to the content inside the buffer (which might complete a chunked </body> tag for instance) // and start our check all over again (which might result in finding a </body> tag or discarding a previously found </body> tag because that one was not the last one. // Anyhow, as long as we are not a the end and a </body> tag has been found previously, the output will be buffered, just to make sure there is no other </body> tag further down the stream. // If there is no next call, then the Flush method will be called and that one will deal with the current state, which means: // - in case there was a </body> tag found, the replacement will be done // - in case there was no </body> tag found, then the warning will be written to the log, indicating something went wrong // either way, the remaining unwritten characters will be sent down the output stream. string contentInBuffer = ContentEncoding.GetString(buffer, offset, count); // Prepend remaining characters from the previous call, if any if (!string.IsNullOrEmpty(UnwrittenCharactersFromPreviousCall)) { contentInBuffer = UnwrittenCharactersFromPreviousCall + contentInBuffer; UnwrittenCharactersFromPreviousCall = null; } Match closingBodyTagMatch = BodyEndRegex.Match(contentInBuffer); if (closingBodyTagMatch.Success) { // Hooray, we found "a" </body> tag, but that doesn't mean that this is "the" last </body> tag we are looking for // so we write everything before that match to the output stream WriteToOutputStream(contentInBuffer.Substring(0, closingBodyTagMatch.Index)); // and keep the remainder for the next call or the Flush if there is no next call UnwrittenCharactersFromPreviousCall = contentInBuffer.Substring(closingBodyTagMatch.Index); } else { // there is no match found for </body> which could have different reasons like case 2.2 for instance // therefor we'll write everything except the last 10 characters to the output stream and we'll keep the last 10 characters for the next call or the Flush method if (contentInBuffer.Length <= 10) { // the content has a maximum length of 10 characters, so we don't need to write anything to the output stream and we'll keep those // characters for the next call (most likely the Flush) UnwrittenCharactersFromPreviousCall = contentInBuffer; } else { WriteToOutputStream(contentInBuffer.Substring(0, contentInBuffer.Length - 10)); UnwrittenCharactersFromPreviousCall = contentInBuffer.Substring(contentInBuffer.Length - 10); } } }