Class: Envjs::Net::CGI

Inherits:
Object
  • Object
show all
Defined in:
lib/envjs/net/cgi.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xhr, data) ⇒ CGI

Returns a new instance of CGI.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/envjs/net/cgi.rb', line 19

def initialize xhr, data
  if (match = %r((.*\.php)(\?(.*))?).match xhr.url).nil?
    raise "Not CGI"
  end

  @saved = {}

  begin

    # p match[0]

    path = match[1]
    path.sub! %r(^file://), ""

    save_and_set( "GATEWAY_INTERFACE", "CGI/1.1" )

    save_and_set( "REQUEST_METHOD", xhr["method"] )
    if ( match[3] )
      save_and_set( "QUERY_STRING", match[3] )
    end

    if ct = xhr["headers"]["Content-Type"]
      save_and_set( "CONTENT_TYPE", ct )
    end

    if data
      save_and_set( "CONTENT_LENGTH", data.length.to_s )
    end

    xhr["headers"].each do |k,v|
      k.gsub!("-","_")
      k = "HTTP_"  + k
      save_and_set( k, v )
    end

    result = nil

    save_and_set( "PATH_INFO",  path )
    save_and_set( "PATH_TRANSLATED",  path )
    save_and_set( "REDIRECT_STATUS", "200" )

    open("|php-cgi", "r+") do |php|
      if data
        php.write data
        php.flush
      end
      result = php.read
    end

    result && result = result.split("\r\n")
    @headers = {}
    while line = result.shift
      break if line == ""
      match = /([^:]*):\s*(.*)/.match line
      @headers[match[1]]=match[2]
    end
    # p "q", result.join("\r\n")
    @body = result.join("\r\n")
  ensure
    restore
  end

end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



92
93
94
# File 'lib/envjs/net/cgi.rb', line 92

def body
  @body
end

Instance Method Details

#eachObject



83
84
85
86
87
# File 'lib/envjs/net/cgi.rb', line 83

def each 
  @headers.each do |k,v|
    yield k, v
  end
end

#finishObject



89
90
# File 'lib/envjs/net/cgi.rb', line 89

def finish
end

#restoreObject



12
13
14
15
16
17
# File 'lib/envjs/net/cgi.rb', line 12

def restore
  @saved.keys.each do |k|
    ENV[k] = @saved[k]
  end
  @saved = {}
end

#save_and_set(k, v) ⇒ Object



7
8
9
10
# File 'lib/envjs/net/cgi.rb', line 7

def save_and_set k, v
  @saved[k] = ENV[k]
  ENV[k] = v
end