svn path=/branches/mono-1-1-9/mcs/; revision=51206
[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.Zero;
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 #if TARGET_JVM
257                 public virtual void SendResponseFromMemory (IntPtr data, int length)
258                 {
259                         throw new NotImplementedException("SendResponseFromMemory: unsafe buffers (IntPtr) are not supported");
260                 }
261 #else
262                 public virtual void SendResponseFromMemory (IntPtr data, int length)
263                 {
264                         byte [] copy = new byte [length];
265
266                         Marshal.Copy (data, copy, 0, length);
267
268                         SendResponseFromMemory (copy, length);
269                 }
270 #endif
271
272                 public virtual void SetEndOfSendNotification (HttpWorkerRequest.EndOfSendNotification callback, object extraData)
273                 {
274                 }
275
276 #region Abstract methods
277                 public abstract void EndOfRequest ();
278                 public abstract void FlushResponse (bool finalFlush);
279                 public abstract string GetHttpVerbName ();
280                 public abstract string GetHttpVersion ();
281                 public abstract string GetLocalAddress ();
282                 public abstract int GetLocalPort ();
283                 public abstract string GetQueryString ();
284                 public abstract string GetRawUrl ();
285                 public abstract string GetRemoteAddress ();
286                 public abstract int GetRemotePort ();
287                 public abstract string GetUriPath ();
288                 public abstract void SendKnownResponseHeader (int index, string value);
289                 public abstract void SendResponseFromFile (IntPtr handle, long offset, long length);
290                 public abstract void SendResponseFromFile (string filename, long offset, long length);
291                 public abstract void SendResponseFromMemory (byte [] data, int length);
292                 public abstract void SendStatus (int statusCode, string statusDescription);
293                 public abstract void SendUnknownResponseHeader (string name, string value);
294 #endregion
295                 
296 #region Static methods
297                 
298                 public static int GetKnownRequestHeaderIndex (string header)
299                 {
300                         switch (header){
301                         case "Cache-Control": return HeaderCacheControl;
302                         case "Connection": return HeaderConnection;
303                         case "Date": return HeaderDate;
304                         case "Keep-Alive": return HeaderKeepAlive;
305                         case "Pragma": return HeaderPragma;
306                         case "Trailer": return HeaderTrailer;
307                         case "Transfer-Encoding": return HeaderTransferEncoding;
308                         case "Upgrade": return HeaderUpgrade;
309                         case "Via": return HeaderVia;
310                         case "Warning": return HeaderWarning;
311                         case "Allow": return HeaderAllow;
312                         case "Content-Length": return HeaderContentLength;
313                         case "Content-Type": return HeaderContentType;
314                         case "Content-Encoding": return HeaderContentEncoding;
315                         case "Content-Language": return HeaderContentLanguage;
316                         case "Content-Location": return HeaderContentLocation;
317                         case "Content-MD5": return HeaderContentMd5;
318                         case "Content-Range": return HeaderContentRange;
319                         case "Expires": return HeaderExpires;
320                         case "Last-Modified": return HeaderLastModified;
321                         case "Accept": return HeaderAccept;
322                         case "Accept-Charset": return HeaderAcceptCharset;
323                         case "Accept-Encoding": return HeaderAcceptEncoding;
324                         case "Accept-Language": return HeaderAcceptLanguage;
325                         case "Authorization": return HeaderAuthorization;
326                         case "Cookie": return HeaderCookie;
327                         case "Expect": return HeaderExpect;
328                         case "From": return HeaderFrom;
329                         case "Host": return HeaderHost;
330                         case "If-Match": return HeaderIfMatch;
331                         case "If-Modified-Since": return HeaderIfModifiedSince;
332                         case "If-None-Match": return HeaderIfNoneMatch;
333                         case "If-Range": return HeaderIfRange;
334                         case "If-Unmodified-Since": return HeaderIfUnmodifiedSince;
335                         case "Max-Forwards": return HeaderMaxForwards;
336                         case "Proxy-Authorization": return HeaderProxyAuthorization;
337                         case "Referer": return HeaderReferer;
338                         case "Range": return HeaderRange;
339                         case "TE": return HeaderTe;
340                         case "User-Agent": return HeaderUserAgent;
341                         }
342                         return -1;
343                 }
344
345                 public static string GetKnownRequestHeaderName (int index)
346                 {
347                         switch (index){
348                         case HeaderCacheControl: return "Cache-Control";
349                         case HeaderConnection: return "Connection";
350                         case HeaderDate: return "Date";
351                         case HeaderKeepAlive: return "Keep-Alive";
352                         case HeaderPragma: return "Pragma";
353                         case HeaderTrailer: return "Trailer";
354                         case HeaderTransferEncoding: return "Transfer-Encoding";
355                         case HeaderUpgrade: return "Upgrade";
356                         case HeaderVia: return "Via";
357                         case HeaderWarning: return "Warning";
358                         case HeaderAllow: return "Allow";
359                         case HeaderContentLength: return "Content-Length";
360                         case HeaderContentType: return "Content-Type";
361                         case HeaderContentEncoding: return "Content-Encoding";
362                         case HeaderContentLanguage: return "Content-Language";
363                         case HeaderContentLocation: return "Content-Location";
364                         case HeaderContentMd5: return "Content-MD5";
365                         case HeaderContentRange: return "Content-Range";
366                         case HeaderExpires: return "Expires";
367                         case HeaderLastModified: return "Last-Modified";
368                         case HeaderAccept: return "Accept";
369                         case HeaderAcceptCharset: return "Accept-Charset";
370                         case HeaderAcceptEncoding: return "Accept-Encoding";
371                         case HeaderAcceptLanguage: return "Accept-Language";
372                         case HeaderAuthorization: return "Authorization";
373                         case HeaderCookie: return "Cookie";
374                         case HeaderExpect: return "Expect";
375                         case HeaderFrom: return "From";
376                         case HeaderHost: return "Host";
377                         case HeaderIfMatch: return "If-Match";
378                         case HeaderIfModifiedSince: return "If-Modified-Since";
379                         case HeaderIfNoneMatch: return "If-None-Match";
380                         case HeaderIfRange: return "If-Range";
381                         case HeaderIfUnmodifiedSince: return "If-Unmodified-Since";
382                         case HeaderMaxForwards: return "Max-Forwards";
383                         case HeaderProxyAuthorization: return "Proxy-Authorization";
384                         case HeaderReferer: return "Referer";
385                         case HeaderRange: return "Range";
386                         case HeaderTe: return "TE";
387                         case HeaderUserAgent: return "User-Agent";
388                         }
389                         throw new IndexOutOfRangeException ("index");
390                                 
391                 }
392
393                 public static int GetKnownResponseHeaderIndex (string header)
394                 {
395                         switch (header){
396                         case "Cache-Control": return HeaderCacheControl;
397                         case "Connection": return HeaderConnection;
398                         case "Date": return HeaderDate;
399                         case "Keep-Alive": return HeaderKeepAlive;
400                         case "Pragma": return HeaderPragma;
401                         case "Trailer": return HeaderTrailer;
402                         case "Transfer-Encoding": return HeaderTransferEncoding;
403                         case "Upgrade": return HeaderUpgrade;
404                         case "Via": return HeaderVia;
405                         case "Warning": return HeaderWarning;
406                         case "Allow": return HeaderAllow;
407                         case "Content-Length": return HeaderContentLength;
408                         case "Content-Type": return HeaderContentType;
409                         case "Content-Encoding": return HeaderContentEncoding;
410                         case "Content-Language": return HeaderContentLanguage;
411                         case "Content-Location": return HeaderContentLocation;
412                         case "Content-MD5": return HeaderContentMd5;
413                         case "Content-Range": return HeaderContentRange;
414                         case "Expires": return HeaderExpires;
415                         case "Last-Modified": return HeaderLastModified;
416                         case "Accept-Ranges": return HeaderAcceptRanges;
417                         case "Age": return HeaderAge;
418                         case "ETag": return HeaderEtag;
419                         case "Location": return HeaderLocation;
420                         case "Proxy-Authenticate":return HeaderProxyAuthenticate;
421                         case "Retry-After": return HeaderRetryAfter;
422                         case "Server": return HeaderServer;
423                         case "Set-Cookie": return HeaderSetCookie;
424                         case "Vary": return HeaderVary;
425                         case "WWW-Authenticate": return HeaderWwwAuthenticate;
426                         }
427
428                         return -1;
429                 }
430
431                 public static string GetKnownResponseHeaderName (int index)
432                 {
433                         switch (index){
434                         case HeaderCacheControl: return "Cache-Control";
435                         case HeaderConnection: return "Connection";
436                         case HeaderDate: return "Date";
437                         case HeaderKeepAlive: return "Keep-Alive";
438                         case HeaderPragma: return "Pragma";
439                         case HeaderTrailer: return "Trailer";
440                         case HeaderTransferEncoding: return "Transfer-Encoding";
441                         case HeaderUpgrade: return "Upgrade";
442                         case HeaderVia: return "Via";
443                         case HeaderWarning: return "Warning";
444                         case HeaderAllow: return "Allow";
445                         case HeaderContentLength: return "Content-Length";
446                         case HeaderContentType: return "Content-Type";
447                         case HeaderContentEncoding: return "Content-Encoding";
448                         case HeaderContentLanguage: return "Content-Language";
449                         case HeaderContentLocation: return "Content-Location";
450                         case HeaderContentMd5: return "Content-MD5";
451                         case HeaderContentRange: return "Content-Range";
452                         case HeaderExpires: return "Expires";
453                         case HeaderLastModified: return "Last-Modified";
454                         case HeaderAcceptRanges: return "Accept-Ranges";
455                         case HeaderAge: return "Age";
456                         case HeaderEtag: return "ETag";
457                         case HeaderLocation: return "Location";
458                         case HeaderProxyAuthenticate: return "Proxy-Authenticate";
459                         case HeaderRetryAfter: return "Retry-After";
460                         case HeaderServer: return "Server";
461                         case HeaderSetCookie: return "Set-Cookie";
462                         case HeaderVary: return "Vary";
463                         case HeaderWwwAuthenticate: return "WWW-Authenticate";
464                         }
465
466                         throw new IndexOutOfRangeException ("index");
467                 }
468                 
469                 public static string GetStatusDescription (int code)
470                 {
471                         switch (code){
472                         case 100: return "Continue";
473                         case 101: return "Switching Protocols";
474                         case 102: return "Processing";
475                         case 200: return "OK";
476                         case 201: return "Created";
477                         case 202: return "Accepted";
478                         case 203: return "Non-Authoritative Information";
479                         case 204: return "No Content";
480                         case 205: return "Reset Content";
481                         case 206: return "Partial Content";
482                         case 207: return "Multi-Status";
483                         case 300: return "Multiple Choices";
484                         case 301: return "Moved Permanently";
485                         case 302: return "Found";
486                         case 303: return "See Other";
487                         case 304: return "Not Modified";
488                         case 305: return "Use Proxy";
489                         case 307: return "Temporary Redirect";
490                         case 400: return "Bad Request";
491                         case 401: return "Unauthorized";
492                         case 402: return "Payment Required";
493                         case 403: return "Forbidden";
494                         case 404: return "Not Found";
495                         case 405: return "Method Not Allowed";
496                         case 406: return "Not Acceptable";
497                         case 407: return "Proxy Authentication Required";
498                         case 408: return "Request Timeout";
499                         case 409: return "Conflict";
500                         case 410: return "Gone";
501                         case 411: return "Length Required";
502                         case 412: return "Precondition Failed";
503                         case 413: return "Request Entity Too Large";
504                         case 414: return "Request-Uri Too Long";
505                         case 415: return "Unsupported Media Type";
506                         case 416: return "Requested Range Not Satisfiable";
507                         case 417: return "Expectation Failed";
508                         case 422: return "Unprocessable Entity";
509                         case 423: return "Locked";
510                         case 424: return "Failed Dependency";
511                         case 500: return "Internal Server Error";
512                         case 501: return "Not Implemented";
513                         case 502: return "Bad Gateway";
514                         case 503: return "Service Unavailable";
515                         case 504: return "Gateway Timeout";
516                         case 505: return "Http Version Not Supported";
517                         case 507: return "Insufficient Storage";
518                         }
519                         return "";
520                 }
521 #endregion
522
523 #region Internals
524                 //
525                 // These are exposed on the public API, but are for internal consumption
526                 //
527                 public virtual byte [] GetClientCertificate ()
528                 {
529                         return new byte [0];
530                 }
531                 
532                 public virtual byte [] GetClientCertificateBinaryIssuer ()
533                 {
534                         return new byte [0];
535                 }
536                 
537                 public virtual int GetClientCertificateEncoding  ()
538                 {
539                         return 0;
540                 }
541                 
542                 public virtual byte [] GetClientCertificatePublicKey ()
543                 {
544                         return new byte [0];
545                 }
546
547                 public virtual DateTime GetClientCertificateValidFrom ()
548                 {
549                         return DateTime.Now;
550                 }
551
552                 public virtual DateTime GetClientCertificateValidUntil ()
553                 {
554                         return DateTime.Now;
555                 }
556
557                 public virtual long GetConnectionID ()
558                 {
559                         return 0;
560                 }
561
562                 public virtual long GetUrlContextID ()
563                 {
564                         return 0;
565                 }
566
567                 public virtual IntPtr GetVirtualPathToken ()
568                 {
569                         return IntPtr.Zero;
570                 }
571 #endregion
572                 
573         }
574 }