diff options
author | John Hurliman | 2010-03-03 14:38:00 -0800 |
---|---|---|
committer | John Hurliman | 2010-03-03 14:38:00 -0800 |
commit | 61a63ff85124d932cca5a2aeba9ae685fa7f146f (patch) | |
tree | ae5337063e486e32131a188c3d6b50b4695b1f09 /OpenSim/Framework/MultipartForm.cs | |
parent | * Adjusted the significant movement magic value from 0.5m to 2.0m and added a... (diff) | |
download | opensim-SC_OLD-61a63ff85124d932cca5a2aeba9ae685fa7f146f.zip opensim-SC_OLD-61a63ff85124d932cca5a2aeba9ae685fa7f146f.tar.gz opensim-SC_OLD-61a63ff85124d932cca5a2aeba9ae685fa7f146f.tar.bz2 opensim-SC_OLD-61a63ff85124d932cca5a2aeba9ae685fa7f146f.tar.xz |
* Added three new helper utility files to OpenSim.Framework. MultipartForm is used for constructing multipart/form-data requests. UntrustedWebRequest places sanity checks and policy on requests to HTTP endpoints that are not in the same trust domain (useful for Hypergrid, OpenID, etc). WebUtil contains misc. functions for managing URLs and network streams
Diffstat (limited to 'OpenSim/Framework/MultipartForm.cs')
-rw-r--r-- | OpenSim/Framework/MultipartForm.cs | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/OpenSim/Framework/MultipartForm.cs b/OpenSim/Framework/MultipartForm.cs new file mode 100644 index 0000000..8ba6d22 --- /dev/null +++ b/OpenSim/Framework/MultipartForm.cs | |||
@@ -0,0 +1,117 @@ | |||
1 | using System; | ||
2 | using System.Collections.Generic; | ||
3 | using System.Net; | ||
4 | using System.IO; | ||
5 | using System.Text; | ||
6 | |||
7 | namespace OpenSim.Framework | ||
8 | { | ||
9 | public static class MultipartForm | ||
10 | { | ||
11 | #region Helper Classes | ||
12 | |||
13 | public abstract class Element | ||
14 | { | ||
15 | public string Name; | ||
16 | } | ||
17 | |||
18 | public class File : Element | ||
19 | { | ||
20 | public string Filename; | ||
21 | public string ContentType; | ||
22 | public byte[] Data; | ||
23 | |||
24 | public File(string name, string filename, string contentType, byte[] data) | ||
25 | { | ||
26 | Name = name; | ||
27 | Filename = filename; | ||
28 | ContentType = contentType; | ||
29 | Data = data; | ||
30 | } | ||
31 | } | ||
32 | |||
33 | public class Parameter : Element | ||
34 | { | ||
35 | public string Value; | ||
36 | |||
37 | public Parameter(string name, string value) | ||
38 | { | ||
39 | Name = name; | ||
40 | Value = value; | ||
41 | } | ||
42 | } | ||
43 | |||
44 | #endregion Helper Classes | ||
45 | |||
46 | public static HttpWebResponse Post(HttpWebRequest request, List<Element> postParameters) | ||
47 | { | ||
48 | string boundary = Boundary(); | ||
49 | |||
50 | // Set up the request properties | ||
51 | request.Method = "POST"; | ||
52 | request.ContentType = "multipart/form-data; boundary=" + boundary; | ||
53 | |||
54 | #region Stream Writing | ||
55 | |||
56 | using (MemoryStream formDataStream = new MemoryStream()) | ||
57 | { | ||
58 | foreach (var param in postParameters) | ||
59 | { | ||
60 | if (param is File) | ||
61 | { | ||
62 | File file = (File)param; | ||
63 | |||
64 | // Add just the first part of this param, since we will write the file data directly to the Stream | ||
65 | string header = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\";\r\nContent-Type: {3}\r\n\r\n", | ||
66 | boundary, | ||
67 | file.Name, | ||
68 | !String.IsNullOrEmpty(file.Filename) ? file.Filename : "tempfile", | ||
69 | file.ContentType); | ||
70 | |||
71 | formDataStream.Write(Encoding.UTF8.GetBytes(header), 0, header.Length); | ||
72 | formDataStream.Write(file.Data, 0, file.Data.Length); | ||
73 | } | ||
74 | else | ||
75 | { | ||
76 | Parameter parameter = (Parameter)param; | ||
77 | |||
78 | string postData = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}\r\n", | ||
79 | boundary, | ||
80 | parameter.Name, | ||
81 | parameter.Value); | ||
82 | formDataStream.Write(Encoding.UTF8.GetBytes(postData), 0, postData.Length); | ||
83 | } | ||
84 | } | ||
85 | |||
86 | // Add the end of the request | ||
87 | byte[] footer = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); | ||
88 | formDataStream.Write(footer, 0, footer.Length); | ||
89 | |||
90 | request.ContentLength = formDataStream.Length; | ||
91 | |||
92 | // Copy the temporary stream to the network stream | ||
93 | formDataStream.Seek(0, SeekOrigin.Begin); | ||
94 | using (Stream requestStream = request.GetRequestStream()) | ||
95 | formDataStream.CopyTo(requestStream, (int)formDataStream.Length); | ||
96 | } | ||
97 | |||
98 | #endregion Stream Writing | ||
99 | |||
100 | return request.GetResponse() as HttpWebResponse; | ||
101 | } | ||
102 | |||
103 | private static string Boundary() | ||
104 | { | ||
105 | Random rnd = new Random(); | ||
106 | string formDataBoundary = String.Empty; | ||
107 | |||
108 | while (formDataBoundary.Length < 15) | ||
109 | formDataBoundary = formDataBoundary + rnd.Next(); | ||
110 | |||
111 | formDataBoundary = formDataBoundary.Substring(0, 15); | ||
112 | formDataBoundary = "-----------------------------" + formDataBoundary; | ||
113 | |||
114 | return formDataBoundary; | ||
115 | } | ||
116 | } | ||
117 | } | ||