2011-02-11 51 views
7

我試圖使用Ruby Sinatra和創建特定的網頁一個簡單的代理單頁的代理。我能做到這一點在C#中,我似乎無法去解決它的末日,在C#代碼如下:創建使用Ruby西納特拉

<%@ WebHandler Language="C#" Class="Map" %> 

using System; 
using System.Web; 
using System.Net; 
using System.IO; 

public class Map : IHttpHandler { 

static void CopyStream(Stream input, Stream output) 
{ 
    byte[] buffer = new byte[0x1000]; 
    int read; 
    while ((read = input.Read(buffer, 0, buffer.Length)) > 0) 
     output.Write(buffer, 0, read); 
} 

public void ProcessRequest(HttpContext context) 
{ 
    string gmapUri = string.Format("http://maps.google.com/maps/api/staticmap{0}", context.Request.Url.Query); 
    WebRequest request = WebRequest.Create(gmapUri); 

    using (WebResponse response = request.GetResponse()) 
    { 
     context.Response.ContentType = response.ContentType; 
     Stream responseStream = response.GetResponseStream(); 

     CopyStream(responseStream, context.Response.OutputStream); 
    } 
} 

public bool IsReusable { 
    get { 
     return false; 
    } 
} 

} 

紅寶石西納特拉的代碼,我已經試過如下:

require 'rubygems' 
require 'sinatra' 

get '/mapsproxy/staticmap' do 
    request.path_info = 'http://maps.google.com/maps/api/staticmap' 
    pass 
end 

我假設西納特拉一個不工作(獲得404)如只將請求傳遞給頁面在同一個域中。任何hep將不勝感激。

編輯:

隨着鐵皮人的幫助下,我想出了一個很好的解決方案簡潔,這對我來說工作得很好:

get '/proxy/path' do 
    URI.parse(<URI> + request.query_string.gsub("|", "%7C")).read 
end 

感謝所有幫助。

回答

4

如果你希望你的末日應用檢索URL,你需要火了某種形式的HTTP客戶端:

get '/mapsproxy/staticmap' do 
    require 'open-uri' 
    open('http://maps.google.com/maps/api/staticmap').read 
end 

我認爲這會工作,大約是最小的,你可以得到。

,如果你需要更多的tweakability你可以使用HTTPClient

而且,我認爲,機架可以做到這一點。 Sinatra建立在Rack之上,但是我在這個級別上玩了一段時間。

我還需要找到一種方法,從響應中提取的contentType

Open-URI文檔:

The opened file has several methods for meta information as follows since 
it is extended by OpenURI::Meta. 

open("http://www.ruby-lang.org/en") {|f| 
    f.each_line {|line| p line} 
    p f.base_uri   # <URI::HTTP:0x40e6ef2 URL:http://www.ruby-lang.org/en/> 
    p f.content_type  # "text/html" 
    p f.charset   # "iso-8859-1" 
    p f.content_encoding # [] 
    p f.last_modified # Thu Dec 05 02:45:02 UTC 2002 
} 

你的目的是這樣應該工作:

content_type = '' 
body = open("http://www.ruby-lang.org/en") {|f| 
    content_type = f.content_type  # "text/html" 
    f.read 
} 

我沒有測試過,但我想換貨政...塊的值將被分配到body。如果不行,那就試試吧:

content_type = '' 
body = '' 
open("http://www.ruby-lang.org/en") {|f| 
    content_type = f.content_type  # "text/html" 
    body = f.read 
} 

但是我覺得第一個會工作。

+0

乾杯。這看起來很有希望......我現在只是在嘗試一些東西...... – 2011-02-11 15:24:04

+0

您需要添加一些異常處理,並且可能希望使用`Timeout`模塊來提供更好的控制,以便在請求也被採用時長。請參閱http://stackoverflow.com/questions/4964044/using-open-uri-to-fetch-xml-and-the-best-practice-in-case-of-problems-with-a-remo/4966240#4966240舉些例子。 – 2011-02-11 15:30:07

1

隨着幫助鐵皮人TK-421我已經研究出瞭解決辦法,請參見下面的西納特拉路線:

get '/proxy/path' do 
    require 'open-uri' 
    uri = URI.parse(<URI>) 
    getresult = uri.read 
    halt 200, {'Content-Type' => getresult.content_type}, getresult 
end 

只是你需要的頁面替換<URI>,你很好走。

再過了些玩,這是我想出來的:

get '/proxy/path' do 
    URI.parse(<URI> + request.query_string.gsub("|", "%7C")).read 
end 

至於其他提到的,你需要require 'open-uri'在代碼的頂部。 gsub的原因是由於某些原因,解析失敗,如果他們留在,我的瀏覽器不會自動編碼它們。