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;
40 bool _ClientDisconnected;
43 string _sCacheControl;
44 string _sTransferEncoding;
46 string _sStatusDescription;
48 HttpCookieCollection _Cookies;
49 HttpCachePolicy _CachePolicy;
51 Encoding _ContentEncoding;
55 TextWriter _TextWriter;
57 HttpWorkerRequest _WorkerRequest;
59 ArrayList fileDependencies;
61 public HttpResponse (TextWriter output)
65 _bHeadersSent = false;
67 _Headers = new ArrayList ();
69 _sContentType = "text/html";
73 _sCacheControl = null;
76 _bSuppressContent = false;
77 _bSuppressHeaders = false;
78 _bClientDisconnected = false;
85 internal HttpResponse (HttpWorkerRequest WorkerRequest, HttpContext Context)
88 _WorkerRequest = WorkerRequest;
92 _bHeadersSent = false;
94 _Headers = new ArrayList ();
96 _sContentType = "text/html";
100 _sCacheControl = null;
103 _bSuppressContent = false;
104 _bSuppressHeaders = false;
105 _bClientDisconnected = false;
109 _Writer = new HttpWriter (this);
110 _TextWriter = _Writer;
113 internal Encoder ContentEncoder
116 return ContentEncoding.GetEncoder ();
120 internal void FinalFlush ()
125 internal void DoFilter ()
128 _Writer.FilterData (true);
131 [MonoTODO("We need to add cache headers also")]
132 private ArrayList GenerateHeaders ()
134 ArrayList oHeaders = new ArrayList (_Headers.ToArray ());
136 oHeaders.Add (new HttpResponseHeader ("X-Powered-By", "Mono"));
137 // save culture info, we need us info here
138 CultureInfo oSavedInfo = Thread.CurrentThread.CurrentCulture;
139 Thread.CurrentThread.CurrentCulture = new CultureInfo (0x0409);
141 string date = DateTime.Now.ToUniversalTime ().ToString ("ddd, d MMM yyyy HH:mm:ss ");
142 oHeaders.Add (new HttpResponseHeader ("Date", date + "GMT"));
144 Thread.CurrentThread.CurrentCulture = oSavedInfo;
146 if (_lContentLength > 0) {
147 oHeaders.Add (new HttpResponseHeader (HttpWorkerRequest.HeaderContentLength,
148 _lContentLength.ToString ()));
151 if (_sContentType != null) {
152 if (_sContentType.IndexOf ("charset=") == -1) {
153 if (Charset.Length == 0) {
154 Charset = ContentEncoding.HeaderName;
157 // Time to build our string
158 if (Charset.Length > 0) {
159 _sContentType += "; charset=" + Charset;
163 oHeaders.Add (new HttpResponseHeader (HttpWorkerRequest.HeaderContentType,
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 ());
186 private void SendHeaders ()
188 _WorkerRequest.SendStatus (StatusCode, StatusDescription);
190 ArrayList oHeaders = GenerateHeaders ();
191 foreach (HttpResponseHeader oHeader in oHeaders)
192 oHeader.SendContent (_WorkerRequest);
194 _bHeadersSent = true;
200 return String.Format ("{0} {1}", StatusCode, StatusDescription);
208 iCode = Int32.Parse (value.Substring (0, value.IndexOf (' ')));
209 sMsg = value.Substring (value.IndexOf (' ') + 1);
210 } catch (Exception) {
211 throw new HttpException ("Invalid status string");
215 StatusDescription = sMsg;
220 public void AddCacheItemDependencies (ArrayList cacheKeys)
222 throw new NotImplementedException ();
226 public void AddCacheItemDependency(string cacheKey)
228 throw new NotImplementedException ();
231 public void AddFileDependencies (ArrayList filenames)
233 if (filenames == null || filenames.Count == 0)
236 if (fileDependencies == null) {
237 fileDependencies = (ArrayList) filenames.Clone ();
241 foreach (string fn in filenames)
242 AddFileDependency (fn);
245 public void AddFileDependency (string filename)
247 if (fileDependencies == null)
248 fileDependencies = new ArrayList ();
250 fileDependencies.Add (filename);
253 public void AddHeader (string name, string value)
255 AppendHeader(name, value);
258 public void AppendCookie (HttpCookie cookie)
261 throw new HttpException ("Cannot append cookies after HTTP headers have been sent");
263 Cookies.Add (cookie);
267 public void AppendToLog (string param)
269 throw new NotImplementedException ();
273 public string ApplyAppPathModifier (string virtualPath)
275 throw new NotImplementedException ();
285 BufferOutput = value;
289 public bool BufferOutput
303 public HttpCachePolicy Cache
306 if (null == _CachePolicy)
307 _CachePolicy = new HttpCachePolicy ();
313 [MonoTODO("Set status in the cache policy")]
314 public string CacheControl
317 return _sCacheControl;
322 throw new HttpException ("Headers has been sent to the client");
324 _sCacheControl = value;
328 public string Charset
331 if (null == _sCharset)
332 _sCharset = ContentEncoding.WebName;
339 throw new HttpException ("Headers has been sent to the client");
345 public Encoding ContentEncoding
348 if (_ContentEncoding == null)
349 _ContentEncoding = WebEncoding.Encoding;
351 return _ContentEncoding;
356 throw new ArgumentException ("Can't set a null as encoding");
358 _ContentEncoding = value;
365 public string ContentType
368 return _sContentType;
373 throw new HttpException ("Headers has been sent to the client");
375 _sContentType = value;
379 public HttpCookieCollection Cookies
382 if (null == _Cookies)
383 _Cookies = new HttpCookieCollection (this, false);
389 [MonoTODO("Set expires in the cache policy")]
393 throw new NotImplementedException ();
397 throw new NotImplementedException ();
401 [MonoTODO("Set expiresabsolute in the cache policy")]
402 public DateTime ExpiresAbsolute
405 throw new NotImplementedException ();
409 throw new NotImplementedException ();
417 return _Writer.GetActiveFilter ();
424 throw new HttpException ("Filtering is not allowed");
426 _Writer.ActivateFilter (value);
430 public bool IsClientConnected
433 if (_ClientDisconnected)
436 if (null != _WorkerRequest && (!_WorkerRequest.IsClientConnected ())) {
437 _ClientDisconnected = false;
445 public TextWriter Output
452 public Stream OutputStream
456 throw new HttpException ("an Output stream not available when " +
457 "running with custom text writer");
459 return _Writer.OutputStream;
463 public string StatusDescription
466 if (null == _sStatusDescription)
467 _sStatusDescription =
468 HttpWorkerRequest.GetStatusDescription (_iStatusCode);
470 return _sStatusDescription;
475 throw new HttpException ("Headers has been sent to the client");
477 _sStatusDescription = value;
481 public int StatusCode
489 throw new HttpException ("Headers has been sent to the client");
491 _sStatusDescription = null;
492 _iStatusCode = value;
496 public bool SuppressContent
499 return _bSuppressContent;
504 throw new HttpException ("Headers has been sent to the client");
506 _bSuppressContent = true;
510 public HttpRequest Request
513 if (_Context == null)
516 return _Context.Request;
520 internal void AppendHeader (int iIndex, string value)
523 throw new HttpException ("Headers has been sent to the client");
526 case HttpWorkerRequest.HeaderContentLength:
527 _lContentLength = Int64.Parse (value);
529 case HttpWorkerRequest.HeaderContentEncoding:
530 _sContentType = value;
532 case HttpWorkerRequest.HeaderTransferEncoding:
533 _sTransferEncoding = value;
534 if (value.Equals ("chunked")) {
540 case HttpWorkerRequest.HeaderPragma:
541 _sCacheControl = value;
544 _Headers.Add (new HttpResponseHeader (iIndex, value));
549 public void AppendHeader (string name, string value)
552 throw new HttpException ("Headers has been sent to the client");
554 switch (name.ToLower ()) {
555 case "content-length":
556 _lContentLength = Int64.Parse (value);
559 _sContentType = value;
561 case "transfer-encoding":
562 _sTransferEncoding = value;
563 if (value.Equals ("chunked")) {
570 _sCacheControl = value;
573 _Headers.Add (new HttpResponseHeader (name, value));
578 public void BinaryWrite (byte [] buffer)
580 OutputStream.Write (buffer, 0, buffer.Length);
589 public void ClearContent ()
594 public void ClearHeaders ()
597 throw new HttpException ("Headers has been sent to the client");
599 _sContentType = "text/html";
603 _Headers = new ArrayList ();
604 _sCacheControl = null;
605 _sTransferEncoding = null;
608 _bSuppressContent = false;
609 _bSuppressHeaders = false;
610 _bClientDisconnected = false;
615 _bClientDisconnected = false;
616 _WorkerRequest.CloseConnection ();
617 _bClientDisconnected = true;
620 internal void Dispose ()
622 if (_Writer != null) {
628 [MonoTODO("Handle callbacks into before done with session, needs to have a non ended flush here")]
629 internal void FlushAtEndOfRequest ()
641 _Context.ApplicationInstance.CompleteRequest ();
649 private void Flush (bool bFinish)
656 if (_Writer == null) {
657 _TextWriter.Flush ();
664 if (!_bHeadersSent && !_bSuppressHeaders && !_bClientDisconnected) {
666 length = _Writer.BufferSize;
667 if (length == 0 && _lContentLength == 0)
668 _sContentType = null;
671 length = _Writer.BufferSize;
673 _WorkerRequest.SendCalculatedContentLength ((int) length);
675 if (_lContentLength == 0 && _iStatusCode == 200 &&
676 _sTransferEncoding == null) {
677 // Check we are going todo chunked encoding
678 string sProto = Request.ServerVariables ["SERVER_PROTOCOL"];
680 if (sProto != null && sProto == "HTTP/1.1") {
682 HttpWorkerRequest.HeaderTransferEncoding,
685 // Just in case, the old browsers send a HTTP/1.0
686 // request with Connection: Keep-Alive
688 HttpWorkerRequest.HeaderConnection,
697 if (!_bSuppressContent && Request.HttpMethod == "HEAD")
698 _bSuppressContent = true;
700 if (!_bSuppressContent) {
701 _bClientDisconnected = false;
703 Encoding oASCII = Encoding.ASCII;
705 string chunk = Convert.ToString(_Writer.BufferSize, 16);
706 byte [] arrPrefix = oASCII.GetBytes (chunk + s_sChunkedPrefix);
708 _WorkerRequest.SendResponseFromMemory (arrPrefix,
711 _Writer.SendContent (_WorkerRequest);
713 _WorkerRequest.SendResponseFromMemory (s_arrChunkSuffix,
714 s_arrChunkSuffix.Length);
716 _WorkerRequest.SendResponseFromMemory (
717 s_arrChunkEnd, s_arrChunkEnd.Length);
719 _Writer.SendContent (_WorkerRequest);
725 _WorkerRequest.FlushResponse (bFinish);
734 public void Pics (string value)
736 AppendHeader ("PICS-Label", value);
740 public void Redirect (string url)
742 Redirect (url, true);
745 public void Redirect (string url, bool endResponse)
748 throw new HttpException ("Headers has been sent to the client");
753 AppendHeader (HttpWorkerRequest.HeaderLocation, url);
755 // Text for browsers that can't handle location header
756 Write ("<html><head><title>Object moved</title></head><body>\r\n");
757 Write ("<h2>Object moved to <a href='" + url + "'>here</a></h2>\r\n");
758 Write ("</body><html>\r\n");
764 public void Write (char ch)
766 _TextWriter.Write(ch);
769 public void Write (object obj)
771 _TextWriter.Write(obj);
774 public void Write (string str)
776 _TextWriter.Write (str);
779 public void Write (char [] buffer, int index, int count)
781 _TextWriter.Write (buffer, index, count);
785 public static void RemoveOutputCacheItem (string path)
787 throw new NotImplementedException ();
790 public void SetCookie (HttpCookie cookie)
793 throw new HttpException ("Cannot append cookies after HTTP headers have been sent");
795 Cookies.Add (cookie);
798 private void WriteFromStream (Stream stream, long offset, long length, long bufsize)
800 if (offset < 0 || length <= 0)
803 long stLength = stream.Length;
804 if (offset + length > stLength)
805 length = stLength - offset;
808 stream.Seek (offset, SeekOrigin.Begin);
810 byte [] fileContent = new byte [bufsize];
811 int count = (int) Math.Min (Int32.MaxValue, bufsize);
812 while (length > 0 && (count = stream.Read (fileContent, 0, count)) != 0) {
813 _Writer.WriteBytes (fileContent, 0, count);
815 count = (int) Math.Min (length, fileContent.Length);
819 public void WriteFile (string filename)
821 WriteFile (filename, false);
824 public void WriteFile (string filename, bool readIntoMemory)
826 FileStream fs = null;
828 fs = File.OpenRead (filename);
829 long size = fs.Length;
830 if (readIntoMemory) {
831 WriteFromStream (fs, 0, size, size);
833 WriteFromStream (fs, 0, size, 8192);
841 public void WriteFile (string filename, long offset, long size)
843 FileStream fs = null;
845 fs = File.OpenRead (filename);
846 WriteFromStream (fs, offset, size, 8192);
853 public void WriteFile (IntPtr fileHandle, long offset, long size)
855 FileStream fs = null;
857 fs = new FileStream (fileHandle, FileAccess.Read);
858 WriteFromStream (fs, offset, size, 8192);
866 internal void OnCookieAdd (HttpCookie cookie)
870 [MonoTODO("Do we need this?")]
871 internal void OnCookieChange (HttpCookie cookie)
876 internal void GoingToChangeCookieColl ()
881 internal void ChangedCookieColl ()