【问题标题】:xampp/wamp based Apache server custom reply基于 xampp/wamp 的 Apache 服务器自定义回复
【发布时间】:2012-03-10 20:45:11
【问题描述】:

如何在基于 xampp/wamp 的 Apache 服务器上的自定义请求连接到特定端口时发送自定义响应?

我正在尝试回复 Flash 应用请求的 \0 以允许跨域 http GET 请求。

闪存策略请求默认发送到端口 843,我希望保持这种状态。

端口应该得到一个 \0(以空字符结尾,\0 仅供参考)并回复如下内容:

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "/xml/dtds/cross-domain-policy.dtd">
    <cross-domain-policy>
        <site-control permitted-cross-domain-policies="master-only"/>
        <allow-http-request-headers-from domain="*" headers="*" secure="true" />
      <allow-access-from domain="*" to-ports="*" />
    </cross-domain-policy>

据我所知,请求应该以纯文本形式返回,尽管可能还需要 Content-type。

我尝试过使用以下内容:http://socketpolicyserver.com/,虽然它侦听端口并接受连接,但它不会根据请求回复指定的 xml。

任何获得正确回复的方法/方式将不胜感激,

关于,

迈克。

!---更新--->

我编写了一个简单的 C# Web 服务器,它侦听端口 843,并服务于上述策略 - 但是,当使用 SecureSocket 连接进行安全连接时(即打开 HTTPS/SSL 协议的套接字) ) - 发送的请求使用主机证书加密。据我所知,没有办法通过外部应用程序监听或获取服务器证书并解密数据,因此,唯一的方法是以某种方式“教”Apache 在通过发送正确请求后以跨域策略响应合适的端口。

我的另一个想法是读取存储在 Apache 目录中的服务器证书文件,而不管服务器本身发生了什么,尽管在 imo 中这是一个矫枉过正。

很想听听你的cmets,

迈克。

【问题讨论】:

    标签: apache-flex apache xampp wamp cross-domain-policy


    【解决方案1】:

    这就是我最终解决它的方法:

    我使用这个家伙的代码做了一些修改:http://www.switchonthecode.com/tutorials/csharp-tutorial-simple-threaded-tcp-server

    并创建了一个简单的多线程 Web 服务器,它侦听端口 843,并根据适当的闪存请求提供一些通用的策略。

    Adobe 提供了几个示例,但由于某些原因,Windows 不喜欢这些示例。

    另外请注意,如果您使用的是 Flash 的 SecureSocket 对象,据称它应该使用目标服务器(IIS'/Apaches'/Tomcats' 等)SSL 凭据,并将使用目标服务器证书,再一次,它可能不是所以这个代码没有 SSL 支持,虽然我已经开始使用 C# 的 SSL Streams 实现一个,但到目前为止没有任何运气。如果您可以通过 SSL 使其工作,请告诉我。

    希望这段代码会有所帮助,

    迈克。

    using System;
    using System.Text;
    using System.Net.Sockets;
    using System.Threading;
    using System.Net;
    using System.IO;
    
    namespace TCPSexyServer
    {
        class Server
    {
        private TcpListener tcpListener;
        private Thread listenThread;
    
        private void ListenForClients(int p)
        {
            throw new NotImplementedException();
        }
    
        public Server()
        {
            this.tcpListener = new TcpListener(IPAddress.Any, 843);
            this.listenThread = new Thread(new ThreadStart(ListenForClients));
            this.listenThread.Start();
        }
    
        private void ListenForClients()
        {
            this.tcpListener.Start();
    
            while (true)
            {
                //blocks until a client has connected to the server
                TcpClient client = this.tcpListener.AcceptTcpClient();
    
                //create a thread to handle communication 
                //with connected client
                Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
                clientThread.Start(client);
            }
        }
    
        private void HandleClientComm(object client)
        {
            TcpClient tcpClient = (TcpClient)client;
            NetworkStream clientStream = tcpClient.GetStream();
            byte[] message = new byte[4096];
            int bytesRead;
    
            while (true)
            {
                bytesRead = 0;
    
                try
                {
                    //blocks until a client sends a message
                    bytesRead = clientStream.Read(message, 0, 4096);
                }
                catch
                {
                    //a socket error has occured
                    break;
                }
    
                if (bytesRead == 0)
                {
                    //the client has disconnected from the server
                    break;
                }
    
                //message has successfully been received
    
                UTF8Encoding encoder = new UTF8Encoding();
    
                string sentData = encoder.GetString(message, 0, bytesRead);
                Console.WriteLine(sentData);
                if (sentData == "<policy-file-request/>\0")
                {
                    String policy = "<?xml version=\"1.0\"?>\n" +
                                    "<!DOCTYPE cross-domain-policy SYSTEM \"/xml/dtds/cross-domain-policy.dtd\">\n" +
                                    "<cross-domain-policy>\n" +
                                    "<site-control permitted-cross-domain-policies=\"master-only\"/>\n" +
                                    "<allow-http-request-headers-from domain=\"*\" headers=\"*\" secure=\"true\" />\n" +
                                    "<allow-access-from domain=\"*\" to-ports=\"*\" />\n" +
                                    "</cross-domain-policy>\0";
                    byte[] buffer = encoder.GetBytes(policy);
                    clientStream.Write(buffer, 0, buffer.Length);
                    clientStream.Flush();
                    Console.WriteLine(policy);
                }
                else
                {
                    tcpClient.Close();
                }
                System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));
            }
    
            tcpClient.Close();
        }
    
            public static void Main(string[] args)
            {
                Server blah = new Server();
           }
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-12-13
      • 2018-02-01
      • 2015-05-24
      • 2012-09-04
      • 2011-01-20
      • 2014-03-19
      • 2016-06-22
      • 2012-06-06
      • 1970-01-01
      相关资源
      最近更新 更多