Class: Tilia::Http::Sapi

Inherits:
Object
  • Object
show all
Defined in:
lib/tilia/http/sapi.rb

Overview

PHP SAPI

This object is responsible for:

  1. Constructing a Request object based on the current HTTP request sent to the PHP process.

  2. Sending the Response object back to the client.

It could be said that this class provides a mapping between the Request and Response objects, and php’s:

  • $_SERVER

  • $_POST

  • $_FILES

  • php://input

  • echo

  • header

  • php://output

You can choose to either call all these methods statically, but you can also instantiate this as an object to allow for polymorhpism.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Sapi

TODO: document



146
147
148
149
# File 'lib/tilia/http/sapi.rb', line 146

def initialize(env)
  @env = env
  @rack_request = Rack::Request.new(env)
end

Class Method Details

.create_from_server_array(server_array) ⇒ Object

This static method will create a new Request object, based on a PHP $_SERVER array.

Parameters:

  • array

    server_array

Returns:

  • Request



67
68
69
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
137
138
139
140
141
142
143
# File 'lib/tilia/http/sapi.rb', line 67

def self.create_from_server_array(server_array)
  headers = {}
  method = nil
  url = ''
  http_version = '1.1'

  protocol = 'http'
  host_name = 'localhost'

  server_array.each do |key, value|
    case key
    when 'SERVER_PROTOCOL'
      http_version = '1.0' if value == 'HTTP/1.0'
    when 'REQUEST_METHOD'
      method = value
    when 'PATH_INFO'
      url += value
    when 'SCRIPT_NAME'
      url = value + url

    # These sometimes should up without a HTTP_ prefix
    when 'CONTENT_TYPE'
      headers['Content-Type'] = value
    when 'CONTENT_LENGTH'
      headers['Content-Length'] = value

    # mod_php on apache will put credentials in these variables.
    # (fast)cgi does not usually do this, however.
    when 'PHP_AUTH_USER'
      if server_array.key? 'PHP_AUTH_PW'
        headers['Authorization'] = "Basic #{Base64.strict_encode64 "#{value}:#{server_array['PHP_AUTH_PW']}"}"
      end
    when 'PHP_AUTH_DIGEST'
      headers['Authorization'] = "Digest #{value}"

    # Apache may prefix the HTTP_AUTHORIZATION header with
    # REDIRECT_, if mod_rewrite was used.
    when 'REDIRECT_HTTP_AUTHORIZATION'
      headers['Authorization'] = value

    when 'HTTP_HOST'
      host_name = value
      headers['Host'] = value

    when 'HTTPS'
      protocol = 'https' if value && value != 'off'

    # RUBY
    when 'rack.url_scheme'
      protocol = value

    else
      if key.index('HTTP_') == 0
        # It's a HTTP header

        # Normalizing it to be prettier
        header = key[5..-1].downcase

        # Transforming dashes into spaces, and uppercasing
        # every first letter.
        # Turning spaces into dashes.
        header = header.split(/_/).map(&:capitalize).join('-')

        headers[header] = value
      end
    end
  end

  # RUBY: fake php ...
  url = "#{url}?#{server_array['QUERY_STRING']}" if url && server_array['QUERY_STRING'].present?

  r = Tilia::Http::Request.new(method, url, headers)
  r.http_version = http_version
  r.raw_server_data = server_array
  r.absolute_url = "#{protocol}://#{host_name}#{url}"
  r
end

.requestObject

This static method will create a new Request object, based on the current PHP request.

Returns:

  • Request



30
31
32
# File 'lib/tilia/http/sapi.rb', line 30

def self.request
  fail NotImplementedError, 'This object method now is an instance method'
end

.send_response(response) ⇒ void

This method returns an undefined value.

Sends the HTTP response back to a HTTP client.

This calls php’s header function and streams the body to php://output.

Parameters:

  • ResponseInterface

    response



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/tilia/http/sapi.rb', line 40

def self.send_response(response)
  # RUBY: Rack does not support HTTP Version (?)
  # header("HTTP/#{response.http_version} #{response.status} #{response.status_text}")

  status = response.status
  headers = {}
  response.headers.each do |key, value|
    headers[key] = value.join("\n")
  end

  body = response.body_as_stream
  content_length = response.header('Content-Length')
  if content_length
    output = StringIO.new
    output.write body.read(content_length.to_i)
    output.rewind
    body = output
  end

  [status, headers, body]
end

Instance Method Details

#create_from_server_array(server_array) ⇒ Object

TODO: document



165
166
167
# File 'lib/tilia/http/sapi.rb', line 165

def create_from_server_array(server_array)
  self.class.create_from_server_array(server_array)
end

#requestObject

TODO: document



152
153
154
155
156
157
# File 'lib/tilia/http/sapi.rb', line 152

def request
  r = create_from_server_array(@env)
  r.body = StringIO.new
  r.post_data = @rack_request.POST
  r
end

#send_response(response) ⇒ Object

TODO: document



160
161
162
# File 'lib/tilia/http/sapi.rb', line 160

def send_response(response)
  self.class.send_response(response)
end