Created
May 31, 2009 21:55
-
-
Save sstephenson/121054 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env macruby | |
# % gdb macruby ns_url_protocol_test.rb | |
# | |
# (gdb) run ns_url_protocol_test.rb http://www.google.com/ | |
# Starting program: /usr/local/bin/macruby ns_url_protocol_test.rb http://www.google.com/ | |
# | |
# received 4900 bytes | |
# | |
# Program exited normally. | |
# | |
# (gdb) run ns_url_protocol_test.rb test: | |
# Starting program: /usr/local/bin/macruby ns_url_protocol_test.rb test: | |
# | |
# Program received signal EXC_BAD_ACCESS, Could not access memory. | |
# Reason: KERN_PROTECTION_FAILURE at address: 0x0000000c | |
# [Switching to process 22060 thread 0x530b] | |
# 0x95ee5027 in _longjmp () | |
# | |
# (gdb) bt | |
# #0 0x95ee5027 in _longjmp () | |
# #1 0x002e7e25 in vm_eval_body () | |
# #2 0x002e96a4 in vm_call0 () | |
# #3 0x003026ab in rb_ruby_to_objc_closure_handler_main () | |
# #4 0x003028d4 in rb_ruby_to_objc_closure_handler () | |
# #5 0x90136424 in ffi_closure_SYSV () | |
# #6 0x964615f4 in bridgeStart () | |
# #7 0x920ebfa5 in URLProtocol_Classic::startLoad () | |
# #8 0x92107877 in URLConnectionLoader::loaderScheduleOriginLoad () | |
# #9 0x921076df in URLConnectionLoader::loaderScheduleLoad () | |
# #10 0x92106753 in URLConnectionLoader::LoaderConnectionEventQueue::processAllEventsAndConsumePayload () | |
# #11 0x92106bdd in URLConnectionLoader::processEvents () | |
# #12 0x920b1dbf in MultiplexerSource::perform () | |
# #13 0x92ff8595 in CFRunLoopRunSpecific () | |
# #14 0x92ff8c78 in CFRunLoopRunInMode () | |
# #15 0x96460530 in +[NSURLConnection(NSURLConnectionReallyInternal) _resourceLoadLoop:] () | |
# #16 0x963fce0d in -[NSThread main] () | |
# #17 0x963fc9b4 in __NSThread__main__ () | |
# #18 0x95eeb155 in _pthread_start () | |
# #19 0x95eeb012 in thread_start () | |
framework "cocoa" | |
class TestURLProtocol < NSURLProtocol | |
def self.canInitWithRequest(request) | |
request.URL.scheme == "test" | |
end | |
def self.canonicalRequestForRequest(request) | |
request | |
end | |
def startLoading | |
response = NSURLResponse.initWithURL(request.URL, MIMEType: "text/html", expectedContentLength: 5, textEncodingName: nil) | |
data = NSData.dataWithBytes("Hello", length: 5) | |
client.URLProtocol(self, didReceiveResponse: response, cacheStoragePolicy: NSURLCacheStorageNotAllowed) | |
client.URLProtocol(self, didLoadData: data) | |
client.URLProtocolDidFinishLoading | |
end | |
def stopLoading | |
end | |
end | |
class TestApplication | |
def initialize(url) | |
@url = NSURL.URLWithString(url) | |
end | |
def applicationDidFinishLaunching(n) | |
request = NSURLRequest.alloc.initWithURL(@url) | |
if @connection = NSURLConnection.alloc.initWithRequest(request, delegate: self) | |
@data = NSMutableData.data | |
else | |
error "couldn't create connection" | |
end | |
end | |
def connection(connection, didReceiveResponse: response) | |
@data.length = 0 | |
end | |
def connection(connection, didReceiveData: data) | |
@data.appendData(data) | |
end | |
def connection(connection, didFailWithError: error) | |
puts error.inspect | |
end | |
def connectionDidFinishLoading(connection) | |
puts "received #{@data.length} bytes" | |
@connection = @data = nil | |
exit | |
end | |
end | |
if __FILE__ == $0 | |
NSURLProtocol.registerClass(TestURLProtocol) | |
NSApplication.sharedApplication | |
NSApp.delegate = TestApplication.new(ARGV.first || "test:") | |
NSApp.run | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment