From 4207f8a4764ad3a0bca402160a24cca714cf0cde Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Sat, 8 Nov 2014 18:02:36 -0500 Subject: Consolidate keywords, update emails --- sx-encoding.el | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'sx-encoding.el') diff --git a/sx-encoding.el b/sx-encoding.el index efb333e..8ac12ca 100644 --- a/sx-encoding.el +++ b/sx-encoding.el @@ -2,8 +2,7 @@ ;; Copyright (C) 2014 Sean Allred -;; Author: Sean Allred -;; Keywords: help +;; Author: Sean Allred ;; This program is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by -- cgit v1.2.3 From 302be04d826a3f09c8cd3d313592f7976fa8c0cb Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Mon, 10 Nov 2014 22:53:10 -0500 Subject: Proper GZIP testing Test for a GZIP'd using magic bytes. See the appropriate answer on Emacs.SE (1) for details. (1): http://emacs.stackexchange.com/a/2978 --- sx-encoding.el | 29 +++++++++++++++++++++++++++++ sx-request.el | 28 +++++++++++----------------- 2 files changed, 40 insertions(+), 17 deletions(-) (limited to 'sx-encoding.el') diff --git a/sx-encoding.el b/sx-encoding.el index 8ac12ca..98f3a7c 100644 --- a/sx-encoding.el +++ b/sx-encoding.el @@ -74,6 +74,35 @@ (substring ss 1)))))))) (replace-regexp-in-string "&[^; ]*;" get-function string))) +(defun sx-encoding-gzipped-p (data) + "Checks for magic bytes in DATA. + +Check if the first two bytes of a string in DATA match magic +numbers identifying the gzip file format. See [1] for the file +format description. + +http://www.gzip.org/zlib/rfc-gzip.html + +http://emacs.stackexchange.com/a/2978" + (equal (substring (string-as-unibyte data) 0 2) + (unibyte-string 31 139))) + +(defun sx-encoding-gzipped-buffer-p (filename) + "Check if the BUFFER is gzip-compressed. + +See `gzip-check-magic' for details." + (sx-encoding-gzip-check-magic (buffer-string))) + +(defun sx-encoding-gzipped-file-p (file) + "Check if the FILE is gzip-compressed. + +See `gzip-check-magic' for details." + (let ((first-two-bytes (with-temp-buffer + (set-buffer-multibyte nil) + (insert-file-contents-literally file nil 0 2) + (buffer-string)))) + (gzip-check-magic first-two-bytes))) + (provide 'sx-encoding) ;;; sx-encoding.el ends here diff --git a/sx-request.el b/sx-request.el index 9d9dca4..12d2b14 100644 --- a/sx-request.el +++ b/sx-request.el @@ -92,24 +92,18 @@ number of requests left every time it finishes a call.") (error "Response headers missing; response corrupt") (delete-region (point-min) (point)) (buffer-string)))) - (response (ignore-errors + (response-zipped-p (sx-encoding-gzipped-p data)) + (data (if (not response-zipped-p) data + (shell-command-on-region + (point-min) (point-max) + sx-request-unzip-program + nil t) + (buffer-string))) + (response (with-demoted-errors "`json' error: %S" (json-read-from-string data)))) - ;; If the response isn't nil, the response was in plain text - (unless response - ;; try to decompress the response - (setq response - (with-demoted-errors "`json-read' error: %S" - (shell-command-on-region - (point-min) (point-max) - sx-request-unzip-program - nil t) - (json-read-from-string - (buffer-substring - (point-min) (point-max))))) - ;; if it still fails, error outline - (unless response - (sx-message "Unable to parse response: %S" response) - (error "Response could not be read by `json-read-from-string'"))) + (unless (not (and (not response) (string-equal data "{}"))) + (sx-message "Unable to parse response: %S" response) + (error "Response could not be read by `json-read-from-string'")) ;; If we get here, the response is a valid data structure (sx-assoc-let response (when error_id -- cgit v1.2.3 From 28500694aa199c1a1bb496067ffd956bb8a9f3a9 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Tue, 11 Nov 2014 08:46:53 -0500 Subject: Use correct function name --- sx-encoding.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sx-encoding.el') diff --git a/sx-encoding.el b/sx-encoding.el index 98f3a7c..0b72365 100644 --- a/sx-encoding.el +++ b/sx-encoding.el @@ -101,7 +101,7 @@ See `gzip-check-magic' for details." (set-buffer-multibyte nil) (insert-file-contents-literally file nil 0 2) (buffer-string)))) - (gzip-check-magic first-two-bytes))) + (sx-encoding-gzipped-p first-two-bytes))) (provide 'sx-encoding) ;;; sx-encoding.el ends here -- cgit v1.2.3