private static Stream AddContentEncodingAndGetStream(HttpSandboxParameters httpSandboxParameters, HttpResponse headers, Stream sourceStream) { switch (httpSandboxParameters.Encoding) { case "gzip": headers.AddHeader("Content-Encoding", "gzip"); return(new GZipOutputStream(sourceStream)); case "deflate": headers.AddHeader("Content-Encoding", "deflate"); return(new DeflaterOutputStream(sourceStream, new Deflater(httpSandboxParameters.DeflateLevel, true))); case "zlib": headers.AddHeader("Content-Encoding", "deflate"); return(new DeflaterOutputStream(sourceStream, new Deflater(httpSandboxParameters.DeflateLevel, false))); default: return(sourceStream); } }
public override async Task ProcessRequestAsync(HttpContext context) { // prepare the headers context.Response.Buffer = false; context.Response.AddHeader("Content-Type", "text/plain"); // parse the parameters HttpSandboxParameters parameters = HttpSandboxParameters.Parse(context.Request.QueryString.ToString()); // build the lines string[] parameterLines = parameters.GetLines().ToArray(); byte[][] lines = Enumerable.Empty <string>() .Concat(parameterLines) .Concat(Enumerable.Repeat(Line, Math.Max(0, parameters.Lines - parameterLines.Length))) .Take(parameters.Lines) .Select(l => Encoding.UTF8.GetBytes(l + "\r\n")) .ToArray(); // calculate the content length, if needed Stream responseStream; if (!parameters.Chunked) { var outputStream = new MemoryStream(); Stream encodingStream = AddContentEncodingAndGetStream(parameters, context.Response, outputStream); responseStream = context.Response.OutputStream; int offset = 0; using (encodingStream) { for (int i = 0; i < lines.Length; i++) { encodingStream.Write(lines[i], 0, lines[i].Length); encodingStream.Flush(); int newLength = (int)outputStream.Length - offset; var newLine = new byte[newLength]; Buffer.BlockCopy(outputStream.GetBuffer(), offset, newLine, 0, newLength); offset = (int)outputStream.Length; lines[i] = newLine; } } context.Response.AddHeader("Content-Length", offset.ToString(CultureInfo.InvariantCulture)); } else { responseStream = AddContentEncodingAndGetStream(parameters, context.Response, context.Response.OutputStream); } // write the lines foreach (var line in lines) { await responseStream.WriteAsync(line, 0, line.Length); await Task.Delay(parameters.Sleep); await responseStream.FlushAsync(); } }
public static HttpSandboxParameters Parse(string queryString) { NameValueCollection query = HttpUtility.ParseQueryString(queryString); var output = new HttpSandboxParameters(); // encoding string encoding = (query[EncodingKey] ?? string.Empty).Trim().ToLower(); if (!Encodings.Contains(encoding)) { encoding = output.Encoding; } output.Encoding = encoding; // chunked bool chunked; if (!bool.TryParse(query[ChunkedKey], out chunked)) { chunked = output.Chunked; } output.Chunked = chunked; // iterations int lines; if (!int.TryParse(query[LinesKey], out lines)) { lines = output.Lines; } else { if (lines < 0) { lines = 0; } } output.Lines = lines; // deflate compression level int deflateLevel; if (!int.TryParse(query[DeflateLevelKey], out deflateLevel)) { deflateLevel = output.DeflateLevel; } else { if (deflateLevel < 0) { deflateLevel = 0; } else if (deflateLevel > 9) { deflateLevel = 9; } } output.DeflateLevel = deflateLevel; // sleep milliseconds int sleep; if (!int.TryParse(query[SleepKey], out sleep)) { sleep = output.Sleep; } else { if (sleep < 0) { sleep = 0; } } output.Sleep = sleep; return(output); }