- 精华
- 0
- 帖子
- 1007
- 威望
- 0 点
- 积分
- 1475 点
- 种子
- 5 点
- 注册时间
- 2005-9-11
- 最后登录
- 2015-3-22
|
发表于 2009-9-22 22:51 · 上海
|
显示全部楼层
ps3 proxy server其实就是一个网关。通过建立了一个特殊的HttpClient类监听PS3发送过来的数据包。并且把地址复制出来。实时检查Replace项中有没有该地址对应的本地文件。如果有则将本地文件作为响应的数据包回送。说到底就是一个自动替换链接的网关。
- ///处理逻辑
- private void ProcessQuery(string Query)
- {
- this.HeaderFields = this.ParseQuery(Query);
- if ((this.HeaderFields == null) || !this.HeaderFields.ContainsKey("Host"))
- {
- this.SendBadRequest();
- }
- else
- {
- int num;
- string requestedPath;
- int index;
- if (this.HttpRequestType.ToUpper().Equals("CONNECT"))
- {
- index = this.RequestedPath.IndexOf(":");
- if (index >= 0)
- {
- requestedPath = this.RequestedPath.Substring(0, index);
- if (this.RequestedPath.Length > (index + 1))
- {
- num = int.Parse(this.RequestedPath.Substring(index + 1));
- }
- else
- {
- num = 0x1bb;
- }
- }
- else
- {
- requestedPath = this.RequestedPath;
- num = 0x1bb;
- }
- }
- else
- {
- index = this.HeaderFields["Host"].IndexOf(":");
- if (index > 0)
- {
- requestedPath = this.HeaderFields["Host"].Substring(0, index);
- num = int.Parse(this.HeaderFields["Host"].Substring(index + 1));
- }
- else
- {
- requestedPath = this.HeaderFields["Host"];
- num = 80;
- }
- if (this.HttpRequestType.ToUpper().Equals("POST"))
- {
- int num3 = Query.IndexOf("rnrn");
- this.m_HttpPost = Query.Substring(num3 + 4);
- }
- }
- //关键代码,使用LocalFile.MatchFile方法遍历Replace中的动态添加的自定义控件中是否存在与这个this.RequestedURL字符串变量相符的URL字符串,如果存在则在下面发送这个本地文件给请求者(PS3)。
- string localFile = LocalFile.MatchFile(this.RequestedURL);
- //是否存在HTTP 200的“CONNECT”字样。
- if (!this.HttpRequestType.ToUpper().Equals("CONNECT") && (localFile != string.Empty))
- {
- //发送本地文件,这个方法里会有一个调用
- this.SendLocalFile(localFile);
- this.updataLogMsg(this.RequestedURL, localFile);
- }
- else
- {
- this.updataLogMsg(this.RequestedURL, string.Empty);
- try
- {
- IPEndPoint remoteEP = new IPEndPoint(Dns.Resolve(requestedPath).AddressList[0], num);
- base.DestinationSocket = new Socket(remoteEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
- if (this.HeaderFields.ContainsKey("Proxy-Connection") && this.HeaderFields["Proxy-Connection"].ToLower().Equals("keep-alive"))
- {
- base.DestinationSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, 1);
- }
- base.DestinationSocket.BeginConnect(remoteEP, new AsyncCallback(this.OnConnected), base.DestinationSocket);
- }
- catch
- {
- this.SendBadRequest();
- }
- }
- }
- }
- private void SendLocalFile(string localFile)
- {
- this.m_LocalFile = new LocalFile(localFile);
- //告诉PS3,你的请求已经找到了,200状态的意思是可以连接。
- string format = "HTTP/1.1 200 OKrnDate:{0}rnConnection: closernContent-Length: {1}rnAccept-Ranges: bytesrnLast-Modified:{2}rnrn";
- format = string.Format(format, DateTime.Now, this.m_LocalFile.Filesize, this.m_LocalFile.LastModified);
- try
- {
- //开始通过ClientSocket发送本地的数据
- base.ClientSocket.BeginSend(Encoding.ASCII.GetBytes(format), 0, format.Length, SocketFlags.None, new AsyncCallback(this.OnLocalFileSent), base.ClientSocket);
- }
- catch
- {
- //如果传输上发生异常了,对不起,吧父类和自己都给销毁掉。哈哈
- base.Dispose();
- }
- }
复制代码 |
|