I am needing to add HTTP headers to any URL request from a WebView.

I've got it working for direct URLs, but redirect URLs are being missed.

I've tried spawning a NSURLConnection and catching the redirected URL there, and that works, but I can't seem to stop the original WebView request, so I get multiple URL requests.

Here's the code I've tried:

-(NSURLRequest *)webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse: (NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)dataSource
{
    // Update the status message
   @try
   {
       if (redirectResponse)
       {
           NSMutableURLRequest *mrequest = [request mutableCopy];
myConnection = [[NSURLConnection alloc] initWithRequest:mrequest delegate:self];
    // attempt to cause this original request to be cancelled
//[[webView mainFrame] stopLoading]; // when uncommented, this results in a crash return mrequest; } else
       {
           NSMutableURLRequest *mrequest = [request mutableCopy];

           if (mrequest)
           {
               [mrequest retain];
[self AppendHeaders:mrequest]; return mrequest;
           }
       }
       return request;
   }
   @catch (id exception)
   {
       return request;
   }
   return request;
}

- (NSURLRequest *)connection:(NSURLConnection *)connection
             willSendRequest:(NSURLRequest *)redirectRequest
            redirectResponse:(NSURLResponse *)redirectResponse
{
if (redirectRequest && redirectResponse) {
       NSMutableURLRequest *mrequest = [redirectRequest mutableCopy];
if (mrequest)
       {
           [mrequest retain];
[self AppendHeaders:mrequest]; return mrequest;
       }
   }
   return redirectRequest;
}

Using Charles, I can see the redirected URL being loaded. It loads 3 times with the code above, the first time with the correct headers. I need it to just load once. Without the NSURLConnection initWithRequest it loads once, but without the headers.

Any suggestions would be greatly appreciated.

Bill Patterson
_______________________________________________
webkit-dev mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev

Reply via email to