Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --cookies option to pass in a cookie file #47

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 59 additions & 1 deletion webkit2png
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,42 @@ except ImportError:
print "Cannot find pyobjc library files. Are you sure it is installed?"
sys.exit()

# given a cookie filename and a url, return the value for an HTTP cookie
# header
def cookieFromFile(filename, url):
import cookielib
import urllib2
import tempfile
import os

# cookielib won't even look at the rest of a cookie file if the first line
# doesn't match a particular regex, so we always guarantee the magic_re
# will succeed
tmp = tempfile.NamedTemporaryFile(delete=False)
tmp.write("# Netscape HTTP Cookie File\n")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused why webkit2png needs to fix the format of the file here - isn't that the responsibility of whatever tool created the file?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, it totally is the responsibility of the tool that created the file: but that tool was a Chrome plugin I don't have any control over. Since then, I've found another plugin that does generate the cookie dump with this required line, so I'd be fine removing this hack.

This line's non-existence was such a silly reason for cookielib to throw out an otherwise perfectly good cookie file, I was annoyed enough to code around it. And since I was already forced to write a named file, it was not a big reach to simply guarantee this line existed in the file.

real = open(filename, "r")
while 1:
line = real.readline()
if line:
tmp.write(line)
else:
break
tmp.close()

# we only need this Request() long enough to have cookielib set the Cookie
# header for us, we never actually pass it urlopen() or the like
req = urllib2.Request(url)
jar = cookielib.MozillaCookieJar(tmp.name)
jar.load()
jar.add_cookie_header(req)

cookie = req.get_header("Cookie")
if not cookie:
cookie = ""

os.unlink(tmp.name)
return cookie

class AppDelegate (Foundation.NSObject):
# what happens when the app starts up
def applicationDidFinishLaunching_(self, aNotification):
Expand Down Expand Up @@ -159,7 +195,25 @@ class WebkitLoad (Foundation.NSObject, WebKit.protocols.WebFrameLoadDelegate):
scriptobject = webview.windowScriptObject()
scriptobject.setValue_forKey_(Webkit2PngScriptBridge.alloc().init(), 'webkit2png')

webview.mainFrame().loadRequest_(Foundation.NSURLRequest.requestWithURL_(Foundation.NSURL.URLWithString_(url)))
req = Foundation.NSMutableURLRequest.requestWithURL_(Foundation.NSURL.URLWithString_(url))

if self.options.cookieFile or self.options.cookieData:
cookie = ''
if self.options.cookieFile:
try:
cookie = cookieFromFile(self.options.cookieFile, url)
except:
print "cookie loading error: ", sys.exc_info()
AppKit.NSApplication.sharedApplication().terminate_(None)
if self.options.cookieData:
if cookie:
cookie += '; '
cookie += '; '.join(self.options.cookieData)

req.setValue_forHTTPHeaderField_(cookie, 'Cookie')

webview.mainFrame().loadRequest_(req)

if not webview.mainFrame().provisionalDataSource():
print " ... not a proper url?"
self.getURL(webview)
Expand Down Expand Up @@ -316,6 +370,10 @@ Examples:
help=optparse.SUPPRESS_HELP)
group.add_option("--no-js", action="store_true",
help="disable JavaScript support")
group.add_option("--cookie-file", type="string", metavar="FILENAME", dest="cookieFile",
help="specify a Netscape cookie file")
group.add_option("--cookie", type="string", metavar="NAME=VALUE", dest="cookieData", action="append",
help="specify a cookie name-value pair (multiple --cookie is allowed)")
group.add_option("--transparent", action="store_true",
help="render output on a transparent background (requires a web page with a transparent background)", default=False)
cmdparser.add_option_group(group)
Expand Down