2009-02-10 Gonzalo Paniagua Javier <gonzalo@novell.com>
[mono.git] / mcs / class / System / System.Net / HttpListenerResponse.cs
index a12d01731950ee3b5d5e907bd414718fc80aa541..0b4bf744cdf3594058232c53f832750034773e3e 100644 (file)
@@ -25,7 +25,9 @@
 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
-#if NET_2_0
+
+#if NET_2_0 && SECURITY_DEP
+
 using System.Globalization;
 using System.IO;
 using System.Text;
@@ -147,9 +149,6 @@ namespace System.Net {
 
                public Stream OutputStream {
                        get {
-                               if (disposed)
-                                       throw new ObjectDisposedException (GetType ().ToString ());
-
                                if (output_stream == null)
                                        output_stream = context.Connection.GetResponseStream ();
                                return output_stream;
@@ -313,7 +312,7 @@ namespace System.Net {
                        if (cookie == null)
                                throw new ArgumentNullException ("cookie");
                        
-                       cookies.Add (cookie);
+                       Cookies.Add (cookie);
                }
 
                public void AppendHeader (string name, string value)
@@ -393,11 +392,9 @@ namespace System.Net {
                        return false;
                }
 
-               internal void SendHeaders (bool closing)
+               internal void SendHeaders (bool closing, MemoryStream ms)
                {
                        //TODO: When do we send KeepAlive?
-                       //TODO: send cookies
-                       MemoryStream ms = new MemoryStream ();
                        Encoding encoding = content_encoding;
                        if (encoding == null)
                                encoding = Encoding.Default;
@@ -445,11 +442,13 @@ namespace System.Net {
                                        status_code == 413 || status_code == 414 || status_code == 500 ||
                                        status_code == 503);
 
-                       if (conn_close == false)
+                       if (conn_close == false) {
                                conn_close = (context.Request.Headers ["connection"] == "close");
+                               conn_close |= (v <= HttpVersion.Version10);
+                       }
 
                        // They sent both KeepAlive: true and Connection: close!?
-                       if (!chunked || conn_close)
+                       if (!keep_alive || conn_close)
                                headers.SetInternal ("Connection", "close");
 
                        if (chunked)
@@ -465,17 +464,29 @@ namespace System.Net {
                        if (location != null)
                                headers.SetInternal ("Location", location);
 
+                       if (cookies != null) {
+                               bool firstDone = false;
+                               StringBuilder cookieSB = new StringBuilder ();
+                               foreach (Cookie cookie in cookies) {
+                                       if (firstDone)
+                                               cookieSB.Append (",");
+                                       firstDone = true;
+                                       cookieSB.Append (cookie.ToClientString ());
+                               }
+                               headers.SetInternal("Set-Cookie2", cookieSB.ToString ());
+                       }
+
                        StreamWriter writer = new StreamWriter (ms, encoding);
                        writer.Write ("HTTP/{0} {1} {2}\r\n", version, status_code, status_description);
                        string headers_str = headers.ToString ();
                        writer.Write (headers_str);
                        writer.Flush ();
-                       // Perf.: use TCP_CORK if we're writing more?
                        int preamble = encoding.GetPreamble ().Length;
                        if (output_stream == null)
                                output_stream = context.Connection.GetResponseStream ();
 
-                       output_stream.InternalWrite (ms.GetBuffer (), 0 + preamble, (int) ms.Length - preamble);
+                       /* Assumes that the ms was at position 0 */
+                       ms.Position = preamble;
                        HeadersSent = true;
                }