예제 #1
0
        public void HandleRequest(System.Net.HttpListenerRequest req, ServerResponse res)
        {
            try
            {
                _log.Verbose("Handling request for {0} from {1}", req.Url, req.RemoteEndPoint.Address);
                TransformAndRun(req, res, (toTransform) => new Uri(_config.UpstreamUrl + toTransform.PathAndQuery),
                                      (requestResponse, serverResponse) =>
                                      {
                                          try
                                          {
                                              if (requestResponse.ContentLength > -1)
                                                  serverResponse.ContentLength64 = requestResponse.ContentLength;

                                              foreach (string item in requestResponse.Headers)
                                              {
                                                  try
                                                  {
                                                      if (!_excludedResponseHeaders.Contains(item))
                                                          serverResponse.Headers.Add(item, requestResponse.Headers[item]);
                                                  }
                                                  catch (Exception ex)
                                                  {
                                                      _log.Warning("Failed to copy Header {0} with Value {1} \r\n {2}", item, requestResponse.Headers[item], ex.Message);
                                                  }
                                              }

                                              if(requestResponse.ContentType.StartsWith("text/html", StringComparison.InvariantCultureIgnoreCase)
                                                  || requestResponse.ContentType.StartsWith("text/javascript", StringComparison.InvariantCultureIgnoreCase)
                                                  || requestResponse.ContentType.StartsWith("text/css", StringComparison.InvariantCultureIgnoreCase))
                                              {
                                                  var buffer = new MemoryStream();
                                                  var rewriter = new AsyncUrlRewriter(requestResponse.GetResponseStream(), buffer,
                                                        Encoding.UTF8, _config.UpstreamUrl, _config.OwnUrl);

                                                   rewriter.OnException += (sender, e) =>
                                                      {
                                                          e.Data.Cancel = true;
                                                          _log.Error(e.Data.Exception);

                                                          try
                                                          {
                                                              serverResponse.Close();
                                                              requestResponse.Close();
                                                          }
                                                          catch (Exception ex)
                                                          {
                                                              _log.Error(ex);
                                                          }
                                                      };

                                                  rewriter.Completed += (sender, e) =>
                                                      {
                                                          serverResponse.ContentLength64 = buffer.Length;
                                                          buffer.Position = 0;

                                                          var copier = new AsyncStreamCopier(buffer, serverResponse.OutputStream);
                                                          copier.Completed += (s, a) =>
                                                              {
                                                                  buffer.Close();
                                                                  serverResponse.Close();
                                                                  requestResponse.Close();
                                                              };
                                                          copier.Copy();
                                                      };
                                                  rewriter.Copy();
                                              }
                                              else
                                              {
                                                  var copier = new AsyncStreamCopier(requestResponse.GetResponseStream(), serverResponse.OutputStream);

                                                  copier.Completed += (sender, e) =>
                                                      {
                                                          serverResponse.Close();
                                                          requestResponse.Close();
                                                      };

                                                  copier.Copy();
                                              }
                                          }
                                          catch (Exception ex)
                                          {
                                              _log.Error(ex);
                                              serverResponse.Do503(ex).Close();
                                              requestResponse.Close();
                                          }
                                      });
            }
            catch (Exception ex)
            {
                _log.Error(ex);
                res.Do503(ex).Close(); ;
            }
        }