The Labs \ Source Viewer \ SSCLI \ System.Runtime.Remoting.Channels.Http \ HttpSocketHandler

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. //==========================================================================
  16. // File: HttpSocketManager.cs
  17. //
  18. // Summary: Provides a base for the client and server http socket
  19. // managers.
  20. //
  21. //==========================================================================
  22. using System;
  23. using System.Collections;
  24. using System.Globalization;
  25. using System.IO;
  26. using System.Net;
  27. using System.Net.Sockets;
  28. using System.Runtime.Remoting.Messaging;
  29. using System.Text;
  30. using System.Threading;
  31. namespace System.Runtime.Remoting.Channels.Http
  32. {
  33.    
  34.     // A client socket manager instance should encapsulate the socket
  35.     // for the purpose of reading a response
  36.     internal abstract class HttpSocketHandler : SocketHandler
  37.     {
  38.         private static byte[] s_httpVersion = Encoding.ASCII.GetBytes("HTTP/1.1");
  39.         private static byte[] s_httpVersionAndSpace = Encoding.ASCII.GetBytes("HTTP/1.1 ");
  40.         private static byte[] s_headerSeparator = new byte[] {(byte)':', (byte)' '};
  41.         private static byte[] s_endOfLine = new byte[] {(byte)'\r', (byte)'\n'};
  42.        
  43.        
  44.         public HttpSocketHandler(Socket socket, RequestQueue requestQueue, Stream stream) : base(socket, requestQueue, stream)
  45.         {
  46.         }
  47.         // HttpSocketHandler
  48.        
  49.         protected void ReadToEndOfHeaders(BaseTransportHeaders headers, out bool bChunked, out int contentLength, ref bool bKeepAlive, ref bool bSendContinue)
  50.         {
  51.             bChunked = false;
  52.             contentLength = 0;
  53.            
  54.             // read and process headers
  55.             for (;;) {
  56.                 string header = ReadToEndOfLine();
  57.                
  58.                 // stop reading headers at first blank line
  59.                 if (header.Length == 0)
  60.                     break;
  61.                
  62.                 int sep = header.IndexOf(":");
  63.                 string headerName = header.Substring(0, sep);
  64.                 string headerValue = header.Substring(sep + 1 + 1);
  65.                 // skip semi-colon and space
  66.                 if (String.Compare(headerName, "Transfer-Encoding", StringComparison.OrdinalIgnoreCase) == 0) {
  67.                     if (String.Compare(headerValue, "chunked", StringComparison.OrdinalIgnoreCase) == 0) {
  68.                         bChunked = true;
  69.                     }
  70.                 }
  71.                 else if (String.Compare(headerName, "Connection", StringComparison.OrdinalIgnoreCase) == 0) {
  72.                     if (String.Compare(headerValue, "Keep-Alive", StringComparison.OrdinalIgnoreCase) == 0)
  73.                         bKeepAlive = true;
  74.                     else if (String.Compare(headerValue, "Close", StringComparison.OrdinalIgnoreCase) == 0)
  75.                         bKeepAlive = false;
  76.                 }
  77.                 else if (String.Compare(headerName, "Expect", StringComparison.OrdinalIgnoreCase) == 0) {
  78.                     if (String.Compare(headerValue, "100-continue", StringComparison.OrdinalIgnoreCase) == 0)
  79.                         bSendContinue = true;
  80.                 }
  81.                 else if (String.Compare(headerName, "Content-Length", StringComparison.OrdinalIgnoreCase) == 0) {
  82.                     contentLength = Int32.Parse(headerValue, CultureInfo.InvariantCulture);
  83.                 }
  84.                 else {
  85.                     headers[headerName] = headerValue;
  86.                 }
  87.             }
  88.         }
  89.         // ReadToEndOfHeaders
  90.        
  91.         protected void WriteHeaders(ITransportHeaders headers, Stream outputStream)
  92.         {
  93.             if (headers == null)
  94.                 return;
  95.            
  96.             foreach (DictionaryEntry header in headers) {
  97.                 string headerName = (string)header.Key;
  98.                
  99.                 // exclude special headers
  100.                 if (!headerName.StartsWith("__", StringComparison.Ordinal)) {
  101.                     WriteHeader(headerName, (string)header.Value, outputStream);
  102.                 }
  103.             }
  104.            
  105.             // write end of headers "\r\n"
  106.             outputStream.Write(s_endOfLine, 0, s_endOfLine.Length);
  107.         }
  108.         // WriteHeaders
  109.         private void WriteHeader(string name, string value, Stream outputStream)
  110.         {
  111.             byte[] nameBytes = Encoding.ASCII.GetBytes(name);
  112.             byte[] valueBytes = Encoding.ASCII.GetBytes(value);
  113.            
  114.             outputStream.Write(nameBytes, 0, nameBytes.Length);
  115.             outputStream.Write(s_headerSeparator, 0, s_headerSeparator.Length);
  116.             outputStream.Write(valueBytes, 0, valueBytes.Length);
  117.             outputStream.Write(s_endOfLine, 0, s_endOfLine.Length);
  118.         }
  119.         // WriteHeader
  120.         protected void WriteResponseFirstLine(string statusCode, string reasonPhrase, Stream outputStream)
  121.         {
  122.             byte[] statusCodeBytes = Encoding.ASCII.GetBytes(statusCode);
  123.             byte[] reasonPhraseBytes = Encoding.ASCII.GetBytes(reasonPhrase);
  124.            
  125.             outputStream.Write(s_httpVersionAndSpace, 0, s_httpVersionAndSpace.Length);
  126.             outputStream.Write(statusCodeBytes, 0, statusCodeBytes.Length);
  127.             outputStream.WriteByte((byte)' ');
  128.             outputStream.Write(reasonPhraseBytes, 0, reasonPhraseBytes.Length);
  129.             outputStream.Write(s_endOfLine, 0, s_endOfLine.Length);
  130.         }
  131.         // WriteResponseFirstLine
  132.        
  133.     }
  134.     // class HttpSocketHandler
  135.    
  136. }
  137. // namespace System.Runtime.Remoting.Channels.Tcp

Developer Fusion