public static PodeForm Parse(byte[] bytes, string contentType, Encoding contentEncoding) { var form = new PodeForm(); if (bytes == default(byte[]) || bytes.Length == 0) { return(form); } var lines = PodeHelpers.ConvertToByteLines(bytes); var parts = contentType.Split(';'); var boundaryStart = $"--{parts[1].Split('=')[1].Trim()}"; var boundaryEnd = $"{boundaryStart}--"; var boundaryLineIndexes = new List <int>(); for (var i = 0; i < lines.Count; i++) { if (IsLineBoundary(lines[i], boundaryStart, contentEncoding) || IsLineBoundary(lines[i], boundaryEnd, contentEncoding)) { boundaryLineIndexes.Add(i); } } return(ParseHttp(form, lines, boundaryLineIndexes, contentEncoding)); }
public virtual void Dispose() { if (IsDisposed) { return; } IsDisposed = true; if (Socket != default(Socket)) { PodeSocket.CloseSocket(Socket); } if (InputStream != default(Stream)) { InputStream.Dispose(); } if (BufferStream != default(MemoryStream)) { BufferStream.Dispose(); } PodeHelpers.WriteErrorMessage($"Request disposed", Context.Listener, PodeLoggingLevel.Verbose, Context); }
public static void HandleAggregateException(AggregateException aex, PodeListener listener = default(PodeListener), PodeLoggingLevel level = PodeLoggingLevel.Error) { aex.Handle((ex) => { if (ex is IOException || ex is OperationCanceledException) { return(true); } PodeHelpers.WriteException(ex, listener, level); return(false); }); }
public static PodeForm Parse(byte[] bytes, string contentType, Encoding contentEncoding) { var form = new PodeForm(); if (bytes == default(byte[]) || bytes.Length == 0) { return(form); } var lines = PodeHelpers.ConvertToByteLines(bytes); var parts = contentType.Split(';'); var boundaryStart = $"--{parts[1].Split('=')[1].Trim()}"; var boundaryEnd = $"{boundaryStart}--"; var boundaryLineIndexes = new List <int>(); for (var i = 0; i < lines.Count; i++) { if (IsLineBoundary(lines[i], boundaryStart, contentEncoding) || IsLineBoundary(lines[i], boundaryEnd, contentEncoding)) { boundaryLineIndexes.Add(i); } } var boundaryLineIndex = 0; var disposition = string.Empty; var fields = new Dictionary <string, string>(); for (var i = 0; i < (boundaryLineIndexes.Count - 1); i++) { fields.Clear(); boundaryLineIndex = boundaryLineIndexes[i]; disposition = contentEncoding.GetString(lines[boundaryLineIndex + 1]).Trim(PodeHelpers.NEW_LINE_ARRAY); foreach (var line in disposition.Split(';')) { var atoms = line.Split('='); if (atoms.Length == 2) { fields.Add(atoms[0].Trim(), atoms[1].Trim(' ', '"')); } } if (!fields.ContainsKey("filename")) { form.Data.Add(new PodeFormData(fields["name"], contentEncoding.GetString(lines[boundaryLineIndex + 3]).Trim(PodeHelpers.NEW_LINE_ARRAY))); } if (fields.ContainsKey("filename")) { form.Data.Add(new PodeFormData(fields["name"], fields["filename"])); if (!string.IsNullOrWhiteSpace(fields["filename"])) { var fileContentType = contentEncoding.GetString(lines[boundaryLineIndex + 2]).Trim(PodeHelpers.NEW_LINE_ARRAY); var fileBytesLength = 0; var stream = new MemoryStream(); for (var j = (boundaryLineIndex + 4); j <= (boundaryLineIndexes[i + 1] - 1); j++) { fileBytesLength = lines[j].Length; if (j == (boundaryLineIndexes[i + 1] - 1)) { if (lines[j][fileBytesLength - 1] == PodeHelpers.NEW_LINE_BYTE) { fileBytesLength--; } if (lines[j][fileBytesLength - 1] == PodeHelpers.CARRIAGE_RETURN_BYTE) { fileBytesLength--; } } stream.Write(lines[j], 0, fileBytesLength); } form.Files.Add(new PodeFormFile(fields["filename"], stream, fields["name"], fileContentType.Split(':')[1].Trim())); } } } return(form); }