Class: Hayabusa::Cgi_tools

Inherits:
Object
  • Object
show all
Defined in:
lib/hayabusa_cgi_tools.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cgiObject

Returns the value of attribute cgi.



4
5
6
# File 'lib/hayabusa_cgi_tools.rb', line 4

def cgi
  @cgi
end

Instance Method Details

#content_typeObject



57
58
59
# File 'lib/hayabusa_cgi_tools.rb', line 57

def content_type
  return ENV["CONTENT_TYPE"]
end

#convert_fcgi_post(params, args = nil) ⇒ Object

Converts CGI-like-post hashes to the normal Hayabusa-type hash.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/hayabusa_cgi_tools.rb', line 7

def convert_fcgi_post(params, args = nil)
  post_hash = {}
  
  params.each do |key, realval|
    val = realval.first
    classn = val.class.name
    
    #Sometimes uploaded files are given as StringIO's.
    if classn == "StringIO" or classn == "Tempfile"
      val = Hayabusa::Http_session::Post_multipart::File_upload.new(
        :fname => val.original_filename,
        :data => val
      )
    end
    
    post_hash[key] = val
  end
  
  post_ret = {}
  post_hash.each do |varname, value|
    Knj::Web.parse_name(post_ret, varname, value)
  end
  
  return post_ret
end

#convert_fcgi_post_fileuploads_to_http2(hash) ⇒ Object

Converts the post-hash to valid hash for Http2 regarding file uploads (when proxied to host-process via Http2-framework).



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/hayabusa_cgi_tools.rb', line 34

def convert_fcgi_post_fileuploads_to_http2(hash)
  hash.each do |key, val|
    if val.is_a?(Hayabusa::Http_session::Post_multipart::File_upload)
      hash[key] = {
        :filename => val.filename,
        :content => val.to_s
      }
    elsif val.is_a?(Hash) or val.is_a?(Array)
      hash[key] = self.convert_fcgi_post_fileuploads_to_http2(val)
    end
  end
  
  return hash
end

#env_tableObject



49
50
51
# File 'lib/hayabusa_cgi_tools.rb', line 49

def env_table
  return ENV
end

#on_content(line) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/hayabusa_cgi_tools.rb', line 138

def on_content(line)
  # STDERR.puts "Line: '#{line}'."
  
  if @count <= 0
    # This is needed to trick FCGI into writing out correct status codes by ignoring the original status code and outputting it as a "Status"-header instead which is defined in the response-file.
  else
    # Write the given line to the content.
    @cgi.out.print(line)
  end
  
  @count += 1
end

#paramsObject



61
62
63
# File 'lib/hayabusa_cgi_tools.rb', line 61

def params
  return self.cgi.params
end


65
66
67
# File 'lib/hayabusa_cgi_tools.rb', line 65

def print(arg)
  Kernel.print arg.to_s
end

#proxy_request_to(args) ⇒ Object

This method is used to proxy a request to another FCGI-process, since a single FCGI-process cant handle more requests simultanious.

Raises:

  • (Errno::ECONNABORTED)


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/hayabusa_cgi_tools.rb', line 70

def proxy_request_to(args)
  @cgi, @http, @fp_log = args[:cgi], args[:http], args[:fp_log]
  
  headers = {"Hayabusa_mode" => "proxy"}
  @cgi.env.each do |key, val|
    keyl = key.to_s.downcase
    
    if key[0, 5] == "HTTP_"
      key = key[5, key.length].gsub("_", " ")
      key = key.to_s.split(" ").select{|w| w.capitalize! or w }.join(" ")
      key = key.gsub(" ", "-")
      keyl = key.downcase
      
      if keyl != "connection" and keyl != "accept-encoding" and keyl != "hayabusa-fcgi-config" and key != "hayabusa-cgi-config"
        headers[key] = val
      end
    end
  end
  
  #Make request.
  uri = Knj::Web.parse_uri(@cgi.env["REQUEST_URI"])
  url = File.basename(uri[:path])
  url = url[1, url.length] if url[0] == "/"
  
  if @cgi.env["QUERY_STRING"].to_s.length > 0
    url << "?#{cgi.env["QUERY_STRING"]}"
  end
  
  @fp_log.puts("Proxying URL: '#{url}'.") if @fp_log
  
  #The HTTP-connection can have closed mean while, so we have to test it.
  raise Errno::ECONNABORTED unless @http.socket_working?
  
  # Count used to know what is the status line.
  @count = 0
  
  if @cgi.env["REQUEST_METHOD"] == "POST"
    # Use CGI to parse post.
    real_cgi = Hayabusa::Cgi.new(@cgi)
    params = real_cgi.params
    
    if cgi.env["CONTENT_TYPE"].to_s.downcase.include?("multipart/form-data")
      @http.post_multipart(
        :url => url,
        :post => self.convert_fcgi_post_fileuploads_to_http2(self.convert_fcgi_post(params, :http2_compatible => true)),
        :default_headers => headers,
        :cookies => false,
        :on_content => self.method(:on_content)
      )
    else
      @http.post(
        :url => url,
        :post => self.convert_fcgi_post(params, :http2_compatible => true),
        :default_headers => headers,
        :cookies => false,
        :on_content => self.method(:on_content)
      )
    end
  else
    @http.get(
      :url => url,
      :default_headers => headers,
      :cookies => false,
      :on_content => self.method(:on_content)
    )
  end
end

#request_methodObject



53
54
55
# File 'lib/hayabusa_cgi_tools.rb', line 53

def request_method
  return ENV["REQUEST_METHOD"]
end