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;
17 using System.Web.Caching;
21 public sealed class HttpResponse
23 // Chunked encoding static helpers
24 static byte [] s_arrChunkSuffix = { 10, 13 };
25 static byte [] s_arrChunkEnd = { 10 , 13 };
26 static string s_sChunkedPrefix = "\r\n";
30 bool _bClientDisconnected;
31 bool _bSuppressHeaders;
32 bool _bSuppressContent;
42 bool _ClientDisconnected;
46 string _sCacheControl;
47 string _sTransferEncoding;
49 string _sStatusDescription;
51 HttpCookieCollection _Cookies;
52 HttpCachePolicy _CachePolicy;
54 Encoding _ContentEncoding;
58 TextWriter _TextWriter;
60 HttpWorkerRequest _WorkerRequest;
62 ArrayList fileDependencies;
63 CachedRawResponse cached_response;
64 ArrayList cached_headers;
66 public HttpResponse (TextWriter output)
70 _bHeadersSent = false;
72 _Headers = new ArrayList ();
74 _sContentType = "text/html";
78 _sCacheControl = null;
81 _bSuppressContent = false;
82 _bSuppressHeaders = false;
83 _bClientDisconnected = false;
90 internal HttpResponse (HttpWorkerRequest WorkerRequest, HttpContext Context)
93 _WorkerRequest = WorkerRequest;
97 _bHeadersSent = false;
99 _Headers = new ArrayList ();
101 _sContentType = "text/html";
105 _sCacheControl = null;
108 _bSuppressContent = false;
109 _bSuppressHeaders = false;
110 _bClientDisconnected = false;
115 internal void InitializeWriter ()
117 // We cannot do this in the .ctor because HttpWriter uses configuration and
118 // it may not be initialized
119 if (_Writer == null) {
120 _Writer = new HttpWriter (this);
121 _TextWriter = _Writer;
125 internal void FinalFlush ()
130 internal void DoFilter (bool really)
132 if (really && null != _Writer)
133 _Writer.FilterData (true);
138 internal bool IsCached {
139 get { return cached_response != null; }
142 internal void CacheResponse (HttpRequest request) {
143 cached_response = new CachedRawResponse (_CachePolicy);
146 internal CachedRawResponse GetCachedResponse () {
147 cached_response.StatusCode = StatusCode;
148 cached_response.StatusDescription = StatusDescription;
149 return cached_response;
152 internal void SetCachedHeaders (ArrayList headers)
154 cached_headers = headers;
157 [MonoTODO("We need to add cache headers also")]
158 private ArrayList GenerateHeaders ()
160 ArrayList oHeaders = new ArrayList (_Headers.ToArray ());
162 oHeaders.Add (new HttpResponseHeader ("X-Powered-By", "Mono"));
163 // save culture info, we need us info here
164 CultureInfo oSavedInfo = Thread.CurrentThread.CurrentCulture;
165 Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
167 string date = DateTime.Now.ToUniversalTime ().ToString ("ddd, d MMM yyyy HH:mm:ss ");
168 oHeaders.Add (new HttpResponseHeader ("Date", date + "GMT"));
170 Thread.CurrentThread.CurrentCulture = oSavedInfo;
172 if (_lContentLength > 0) {
173 oHeaders.Add (new HttpResponseHeader (HttpWorkerRequest.HeaderContentLength,
174 _lContentLength.ToString ()));
177 if (_sContentType != null) {
178 if (_sContentType.IndexOf ("charset=") == -1) {
179 if (Charset.Length == 0) {
180 Charset = ContentEncoding.HeaderName;
183 // Time to build our string
184 if (Charset.Length > 0) {
185 _sContentType += "; charset=" + Charset;
189 oHeaders.Add (new HttpResponseHeader (HttpWorkerRequest.HeaderContentType,
193 if (_CachePolicy != null)
194 _CachePolicy.SetHeaders (this, oHeaders);
196 if (_sCacheControl != null) {
197 oHeaders.Add (new HttpResponseHeader (HttpWorkerRequest.HeaderPragma,
201 if (_sTransferEncoding != null) {
202 oHeaders.Add (new HttpResponseHeader (HttpWorkerRequest.HeaderTransferEncoding,
203 _sTransferEncoding));
206 if (_Cookies != null) {
207 int length = _Cookies.Count;
208 for (int i = 0; i < length; i++) {
209 oHeaders.Add (_Cookies.Get (i).GetCookieHeader ());
216 private void SendHeaders ()
218 _WorkerRequest.SendStatus (StatusCode, StatusDescription);
222 if (cached_headers != null)
223 oHeaders = cached_headers;
225 oHeaders = GenerateHeaders ();
227 if (cached_response != null)
228 cached_response.SetHeaders (oHeaders);
230 foreach (HttpResponseHeader oHeader in oHeaders)
231 oHeader.SendContent (_WorkerRequest);
233 _bHeadersSent = true;
239 return String.Format ("{0} {1}", StatusCode, StatusDescription);
247 iCode = Int32.Parse (value.Substring (0, value.IndexOf (' ')));
248 sMsg = value.Substring (value.IndexOf (' ') + 1);
249 } catch (Exception) {
250 throw new HttpException ("Invalid status string");
254 StatusDescription = sMsg;
259 public void AddCacheItemDependencies (ArrayList cacheKeys)
261 throw new NotImplementedException ();
265 public void AddCacheItemDependency(string cacheKey)
267 throw new NotImplementedException ();
270 public void AddFileDependencies (ArrayList filenames)
272 if (filenames == null || filenames.Count == 0)
275 if (fileDependencies == null) {
276 fileDependencies = (ArrayList) filenames.Clone ();
280 foreach (string fn in filenames)
281 AddFileDependency (fn);
284 public void AddFileDependency (string filename)
286 if (fileDependencies == null)
287 fileDependencies = new ArrayList ();
289 fileDependencies.Add (filename);
292 public void AddHeader (string name, string value)
294 AppendHeader(name, value);
297 public void AppendCookie (HttpCookie cookie)
300 throw new HttpException ("Cannot append cookies after HTTP headers have been sent");
302 Cookies.Add (cookie);
306 public void AppendToLog (string param)
308 throw new NotImplementedException ();
311 public string ApplyAppPathModifier (string virtualPath)
313 if (virtualPath == null)
316 if (virtualPath == "")
317 return _Context.Request.RootVirtualDir;
319 if (UrlUtils.IsRelativeUrl (virtualPath)) {
320 virtualPath = UrlUtils.Combine (_Context.Request.RootVirtualDir, virtualPath);
321 } else if (UrlUtils.IsRooted (virtualPath)) {
322 virtualPath = UrlUtils.Reduce (virtualPath);
335 BufferOutput = value;
339 public bool BufferOutput
353 public HttpCachePolicy Cache
356 if (null == _CachePolicy)
357 _CachePolicy = new HttpCachePolicy ();
363 [MonoTODO("Set status in the cache policy")]
364 public string CacheControl
367 return _sCacheControl;
372 throw new HttpException ("Headers has been sent to the client");
374 _sCacheControl = value;
378 public string Charset
381 if (null == _sCharset)
382 _sCharset = ContentEncoding.WebName;
389 throw new HttpException ("Headers has been sent to the client");
395 public Encoding ContentEncoding
398 if (_ContentEncoding == null)
399 _ContentEncoding = WebEncoding.ResponseEncoding;
401 return _ContentEncoding;
406 throw new ArgumentNullException ("Can't set a null as encoding");
408 _ContentEncoding = value;
415 public string ContentType
418 return _sContentType;
423 throw new HttpException ("Headers has been sent to the client");
425 _sContentType = value;
429 public HttpCookieCollection Cookies
432 if (null == _Cookies)
433 _Cookies = new HttpCookieCollection (this, false);
439 [MonoTODO("Set expires in the cache policy")]
443 throw new NotImplementedException ();
447 throw new NotImplementedException ();
451 [MonoTODO("Set expiresabsolute in the cache policy")]
452 public DateTime ExpiresAbsolute
455 throw new NotImplementedException ();
459 throw new NotImplementedException ();
467 return _Writer.GetActiveFilter ();
474 throw new HttpException ("Filtering is not allowed");
476 _Writer.ActivateFilter (value);
480 public bool IsClientConnected
483 if (_ClientDisconnected)
486 if (null != _WorkerRequest && (!_WorkerRequest.IsClientConnected ())) {
487 _ClientDisconnected = false;
495 public TextWriter Output
502 public Stream OutputStream
506 throw new HttpException ("an Output stream not available when " +
507 "running with custom text writer");
509 return _Writer.OutputStream;
513 public string StatusDescription
516 if (null == _sStatusDescription)
517 _sStatusDescription =
518 HttpWorkerRequest.GetStatusDescription (_iStatusCode);
520 return _sStatusDescription;
525 throw new HttpException ("Headers has been sent to the client");
527 _sStatusDescription = value;
531 public int StatusCode
539 throw new HttpException ("Headers has been sent to the client");
541 _sStatusDescription = null;
542 _iStatusCode = value;
546 public bool SuppressContent
549 return _bSuppressContent;
554 throw new HttpException ("Headers has been sent to the client");
556 _bSuppressContent = true;
563 if (_Context == null)
566 return _Context.Request;
570 internal void AppendHeader (int iIndex, string value)
573 throw new HttpException ("Headers has been sent to the client");
576 case HttpWorkerRequest.HeaderContentLength:
577 _lContentLength = Int64.Parse (value);
579 case HttpWorkerRequest.HeaderContentEncoding:
580 _sContentType = value;
582 case HttpWorkerRequest.HeaderTransferEncoding:
583 _sTransferEncoding = value;
584 if (value.Equals ("chunked")) {
590 case HttpWorkerRequest.HeaderPragma:
591 _sCacheControl = value;
594 _Headers.Add (new HttpResponseHeader (iIndex, value));
599 public void AppendHeader (string name, string value)
602 throw new HttpException ("Headers has been sent to the client");
604 switch (name.ToLower ()) {
605 case "content-length":
606 _lContentLength = Int64.Parse (value);
609 _sContentType = value;
611 case "transfer-encoding":
612 _sTransferEncoding = value;
613 if (value.Equals ("chunked")) {
620 _sCacheControl = value;
623 _Headers.Add (new HttpResponseHeader (name, value));
628 internal TextWriter SetTextWriter (TextWriter w)
630 TextWriter prev = _TextWriter;
635 public void BinaryWrite (byte [] buffer)
637 OutputStream.Write (buffer, 0, buffer.Length);
640 internal void BinaryWrite (byte [] buffer, int start, int length)
642 OutputStream.Write (buffer, start, length);
651 public void ClearContent ()
656 public void ClearHeaders ()
659 throw new HttpException ("Headers has been sent to the client");
661 _sContentType = "text/html";
665 _Headers = new ArrayList ();
666 _sCacheControl = null;
667 _sTransferEncoding = null;
670 _bSuppressContent = false;
671 _bSuppressHeaders = false;
672 _bClientDisconnected = false;
677 if (closed && !_bClientDisconnected) {
678 _bClientDisconnected = false;
679 _WorkerRequest.CloseConnection ();
680 _bClientDisconnected = true;
684 internal void Dispose ()
686 if (_Writer != null) {
692 [MonoTODO("Handle callbacks into before done with session, needs to have a non ended flush here")]
693 internal void FlushAtEndOfRequest ()
705 _Context.ApplicationInstance.CompleteRequest ();
711 throw new HttpException ("Response already finished.");
716 private void Flush (bool bFinish)
718 if (_bFlushing || closed)
723 if (_Writer == null) {
724 _TextWriter.Flush ();
730 if (_bClientDisconnected)
733 long length = _Writer.BufferSize;
734 if (!_bHeadersSent && !_bSuppressHeaders) {
736 if (length == 0 && _lContentLength == 0)
737 _sContentType = null;
740 length = _Writer.BufferSize;
742 _WorkerRequest.SendCalculatedContentLength ((int) length);
744 if (_lContentLength == 0 && _iStatusCode == 200 &&
745 _sTransferEncoding == null) {
746 // Check we are going todo chunked encoding
747 string sProto = Request.ServerVariables ["SERVER_PROTOCOL"];
748 sProto = "HTTP/1.0"; // Remove this line when we support properly
751 if (sProto != null && sProto == "HTTP/1.1") {
753 HttpWorkerRequest.HeaderTransferEncoding,
756 // Just in case, the old browsers send a HTTP/1.0
757 // request with Connection: Keep-Alive
759 HttpWorkerRequest.HeaderConnection,
764 length = _Writer.BufferSize;
770 _Writer.FilterData (false);
771 length = _Writer.BufferSize;
775 _WorkerRequest.FlushResponse (bFinish);
781 if (!_bSuppressContent && Request.HttpMethod == "HEAD")
782 _bSuppressContent = true;
784 if (!_bSuppressContent) {
785 _bClientDisconnected = false;
787 Encoding oASCII = Encoding.ASCII;
789 string chunk = Convert.ToString(_Writer.BufferSize, 16);
790 byte [] arrPrefix = oASCII.GetBytes (chunk + s_sChunkedPrefix);
792 _WorkerRequest.SendResponseFromMemory (arrPrefix,
795 _Writer.SendContent (_WorkerRequest);
797 _WorkerRequest.SendResponseFromMemory (s_arrChunkSuffix,
798 s_arrChunkSuffix.Length);
800 _WorkerRequest.SendResponseFromMemory (
801 s_arrChunkEnd, s_arrChunkEnd.Length);
803 _Writer.SendContent (_WorkerRequest);
807 _WorkerRequest.FlushResponse (bFinish);
809 cached_response.ContentLength = (int) length;
810 cached_response.SetData (_Writer.GetBuffer ());
820 public void Pics (string value)
822 AppendHeader ("PICS-Label", value);
826 public void Redirect (string url)
828 Redirect (url, true);
831 public void Redirect (string url, bool endResponse)
834 throw new HttpException ("Headers has been sent to the client");
838 url = ApplyAppPathModifier (url);
840 AppendHeader (HttpWorkerRequest.HeaderLocation, url);
842 // Text for browsers that can't handle location header
843 Write ("<html><head><title>Object moved</title></head><body>\r\n");
844 Write ("<h2>Object moved to <a href='" + url + "'>here</a></h2>\r\n");
845 Write ("</body><html>\r\n");
851 public void Write (char ch)
853 _TextWriter.Write(ch);
856 public void Write (object obj)
858 _TextWriter.Write(obj);
861 public void Write (string str)
863 _TextWriter.Write (str);
866 public void Write (char [] buffer, int index, int count)
868 _TextWriter.Write (buffer, index, count);
872 public static void RemoveOutputCacheItem (string path)
874 throw new NotImplementedException ();
877 public void SetCookie (HttpCookie cookie)
880 throw new HttpException ("Cannot append cookies after HTTP headers have been sent");
882 Cookies.Add (cookie);
885 private void WriteFromStream (Stream stream, long offset, long length, long bufsize)
887 if (offset < 0 || length <= 0)
890 long stLength = stream.Length;
891 if (offset + length > stLength)
892 length = stLength - offset;
895 stream.Seek (offset, SeekOrigin.Begin);
897 byte [] fileContent = new byte [bufsize];
898 int count = (int) Math.Min (Int32.MaxValue, bufsize);
899 while (length > 0 && (count = stream.Read (fileContent, 0, count)) != 0) {
900 _Writer.WriteBytes (fileContent, 0, count);
902 count = (int) Math.Min (length, fileContent.Length);
906 public void WriteFile (string filename)
908 WriteFile (filename, false);
911 public void WriteFile (string filename, bool readIntoMemory)
913 FileStream fs = null;
915 fs = File.OpenRead (filename);
916 long size = fs.Length;
917 if (readIntoMemory) {
918 WriteFromStream (fs, 0, size, size);
920 WriteFromStream (fs, 0, size, 8192);
928 public void WriteFile (string filename, long offset, long size)
930 FileStream fs = null;
932 fs = File.OpenRead (filename);
933 WriteFromStream (fs, offset, size, 8192);
940 public void WriteFile (IntPtr fileHandle, long offset, long size)
942 FileStream fs = null;
944 fs = new FileStream (fileHandle, FileAccess.Read);
945 WriteFromStream (fs, offset, size, 8192);
953 internal void OnCookieAdd (HttpCookie cookie)
957 [MonoTODO("Do we need this?")]
958 internal void OnCookieChange (HttpCookie cookie)
963 internal void GoingToChangeCookieColl ()
968 internal void ChangedCookieColl ()