public async Task <QueryParamCollection> GetFormData() { var form = new WebROCollection(); files = new Dictionary <string, HttpPostedFile>(); if (IsContentType("multipart/form-data", true)) { await LoadMultiPart(form).ConfigureAwait(false); } else if (IsContentType("application/x-www-form-urlencoded", true)) { await LoadWwwForm(form).ConfigureAwait(false); } #if NET_4_0 if (validateRequestNewMode && !checked_form) { // Setting this before calling the validator prevents // possible endless recursion checked_form = true; ValidateNameValueCollection("Form", query_string_nvc, RequestValidationSource.Form); } else #endif if (validate_form && !checked_form) { checked_form = true; ValidateNameValueCollection("Form", form); } return(form); }
private static void AddRawKeyValue(WebROCollection form, StringBuilder key, StringBuilder value) { form.Add(WebUtility.UrlDecode(key.ToString()), WebUtility.UrlDecode(value.ToString())); key.Length = 0; value.Length = 0; }
private async Task LoadWwwForm(WebROCollection form) { using (var input = InputStream) { using (var ms = new MemoryStream()) { await input.CopyToAsync(ms).ConfigureAwait(false); ms.Position = 0; using (var s = new StreamReader(ms, ContentEncoding)) { var key = new StringBuilder(); var value = new StringBuilder(); int c; while ((c = s.Read()) != -1) { if (c == '=') { value.Length = 0; while ((c = s.Read()) != -1) { if (c == '&') { AddRawKeyValue(form, key, value); break; } else { value.Append((char)c); } } if (c == -1) { AddRawKeyValue(form, key, value); return; } } else if (c == '&') { AddRawKeyValue(form, key, value); } else { key.Append((char)c); } } if (c == -1) { AddRawKeyValue(form, key, value); } } } } }
void AddRawKeyValue(WebROCollection form, StringBuilder key, StringBuilder value) { string decodedKey = WebUtility.UrlDecode(key.ToString()); form.Add(decodedKey, WebUtility.UrlDecode(value.ToString())); key.Length = 0; value.Length = 0; }
public LazyWebROCollection(RequestValidationSource validationSource, WebROCollection wrapped) { if (wrapped == null) { throw new ArgumentNullException("wrapped"); } this.validationSource = validationSource; this.wrapped = wrapped; }
private async Task LoadMultiPart(WebROCollection form) { string boundary = GetParameter(ContentType, "; boundary="); if (boundary == null) { return; } using (var requestStream = 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); await requestStream.CopyToAsync(ms).ConfigureAwait(false); var 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, (e.Encoding ?? ContentEncoding).GetString(copy, 0, copy.Length)); } else { // // We use a substream, as in 2.x we will support large uploads streamed to disk, // var sub = new HttpPostedFile(e.Filename, e.ContentType, input, e.Start, e.Length); files[e.Name] = sub; } } } }
public async Task <QueryParamCollection> GetFormData() { var form = new WebROCollection(); files = new Dictionary <string, HttpPostedFile>(); if (IsContentType("multipart/form-data")) { await LoadMultiPart(form).ConfigureAwait(false); } else if (IsContentType("application/x-www-form-urlencoded")) { await LoadWwwForm(form).ConfigureAwait(false); } if (validate_form && !checked_form) { checked_form = true; ValidateNameValueCollection("Form", form); } return(form); }