~ubuntu-branches/debian/squeeze/f-spot/squeeze

« back to all changes in this revision

Viewing changes to extensions/PicasaWebExport/google-sharp/MultipartRequest.cs

  • Committer: Bazaar Package Importer
  • Author(s): Iain Lane, Mirco Bauer, Iain Lane
  • Date: 2009-02-07 20:23:32 UTC
  • mfrom: (1.1.18 upstream)
  • Revision ID: james.westby@ubuntu.com-20090207202332-oc93rfjo1st0571s
Tags: 0.5.0.3-2
[ Mirco Bauer]
* Upload to unstable.
* debian/control:
  + Lowered GNOME# build-deps to 2.0 ABI as that transition didn't happen
    yet in unstable.

[ Iain Lane ]
* debian/patches/svn-r4545_locales-import.dpatch: Patch backported from SVN
  trunk revision 4545 - initialize the translation catalog earlier (LP: #293305)
  (Closes: #514457). Thanks to Florian Heinle for finding the patch and to
  Chris Coulson for preparing the update.
* debian/control: Build-depend on libmono-dev (>= 1.2.4) to match configure
  checks.
* debian/rules: Pass CSC=/usr/bin/csc to configure for gio-sharp to fix FTBFS

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
// Mono.Google.MultipartRequest
3
 
//
4
 
// Authors:
5
 
//      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6
 
//      Stephane Delcroix (stephane@delcroix.org)
7
 
//
8
 
// (C) Copyright 2006 Novell, Inc. (http://www.novell.com)
9
 
// (C) Copyright 2007 S. Delcroix
10
 
//
11
 
 
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:
19
 
// 
20
 
// The above copyright notice and this permission notice shall be
21
 
// included in all copies or substantial portions of the Software.
22
 
// 
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.
30
 
//
31
 
 
32
 
using System;
33
 
using System.Collections;
34
 
using System.Globalization;
35
 
using System.IO;
36
 
using System.Net;
37
 
using System.Text;
38
 
using System.Xml;
39
 
 
40
 
namespace Mono.Google {
41
 
        class MultipartRequest {
42
 
                static byte [] crlf = new byte [] { 13, 10 };
43
 
                HttpWebRequest request;
44
 
                Stream output_stream;
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);
50
 
                bool output_set;
51
 
 
52
 
                public MultipartRequest (GoogleConnection conn, string url)
53
 
                {
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");
58
 
                }
59
 
 
60
 
                public HttpWebRequest Request {
61
 
                        get { return request; }
62
 
                }
63
 
 
64
 
                public Stream OutputStream {
65
 
                        get { return output_stream; }
66
 
                        set {
67
 
                                output_set = true;
68
 
                                output_stream = value;
69
 
                        }
70
 
                }
71
 
 
72
 
                public void BeginPart ()
73
 
                {
74
 
                        BeginPart (false);
75
 
                }
76
 
 
77
 
                public void BeginPart (bool first)
78
 
                {
79
 
                        if (!first)
80
 
                                return;
81
 
                        if (output_stream == null)
82
 
                                output_stream = request.GetRequestStream ();
83
 
 
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);
88
 
                }
89
 
 
90
 
                public void AddHeader (string name, string val)
91
 
                {
92
 
                        AddHeader (name, val, false);
93
 
                }
94
 
 
95
 
                public void AddHeader (string name, string val, bool last)
96
 
                {
97
 
                        AddHeader (String.Format ("{0}: {1}"), last);
98
 
                }
99
 
 
100
 
                public void AddHeader (string header)
101
 
                {
102
 
                        AddHeader (header, false);
103
 
                }
104
 
 
105
 
                public void AddHeader (string header, bool last)
106
 
                {
107
 
                        bool need_crlf = !header.EndsWith ("\r\n");
108
 
                        byte [] bytes = Encoding.UTF8.GetBytes (header);
109
 
                        output_stream.Write (bytes, 0, bytes.Length);
110
 
                        if (need_crlf)
111
 
                                output_stream.Write (crlf, 0, 2);
112
 
                        if (last)
113
 
                                output_stream.Write (crlf, 0, 2);
114
 
                }
115
 
 
116
 
                public void WriteContent (string content)
117
 
                {
118
 
                        WriteContent (Encoding.UTF8.GetBytes (content));
119
 
                }
120
 
 
121
 
                public void WriteContent (byte [] content)
122
 
                {
123
 
                        output_stream.Write (content, 0, content.Length);
124
 
                        output_stream.Write (crlf, 0, crlf.Length);
125
 
                }
126
 
 
127
 
                public void WritePartialContent (byte [] content, int offset, int nbytes)
128
 
                {
129
 
                        output_stream.Write (content, offset, nbytes);
130
 
                }
131
 
 
132
 
                public void EndPartialContent ()
133
 
                {
134
 
                        output_stream.Write (crlf, 0, crlf.Length);
135
 
                }
136
 
 
137
 
                public void EndPart (bool last)
138
 
                {
139
 
                        if (last) {
140
 
                                output_stream.Write (separator_end_bytes, 0, separator_end_bytes.Length);
141
 
                                if (!output_set)
142
 
                                        output_stream.Close ();
143
 
                        } else {
144
 
                                output_stream.Write (separator_bytes, 0, separator_bytes.Length);
145
 
                        }
146
 
                }
147
 
 
148
 
                public string GetResponseAsString ()
149
 
                {
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 ();
157
 
                        }
158
 
                        response.Close ();
159
 
                        return received;
160
 
                }
161
 
        }
162
 
}
163