1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
//------------------------------------------------------------------------------
// <copyright file="HttpFileCollection.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
/*
* Collection of posted files for the request intrinsic
*
* Copyright (c) 1998 Microsoft Corporation
*/
namespace System.Web {
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Diagnostics.CodeAnalysis;
using System.Web.Util;
/// <devdoc>
/// <para>
/// Accesses incoming files uploaded by a client (using
/// multipart MIME and the Http Content-Type of multipart/formdata).
/// </para>
/// </devdoc>
public sealed class HttpFileCollection : NameObjectCollectionBase {
// cached All[] arrays
private HttpPostedFile[] _all;
private String[] _allKeys;
// for implementing granular request validation
private ValidateStringCallback _validationCallback;
private HashSet<HttpPostedFile> _filesAwaitingValidation;
[SuppressMessage("Microsoft.Globalization", "CA1309:UseOrdinalStringComparison", MessageId = "System.Collections.Specialized.NameObjectCollectionBase.#ctor(System.Collections.IEqualityComparer)", Justification = @"By design")]
internal HttpFileCollection()
: base(StringComparer.InvariantCultureIgnoreCase) {
}
// This copy constructor is used by the granular request validation feature. Since these collections are immutable
// once created, it's ok for us to have two collections containing the same data.
internal HttpFileCollection(HttpFileCollection col)
: this() {
// We explicitly don't copy validation-related fields, as we want the copy to "reset" validation state.
// Copy the file references from the original collection into this instance
for (int i = 0; i < col.Count; i++) {
ThrowIfMaxHttpCollectionKeysExceeded();
string key = col.BaseGetKey(i);
object value = col.BaseGet(i);
BaseAdd(key, value);
}
IsReadOnly = col.IsReadOnly;
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public void CopyTo(Array dest, int index) {
if (_all == null) {
int n = Count;
HttpPostedFile[] all = new HttpPostedFile[n];
for (int i = 0; i < n; i++)
all[i] = Get(i);
_all = all; // wait until end of loop to set _all reference in case Get throws
}
if (_all != null) {
_all.CopyTo(dest, index);
}
}
internal void AddFile(String key, HttpPostedFile file) {
ThrowIfMaxHttpCollectionKeysExceeded();
_all = null;
_allKeys = null;
BaseAdd(key, file);
}
// MSRC 12038: limit the maximum number of items that can be added to the collection,
// as a large number of items potentially can result in too many hash collisions that may cause DoS
private void ThrowIfMaxHttpCollectionKeysExceeded() {
if (Count >= AppSettings.MaxHttpCollectionKeys) {
throw new InvalidOperationException(SR.GetString(SR.CollectionCountExceeded_HttpValueCollection, AppSettings.MaxHttpCollectionKeys));
}
}
internal void EnableGranularValidation(ValidateStringCallback validationCallback) {
// Iterate over all the files, adding each to the set containing the files awaiting validation.
// Unlike dictionaries, HashSet<T> can contain null keys, so don't need to special-case them.
_filesAwaitingValidation = new HashSet<HttpPostedFile>();
for (int i = 0; i < Count; i++) {
_filesAwaitingValidation.Add((HttpPostedFile)BaseGet(i));
}
_validationCallback = validationCallback;
}
private void EnsureFileValidated(HttpPostedFile file) {
if (_filesAwaitingValidation == null) {
// If dynamic validation hasn't been enabled, no-op.
return;
}
if (!_filesAwaitingValidation.Contains(file)) {
// If this file has already been validated (or is excluded), no-op.
return;
}
// If validation fails, the callback will throw an exception. If validation succeeds,
// we can remove it from the candidates list. Key is unused.
_validationCallback(null /* key */, file.FileName);
_filesAwaitingValidation.Remove(file);
}
//
// Access by name
//
/// <devdoc>
/// <para>
/// Returns a file from
/// the collection by file name.
/// </para>
/// </devdoc>
public HttpPostedFile Get(String name) {
HttpPostedFile file = (HttpPostedFile)BaseGet(name);
if (file != null) {
EnsureFileValidated(file);
}
return file;
}
/// <devdoc>
/// Returns all files from this collection that match the given name.
/// This is to support multi-file uploads (either via HTML5 or JS emulators).
/// This method returns a new collection instance for each invocation and callers are
/// encouraged to call this method once per name per request.
/// </devdoc>
[SuppressMessage("Microsoft.Globalization", "CA1309:UseOrdinalStringComparison", MessageId = "System.String.Equals(System.String,System.String,System.StringComparison)", Justification = @"By design")]
public IList<HttpPostedFile> GetMultiple(string name) {
List<HttpPostedFile> result = new List<HttpPostedFile>();
for (int i = 0; i < Count; i++) {
string key = GetKey(i);
// Use InvariantCultureIgnoreCase since this is the comparison used for this collection
if (String.Equals(key, name, StringComparison.InvariantCultureIgnoreCase)) {
// Call the Get() method instead of looking at the _all array directly
// to ensure that request validation happens for the file.
result.Add(Get(i));
}
}
return result.AsReadOnly();
}
/// <devdoc>
/// <para>Returns item value from collection.</para>
/// </devdoc>
public HttpPostedFile this[String name]
{
get { return Get(name);}
}
//
// Indexed access
//
/// <devdoc>
/// <para>
/// Returns a file from
/// the file collection by index.
/// </para>
/// </devdoc>
public HttpPostedFile Get(int index) {
HttpPostedFile file = (HttpPostedFile)BaseGet(index);
if (file != null) {
EnsureFileValidated(file);
}
return file;
}
/// <devdoc>
/// <para>
/// Returns key name from collection.
/// </para>
/// </devdoc>
public String GetKey(int index) {
return BaseGetKey(index);
}
/// <devdoc>
/// <para>
/// Returns an
/// item from the collection.
/// </para>
/// </devdoc>
public HttpPostedFile this[int index]
{
get { return Get(index);}
}
//
// Access to keys and values as arrays
//
/// <devdoc>
/// <para>
/// Creates an
/// array of keys in the collection.
/// </para>
/// </devdoc>
public String[] AllKeys {
get {
if (_allKeys == null)
_allKeys = BaseGetAllKeys();
return _allKeys;
}
}
}
}
|