require 'net/http' require 'uri' class WikiProxyController < ApplicationController def edit tag = params[:tag] uri = "http://localhost:4567/gollum/edit/#{ URI.encode_www_form_component(tag) }" begin res = fetch_with_redirect(uri) render html: res.body.html_safe, content_type: res.content_type rescue => e render plain: "Wiki システムとの通信に失敗しました:#{ e.message }", status: 502 end end private def fetch_with_redirect uri_str, limit = 5 raise 'Too many redirects' if limit == 0 uri = URI.parse(uri_str) res = Net::HTTP.get_response(uri) if res.is_a? Net::HTTPRedirection location = res['location'] fetch_with_redirect(location, limit - 1) else res end end end