2005-10-03 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web / HttpWorkerRequest.cs
1 //
2 // System.Web.HttoWorkerRequest.cs 
3 //
4 // Author:
5 //      Miguel de Icaza (miguel@novell.com)
6 //
7
8 //
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Collections;
32 using System.Runtime.InteropServices;
33 using System.Security.Permissions;
34 using System.Web.UI;
35
36 namespace System.Web {
37
38         // CAS
39         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41         // attributes
42         [ComVisible (false)]
43         public abstract class HttpWorkerRequest {
44
45                 public delegate void EndOfSendNotification (HttpWorkerRequest wr, object extraData);
46
47                 public const int HeaderCacheControl = 0;
48                 public const int HeaderConnection = 1;
49                 public const int HeaderDate = 2;
50                 public const int HeaderKeepAlive = 3;
51                 public const int HeaderPragma = 4;
52                 public const int HeaderTrailer = 5;
53                 public const int HeaderTransferEncoding = 6;
54                 public const int HeaderUpgrade = 7;
55                 public const int HeaderVia = 8;
56                 public const int HeaderWarning = 9;
57                 public const int HeaderAllow = 10;
58                 public const int HeaderContentLength = 11;
59                 public const int HeaderContentType = 12;
60                 public const int HeaderContentEncoding = 13;
61                 public const int HeaderContentLanguage = 14;
62                 public const int HeaderContentLocation = 15;
63                 public const int HeaderContentMd5 = 16;
64                 public const int HeaderContentRange = 17;
65                 public const int HeaderExpires = 18;
66                 public const int HeaderLastModified = 19;
67                 public const int HeaderAccept = 20;
68                 public const int HeaderAcceptCharset = 21;
69                 public const int HeaderAcceptEncoding = 22;
70                 public const int HeaderAcceptLanguage = 23;
71                 public const int HeaderAuthorization = 24;
72                 public const int HeaderCookie = 25;
73                 public const int HeaderExpect = 26;
74                 public const int HeaderFrom = 27;
75                 public const int HeaderHost = 28;
76                 public const int HeaderIfMatch = 29;
77                 public const int HeaderIfModifiedSince = 30;
78                 public const int HeaderIfNoneMatch = 31;
79                 public const int HeaderIfRange = 32;
80                 public const int HeaderIfUnmodifiedSince = 33;
81                 public const int HeaderMaxForwards = 34;
82                 public const int HeaderProxyAuthorization = 35;
83                 public const int HeaderReferer = 36;
84                 public const int HeaderRange = 37;
85                 public const int HeaderTe = 38;
86                 public const int HeaderUserAgent = 39;
87                 public const int RequestHeaderMaximum = 40;
88
89                 public const int HeaderAcceptRanges = 20;
90                 public const int HeaderAge = 21;
91                 public const int HeaderEtag = 22;
92                 public const int HeaderLocation = 23;
93                 public const int HeaderProxyAuthenticate = 24;
94                 public const int HeaderRetryAfter = 25;
95                 public const int HeaderServer = 26;
96                 public const int HeaderSetCookie = 27;
97                 public const int HeaderVary = 28;
98                 public const int HeaderWwwAuthenticate = 29;
99                 public const int ResponseHeaderMaximum = 30;
100
101                 public const int ReasonResponseCacheMiss = 0;
102                 public const int ReasonFileHandleCacheMiss = 1;
103                 public const int ReasonCachePolicy = 2;
104                 public const int ReasonCacheSecurity = 3;
105                 public const int ReasonClientDisconnect = 4;
106                 public const int ReasonDefault = 0;
107
108                 public virtual string MachineConfigPath {
109                         get {
110                                 return null;
111                         }
112                 }
113
114                 public virtual string MachineInstallDirectory {
115                         get {
116                                 return null;
117                         }
118                 }
119
120 #if NET_2_0
121                 public virtual Guid RequestTraceIdentifier {
122                         get { return Guid.Empty; }
123                 }
124
125                 public virtual string RootWebConfigPath {
126                         get { return null; }
127                 }
128 #endif
129
130                 public virtual void CloseConnection ()
131                 {
132                 }
133
134                 public virtual string GetAppPath ()
135                 {
136                         return null;
137                 }
138
139                 public virtual string GetAppPathTranslated ()
140                 {
141                         return null;
142                 }
143
144                 public virtual string GetAppPoolID ()
145                 {
146                         return null;
147                 }
148                 
149                 public virtual long GetBytesRead ()
150                 {
151                         return 0;
152                 }
153
154                 public virtual string GetFilePath ()
155                 {
156                         return null;
157                 }
158
159                 public virtual string GetFilePathTranslated ()
160                 {
161                         return null;
162                 }
163
164                 public virtual string GetKnownRequestHeader (int index)
165                 {
166                         return null;
167                 }
168
169                 public virtual string GetPathInfo ()
170                 {
171                         return "";
172                 }
173
174                 public virtual byte [] GetPreloadedEntityBody ()
175                 {
176                         return null;
177                 }
178 #if NET_2_0
179                 public virtual int GetPreloadedEntityBody (byte[] buffer, int offset)
180                 {
181                         return 0;
182                 }
183
184                 public virtual int GetPreloadedEntityBodyLength ()
185                 {
186                         return 0;
187                 }
188 #endif
189                 public virtual string GetProtocol ()
190                 {
191                         if (IsSecure ())
192                                 return "https";
193                         else
194                                 return "http";
195                 }
196                 
197                 public virtual byte [] GetQueryStringRawBytes ()
198                 {
199                         return null;
200                 }
201
202                 public virtual string GetRemoteName ()
203                 {
204                         return GetRemoteAddress ();
205                 }
206                 
207                 public virtual int GetRequestReason ()
208                 {
209                         return 0;
210                 }
211
212                 public virtual string GetServerName ()
213                 {
214                         return GetLocalAddress ();
215                 }
216
217                 public virtual string GetServerVariable (string name)
218                 {
219                         return null;
220                 }
221 #if NET_2_0
222                 public virtual int GetTotalEntityBodyLength ()
223                 {
224                         return 0;
225                 }
226 #endif
227                 public virtual string GetUnknownRequestHeader (string name)
228                 {
229                         return null;
230                 }
231
232                 [CLSCompliant (false)]
233                 public virtual string [][] GetUnknownRequestHeaders ()
234                 {
235                         return null;
236                 }
237
238                 public virtual IntPtr GetUserToken ()
239                 {
240                         return IntPtr.Zero;
241                 }
242
243                 public bool HasEntityBody ()
244                 {
245                         // TODO this is not virtual.
246                         
247                         return false;
248                 }
249
250                 public virtual bool HeadersSent ()
251                 {
252                         return true;
253                 }
254
255                 public virtual bool IsClientConnected ()
256                 {
257                         return true;
258                 }
259
260                 public virtual bool IsEntireEntityBodyIsPreloaded ()
261                 {
262                         return false;
263                 }
264
265                 public virtual bool IsSecure ()
266                 {
267                         return false;
268                 }
269
270                 public virtual string MapPath (string virtualPath)
271                 {
272                         return null;
273                 }
274
275                 public virtual int ReadEntityBody (byte [] buffer, int size)
276                 {
277                         return 0;
278                 }
279 #if NET_2_0
280                 public virtual int ReadEntityBody (byte [] buffer, int offset, int size)
281                 {
282                         return 0;
283                 }
284 #endif
285                 public virtual void SendCalculatedContentLength (int contentLength)
286                 {
287                         throw new NotImplementedException ();
288                 }
289
290 #if TARGET_JVM
291                 public virtual void SendResponseFromMemory (IntPtr data, int length)
292                 {
293                         throw new NotImplementedException("SendResponseFromMemory: unsafe buffers (IntPtr) are not supported");
294                 }
295 #else
296                 public virtual void SendResponseFromMemory (IntPtr data, int length)
297                 {
298                         if (data != IntPtr.Zero) {
299                                 byte [] copy = new byte [length];
300                                 Marshal.Copy (data, copy, 0, length);
301                                 SendResponseFromMemory (copy, length);
302                         }
303                 }
304 #endif
305
306                 public virtual void SetEndOfSendNotification (HttpWorkerRequest.EndOfSendNotification callback, object extraData)
307                 {
308                 }
309
310 #region Abstract methods
311                 public abstract void EndOfRequest ();
312                 public abstract void FlushResponse (bool finalFlush);
313                 public abstract string GetHttpVerbName ();
314                 public abstract string GetHttpVersion ();
315                 public abstract string GetLocalAddress ();
316                 public abstract int GetLocalPort ();
317                 public abstract string GetQueryString ();
318                 public abstract string GetRawUrl ();
319                 public abstract string GetRemoteAddress ();
320                 public abstract int GetRemotePort ();
321                 public abstract string GetUriPath ();
322                 public abstract void SendKnownResponseHeader (int index, string value);
323                 public abstract void SendResponseFromFile (IntPtr handle, long offset, long length);
324                 public abstract void SendResponseFromFile (string filename, long offset, long length);
325                 public abstract void SendResponseFromMemory (byte [] data, int length);
326                 public abstract void SendStatus (int statusCode, string statusDescription);
327                 public abstract void SendUnknownResponseHeader (string name, string value);
328 #endregion
329                 
330 #region Static methods
331                 
332                 public static int GetKnownRequestHeaderIndex (string header)
333                 {
334                         switch (header){
335                         case "Cache-Control": return HeaderCacheControl;
336                         case "Connection": return HeaderConnection;
337                         case "Date": return HeaderDate;
338                         case "Keep-Alive": return HeaderKeepAlive;
339                         case "Pragma": return HeaderPragma;
340                         case "Trailer": return HeaderTrailer;
341                         case "Transfer-Encoding": return HeaderTransferEncoding;
342                         case "Upgrade": return HeaderUpgrade;
343                         case "Via": return HeaderVia;
344                         case "Warning": return HeaderWarning;
345                         case "Allow": return HeaderAllow;
346                         case "Content-Length": return HeaderContentLength;
347                         case "Content-Type": return HeaderContentType;
348                         case "Content-Encoding": return HeaderContentEncoding;
349                         case "Content-Language": return HeaderContentLanguage;
350                         case "Content-Location": return HeaderContentLocation;
351                         case "Content-MD5": return HeaderContentMd5;
352                         case "Content-Range": return HeaderContentRange;
353                         case "Expires": return HeaderExpires;
354                         case "Last-Modified": return HeaderLastModified;
355                         case "Accept": return HeaderAccept;
356                         case "Accept-Charset": return HeaderAcceptCharset;
357                         case "Accept-Encoding": return HeaderAcceptEncoding;
358                         case "Accept-Language": return HeaderAcceptLanguage;
359                         case "Authorization": return HeaderAuthorization;
360                         case "Cookie": return HeaderCookie;
361                         case "Expect": return HeaderExpect;
362                         case "From": return HeaderFrom;
363                         case "Host": return HeaderHost;
364                         case "If-Match": return HeaderIfMatch;
365                         case "If-Modified-Since": return HeaderIfModifiedSince;
366                         case "If-None-Match": return HeaderIfNoneMatch;
367                         case "If-Range": return HeaderIfRange;
368                         case "If-Unmodified-Since": return HeaderIfUnmodifiedSince;
369                         case "Max-Forwards": return HeaderMaxForwards;
370                         case "Proxy-Authorization": return HeaderProxyAuthorization;
371                         case "Referer": return HeaderReferer;
372                         case "Range": return HeaderRange;
373                         case "TE": return HeaderTe;
374                         case "User-Agent": return HeaderUserAgent;
375                         }
376                         return -1;
377                 }
378
379                 public static string GetKnownRequestHeaderName (int index)
380                 {
381                         switch (index){
382                         case HeaderCacheControl: return "Cache-Control";
383                         case HeaderConnection: return "Connection";
384                         case HeaderDate: return "Date";
385                         case HeaderKeepAlive: return "Keep-Alive";
386                         case HeaderPragma: return "Pragma";
387                         case HeaderTrailer: return "Trailer";
388                         case HeaderTransferEncoding: return "Transfer-Encoding";
389                         case HeaderUpgrade: return "Upgrade";
390                         case HeaderVia: return "Via";
391                         case HeaderWarning: return "Warning";
392                         case HeaderAllow: return "Allow";
393                         case HeaderContentLength: return "Content-Length";
394                         case HeaderContentType: return "Content-Type";
395                         case HeaderContentEncoding: return "Content-Encoding";
396                         case HeaderContentLanguage: return "Content-Language";
397                         case HeaderContentLocation: return "Content-Location";
398                         case HeaderContentMd5: return "Content-MD5";
399                         case HeaderContentRange: return "Content-Range";
400                         case HeaderExpires: return "Expires";
401                         case HeaderLastModified: return "Last-Modified";
402                         case HeaderAccept: return "Accept";
403                         case HeaderAcceptCharset: return "Accept-Charset";
404                         case HeaderAcceptEncoding: return "Accept-Encoding";
405                         case HeaderAcceptLanguage: return "Accept-Language";
406                         case HeaderAuthorization: return "Authorization";
407                         case HeaderCookie: return "Cookie";
408                         case HeaderExpect: return "Expect";
409                         case HeaderFrom: return "From";
410                         case HeaderHost: return "Host";
411                         case HeaderIfMatch: return "If-Match";
412                         case HeaderIfModifiedSince: return "If-Modified-Since";
413                         case HeaderIfNoneMatch: return "If-None-Match";
414                         case HeaderIfRange: return "If-Range";
415                         case HeaderIfUnmodifiedSince: return "If-Unmodified-Since";
416                         case HeaderMaxForwards: return "Max-Forwards";
417                         case HeaderProxyAuthorization: return "Proxy-Authorization";
418                         case HeaderReferer: return "Referer";
419                         case HeaderRange: return "Range";
420                         case HeaderTe: return "TE";
421                         case HeaderUserAgent: return "User-Agent";
422                         }
423                         throw new IndexOutOfRangeException ("index");
424                                 
425                 }
426
427                 public static int GetKnownResponseHeaderIndex (string header)
428                 {
429                         switch (header){
430                         case "Cache-Control": return HeaderCacheControl;
431                         case "Connection": return HeaderConnection;
432                         case "Date": return HeaderDate;
433                         case "Keep-Alive": return HeaderKeepAlive;
434                         case "Pragma": return HeaderPragma;
435                         case "Trailer": return HeaderTrailer;
436                         case "Transfer-Encoding": return HeaderTransferEncoding;
437                         case "Upgrade": return HeaderUpgrade;
438                         case "Via": return HeaderVia;
439                         case "Warning": return HeaderWarning;
440                         case "Allow": return HeaderAllow;
441                         case "Content-Length": return HeaderContentLength;
442                         case "Content-Type": return HeaderContentType;
443                         case "Content-Encoding": return HeaderContentEncoding;
444                         case "Content-Language": return HeaderContentLanguage;
445                         case "Content-Location": return HeaderContentLocation;
446                         case "Content-MD5": return HeaderContentMd5;
447                         case "Content-Range": return HeaderContentRange;
448                         case "Expires": return HeaderExpires;
449                         case "Last-Modified": return HeaderLastModified;
450                         case "Accept-Ranges": return HeaderAcceptRanges;
451                         case "Age": return HeaderAge;
452                         case "ETag": return HeaderEtag;
453                         case "Location": return HeaderLocation;
454                         case "Proxy-Authenticate":return HeaderProxyAuthenticate;
455                         case "Retry-After": return HeaderRetryAfter;
456                         case "Server": return HeaderServer;
457                         case "Set-Cookie": return HeaderSetCookie;
458                         case "Vary": return HeaderVary;
459                         case "WWW-Authenticate": return HeaderWwwAuthenticate;
460                         }
461
462                         return -1;
463                 }
464
465                 public static string GetKnownResponseHeaderName (int index)
466                 {
467                         switch (index){
468                         case HeaderCacheControl: return "Cache-Control";
469                         case HeaderConnection: return "Connection";
470                         case HeaderDate: return "Date";
471                         case HeaderKeepAlive: return "Keep-Alive";
472                         case HeaderPragma: return "Pragma";
473                         case HeaderTrailer: return "Trailer";
474                         case HeaderTransferEncoding: return "Transfer-Encoding";
475                         case HeaderUpgrade: return "Upgrade";
476                         case HeaderVia: return "Via";
477                         case HeaderWarning: return "Warning";
478                         case HeaderAllow: return "Allow";
479                         case HeaderContentLength: return "Content-Length";
480                         case HeaderContentType: return "Content-Type";
481                         case HeaderContentEncoding: return "Content-Encoding";
482                         case HeaderContentLanguage: return "Content-Language";
483                         case HeaderContentLocation: return "Content-Location";
484                         case HeaderContentMd5: return "Content-MD5";
485                         case HeaderContentRange: return "Content-Range";
486                         case HeaderExpires: return "Expires";
487                         case HeaderLastModified: return "Last-Modified";
488                         case HeaderAcceptRanges: return "Accept-Ranges";
489                         case HeaderAge: return "Age";
490                         case HeaderEtag: return "ETag";
491                         case HeaderLocation: return "Location";
492                         case HeaderProxyAuthenticate: return "Proxy-Authenticate";
493                         case HeaderRetryAfter: return "Retry-After";
494                         case HeaderServer: return "Server";
495                         case HeaderSetCookie: return "Set-Cookie";
496                         case HeaderVary: return "Vary";
497                         case HeaderWwwAuthenticate: return "WWW-Authenticate";
498                         }
499
500                         throw new IndexOutOfRangeException ("index");
501                 }
502                 
503                 public static string GetStatusDescription (int code)
504                 {
505                         switch (code){
506                         case 100: return "Continue";
507                         case 101: return "Switching Protocols";
508                         case 102: return "Processing";
509                         case 200: return "OK";
510                         case 201: return "Created";
511                         case 202: return "Accepted";
512                         case 203: return "Non-Authoritative Information";
513                         case 204: return "No Content";
514                         case 205: return "Reset Content";
515                         case 206: return "Partial Content";
516                         case 207: return "Multi-Status";
517                         case 300: return "Multiple Choices";
518                         case 301: return "Moved Permanently";
519                         case 302: return "Found";
520                         case 303: return "See Other";
521                         case 304: return "Not Modified";
522                         case 305: return "Use Proxy";
523                         case 307: return "Temporary Redirect";
524                         case 400: return "Bad Request";
525                         case 401: return "Unauthorized";
526                         case 402: return "Payment Required";
527                         case 403: return "Forbidden";
528                         case 404: return "Not Found";
529                         case 405: return "Method Not Allowed";
530                         case 406: return "Not Acceptable";
531                         case 407: return "Proxy Authentication Required";
532                         case 408: return "Request Timeout";
533                         case 409: return "Conflict";
534                         case 410: return "Gone";
535                         case 411: return "Length Required";
536                         case 412: return "Precondition Failed";
537                         case 413: return "Request Entity Too Large";
538                         case 414: return "Request-Uri Too Long";
539                         case 415: return "Unsupported Media Type";
540                         case 416: return "Requested Range Not Satisfiable";
541                         case 417: return "Expectation Failed";
542                         case 422: return "Unprocessable Entity";
543                         case 423: return "Locked";
544                         case 424: return "Failed Dependency";
545                         case 500: return "Internal Server Error";
546                         case 501: return "Not Implemented";
547                         case 502: return "Bad Gateway";
548                         case 503: return "Service Unavailable";
549                         case 504: return "Gateway Timeout";
550                         case 505: return "Http Version Not Supported";
551                         case 507: return "Insufficient Storage";
552                         }
553                         return "";
554                 }
555 #endregion
556
557 #region Internals
558                 //
559                 // These are exposed on the public API, but are for internal consumption
560                 //
561                 public virtual byte [] GetClientCertificate ()
562                 {
563                         return new byte [0];
564                 }
565                 
566                 public virtual byte [] GetClientCertificateBinaryIssuer ()
567                 {
568                         return new byte [0];
569                 }
570                 
571                 public virtual int GetClientCertificateEncoding  ()
572                 {
573                         return 0;
574                 }
575                 
576                 public virtual byte [] GetClientCertificatePublicKey ()
577                 {
578                         return new byte [0];
579                 }
580
581                 public virtual DateTime GetClientCertificateValidFrom ()
582                 {
583                         return DateTime.Now;
584                 }
585
586                 public virtual DateTime GetClientCertificateValidUntil ()
587                 {
588                         return DateTime.Now;
589                 }
590
591                 public virtual long GetConnectionID ()
592                 {
593                         return 0;
594                 }
595
596                 public virtual long GetUrlContextID ()
597                 {
598                         return 0;
599                 }
600
601                 public virtual IntPtr GetVirtualPathToken ()
602                 {
603                         return IntPtr.Zero;
604                 }
605 #endregion
606                 
607         }
608 }