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;
44 string _sCacheControl;
45 string _sTransferEncoding;
47 string _sStatusDescription;
49 HttpCookieCollection _Cookies;
50 HttpCachePolicy _CachePolicy;
52 Encoding _ContentEncoding;
56 TextWriter _TextWriter;
58 HttpWorkerRequest _WorkerRequest;
60 ArrayList fileDependencies;
62 public HttpResponse (TextWriter output)
66 _bHeadersSent = false;
68 _Headers = new ArrayList ();
70 _sContentType = "text/html";
74 _sCacheControl = null;
77 _bSuppressContent = false;
78 _bSuppressHeaders = false;
79 _bClientDisconnected = false;
86 internal HttpResponse (HttpWorkerRequest WorkerRequest, HttpContext Context)
89 _WorkerRequest = WorkerRequest;
93 _bHeadersSent = false;
95 _Headers = new ArrayList ();
97 _sContentType = "text/html";
101 _sCacheControl = null;
104 _bSuppressContent = false;
105 _bSuppressHeaders = false;
106 _bClientDisconnected = false;
110 _Writer = new HttpWriter (this);
111 _TextWriter = _Writer;
114 internal Encoder ContentEncoder
117 return ContentEncoding.GetEncoder ();
121 internal void FinalFlush ()
126 internal void DoFilter (bool really)
128 if (really && null != _Writer)
129 _Writer.FilterData (true);
134 [MonoTODO("We need to add cache headers also")]
135 private ArrayList GenerateHeaders ()
137 ArrayList oHeaders = new ArrayList (_Headers.ToArray ());
139 oHeaders.Add (new HttpResponseHeader ("X-Powered-By", "Mono"));
140 // save culture info, we need us info here
141 CultureInfo oSavedInfo = Thread.CurrentThread.CurrentCulture;
142 Thread.CurrentThread.CurrentCulture = new CultureInfo (0x0409);
144 string date = DateTime.Now.ToUniversalTime ().ToString ("ddd, d MMM yyyy HH:mm:ss ");
145 oHeaders.Add (new HttpResponseHeader ("Date", date + "GMT"));
147 Thread.CurrentThread.CurrentCulture = oSavedInfo;
149 if (_lContentLength > 0) {
150 oHeaders.Add (new HttpResponseHeader (HttpWorkerRequest.HeaderContentLength,
151 _lContentLength.ToString ()));
154 if (_sContentType != null) {
155 if (_sContentType.IndexOf ("charset=") == -1) {
156 if (Charset.Length == 0) {
157 Charset = ContentEncoding.HeaderName;
160 // Time to build our string
161 if (Charset.Length > 0) {
162 _sContentType += "; charset=" + Charset;
166 oHeaders.Add (new HttpResponseHeader (HttpWorkerRequest.HeaderContentType,
170 if (_sCacheControl != null) {
171 oHeaders.Add (new HttpResponseHeader (HttpWorkerRequest.HeaderPragma,
175 if (_sTransferEncoding != null) {
176 oHeaders.Add (new HttpResponseHeader (HttpWorkerRequest.HeaderTransferEncoding,
177 _sTransferEncoding));
180 if (_Cookies != null) {
181 int length = _Cookies.Count;
182 for (int i = 0; i < length; i++) {
183 oHeaders.Add (_Cookies.Get (i).GetCookieHeader ());
190 private void SendHeaders ()
192 _WorkerRequest.SendStatus (StatusCode, StatusDescription);
194 ArrayList oHeaders = GenerateHeaders ();
195 foreach (HttpResponseHeader oHeader in oHeaders)
196 oHeader.SendContent (_WorkerRequest);
198 _bHeadersSent = true;
204 return String.Format ("{0} {1}", StatusCode, StatusDescription);
212 iCode = Int32.Parse (value.Substring (0, value.IndexOf (' ')));
213 sMsg = value.Substring (value.IndexOf (' ') + 1);
214 } catch (Exception) {
215 throw new HttpException ("Invalid status string");
219 StatusDescription = sMsg;
224 public void AddCacheItemDependencies (ArrayList cacheKeys)
226 throw new NotImplementedException ();
230 public void AddCacheItemDependency(string cacheKey)
232 throw new NotImplementedException ();
235 public void AddFileDependencies (ArrayList filenames)
237 if (filenames == null || filenames.Count == 0)
240 if (fileDependencies == null) {
241 fileDependencies = (ArrayList) filenames.Clone ();
245 foreach (string fn in filenames)
246 AddFileDependency (fn);
249 public void AddFileDependency (string filename)
251 if (fileDependencies == null)
252 fileDependencies = new ArrayList ();
254 fileDependencies.Add (filename);
257 public void AddHeader (string name, string value)
259 AppendHeader(name, value);
262 public void AppendCookie (HttpCookie cookie)
265 throw new HttpException ("Cannot append cookies after HTTP headers have been sent");
267 Cookies.Add (cookie);
271 public void AppendToLog (string param)
273 throw new NotImplementedException ();
276 public string ApplyAppPathModifier (string virtualPath)
278 if (virtualPath == null)
281 if (virtualPath == "")
282 return _Context.Request.RootVirtualDir;
284 if (UrlUtils.IsRelativeUrl (virtualPath)) {
285 virtualPath = UrlUtils.Combine (_Context.Request.RootVirtualDir, virtualPath);
286 } else if (UrlUtils.IsRooted (virtualPath)) {
287 virtualPath = UrlUtils.Reduce (virtualPath);
300 BufferOutput = value;
304 public bool BufferOutput
318 public HttpCachePolicy Cache
321 if (null == _CachePolicy)
322 _CachePolicy = new HttpCachePolicy ();
328 [MonoTODO("Set status in the cache policy")]
329 public string CacheControl
332 return _sCacheControl;
337 throw new HttpException ("Headers has been sent to the client");
339 _sCacheControl = value;
343 public string Charset
346 if (null == _sCharset)
347 _sCharset = ContentEncoding.WebName;
354 throw new HttpException ("Headers has been sent to the client");
360 public Encoding ContentEncoding
363 if (_ContentEncoding == null)
364 _ContentEncoding = WebEncoding.Encoding;
366 return _ContentEncoding;
371 throw new ArgumentException ("Can't set a null as encoding");
373 _ContentEncoding = value;
380 public string ContentType
383 return _sContentType;
388 throw new HttpException ("Headers has been sent to the client");
390 _sContentType = value;
394 public HttpCookieCollection Cookies
397 if (null == _Cookies)
398 _Cookies = new HttpCookieCollection (this, false);
404 [MonoTODO("Set expires in the cache policy")]
408 throw new NotImplementedException ();
412 throw new NotImplementedException ();
416 [MonoTODO("Set expiresabsolute in the cache policy")]
417 public DateTime ExpiresAbsolute
420 throw new NotImplementedException ();
424 throw new NotImplementedException ();
432 return _Writer.GetActiveFilter ();
439 throw new HttpException ("Filtering is not allowed");
441 _Writer.ActivateFilter (value);
445 public bool IsClientConnected
448 if (_ClientDisconnected)
451 if (null != _WorkerRequest && (!_WorkerRequest.IsClientConnected ())) {
452 _ClientDisconnected = false;
460 public TextWriter Output
467 public Stream OutputStream
471 throw new HttpException ("an Output stream not available when " +
472 "running with custom text writer");
474 return _Writer.OutputStream;
478 public string StatusDescription
481 if (null == _sStatusDescription)
482 _sStatusDescription =
483 HttpWorkerRequest.GetStatusDescription (_iStatusCode);
485 return _sStatusDescription;
490 throw new HttpException ("Headers has been sent to the client");
492 _sStatusDescription = value;
496 public int StatusCode
504 throw new HttpException ("Headers has been sent to the client");
506 _sStatusDescription = null;
507 _iStatusCode = value;
511 public bool SuppressContent
514 return _bSuppressContent;
519 throw new HttpException ("Headers has been sent to the client");
521 _bSuppressContent = true;
528 if (_Context == null)
531 return _Context.Request;
535 internal void AppendHeader (int iIndex, string value)
538 throw new HttpException ("Headers has been sent to the client");
541 case HttpWorkerRequest.HeaderContentLength:
542 _lContentLength = Int64.Parse (value);
544 case HttpWorkerRequest.HeaderContentEncoding:
545 _sContentType = value;
547 case HttpWorkerRequest.HeaderTransferEncoding:
548 _sTransferEncoding = value;
549 if (value.Equals ("chunked")) {
555 case HttpWorkerRequest.HeaderPragma:
556 _sCacheControl = value;
559 _Headers.Add (new HttpResponseHeader (iIndex, value));
564 public void AppendHeader (string name, string value)
567 throw new HttpException ("Headers has been sent to the client");
569 switch (name.ToLower ()) {
570 case "content-length":
571 _lContentLength = Int64.Parse (value);
574 _sContentType = value;
576 case "transfer-encoding":
577 _sTransferEncoding = value;
578 if (value.Equals ("chunked")) {
585 _sCacheControl = value;
588 _Headers.Add (new HttpResponseHeader (name, value));
593 internal TextWriter SetTextWriter (TextWriter w)
595 TextWriter prev = _TextWriter;
600 public void BinaryWrite (byte [] buffer)
602 OutputStream.Write (buffer, 0, buffer.Length);
611 public void ClearContent ()
616 public void ClearHeaders ()
619 throw new HttpException ("Headers has been sent to the client");
621 _sContentType = "text/html";
625 _Headers = new ArrayList ();
626 _sCacheControl = null;
627 _sTransferEncoding = null;
630 _bSuppressContent = false;
631 _bSuppressHeaders = false;
632 _bClientDisconnected = false;
637 _bClientDisconnected = false;
638 _WorkerRequest.CloseConnection ();
639 _bClientDisconnected = true;
642 internal void Dispose ()
644 if (_Writer != null) {
650 [MonoTODO("Handle callbacks into before done with session, needs to have a non ended flush here")]
651 internal void FlushAtEndOfRequest ()
663 _Context.ApplicationInstance.CompleteRequest ();
671 private void Flush (bool bFinish)
678 if (_Writer == null) {
679 _TextWriter.Flush ();
685 if (_bClientDisconnected)
688 long length = _Writer.BufferSize;
689 if (!_bHeadersSent && !_bSuppressHeaders) {
691 if (length == 0 && _lContentLength == 0)
692 _sContentType = null;
695 length = _Writer.BufferSize;
697 _WorkerRequest.SendCalculatedContentLength ((int) length);
699 if (_lContentLength == 0 && _iStatusCode == 200 &&
700 _sTransferEncoding == null) {
701 // Check we are going todo chunked encoding
702 string sProto = Request.ServerVariables ["SERVER_PROTOCOL"];
703 sProto = "HTTP/1.0"; // Remove this line when we support properly
706 if (sProto != null && sProto == "HTTP/1.1") {
708 HttpWorkerRequest.HeaderTransferEncoding,
711 // Just in case, the old browsers send a HTTP/1.0
712 // request with Connection: Keep-Alive
714 HttpWorkerRequest.HeaderConnection,
719 length = _Writer.BufferSize;
725 _Writer.FilterData (false);
726 length = _Writer.BufferSize;
730 _WorkerRequest.FlushResponse (bFinish);
736 if (!_bSuppressContent && Request.HttpMethod == "HEAD")
737 _bSuppressContent = true;
739 if (!_bSuppressContent) {
740 _bClientDisconnected = false;
742 Encoding oASCII = Encoding.ASCII;
744 string chunk = Convert.ToString(_Writer.BufferSize, 16);
745 byte [] arrPrefix = oASCII.GetBytes (chunk + s_sChunkedPrefix);
747 _WorkerRequest.SendResponseFromMemory (arrPrefix,
750 _Writer.SendContent (_WorkerRequest);
752 _WorkerRequest.SendResponseFromMemory (s_arrChunkSuffix,
753 s_arrChunkSuffix.Length);
755 _WorkerRequest.SendResponseFromMemory (
756 s_arrChunkEnd, s_arrChunkEnd.Length);
758 _Writer.SendContent (_WorkerRequest);
764 _WorkerRequest.FlushResponse (bFinish);
773 public void Pics (string value)
775 AppendHeader ("PICS-Label", value);
779 public void Redirect (string url)
781 Redirect (url, true);
784 public void Redirect (string url, bool endResponse)
787 throw new HttpException ("Headers has been sent to the client");
791 url = ApplyAppPathModifier (url);
793 AppendHeader (HttpWorkerRequest.HeaderLocation, url);
795 // Text for browsers that can't handle location header
796 Write ("<html><head><title>Object moved</title></head><body>\r\n");
797 Write ("<h2>Object moved to <a href='" + url + "'>here</a></h2>\r\n");
798 Write ("</body><html>\r\n");
804 public void Write (char ch)
806 _TextWriter.Write(ch);
809 public void Write (object obj)
811 _TextWriter.Write(obj);
814 public void Write (string str)
816 _TextWriter.Write (str);
819 public void Write (char [] buffer, int index, int count)
821 _TextWriter.Write (buffer, index, count);
825 public static void RemoveOutputCacheItem (string path)
827 throw new NotImplementedException ();
830 public void SetCookie (HttpCookie cookie)
833 throw new HttpException ("Cannot append cookies after HTTP headers have been sent");
835 Cookies.Add (cookie);
838 private void WriteFromStream (Stream stream, long offset, long length, long bufsize)
840 if (offset < 0 || length <= 0)
843 long stLength = stream.Length;
844 if (offset + length > stLength)
845 length = stLength - offset;
848 stream.Seek (offset, SeekOrigin.Begin);
850 byte [] fileContent = new byte [bufsize];
851 int count = (int) Math.Min (Int32.MaxValue, bufsize);
852 while (length > 0 && (count = stream.Read (fileContent, 0, count)) != 0) {
853 _Writer.WriteBytes (fileContent, 0, count);
855 count = (int) Math.Min (length, fileContent.Length);
859 public void WriteFile (string filename)
861 WriteFile (filename, false);
864 public void WriteFile (string filename, bool readIntoMemory)
866 FileStream fs = null;
868 fs = File.OpenRead (filename);
869 long size = fs.Length;
870 if (readIntoMemory) {
871 WriteFromStream (fs, 0, size, size);
873 WriteFromStream (fs, 0, size, 8192);
881 public void WriteFile (string filename, long offset, long size)
883 FileStream fs = null;
885 fs = File.OpenRead (filename);
886 WriteFromStream (fs, offset, size, 8192);
893 public void WriteFile (IntPtr fileHandle, long offset, long size)
895 FileStream fs = null;
897 fs = new FileStream (fileHandle, FileAccess.Read);
898 WriteFromStream (fs, offset, size, 8192);
906 internal void OnCookieAdd (HttpCookie cookie)
910 [MonoTODO("Do we need this?")]
911 internal void OnCookieChange (HttpCookie cookie)
916 internal void GoingToChangeCookieColl ()
921 internal void ChangedCookieColl ()