Changeset 188517 in webkit


Ignore:
Timestamp:
Aug 15, 2015 8:29:01 PM (9 years ago)
Author:
aestes@apple.com
Message:

[Cocoa] Add redirect support to CustomProtocolManager
https://bugs.webkit.org/show_bug.cgi?id=147871

Reviewed by Dan Bernstein.
Source/WebKit2:

NSURLProtocols have the ability to generate redirect responses. This change teaches CustomProtocolManager how to handle them.

  • Shared/Network/CustomProtocols/Cocoa/CustomProtocolManagerCocoa.mm:

(WebKit::CustomProtocolManager::wasRedirectedToRequest): Called -URLProtocol:wasRedirectedToRequest:redirectResponse: on the NSURLProtocolClient.

  • Shared/Network/CustomProtocols/CustomProtocolManager.h:
  • Shared/Network/CustomProtocols/CustomProtocolManager.messages.in: Defined WasRedirectedToRequest.
  • Shared/Network/CustomProtocols/soup/CustomProtocolManagerSoup.cpp:

(WebKit::CustomProtocolManager::wasRedirectedToRequest): Defined empty function.

  • UIProcess/Network/CustomProtocols/mac/CustomProtocolManagerProxyMac.mm:

(-[WKCustomProtocolLoader connection:willSendRequest:redirectResponse:]): If a redirect response is received, send WasRedirectedToRequest and return nil to ignore the redirect.

Tools:

Updated WebKit2CustomProtocolsTest.MainResource to generate a redirect response.

  • TestWebKitAPI/Tests/CustomProtocolsSyncXHRTest.mm:

(TestWebKitAPI::TEST): Unregesitered TestProtocol.

  • TestWebKitAPI/Tests/WebKit2/custom-protocol-sync-xhr.html: Changed scheme to http.
  • TestWebKitAPI/Tests/WebKit2ObjC/CustomProtocolsTest.mm:

(-[CustomProtocolsLoadDelegate browsingContextControllerDidStartProvisionalLoad:]): Expected a certain provisional URL.
(-[CustomProtocolsLoadDelegate browsingContextControllerDidReceiveServerRedirectForProvisionalLoad:]): Ditto.
(-[CustomProtocolsLoadDelegate browsingContextControllerDidCommitLoad:]): Expected a certain committed URL.
(-[CustomProtocolsLoadDelegate browsingContextControllerDidFinishLoad:]): Expected isLoading to be false.
(TestWebKitAPI::TEST): Used the new load delegate and unregistered TestProtocol.

  • TestWebKitAPI/Tests/WebKit2ObjC/PreventImageLoadWithAutoResizing.mm:

(TestWebKitAPI::TEST): Unregistered TestProtocol.

  • TestWebKitAPI/mac/TestProtocol.mm: Changed scheme to http.

(+[TestProtocol canInitWithRequest:]): Changed to use property syntax.
(-[TestProtocol startLoading]): Taught to handle redirect responses.

Location:
trunk
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebKit2/ChangeLog

    r188486 r188517  
     12015-08-11  Andy Estes  <aestes@apple.com>
     2
     3        [Cocoa] Add redirect support to CustomProtocolManager
     4        https://bugs.webkit.org/show_bug.cgi?id=147871
     5
     6        Reviewed by Dan Bernstein.
     7       
     8        NSURLProtocols have the ability to generate redirect responses. This change teaches CustomProtocolManager how to handle them.
     9
     10        * Shared/Network/CustomProtocols/Cocoa/CustomProtocolManagerCocoa.mm:
     11        (WebKit::CustomProtocolManager::wasRedirectedToRequest): Called -URLProtocol:wasRedirectedToRequest:redirectResponse: on the NSURLProtocolClient.
     12        * Shared/Network/CustomProtocols/CustomProtocolManager.h:
     13        * Shared/Network/CustomProtocols/CustomProtocolManager.messages.in: Defined WasRedirectedToRequest.
     14        * Shared/Network/CustomProtocols/soup/CustomProtocolManagerSoup.cpp:
     15        (WebKit::CustomProtocolManager::wasRedirectedToRequest): Defined empty function.
     16        * UIProcess/Network/CustomProtocols/mac/CustomProtocolManagerProxyMac.mm:
     17        (-[WKCustomProtocolLoader connection:willSendRequest:redirectResponse:]): If a redirect response is received, send WasRedirectedToRequest and return nil to ignore the redirect.
     18
    1192015-08-13  Andy Estes  <aestes@apple.com>
    220
  • trunk/Source/WebKit2/Shared/Network/CustomProtocols/Cocoa/CustomProtocolManagerCocoa.mm

    r188002 r188517  
    260260}
    261261
     262void CustomProtocolManager::wasRedirectedToRequest(uint64_t customProtocolID, const WebCore::ResourceRequest& request, const WebCore::ResourceResponse& redirectResponse)
     263{
     264    RetainPtr<WKCustomProtocol> protocol = protocolForID(customProtocolID);
     265    if (!protocol)
     266        return;
     267
     268    RetainPtr<NSURLRequest> nsRequest = request.nsURLRequest(WebCore::DoNotUpdateHTTPBody);
     269    RetainPtr<NSURLResponse> nsRedirectResponse = redirectResponse.nsURLResponse();
     270
     271    dispatchOnResourceLoaderRunLoop([protocol, nsRequest, nsRedirectResponse]() {
     272        [[protocol client] URLProtocol:protocol.get() wasRedirectedToRequest:nsRequest.get() redirectResponse:nsRedirectResponse.get()];
     273    });
     274}
     275
    262276RetainPtr<WKCustomProtocol> CustomProtocolManager::protocolForID(uint64_t customProtocolID)
    263277{
  • trunk/Source/WebKit2/Shared/Network/CustomProtocols/CustomProtocolManager.h

    r188002 r188517  
    5050namespace WebCore {
    5151class ResourceError;
     52class ResourceRequest;
    5253class ResourceResponse;
    5354} // namespace WebCore
     
    9596    void didReceiveResponse(uint64_t customProtocolID, const WebCore::ResourceResponse&, uint32_t cacheStoragePolicy);
    9697    void didFinishLoading(uint64_t customProtocolID);
     98    void wasRedirectedToRequest(uint64_t customProtocolID, const WebCore::ResourceRequest&, const WebCore::ResourceResponse& redirectResponse);
    9799
    98100    ChildProcess* m_childProcess;
  • trunk/Source/WebKit2/Shared/Network/CustomProtocols/CustomProtocolManager.messages.in

    r174803 r188517  
    2626    DidReceiveResponse(uint64_t customProtocolID, WebCore::ResourceResponse response, uint32_t cacheStoragePolicy)
    2727    DidFinishLoading(uint64_t customProtocolID)
     28    WasRedirectedToRequest(uint64_t customProtocolID, WebCore::ResourceRequest request, WebCore::ResourceResponse redirectResponse);
    2829
    2930    RegisterScheme(String name)
  • trunk/Source/WebKit2/Shared/Network/CustomProtocols/soup/CustomProtocolManagerSoup.cpp

    r174803 r188517  
    107107}
    108108
     109void CustomProtocolManager::wasRedirectedToRequest(uint64_t, const WebCore::ResourceRequest&, const WebCore::ResourceResponse&)
     110{
     111    notImplemented();
     112}
     113
    109114} // namespace WebKit
  • trunk/Source/WebKit2/UIProcess/Network/CustomProtocols/mac/CustomProtocolManagerProxyMac.mm

    r186464 r188517  
    111111- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse
    112112{
     113    if (redirectResponse) {
     114        _connection->send(Messages::CustomProtocolManager::WasRedirectedToRequest(_customProtocolID, request, redirectResponse), 0);
     115        return nil;
     116    }
    113117    return request;
    114118}
  • trunk/Tools/ChangeLog

    r188486 r188517  
     12015-08-11  Andy Estes  <aestes@apple.com>
     2
     3        [Cocoa] Add redirect support to CustomProtocolManager
     4        https://bugs.webkit.org/show_bug.cgi?id=147871
     5
     6        Reviewed by Dan Bernstein.
     7
     8        Updated WebKit2CustomProtocolsTest.MainResource to generate a redirect response.
     9
     10        * TestWebKitAPI/Tests/CustomProtocolsSyncXHRTest.mm:
     11        (TestWebKitAPI::TEST): Unregesitered TestProtocol.
     12        * TestWebKitAPI/Tests/WebKit2/custom-protocol-sync-xhr.html: Changed scheme to http.
     13        * TestWebKitAPI/Tests/WebKit2ObjC/CustomProtocolsTest.mm:
     14        (-[CustomProtocolsLoadDelegate browsingContextControllerDidStartProvisionalLoad:]): Expected a certain provisional URL.
     15        (-[CustomProtocolsLoadDelegate browsingContextControllerDidReceiveServerRedirectForProvisionalLoad:]): Ditto.
     16        (-[CustomProtocolsLoadDelegate browsingContextControllerDidCommitLoad:]): Expected a certain committed URL.
     17        (-[CustomProtocolsLoadDelegate browsingContextControllerDidFinishLoad:]): Expected isLoading to be false.
     18        (TestWebKitAPI::TEST): Used the new load delegate and unregistered TestProtocol.
     19        * TestWebKitAPI/Tests/WebKit2ObjC/PreventImageLoadWithAutoResizing.mm:
     20        (TestWebKitAPI::TEST): Unregistered TestProtocol.
     21        * TestWebKitAPI/mac/TestProtocol.mm: Changed scheme to http.
     22        (+[TestProtocol canInitWithRequest:]): Changed to use property syntax.
     23        (-[TestProtocol startLoading]): Taught to handle redirect responses.
     24
    1252015-08-13  Andy Estes  <aestes@apple.com>
    226
  • trunk/Tools/TestWebKitAPI/Tests/CustomProtocolsSyncXHRTest.mm

    r177506 r188517  
    7070
    7171    TestWebKitAPI::Util::run(&testFinished);
     72    [NSURLProtocol unregisterClass:[TestProtocol class]];
     73    [WKBrowsingContextController unregisterSchemeForCustomProtocol:[TestProtocol scheme]];
    7274}
    7375
  • trunk/Tools/TestWebKitAPI/Tests/WebKit2/custom-protocol-sync-xhr.html

    r149194 r188517  
    11<script>
    22    var request = new XMLHttpRequest();
    3     request.open('GET', 'test://test', false);
     3    request.open('GET', 'http://test', false);
    44    request.send(null);
    55    window._testResult = request.responseText;
  • trunk/Tools/TestWebKitAPI/Tests/WebKit2ObjC/CustomProtocolsTest.mm

    r177506 r188517  
    4040static bool testFinished = false;
    4141
     42@interface CustomProtocolsLoadDelegate : NSObject <WKBrowsingContextLoadDelegate>
     43@end
     44
     45@implementation CustomProtocolsLoadDelegate
     46
     47- (void)browsingContextControllerDidStartProvisionalLoad:(WKBrowsingContextController *)sender
     48{
     49    EXPECT_TRUE([sender.provisionalURL.absoluteString isEqualToString:@"http://redirect/?test"]);
     50}
     51
     52- (void)browsingContextControllerDidReceiveServerRedirectForProvisionalLoad:(WKBrowsingContextController *)sender
     53{
     54    EXPECT_TRUE([sender.provisionalURL.absoluteString isEqualToString:@"http://test/"]);
     55}
     56
     57- (void)browsingContextControllerDidCommitLoad:(WKBrowsingContextController *)sender
     58{
     59    EXPECT_TRUE([sender.committedURL.absoluteString isEqualToString:@"http://test/"]);
     60}
     61
     62- (void)browsingContextControllerDidFinishLoad:(WKBrowsingContextController *)sender
     63{
     64    EXPECT_FALSE(sender.isLoading);
     65    testFinished = true;
     66}
     67
     68@end
     69
    4270namespace TestWebKitAPI {
    4371
     
    5078    RetainPtr<WKBrowsingContextGroup> browsingContextGroup = adoptNS([[WKBrowsingContextGroup alloc] initWithIdentifier:@"TestIdentifier"]);
    5179    RetainPtr<WKView> wkView = adoptNS([[WKView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) processGroup:processGroup.get() browsingContextGroup:browsingContextGroup.get()]);
    52     wkView.get().browsingContextController.loadDelegate = [[TestBrowsingContextLoadDelegate alloc] initWithBlockToRunOnLoad:^(WKBrowsingContextController *sender) {
    53         testFinished = true;
    54     }];
    55     [wkView.get().browsingContextController loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@://test", [TestProtocol scheme]]]]];
     80    RetainPtr<CustomProtocolsLoadDelegate> loadDelegate = adoptNS([[CustomProtocolsLoadDelegate alloc] init]);
     81    [wkView browsingContextController].loadDelegate = loadDelegate.get();
     82    [[wkView browsingContextController] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@://redirect?test", [TestProtocol scheme]]]]];
    5683
    5784    Util::run(&testFinished);
     85    [NSURLProtocol unregisterClass:[TestProtocol class]];
     86    [WKBrowsingContextController unregisterSchemeForCustomProtocol:[TestProtocol scheme]];
    5887}
    5988
  • trunk/Tools/TestWebKitAPI/Tests/WebKit2ObjC/PreventImageLoadWithAutoResizing.mm

    r177506 r188517  
    5858
    5959    Util::run(&testFinished);
     60    [NSURLProtocol unregisterClass:[TestProtocol class]];
     61    [WKBrowsingContextController unregisterSchemeForCustomProtocol:[TestProtocol scheme]];
    6062}
    6163
  • trunk/Tools/TestWebKitAPI/mac/TestProtocol.mm

    r149194 r188517  
    2727#import "TestProtocol.h"
    2828
    29 static NSString *testScheme = @"test";
     29#import <wtf/RetainPtr.h>
     30
     31// Even though NSURLProtocol is capable of generating redirect responses for any protocol, WebCore asserts if a redirect is not in the http family.
     32// See http://webkit.org/b/147870 for details.
     33static NSString *testScheme = @"http";
    3034
    3135@implementation TestProtocol
     
    3337+ (BOOL)canInitWithRequest:(NSURLRequest *)request
    3438{
    35     return [[[request URL] scheme] caseInsensitiveCompare:testScheme] == NSOrderedSame;
     39    return [request.URL.scheme caseInsensitiveCompare:testScheme] == NSOrderedSame;
    3640}
    3741
     
    5357- (void)startLoading
    5458{
    55     EXPECT_TRUE([[[[self request] URL] scheme] isEqualToString:testScheme]);
    56    
     59    NSURL *requestURL = self.request.URL;
     60    EXPECT_TRUE([requestURL.scheme isEqualToString:testScheme]);
     61
    5762    NSData *data = [@"PASS" dataUsingEncoding:NSASCIIStringEncoding];
    58     NSURLResponse *response = [[NSURLResponse alloc] initWithURL:[[self request] URL] MIMEType:@"text/html" expectedContentLength:[data length] textEncodingName:nil];
    59     [[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
    60     [[self client] URLProtocol:self didLoadData:data];
    61     [[self client] URLProtocolDidFinishLoading:self];
    62     [response release];
     63    RetainPtr<NSURLResponse> response = adoptNS([[NSURLResponse alloc] initWithURL:requestURL MIMEType:@"text/html" expectedContentLength:data.length textEncodingName:nil]);
     64
     65    if ([requestURL.host isEqualToString:@"redirect"]) {
     66        NSURL *redirectURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@://%@", testScheme, requestURL.query]];
     67        [self.client URLProtocol:self wasRedirectedToRequest:[NSURLRequest requestWithURL:redirectURL] redirectResponse:response.get()];
     68        return;
     69    }
     70
     71    [self.client URLProtocol:self didReceiveResponse:response.get() cacheStoragePolicy:NSURLCacheStorageNotAllowed];
     72    [self.client URLProtocol:self didLoadData:data];
     73    [self.client URLProtocolDidFinishLoading:self];
    6374}
    6475
Note: See TracChangeset for help on using the changeset viewer.