如何在 Ruby 中编写更改 Web 代理服务器的内容? [英] How to write content altering web proxy server in Ruby?

查看:29
本文介绍了如何在 Ruby 中编写更改 Web 代理服务器的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以给出一个工作代码示例,说明如何在 Ruby 中编写内容更改 Web 代理服务器吗?(例如将所有小写文本重写为大写,或删除所有 img 标签).

Can someone give a working code example of how to write a content altering web proxy server in Ruby? (for example rewriting all lowercase text to uppercase, or removing all img tags).

我查看了 mouseholeem-proxy 之前,我无法让一个简单的例子工作.

I had a look at mousehole and em-proxy before, with both I was unable to get a simple example working.

推荐答案

理论上是这样的:http://www.ruby-doc.org/stdlib-2.0/libdoc/webrick/rdoc/WEBrick/HTTPProxyServer.html

require 'webrick'
require 'webrick/httpproxy'

handler = proc do |req, res|
  if res['content-type'] == 'text/plain'
    res.body << "\nThis content was proxied!\n"
  end
end

proxy = WEBrick::HTTPProxyServer.new Port: 8000, ProxyContentHandler: handler


trap 'INT'  do proxy.shutdown end
trap 'TERM' do proxy.shutdown end
proxy.start

但由于某种原因,我无法更改内容.也许它对你很有效.

But for some reason I can't get it to alter the content. Maybe it works for you tough.

这篇关于如何在 Ruby 中编写更改 Web 代理服务器的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆