HttpSocketManager.cs

  1. // ==++==
  2. // 
  3. //   
  4. //    Copyright (c) 2002 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.     // A client socket manager instance should encapsulate the socket
  34.     //   for the purpose of reading a response
  35.     internal abstract class HttpSocketHandler : SocketHandler
  36.     {            
  37.         private static byte[] s_httpVersion = Encoding.ASCII.GetBytes("HTTP/1.1");
  38.         private static byte[] s_httpVersionAndSpace = Encoding.ASCII.GetBytes("HTTP/1.1 ");
  39.         private static byte[] s_headerSeparator = new byte[]{(byte)':',(byte)' '};
  40.         private static byte[] s_endOfLine = new byte[]{(byte)'/r',(byte)'/n'};
  41.     
  42.         public HttpSocketHandler(Socket socket, RequestQueue requestQueue) : base(socket, requestQueue)
  43.         {          
  44.         } // HttpSocketHandler
  45.         protected void ReadToEndOfHeaders(BaseTransportHeaders headers, 
  46.                                           out bool bChunked,
  47.                                           out int contentLength,
  48.                                           ref bool bKeepAlive,
  49.                                           ref bool bSendContinue)
  50.         {
  51.             bChunked = false;
  52.             contentLength = 0;
  53.         
  54.             // read and process headers
  55.             for (;;)
  56.             {
  57.                 String header = ReadToEndOfLine();
  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); // skip semi-colon and space
  65.                 if (String.Compare(headerName, "Transfer-Encoding"true, CultureInfo.InvariantCulture) == 0)          
  66.                 {
  67.                     if (String.Compare(headerValue, "chunked"true, CultureInfo.InvariantCulture) == 0)
  68.                     {
  69.                         bChunked = true;
  70.                     }
  71.                 }
  72.                 else
  73.                 if (String.Compare(headerName, "Connection"true, CultureInfo.InvariantCulture) == 0)
  74.                 {
  75.                     if (String.Compare(headerValue, "Keep-Alive"true, CultureInfo.InvariantCulture) == 0)
  76.                         bKeepAlive = true;
  77.                     else
  78.                     if (String.Compare(headerValue, "Close"true, CultureInfo.InvariantCulture) == 0)
  79.                         bKeepAlive = false;
  80.                 }
  81.                 else
  82.                 if (String.Compare(headerName, "Expect"true, CultureInfo.InvariantCulture) == 0)
  83.                 {
  84.                     if (String.Compare(headerValue, "100-continue"true, CultureInfo.InvariantCulture) == 0)
  85.                         bSendContinue = true;
  86.                 }
  87.                 else
  88.                 if (String.Compare(headerName, "Content-Length"true, CultureInfo.InvariantCulture) == 0)
  89.                 {
  90.                     contentLength = Int32.Parse(headerValue);
  91.                 }
  92.                 else
  93.                 {                
  94.                     headers[headerName] = headerValue;
  95.                 }
  96.             }
  97.         } // ReadToEndOfHeaders
  98.         protected void WriteHeaders(ITransportHeaders headers, Stream outputStream)
  99.         {
  100.             if (headers == null)
  101.                 return;
  102.         
  103.             foreach (DictionaryEntry header in headers)
  104.             {
  105.                 String headerName = (String)header.Key;
  106.                 if (!headerName.StartsWith("__")) // exclude special headers
  107.                 {
  108.                     WriteHeader(headerName, (String)header.Value, outputStream);
  109.                 }
  110.             }
  111.             // write end of headers "/r/n"
  112.             outputStream.Write(s_endOfLine, 0, s_endOfLine.Length);
  113.         } // WriteHeaders
  114.         private void WriteHeader(String name, String value, Stream outputStream)
  115.         {
  116.             byte[] nameBytes = Encoding.ASCII.GetBytes(name); 
  117.             byte[] valueBytes = Encoding.ASCII.GetBytes(value); 
  118.             
  119.             outputStream.Write(nameBytes, 0, nameBytes.Length);
  120.             outputStream.Write(s_headerSeparator, 0, s_headerSeparator.Length);
  121.             outputStream.Write(valueBytes, 0, valueBytes.Length);
  122.             outputStream.Write(s_endOfLine, 0, s_endOfLine.Length);            
  123.         } // WriteHeader
  124.         protected void WriteRequestFirstLine(String requestVerb, String url, Stream outputStream)
  125.         {
  126.             byte[] requestVerbBytes = Encoding.ASCII.GetBytes(requestVerb); 
  127.             byte[] urlBytes = Encoding.ASCII.GetBytes(HttpEncodingHelper.EncodeUriAsXLinkHref(url));
  128.             outputStream.Write(requestVerbBytes, 0, requestVerbBytes.Length);
  129.             outputStream.WriteByte((byte)' ');
  130.             outputStream.Write(urlBytes, 0, urlBytes.Length);
  131.             outputStream.WriteByte((byte)' ');
  132.             outputStream.Write(s_httpVersion, 0, s_httpVersion.Length);
  133.             outputStream.Write(s_endOfLine, 0, s_endOfLine.Length);
  134.         } // WriteRequestFirstLine
  135.         protected void ReadResponseFirstLine(out String version, out String statusCode, out String reasonPhrase)
  136.         {
  137.             version = ReadToChar(' ');
  138.             statusCode = ReadToChar(' ');
  139.             reasonPhrase = ReadToEndOfLine();
  140.         } // ReadResponseFirstLine
  141.         protected void WriteResponseFirstLine(String statusCode, String reasonPhrase, Stream outputStream)
  142.         {
  143.             byte[] statusCodeBytes = Encoding.ASCII.GetBytes(statusCode); 
  144.             byte[] reasonPhraseBytes = Encoding.ASCII.GetBytes(reasonPhrase); 
  145.         
  146.             outputStream.Write(s_httpVersionAndSpace, 0, s_httpVersionAndSpace.Length);
  147.             outputStream.Write(statusCodeBytes, 0, statusCodeBytes.Length);
  148.             outputStream.WriteByte((byte)' ');
  149.             outputStream.Write(reasonPhraseBytes, 0, reasonPhraseBytes.Length);
  150.             outputStream.Write(s_endOfLine, 0, s_endOfLine.Length);
  151.         } // WriteResponseFirstLine        
  152.     } // class HttpSocketHandler
  153. // namespace System.Runtime.Remoting.Channels.Tcp
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值