summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Core/Messaging/MultipartPostPart.cs
blob: b4a0968d5125e6c583d303e9c5474b58cab1bcf6 (plain)
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
//-----------------------------------------------------------------------
// <copyright file="MultipartPostPart.cs" company="Outercurve Foundation">
//     Copyright (c) Outercurve Foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.Messaging {
	using System;
	using System.Collections.Generic;
	using System.Diagnostics.CodeAnalysis;
	using System.IO;
	using System.Net;
	using System.Text;
	using Validation;

	/// <summary>
	/// Represents a single part in a HTTP multipart POST request.
	/// </summary>
	public class MultipartPostPart : IDisposable {
		/// <summary>
		/// The "Content-Disposition" string.
		/// </summary>
		private const string ContentDispositionHeader = "Content-Disposition";

		/// <summary>
		/// The two-character \r\n newline character sequence to use.
		/// </summary>
		private const string NewLine = "\r\n";

		/// <summary>
		/// Initializes a new instance of the <see cref="MultipartPostPart"/> class.
		/// </summary>
		/// <param name="contentDisposition">The content disposition of the part.</param>
		public MultipartPostPart(string contentDisposition) {
			Requires.NotNullOrEmpty(contentDisposition, "contentDisposition");

			this.ContentDisposition = contentDisposition;
			this.ContentAttributes = new Dictionary<string, string>();
			this.PartHeaders = new WebHeaderCollection();
		}

		/// <summary>
		/// Gets or sets the content disposition.
		/// </summary>
		/// <value>The content disposition.</value>
		public string ContentDisposition { get; set; }

		/// <summary>
		/// Gets the key=value attributes that appear on the same line as the Content-Disposition.
		/// </summary>
		/// <value>The content attributes.</value>
		public IDictionary<string, string> ContentAttributes { get; private set; }

		/// <summary>
		/// Gets the headers that appear on subsequent lines after the Content-Disposition.
		/// </summary>
		public WebHeaderCollection PartHeaders { get; private set; }

		/// <summary>
		/// Gets or sets the content of the part.
		/// </summary>
		public Stream Content { get; set; }

		/// <summary>
		/// Gets the length of this entire part.
		/// </summary>
		/// <remarks>Useful for calculating the ContentLength HTTP header to send before actually serializing the content.</remarks>
		public long Length {
			get {
				ErrorUtilities.VerifyOperation(this.Content != null && this.Content.Length >= 0, MessagingStrings.StreamMustHaveKnownLength);

				long length = 0;
				length += ContentDispositionHeader.Length;
				length += ": ".Length;
				length += this.ContentDisposition.Length;
				foreach (var pair in this.ContentAttributes) {
					length += "; ".Length + pair.Key.Length + "=\"".Length + pair.Value.Length + "\"".Length;
				}

				length += NewLine.Length;
				foreach (string headerName in this.PartHeaders) {
					length += headerName.Length;
					length += ": ".Length;
					length += this.PartHeaders[headerName].Length;
					length += NewLine.Length;
				}

				length += NewLine.Length;
				length += this.Content.Length;

				return length;
			}
		}

		/// <summary>
		/// Creates a part that represents a simple form field.
		/// </summary>
		/// <param name="name">The name of the form field.</param>
		/// <param name="value">The value.</param>
		/// <returns>The constructed part.</returns>
		public static MultipartPostPart CreateFormPart(string name, string value) {
			Requires.NotNullOrEmpty(name, "name");
			Requires.NotNull(value, "value");

			var part = new MultipartPostPart("form-data");
			try {
				part.ContentAttributes["name"] = name;
				part.Content = new MemoryStream(Encoding.UTF8.GetBytes(value));
				return part;
			} catch {
				part.Dispose();
				throw;
			}
		}

		/// <summary>
		/// Creates a part that represents a file attachment.
		/// </summary>
		/// <param name="name">The name of the form field.</param>
		/// <param name="filePath">The path to the file to send.</param>
		/// <param name="contentType">Type of the content in HTTP Content-Type format.</param>
		/// <returns>The constructed part.</returns>
		public static MultipartPostPart CreateFormFilePart(string name, string filePath, string contentType) {
			Requires.NotNullOrEmpty(name, "name");
			Requires.NotNullOrEmpty(filePath, "filePath");
			Requires.NotNullOrEmpty(contentType, "contentType");

			string fileName = Path.GetFileName(filePath);
			var fileStream = File.OpenRead(filePath);
			try {
				return CreateFormFilePart(name, fileName, contentType, fileStream);
			} catch {
				fileStream.Dispose();
				throw;
			}
		}

		/// <summary>
		/// Creates a part that represents a file attachment.
		/// </summary>
		/// <param name="name">The name of the form field.</param>
		/// <param name="fileName">Name of the file as the server should see it.</param>
		/// <param name="contentType">Type of the content in HTTP Content-Type format.</param>
		/// <param name="content">The content of the file.</param>
		/// <returns>The constructed part.</returns>
		public static MultipartPostPart CreateFormFilePart(string name, string fileName, string contentType, Stream content) {
			Requires.NotNullOrEmpty(name, "name");
			Requires.NotNullOrEmpty(fileName, "fileName");
			Requires.NotNullOrEmpty(contentType, "contentType");
			Requires.NotNull(content, "content");

			var part = new MultipartPostPart("file");
			try {
				part.ContentAttributes["name"] = name;
				part.ContentAttributes["filename"] = fileName;
				part.PartHeaders[HttpRequestHeader.ContentType] = contentType;
				if (!contentType.StartsWith("text/", StringComparison.Ordinal)) {
					part.PartHeaders["Content-Transfer-Encoding"] = "binary";
				}

				part.Content = content;
				return part;
			} catch {
				part.Dispose();
				throw;
			}
		}

		/// <summary>
		/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
		/// </summary>
		public void Dispose() {
			this.Dispose(true);
			GC.SuppressFinalize(this);
		}

		/// <summary>
		/// Serializes the part to a stream.
		/// </summary>
		/// <param name="streamWriter">The stream writer.</param>
		internal void Serialize(StreamWriter streamWriter) {
			// VERY IMPORTANT: any changes at all made to this must be kept in sync with the
			// Length property which calculates exactly how many bytes this method will write.
			streamWriter.NewLine = NewLine;
			streamWriter.Write("{0}: {1}", ContentDispositionHeader, this.ContentDisposition);
			foreach (var pair in this.ContentAttributes) {
				streamWriter.Write("; {0}=\"{1}\"", pair.Key, pair.Value);
			}

			streamWriter.WriteLine();
			foreach (string headerName in this.PartHeaders) {
				streamWriter.WriteLine("{0}: {1}", headerName, this.PartHeaders[headerName]);
			}

			streamWriter.WriteLine();
			streamWriter.Flush();
			this.Content.CopyTo(streamWriter.BaseStream);
		}

		/// <summary>
		/// Releases unmanaged and - optionally - managed resources
		/// </summary>
		/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
		protected virtual void Dispose(bool disposing) {
			if (disposing) {
				this.Content.Dispose();
			}
		}

#if CONTRACTS_FULL
		/// <summary>
		/// Verifies conditions that should be true for any valid state of this object.
		/// </summary>
		[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Called by code contracts.")]
		[ContractInvariantMethod]
		private void Invariant() {
			Contract.Invariant(!string.IsNullOrEmpty(this.ContentDisposition));
			Contract.Invariant(this.PartHeaders != null);
			Contract.Invariant(this.ContentAttributes != null);
		}
#endif
	}
}