2 // System.Web.HttpResponse
5 // Patrik Torstensson (Patrik.Torstensson@labs2.com)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // (c) 2002 Ximian, Inc. (http://www.ximian.com)
11 using System.Collections;
12 using System.Globalization;
15 using System.Threading;
16 using System.Web.Util;
20 public sealed class HttpResponse
22 // Chunked encoding static helpers
23 static byte [] s_arrChunkSuffix = { 10, 13 };
24 static byte [] s_arrChunkEnd = { 10 , 13 };
25 static string s_sChunkedPrefix = "\r\n";
29 bool _bClientDisconnected;
30 bool _bSuppressHeaders;
31 bool _bSuppressContent;
41 bool _ClientDisconnected;
45 string _sCacheControl;
46 string _sTransferEncoding;
48 string _sStatusDescription;
50 HttpCookieCollection _Cookies;
51 HttpCachePolicy _CachePolicy;
53 Encoding _ContentEncoding;
57 TextWriter _TextWriter;
59 HttpWorkerRequest _WorkerRequest;
61 ArrayList fileDependencies;
63 public HttpResponse (TextWriter output)
67 _bHeadersSent = false;
69 _Headers = new ArrayList ();
71 _sContentType = "text/html";
75 _sCacheControl = null;
78 _bSuppressContent = false;
79 _bSuppressHeaders = false;
80 _bClientDisconnected = false;
87 internal HttpResponse (HttpWorkerRequest WorkerRequest, HttpContext Context)
90 _WorkerRequest = WorkerRequest;
94 _bHeadersSent = false;
96 _Headers = new ArrayList ();
98 _sContentType = "text/html";
102 _sCacheControl = null;
105 _bSuppressContent = false;
106 _bSuppressHeaders = false;
107 _bClientDisconnected = false;
111 _Writer = new HttpWriter (this);
112 _TextWriter = _Writer;
115 internal void FinalFlush ()
120 internal void DoFilter (bool really)
122 if (really && null != _Writer)
123 _Writer.FilterData (true);
128 [MonoTODO("We need to add cache headers also")]
129 private ArrayList GenerateHeaders ()
131 ArrayList oHeaders = new ArrayList (_Headers.ToArray ());
133 oHeaders.Add (new HttpResponseHeader ("X-Powered-By", "Mono"));
134 // save culture info, we need us info here
135 CultureInfo oSavedInfo = Thread.CurrentThread.CurrentCulture;
136 Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
138 string date = DateTime.Now.ToUniversalTime ().ToString ("ddd, d MMM yyyy HH:mm:ss ");
139 oHeaders.Add (new HttpResponseHeader ("Date", date + "GMT"));
141 Thread.CurrentThread.CurrentCulture = oSavedInfo;
143 if (_lContentLength > 0) {
144 oHeaders.Add (new HttpResponseHeader (HttpWorkerRequest.HeaderContentLength,
145 _lContentLength.ToString ()));
148 if (_sContentType != null) {
149 if (_sContentType.IndexOf ("charset=") == -1) {
150 if (Charset.Length == 0) {
151 Charset = ContentEncoding.HeaderName;
154 // Time to build our string
155 if (Charset.Length > 0) {
156 _sContentType += "; charset=" + Charset;
160 oHeaders.Add (new HttpResponseHeader (HttpWorkerRequest.HeaderContentType,
164 if (_CachePolicy != null)
165 _CachePolicy.SetHeaders (this, oHeaders);
167 if (_sCacheControl != null) {
168 oHeaders.Add (new HttpResponseHeader (HttpWorkerRequest.HeaderPragma,
172 if (_sTransferEncoding != null) {
173 oHeaders.Add (new HttpResponseHeader (HttpWorkerRequest.HeaderTransferEncoding,
174 _sTransferEncoding));
177 if (_Cookies != null) {
178 int length = _Cookies.Count;
179 for (int i = 0; i < length; i++) {
180 oHeaders.Add (_Cookies.Get (i).GetCookieHeader ());
187 private void SendHeaders ()
189 _WorkerRequest.SendStatus (StatusCode, StatusDescription);
191 ArrayList oHeaders = GenerateHeaders ();
192 foreach (HttpResponseHeader oHeader in oHeaders)
193 oHeader.SendContent (_WorkerRequest);
195 _bHeadersSent = true;
201 return String.Format ("{0} {1}", StatusCode, StatusDescription);
209 iCode = Int32.Parse (value.Substring (0, value.IndexOf (' ')));
210 sMsg = value.Substring (value.IndexOf (' ') + 1);
211 } catch (Exception) {
212 throw new HttpException ("Invalid status string");
216 StatusDescription = sMsg;
221 public void AddCacheItemDependencies (ArrayList cacheKeys)
223 throw new NotImplementedException ();
227 public void AddCacheItemDependency(string cacheKey)
229 throw new NotImplementedException ();
232 public void AddFileDependencies (ArrayList filenames)
234 if (filenames == null || filenames.Count == 0)
237 if (fileDependencies == null) {
238 fileDependencies = (ArrayList) filenames.Clone ();
242 foreach (string fn in filenames)
243 AddFileDependency (fn);
246 public void AddFileDependency (string filename)
248 if (fileDependencies == null)
249 fileDependencies = new ArrayList ();
251 fileDependencies.Add (filename);
254 public void AddHeader (string name, string value)
256 AppendHeader(name, value);
259 public void AppendCookie (HttpCookie cookie)
262 throw new HttpException ("Cannot append cookies after HTTP headers have been sent");
264 Cookies.Add (cookie);
268 public void AppendToLog (string param)
270 throw new NotImplementedException ();
273 public string ApplyAppPathModifier (string virtualPath)
275 if (virtualPath == null)
278 if (virtualPath == "")
279 return _Context.Request.RootVirtualDir;
281 if (UrlUtils.IsRelativeUrl (virtualPath)) {
282 virtualPath = UrlUtils.Combine (_Context.Request.RootVirtualDir, virtualPath);
283 } else if (UrlUtils.IsRooted (virtualPath)) {
284 virtualPath = UrlUtils.Reduce (virtualPath);
297 BufferOutput = value;
301 public bool BufferOutput
315 public HttpCachePolicy Cache
318 if (null == _CachePolicy)
319 _CachePolicy = new HttpCachePolicy ();
325 [MonoTODO("Set status in the cache policy")]
326 public string CacheControl
329 return _sCacheControl;
334 throw new HttpException ("Headers has been sent to the client");
336 _sCacheControl = value;
340 public string Charset
343 if (null == _sCharset)
344 _sCharset = ContentEncoding.WebName;
351 throw new HttpException ("Headers has been sent to the client");
357 public Encoding ContentEncoding
360 if (_ContentEncoding == null)
361 _ContentEncoding = WebEncoding.ResponseEncoding;
363 return _ContentEncoding;
368 throw new ArgumentNullException ("Can't set a null as encoding");
370 _ContentEncoding = value;
377 public string ContentType
380 return _sContentType;
385 throw new HttpException ("Headers has been sent to the client");
387 _sContentType = value;
391 public HttpCookieCollection Cookies
394 if (null == _Cookies)
395 _Cookies = new HttpCookieCollection (this, false);
401 [MonoTODO("Set expires in the cache policy")]
405 throw new NotImplementedException ();
409 throw new NotImplementedException ();
413 [MonoTODO("Set expiresabsolute in the cache policy")]
414 public DateTime ExpiresAbsolute
417 throw new NotImplementedException ();
421 throw new NotImplementedException ();
429 return _Writer.GetActiveFilter ();
436 throw new HttpException ("Filtering is not allowed");
438 _Writer.ActivateFilter (value);
442 public bool IsClientConnected
445 if (_ClientDisconnected)
448 if (null != _WorkerRequest && (!_WorkerRequest.IsClientConnected ())) {
449 _ClientDisconnected = false;
457 public TextWriter Output
464 public Stream OutputStream
468 throw new HttpException ("an Output stream not available when " +
469 "running with custom text writer");
471 return _Writer.OutputStream;
475 public string StatusDescription
478 if (null == _sStatusDescription)
479 _sStatusDescription =
480 HttpWorkerRequest.GetStatusDescription (_iStatusCode);
482 return _sStatusDescription;
487 throw new HttpException ("Headers has been sent to the client");
489 _sStatusDescription = value;
493 public int StatusCode
501 throw new HttpException ("Headers has been sent to the client");
503 _sStatusDescription = null;
504 _iStatusCode = value;
508 public bool SuppressContent
511 return _bSuppressContent;
516 throw new HttpException ("Headers has been sent to the client");
518 _bSuppressContent = true;
525 if (_Context == null)
528 return _Context.Request;
532 internal void AppendHeader (int iIndex, string value)
535 throw new HttpException ("Headers has been sent to the client");
538 case HttpWorkerRequest.HeaderContentLength:
539 _lContentLength = Int64.Parse (value);
541 case HttpWorkerRequest.HeaderContentEncoding:
542 _sContentType = value;
544 case HttpWorkerRequest.HeaderTransferEncoding:
545 _sTransferEncoding = value;
546 if (value.Equals ("chunked")) {
552 case HttpWorkerRequest.HeaderPragma:
553 _sCacheControl = value;
556 _Headers.Add (new HttpResponseHeader (iIndex, value));
561 public void AppendHeader (string name, string value)
564 throw new HttpException ("Headers has been sent to the client");
566 switch (name.ToLower ()) {
567 case "content-length":
568 _lContentLength = Int64.Parse (value);
571 _sContentType = value;
573 case "transfer-encoding":
574 _sTransferEncoding = value;
575 if (value.Equals ("chunked")) {
582 _sCacheControl = value;
585 _Headers.Add (new HttpResponseHeader (name, value));
590 internal TextWriter SetTextWriter (TextWriter w)
592 TextWriter prev = _TextWriter;
597 public void BinaryWrite (byte [] buffer)
599 OutputStream.Write (buffer, 0, buffer.Length);
608 public void ClearContent ()
613 public void ClearHeaders ()
616 throw new HttpException ("Headers has been sent to the client");
618 _sContentType = "text/html";
622 _Headers = new ArrayList ();
623 _sCacheControl = null;
624 _sTransferEncoding = null;
627 _bSuppressContent = false;
628 _bSuppressHeaders = false;
629 _bClientDisconnected = false;
634 if (closed && !_bClientDisconnected) {
635 _bClientDisconnected = false;
636 _WorkerRequest.CloseConnection ();
637 _bClientDisconnected = true;
641 internal void Dispose ()
643 if (_Writer != null) {
649 [MonoTODO("Handle callbacks into before done with session, needs to have a non ended flush here")]
650 internal void FlushAtEndOfRequest ()
662 _Context.ApplicationInstance.CompleteRequest ();
668 throw new HttpException ("Response already finished.");
673 private void Flush (bool bFinish)
675 if (_bFlushing || closed)
680 if (_Writer == null) {
681 _TextWriter.Flush ();
687 if (_bClientDisconnected)
690 long length = _Writer.BufferSize;
691 if (!_bHeadersSent && !_bSuppressHeaders) {
693 if (length == 0 && _lContentLength == 0)
694 _sContentType = null;
697 length = _Writer.BufferSize;
699 _WorkerRequest.SendCalculatedContentLength ((int) length);
701 if (_lContentLength == 0 && _iStatusCode == 200 &&
702 _sTransferEncoding == null) {
703 // Check we are going todo chunked encoding
704 string sProto = Request.ServerVariables ["SERVER_PROTOCOL"];
705 sProto = "HTTP/1.0"; // Remove this line when we support properly
708 if (sProto != null && sProto == "HTTP/1.1") {
710 HttpWorkerRequest.HeaderTransferEncoding,
713 // Just in case, the old browsers send a HTTP/1.0
714 // request with Connection: Keep-Alive
716 HttpWorkerRequest.HeaderConnection,
721 length = _Writer.BufferSize;
727 _Writer.FilterData (false);
728 length = _Writer.BufferSize;
732 _WorkerRequest.FlushResponse (bFinish);
738 if (!_bSuppressContent && Request.HttpMethod == "HEAD")
739 _bSuppressContent = true;
741 if (!_bSuppressContent) {
742 _bClientDisconnected = false;
744 Encoding oASCII = Encoding.ASCII;
746 string chunk = Convert.ToString(_Writer.BufferSize, 16);
747 byte [] arrPrefix = oASCII.GetBytes (chunk + s_sChunkedPrefix);
749 _WorkerRequest.SendResponseFromMemory (arrPrefix,
752 _Writer.SendContent (_WorkerRequest);
754 _WorkerRequest.SendResponseFromMemory (s_arrChunkSuffix,
755 s_arrChunkSuffix.Length);
757 _WorkerRequest.SendResponseFromMemory (
758 s_arrChunkEnd, s_arrChunkEnd.Length);
760 _Writer.SendContent (_WorkerRequest);
764 _WorkerRequest.FlushResponse (bFinish);
774 public void Pics (string value)
776 AppendHeader ("PICS-Label", value);
780 public void Redirect (string url)
782 Redirect (url, true);
785 public void Redirect (string url, bool endResponse)
788 throw new HttpException ("Headers has been sent to the client");
792 url = ApplyAppPathModifier (url);
794 AppendHeader (HttpWorkerRequest.HeaderLocation, url);
796 // Text for browsers that can't handle location header
797 Write ("<html><head><title>Object moved</title></head><body>\r\n");
798 Write ("<h2>Object moved to <a href='" + url + "'>here</a></h2>\r\n");
799 Write ("</body><html>\r\n");
805 public void Write (char ch)
807 _TextWriter.Write(ch);
810 public void Write (object obj)
812 _TextWriter.Write(obj);
815 public void Write (string str)
817 _TextWriter.Write (str);
820 public void Write (char [] buffer, int index, int count)
822 _TextWriter.Write (buffer, index, count);
826 public static void RemoveOutputCacheItem (string path)
828 throw new NotImplementedException ();
831 public void SetCookie (HttpCookie cookie)
834 throw new HttpException ("Cannot append cookies after HTTP headers have been sent");
836 Cookies.Add (cookie);
839 private void WriteFromStream (Stream stream, long offset, long length, long bufsize)
841 if (offset < 0 || length <= 0)
844 long stLength = stream.Length;
845 if (offset + length > stLength)
846 length = stLength - offset;
849 stream.Seek (offset, SeekOrigin.Begin);
851 byte [] fileContent = new byte [bufsize];
852 int count = (int) Math.Min (Int32.MaxValue, bufsize);
853 while (length > 0 && (count = stream.Read (fileContent, 0, count)) != 0) {
854 _Writer.WriteBytes (fileContent, 0, count);
856 count = (int) Math.Min (length, fileContent.Length);
860 public void WriteFile (string filename)
862 WriteFile (filename, false);
865 public void WriteFile (string filename, bool readIntoMemory)
867 FileStream fs = null;
869 fs = File.OpenRead (filename);
870 long size = fs.Length;
871 if (readIntoMemory) {
872 WriteFromStream (fs, 0, size, size);
874 WriteFromStream (fs, 0, size, 8192);
882 public void WriteFile (string filename, long offset, long size)
884 FileStream fs = null;
886 fs = File.OpenRead (filename);
887 WriteFromStream (fs, offset, size, 8192);
894 public void WriteFile (IntPtr fileHandle, long offset, long size)
896 FileStream fs = null;
898 fs = new FileStream (fileHandle, FileAccess.Read);
899 WriteFromStream (fs, offset, size, 8192);
907 internal void OnCookieAdd (HttpCookie cookie)
911 [MonoTODO("Do we need this?")]
912 internal void OnCookieChange (HttpCookie cookie)
917 internal void GoingToChangeCookieColl ()
922 internal void ChangedCookieColl ()