Permalink
Browse files

Pass along request and response headers

  • Loading branch information...
1 parent 8d029b6 commit 99dc0480e27c3a653730e1b186462d029273c037 @sstephenson committed Nov 21, 2009
Showing with 32 additions and 23 deletions.
  1. +32 −23 lib/rack_url_protocol.rb
View
@@ -26,37 +26,46 @@ def canonicalRequestForRequest(request)
end
def startLoading
- app = self.class.apps[request.URL.scheme]
- env = {
- "REQUEST_METHOD" => request.HTTPMethod,
- "SCRIPT_NAME" => "",
- "PATH_INFO" => request.URL.path,
- "QUERY_STRING" => request.URL.query,
- "SERVER_NAME" => "localhost",
- "SERVER_PORT" => "0",
- "rack.version" => [1, 0],
- "rack.url_scheme" => "http",
- "rack.input" => inputStreamFromRequest,
- "rack.errors" => $stderr,
- "rack.multithread" => false,
- "rack.multiprocess" => false,
- "rack.run_once" => false
- }
-
- status, headers, parts = app.call(env)
- data = dataFromRackResponseParts(parts)
-
- response = NSURLResponse.alloc.initWithURL(request.URL, MIMEType: "text/html", expectedContentLength: data.length, textEncodingName: nil)
+ status, headers, parts = rackApplication.call(rackEnvironmentFromRequest)
+ response = NSHTTPURLResponse.alloc.initWithURL(request.URL, statusCode: status, headerFields: headers, requestTime: 0)
+
client.URLProtocol(self, didReceiveResponse: response, cacheStoragePolicy: NSURLCacheStorageNotAllowed)
-
- client.URLProtocol(self, didLoadData: data)
+ client.URLProtocol(self, didLoadData: dataFromRackResponseParts(parts))
client.URLProtocolDidFinishLoading(self)
end
def stopLoading
end
+ def rackApplication
+ self.class.apps[request.URL.scheme]
+ end
+
private
+ def rackEnvironmentFromRequest
+ env = {
+ "REQUEST_METHOD" => request.HTTPMethod,
+ "SCRIPT_NAME" => "",
+ "PATH_INFO" => request.URL.path,
+ "QUERY_STRING" => request.URL.query,
+ "SERVER_NAME" => "localhost",
+ "SERVER_PORT" => "0",
+ "rack.version" => [1, 0],
+ "rack.url_scheme" => "http",
+ "rack.input" => inputStreamFromRequest,
+ "rack.errors" => $stderr,
+ "rack.multithread" => false,
+ "rack.multiprocess" => false,
+ "rack.run_once" => false
+ }
+
+ request.allHTTPHeaderFields.each do |name, value|
+ env["HTTP_" + name.upcase.gsub("-", "_")] = value
+ end
+
+ env
+ end
+
def inputStreamFromRequest
if body = request.HTTPBody
input = NSString.alloc.initWithData(body, encoding: requestEncoding)

0 comments on commit 99dc048

Please sign in to comment.