all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* [PATCH] Add parser for Netscape/Mozilla cookie file format
@ 2017-10-06 14:34 Aurelien Aptel
  2017-10-09  8:50 ` Aurélien Aptel
  0 siblings, 1 reply; 6+ messages in thread
From: Aurelien Aptel @ 2017-10-06 14:34 UTC (permalink / raw)
  To: emacs-devel; +Cc: Aurelien Aptel

* lisp/url/url-cookie.el (url-cookie-parse-file-netscape): New function.
---
 lisp/url/url-cookie.el | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)

diff --git a/lisp/url/url-cookie.el b/lisp/url/url-cookie.el
index 453d4fe5b6..b0dc40475b 100644
--- a/lisp/url/url-cookie.el
+++ b/lisp/url/url-cookie.el
@@ -74,6 +74,55 @@ url-cookie-parse-file
   ;; It's completely normal for the cookies file not to exist yet.
   (load (or fname url-cookie-file) t t))
 
+(defun url-cookie-parse-file-netscape (filename &optional long-session)
+  "Load cookies from FILENAME in Netscape/Mozilla format.
+When LONG-SESSION is non-nil, session cookies (expiring at t=0
+i.e. 1970-1-1) are loaded as expiring one year from now instead."
+  (interactive "fLoad Netscape/Mozilla cookie file: ")
+  (let ((n 0))
+    (with-temp-buffer
+      (insert-file-contents-literally filename)
+      (goto-char (point-min))
+      (when (not (looking-at-p "# Netscape HTTP Cookie File\n"))
+	(error (format "File %s doesn't look like a netscape cookie file" filename)))
+      (while (not (eobp))
+	(when (not (looking-at-p (rx bol (* space) "#")))
+	  (let* ((line (buffer-substring (point) (save-excursion (end-of-line) (point))))
+		 (fields (split-string line "\t")))
+	    (cond
+	     ;;((>= 1 (length line) 0)
+	     ;; (message "skipping empty line"))
+	     ((= (length fields) 7)
+	      (let ((dom (nth 0 fields))
+		    (match (nth 1 fields))
+		    (path (nth 2 fields))
+		    (secure (string= (nth 3 fields) "TRUE"))
+		    ;; session cookies (expire time = 0) are supposed
+		    ;; to be removed when the browser is closed, but
+		    ;; the main point of loading external cookie is to
+		    ;; reuse a browser session, so to prevent the
+		    ;; cookie from being detected as expired straight
+		    ;; away, make it expire a year from now
+		    (expires (format-time-string
+			      "%d %b %Y %T [GMT]"
+			      (seconds-to-time
+			       (let ((s (string-to-number (nth 4 fields))))
+				 (if (and (= s 0) long-session)
+				     (seconds-to-time (+ (* 365 24 60 60) (float-time)))
+				   s)))))
+		    (key (nth 5 fields))
+		    (val (nth 6 fields)))
+		(incf n)
+		;;(message "adding <%s>=<%s> exp=<%s> dom=<%s> path=<%s> sec=%S" key val expires dom path secure)
+		(url-cookie-store key val expires dom path secure)
+		))
+	     (t
+	      (message "ignoring malformed cookie line <%s>" line)))))
+	(forward-line))
+      (when (< 0 n)
+	(setq url-cookies-changed-since-last-save t))
+      (message "added %d cookies from file %s" n filename))))
+
 (defun url-cookie-clean-up (&optional secure)
   (let ((var (if secure 'url-cookie-secure-storage 'url-cookie-storage))
 	new new-cookies)
-- 
2.12.3




^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] Add parser for Netscape/Mozilla cookie file format
  2017-10-06 14:34 [PATCH] Add parser for Netscape/Mozilla cookie file format Aurelien Aptel
@ 2017-10-09  8:50 ` Aurélien Aptel
  2017-10-09  8:53   ` Eli Zaretskii
  0 siblings, 1 reply; 6+ messages in thread
From: Aurélien Aptel @ 2017-10-09  8:50 UTC (permalink / raw)
  To: Aurelien Aptel; +Cc: Emacs development discussions

will commit this tomorrow if no one sees anything wrong with it.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Add parser for Netscape/Mozilla cookie file format
  2017-10-09  8:50 ` Aurélien Aptel
@ 2017-10-09  8:53   ` Eli Zaretskii
  2017-10-09  9:12     ` Aurélien Aptel
  0 siblings, 1 reply; 6+ messages in thread
From: Eli Zaretskii @ 2017-10-09  8:53 UTC (permalink / raw)
  To: Aurélien Aptel; +Cc: aaptel, emacs-devel

> From: Aurélien Aptel <aurelien.aptel+emacs@gmail.com>
> Date: Mon, 9 Oct 2017 10:50:47 +0200
> Cc: Emacs development discussions <emacs-devel@gnu.org>
> 
> will commit this tomorrow if no one sees anything wrong with it.

Is this format still used nowadays?  E.g., on my system the cookies
are stored in an SQLite db, not a text file.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Add parser for Netscape/Mozilla cookie file format
  2017-10-09  8:53   ` Eli Zaretskii
@ 2017-10-09  9:12     ` Aurélien Aptel
  2017-10-09  9:34       ` Aurélien Aptel
  2017-10-09 10:55       ` Eli Zaretskii
  0 siblings, 2 replies; 6+ messages in thread
From: Aurélien Aptel @ 2017-10-09  9:12 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: Aurelien Aptel, Emacs development discussions

On Mon, Oct 9, 2017 at 10:53 AM, Eli Zaretskii <eliz@gnu.org> wrote:
> Is this format still used nowadays?  E.g., on my system the cookies
> are stored in an SQLite db, not a text file.

It's the closest thing we have to a standard I think. It's accepted by
wget, curl, lynx, w3m, ... In fact it's the only format they know how
to parse & generate IIUC.
Also implemented by various programming language libs, including python [1].

1: https://docs.python.org/3.3/library/http.cookiejar.html?highlight=cookie#file-cookie-jar-classes



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Add parser for Netscape/Mozilla cookie file format
  2017-10-09  9:12     ` Aurélien Aptel
@ 2017-10-09  9:34       ` Aurélien Aptel
  2017-10-09 10:55       ` Eli Zaretskii
  1 sibling, 0 replies; 6+ messages in thread
From: Aurélien Aptel @ 2017-10-09  9:34 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: Aurelien Aptel, Emacs development discussions

I've found a little shell script that opens the firefox sqlite cookie
db and dumps them in netscape format (using the sqlite cli program).
Perhaps I could add another function doing something similar if the
sqlite executable is available on the system Emacs is running.

https://gist.githubusercontent.com/hackerb9/d382e09683a52dcac492ebcdaf1b79af/raw/fced8cae8fd1f9573646336dcc8093a10e373e03/extract_cookies.sh



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Add parser for Netscape/Mozilla cookie file format
  2017-10-09  9:12     ` Aurélien Aptel
  2017-10-09  9:34       ` Aurélien Aptel
@ 2017-10-09 10:55       ` Eli Zaretskii
  1 sibling, 0 replies; 6+ messages in thread
From: Eli Zaretskii @ 2017-10-09 10:55 UTC (permalink / raw)
  To: Aurélien Aptel; +Cc: aaptel, emacs-devel

> From: Aurélien Aptel <aurelien.aptel+emacs@gmail.com>
> Date: Mon, 9 Oct 2017 11:12:53 +0200
> Cc: Aurelien Aptel <aaptel@suse.com>, Emacs development discussions <emacs-devel@gnu.org>
> 
> On Mon, Oct 9, 2017 at 10:53 AM, Eli Zaretskii <eliz@gnu.org> wrote:
> > Is this format still used nowadays?  E.g., on my system the cookies
> > are stored in an SQLite db, not a text file.
> 
> It's the closest thing we have to a standard I think. It's accepted by
> wget, curl, lynx, w3m, ... In fact it's the only format they know how
> to parse & generate IIUC.
> Also implemented by various programming language libs, including python [1].
> 
> 1: https://docs.python.org/3.3/library/http.cookiejar.html?highlight=cookie#file-cookie-jar-classes

Thanks.  If this is useful, I have no objections, but please push to
the master branch.



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2017-10-09 10:55 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-10-06 14:34 [PATCH] Add parser for Netscape/Mozilla cookie file format Aurelien Aptel
2017-10-09  8:50 ` Aurélien Aptel
2017-10-09  8:53   ` Eli Zaretskii
2017-10-09  9:12     ` Aurélien Aptel
2017-10-09  9:34       ` Aurélien Aptel
2017-10-09 10:55       ` Eli Zaretskii

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.