2 // System.Web.HttoWorkerRequest.cs
5 // Miguel de Icaza (miguel@novell.com)
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
33 using System.Collections;
34 using System.Runtime.InteropServices;
36 namespace System.Web {
39 public abstract class HttpWorkerRequest {
41 public delegate void EndOfSendNotification (HttpWorkerRequest wr, object extraData);
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;
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;
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;
104 public virtual string MachineConfigPath {
110 public virtual string MachineInstallDirectory {
116 public virtual void CloseConnection ()
120 public virtual string GetAppPath ()
125 public virtual string GetAppPathTranslated ()
130 public virtual string GetAppPoolID ()
135 public virtual long GetBytesRead ()
140 public virtual string GetFilePath ()
145 public virtual string GetFilePathTranslated ()
150 public virtual string GetKnownRequestHeader (int index)
155 public virtual string GetPathInfo ()
160 public virtual byte [] GetPreloadedEntityBody ()
165 public virtual string GetProtocol ()
173 public virtual byte [] GetQueryStringRawBytes ()
178 public virtual string GetRemoteName ()
180 return GetRemoteAddress ();
183 public virtual int GetRequestReason ()
188 public virtual string GetServerName ()
190 return GetLocalAddress ();
193 public virtual string GetServerVariable (string name)
198 public virtual string GetUnknownRequestHeader (string name)
203 [CLSCompliant (false)]
204 public virtual string [][] GetUnknownRequestHeaders ()
209 public virtual IntPtr GetUserToken ()
214 public bool HasEntityBody ()
216 // TODO this is not virtual.
221 public virtual bool HeadersSent ()
226 public virtual bool IsClientConnected ()
231 public virtual bool IsEntireEntityBodyIsPreloaded ()
236 public virtual bool IsSecure ()
241 public virtual string MapPath (string virtualPath)
246 public virtual int ReadEntityBody (byte [] buffer, int size)
248 throw new NotImplementedException ("");
251 public virtual void SendCalculatedContentLength (int contentLength)
253 throw new NotImplementedException ();
257 public virtual void SendResponseFromMemory (IntPtr data, int length)
259 throw new NotImplementedException("SendResponseFromMemory: unsafe buffers (IntPtr) are not supported");
262 public virtual void SendResponseFromMemory (IntPtr data, int length)
264 byte [] copy = new byte [length];
266 Marshal.Copy (data, copy, 0, length);
268 SendResponseFromMemory (copy, length);
272 public virtual void SetEndOfSendNotification (HttpWorkerRequest.EndOfSendNotification callback, object extraData)
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);
296 #region Static methods
298 public static int GetKnownRequestHeaderIndex (string 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;
345 public static string GetKnownRequestHeaderName (int 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";
389 throw new IndexOutOfRangeException ("index");
393 public static int GetKnownResponseHeaderIndex (string 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;
431 public static string GetKnownResponseHeaderName (int 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";
466 throw new IndexOutOfRangeException ("index");
469 public static string GetStatusDescription (int 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";
525 // These are exposed on the public API, but are for internal consumption
527 public virtual byte [] GetClientCertificate ()
532 public virtual byte [] GetClientCertificateBinaryIssuer ()
537 public virtual int GetClientCertificateEncoding ()
542 public virtual byte [] GetClientCertificatePublicKey ()
547 public virtual DateTime GetClientCertificateValidFrom ()
552 public virtual DateTime GetClientCertificateValidUntil ()
557 public virtual long GetConnectionID ()
562 public virtual long GetUrlContextID ()
567 public virtual IntPtr GetVirtualPathToken ()