void ReceivedTcpClient(object[] arg)
 {
     var tc = arg[0] as TcpClient;
     var srv = arg[1] as HttpServiceBase;
     try
     {
         tc.ReceiveTimeout = srv.Timeout > -1 ? srv.Timeout : DefaultTimeout;
         var keepAlive = true;
         var netStrm = tc.GetStream();
         using (var strm = new System.IO.BufferedStream(netStrm))
         {
             do
             {
                 var context = new HttpContext(strm);
                 tc.ReceiveTimeout = srv.KeepAliveTimeout > -1 ? srv.KeepAliveTimeout : DefaultKeepAliveTimeout;
                 keepAlive = context.Request.KeepAlive;
                 try
                 {
                     srv.OnCalledService(context);
                 }
                 finally
                 {
                     context.Close();
                 }
             }
             while (keepAlive);
         }
     }
     //タイムアウト用
     catch (IOException e) { }
     //Connection: close用
     catch (InvalidDataException e) { }
     finally
     {
         tc.Close();
     }
 }
 public ReceivedContextEventArgs(HttpContext context)
 {
     Context = context;
 }
        HttpWebRequest SetHeader(HttpContext context)
        {
            var req = WebRequest.Create(context.Request.Url) as HttpWebRequest;
            req.Method = context.Request.Method;
            req.Timeout = 15000;
            SetMessageHeader(context.Request.Headers, req);
            if (context.Request.ContentLength <= 0)
                return req;

            var inputStream = context.Request.GetEntityStream();
            var requestStream = req.GetRequestStream();

            int cnt;
            var buffer = new byte[1024];
            for (var len = context.Request.ContentLength; len > 0; len -= cnt)
            {
                cnt = Math.Min(buffer.Length, (int)len);
                cnt = inputStream.Read(buffer, 0, cnt);
                requestStream.Write(buffer, 0, cnt);
            }
            return req;
        }
 HttpWebResponse GetHeader(HttpContext context, HttpWebRequest request)
 {
     var response = request.GetResponse() as HttpWebResponse;
     context.Response.ProtocolVersion = response.ProtocolVersion;
     context.Response.StatusCode = response.StatusCode;
     foreach (var str in response.Headers.AllKeys)
     {
         context.Response.Headers[str] = response.Headers[str];
     }
     return response;
 }
 public HttpRequestWriter(HttpContext context)
 {
     Request = SetHeader(context);
     Response = GetHeader(context, Request);
 }
 public abstract void OnCalledService(HttpContext context);
        public override void OnCalledService(HttpContext context)
        {
            HttpWebResponse response = null;
            try
            {
                if (context.Request.Method == "CONNECT")
                    throw new IOException();

                var writer = new HttpRequestWriter(context);
                using (var inStrm = writer.Response.GetResponseStream())
                using (var outStrm = context.Response.GetOutputStream())
                {
                    var buffer = new byte[5120];
                    var count = 0;
                    while ((count = inStrm.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        outStrm.Write(buffer, 0, count);
                    }
                }
            }
            catch (WebException exception)
            {
                var res = (HttpWebResponse)exception.Response;
                if (res != null)
                {
                    context.Response.ProtocolVersion = new Version("1.1");
                    context.Response.StatusCode = res.StatusCode;
                    foreach (var str in res.Headers.AllKeys)
                    {
                        context.Response.Headers[str] = exception.Response.Headers[str];
                    }
                    using (var outStrm = context.Response.GetOutputStream())
                    {
                        using (var inStrm = res.GetResponseStream())
                        {
                            byte[] buffer2 = new byte[1024];
                            int num2 = 0;
                            while ((num2 = inStrm.Read(buffer2, 0, buffer2.Length)) > 0)
                            {
                                outStrm.Write(buffer2, 0, num2);
                            }
                        }
                        return;
                    }
                }
                context.Response.StatusCode = HttpStatusCode.BadGateway;
            }
            catch (IOException e) { }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
            }
        }
 public override void OnCalledService(HttpContext context)
 {
     HttpWebResponse response = null;
     try
     {
         if (context.Request.Method == "CONNECT")
             throw new IOException();
         var args = new ReceivedContextEventArgs(context);
         OnReceivedContext(args);
     }
     finally
     {
         if (response != null)
         {
             response.Close();
         }
     }
 }