lib/bubble-wrap/http.rb in bubble-wrap-0.2.1 vs lib/bubble-wrap/http.rb in bubble-wrap-0.3.0

- old
+ new

@@ -116,10 +116,11 @@ headers = options.delete(:headers) if headers @headers = {} headers.each{|k,v| @headers[k] = v.gsub("\n", '\\n') } # escaping LFs end + @cachePolicy = options.delete(:cache_policy) || NSURLRequestUseProtocolCachePolicy @options = options @response = HTTP::Response.new initiate_request(url) connection.start UIApplication.sharedApplication.networkActivityIndicatorVisible = true @@ -150,14 +151,14 @@ @payload = params.join("&") end url_string = "#{url_string}?#{@payload}" if @method == "GET" end - p "BubbleWrap::HTTP building a NSRequest for #{url_string}"# if SETTINGS[:debug] - @url = NSURL.URLWithString(url_string) + p "BubbleWrap::HTTP building a NSRequest for #{url_string}" if SETTINGS[:debug] + @url = NSURL.URLWithString(url_string.stringByAddingPercentEscapesUsingEncoding NSUTF8StringEncoding) @request = NSMutableURLRequest.requestWithURL(@url, - cachePolicy:NSURLRequestUseProtocolCachePolicy, + cachePolicy:@cachePolicy, timeoutInterval:@timeout) @request.setHTTPMethod @method @request.setAllHTTPHeaderFields(@headers) if @headers # @payload needs to be converted to data @@ -166,78 +167,83 @@ @request.setHTTPBody @payload end # NSHTTPCookieStorage.sharedHTTPCookieStorage - @connection = NSURLConnection.connectionWithRequest(request, delegate:self) + @connection = create_connection(request, self) @request.instance_variable_set("@done_loading", false) def @request.done_loading; @done_loading; end def @request.done_loading!; @done_loading = true; end end def connection(connection, didReceiveResponse:response) @status_code = response.statusCode @response_headers = response.allHeaderFields @response_size = response.expectedContentLength.to_f - # p "HTTP status code: #{@status_code}, content length: #{@response_size}, headers: #{@response_headers}" if SETTINGS[:debug] end # This delegate method get called every time a chunk of data is being received def connection(connection, didReceiveData:received_data) @received_data ||= NSMutableData.new @received_data.appendData(received_data) end def connection(connection, willSendRequest:request, redirectResponse:redirect_response) - p "HTTP redirected #{request.description}" #if SETTINGS[:debug] + p "HTTP redirected #{request.description}" if SETTINGS[:debug] new_request = request.mutableCopy # new_request.setValue(@credentials.inspect, forHTTPHeaderField:'Authorization') # disabled while we figure this one out new_request.setAllHTTPHeaderFields(@headers) if @headers @connection.cancel - @connection = NSURLConnection.connectionWithRequest(new_request, delegate:self) + @connection = create_connection(new_request, self) new_request end def connection(connection, didFailWithError: error) UIApplication.sharedApplication.networkActivityIndicatorVisible = false @request.done_loading! - NSLog"HTTP Connection failed #{error.localizedDescription}" + NSLog"HTTP Connection failed #{error.localizedDescription}" if SETTINGS[:debug] @response.error_message = error.localizedDescription - if @delegator.respond_to?(:call) - @delegator.call( @response, self ) - end + call_delegator_with_response end + # The transfer is done and everything went well def connectionDidFinishLoading(connection) UIApplication.sharedApplication.networkActivityIndicatorVisible = false @request.done_loading! - # copy the data in a local var that we will attach to the response object response_body = NSData.dataWithData(@received_data) if @received_data @response.update(status_code: status_code, body: response_body, headers: response_headers, url: @url) - # Don't reset the received data since this method can be called multiple times if the headers can report the wrong length. - # @received_data = nil - if @delegator.respond_to?(:call) - @delegator.call( @response, self ) - end + + call_delegator_with_response end def connection(connection, didReceiveAuthenticationChallenge:challenge) - # p "HTTP auth required" if SETTINGS[:debug] + if (challenge.previousFailureCount == 0) # by default we are keeping the credential for the entire session # Eventually, it would be good to let the user pick one of the 3 possible credential persistence options: # NSURLCredentialPersistenceNone, # NSURLCredentialPersistenceForSession, # NSURLCredentialPersistencePermanent - p "auth challenged, answered with credentials: #{credentials.inspect}" + p "auth challenged, answered with credentials: #{credentials.inspect}" if SETTINGS[:debug] new_credential = NSURLCredential.credentialWithUser(credentials[:username], password:credentials[:password], persistence:NSURLCredentialPersistenceForSession) challenge.sender.useCredential(new_credential, forAuthenticationChallenge:challenge) else challenge.sender.cancelAuthenticationChallenge(challenge) p 'Auth Failed :(' end + end + + def call_delegator_with_response + if @delegator.respond_to?(:call) + @delegator.call( @response, self ) + end + end + + # This is a temporary method used for mocking. + def create_connection(request, delegate) + NSURLConnection.connectionWithRequest(request, delegate:delegate) end end end end