2
// Mono.Google.MultipartRequest
5
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
6
// Stephane Delcroix (stephane@delcroix.org)
8
// (C) Copyright 2006 Novell, Inc. (http://www.novell.com)
9
// (C) Copyright 2007 S. Delcroix
12
// Permission is hereby granted, free of charge, to any person obtaining
13
// a copy of this software and associated documentation files (the
14
// "Software"), to deal in the Software without restriction, including
15
// without limitation the rights to use, copy, modify, merge, publish,
16
// distribute, sublicense, and/or sell copies of the Software, and to
17
// permit persons to whom the Software is furnished to do so, subject to
18
// the following conditions:
20
// The above copyright notice and this permission notice shall be
21
// included in all copies or substantial portions of the Software.
23
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33
using System.Collections;
34
using System.Globalization;
40
namespace Mono.Google {
41
class MultipartRequest {
42
static byte [] crlf = new byte [] { 13, 10 };
43
HttpWebRequest request;
45
const string separator_string = "PART_SEPARATOR";
46
const string separator = "--" + separator_string + "\r\n";
47
const string separator_end = "--" + separator_string + "--\r\n";
48
byte [] separator_bytes = Encoding.ASCII.GetBytes (separator);
49
byte [] separator_end_bytes = Encoding.ASCII.GetBytes (separator_end);
52
public MultipartRequest (GoogleConnection conn, string url)
54
request = conn.AuthenticatedRequest (url);
55
request.Method = "POST";
56
request.ContentType = "multipart/related; boundary=\"" + separator_string + "\"";
57
request.Headers.Add ("MIME-version", "1.0");
60
public HttpWebRequest Request {
61
get { return request; }
64
public Stream OutputStream {
65
get { return output_stream; }
68
output_stream = value;
72
public void BeginPart ()
77
public void BeginPart (bool first)
81
if (output_stream == null)
82
output_stream = request.GetRequestStream ();
84
string multipart = "Media multipart posting\r\n";
85
byte [] multipart_bytes = Encoding.ASCII.GetBytes (multipart);
86
output_stream.Write (multipart_bytes, 0, multipart_bytes.Length);
87
output_stream.Write (separator_bytes, 0, separator_bytes.Length);
90
public void AddHeader (string name, string val)
92
AddHeader (name, val, false);
95
public void AddHeader (string name, string val, bool last)
97
AddHeader (String.Format ("{0}: {1}"), last);
100
public void AddHeader (string header)
102
AddHeader (header, false);
105
public void AddHeader (string header, bool last)
107
bool need_crlf = !header.EndsWith ("\r\n");
108
byte [] bytes = Encoding.UTF8.GetBytes (header);
109
output_stream.Write (bytes, 0, bytes.Length);
111
output_stream.Write (crlf, 0, 2);
113
output_stream.Write (crlf, 0, 2);
116
public void WriteContent (string content)
118
WriteContent (Encoding.UTF8.GetBytes (content));
121
public void WriteContent (byte [] content)
123
output_stream.Write (content, 0, content.Length);
124
output_stream.Write (crlf, 0, crlf.Length);
127
public void WritePartialContent (byte [] content, int offset, int nbytes)
129
output_stream.Write (content, offset, nbytes);
132
public void EndPartialContent ()
134
output_stream.Write (crlf, 0, crlf.Length);
137
public void EndPart (bool last)
140
output_stream.Write (separator_end_bytes, 0, separator_end_bytes.Length);
142
output_stream.Close ();
144
output_stream.Write (separator_bytes, 0, separator_bytes.Length);
148
public string GetResponseAsString ()
150
HttpWebResponse response = null;
151
response = (HttpWebResponse) request.GetResponse ();
152
string received = "";
153
// FIXME: use CharacterSet?
154
using (Stream stream = response.GetResponseStream ()) {
155
StreamReader sr = new StreamReader (stream, Encoding.UTF8);
156
received = sr.ReadToEnd ();