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