How can I use Ruby Webrick to do html content modification as it passes through a proxy server?
require 'webrick'
require 'webrick/httpproxy'
handler = proc do |req, res|
# if the_site_url == "youtube.com"
# html_of_the_page = "<body>Custom Html<body>"
# end
end
proxy = WEBrick::HTTPProxyServer.new(
Port: 8080,
ProxyContentHandler: handler
)
trap 'INT' do proxy.shutdown end
server.start
This question is similar but its solution does not work. If Webrick does not support content-altering functionality, is there another proxy server library that does?
Update
Ideally, I should be able to modify existing HTML. I would think there is some other variable like res.body
in a proxy handler that represents the html of the page: writable, parsable, readable (whether that is a stream of data or the full data).