Download using Safari's cookies

T

Tobias Weber

Hi,
the httpclient library includes a custom file-based cookie storage.
I made a primitive drop-in replacement (myclient.cookie_manager =
NSCookieManager.new) that uses OS X' built-in jar.

It works for getting stuff from Safari, but new ones I save don't
propagate back there. Probably to do with the AcceptPolicy.

Anyway I thought I'd share it. The author's email is well hidden, so he
probably doesn't like to be contacted. And I'm too lazy to create yet
another Trac account. So see below.

http://dev.ctor.org/http-access2/

require 'osx/cocoa'

class NSCookieManager
attr_accessor :cookies_file

def initialize(file = nil)
@cookies_file = file
@jar = OSX::NSHTTPCookieStorage.sharedHTTPCookieStorage
end

def load_cookies
end

def save_cookies(force = nil)
end

def find(url)
u = OSX::NSURL.URLWithString url.to_s
a = @jar.cookiesForURL u
d = OSX::NSHTTPCookie.requestHeaderFieldsWithCookies a
return d["Cookie"]
end

def parse(str, url)
u = OSX::NSURL.URLWithString url.to_s
d = {"Set-Cookie" => str}
a = OSX::NSHTTPCookie.cookiesWithResponseHeaderFields_forURL(d, u)
@jar.setCookie a.shift
raise unless a.empty?
end
end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top