<commit_before>;;; skewer-proxy.el --- proxy a website so that it can be skewered

;; This is free and unencumbered software released into the public domain.

;;; Commentary:

;; Due to same origin policy, simply including the skewer JavaScript
;; in a page is not enough to skewer that website. The page must be
;; served by Emacs. To help accommodate this, Emacs can act as a
;; transparent proxy.

;; http://en.wikipedia.org/wiki/Same_origin_policy

;; Set `skewer-proxy-url' to the remote URL. Don't include the path,
;; so there's no trailing slash. Then enable the proxy with
;; `skewer-proxy-enable', which will overwrite the servlet currently
;; installed in `httpd/', probably the simple-httpd file server.
;; Finally, `skewer-proxy-disable' will restore the original `httpd/'
;; servlet.

;;; Code:

(require 'skewer-mode)

(defvar skewer-proxy-url "http://example.com"
  "Remote server for the silent proxy.")

(defvar skewer--orig-httpd/ (symbol-function 'httpd/)
  "Original definition of the servlet at httpd/.")

(defvar skewer--enabled nil
  "Current status of skewer-proxy.")

(defun skewer-proxy-servlet (proc p &rest args)
  (with-current-buffer (url-retrieve-synchronously (concat skewer-proxy-url p))
    (let* ((header-string (car (split-string (buffer-string) "\n\r?\n\r?")))
           (header (httpd-parse header-string)))
      (process-send-region proc (point-min) (point-max))
      (if (equal "close" (cadr (assoc "Connection" header)))
          (process-send-eof proc)))))

(defun skewer-proxy-enable ()
  "Enable the skewer proxy, overwriting the httpd/ servlet."
  (interactive)
  (unless skewer--enabled
    (setq skewer--enabled t)
    (setq skewer--orig-httpd/ (symbol-function 'httpd/))
    (fset 'httpd/ (symbol-function 'skewer-proxy-servlet))))

(defun skewer-proxy-disable ()
  "Disable the skewer proxy, restoring the original httpd/ function."
  (interactive)
  (fset 'httpd/ skewer--orig-httpd/)
  (setq skewer--enabled nil))

(provide 'skewer-proxy)

;;; skewer-proxy.el ends here<commit_msg>Implement page manipulation in the proxy.<commit_after>;;; skewer-proxy.el --- proxy a website so that it can be skewered

;; This is free and unencumbered software released into the public domain.

;;; Commentary:

;; Due to same origin policy, simply including the skewer JavaScript
;; in a page is not enough to skewer that website. The page must be
;; served by Emacs. To help accommodate this, Emacs can act as a
;; transparent proxy.

;; http://en.wikipedia.org/wiki/Same_origin_policy

;; Set `skewer-proxy-url' to the remote URL. Don't include the path,
;; so there's no trailing slash. Then enable the proxy with
;; `skewer-proxy-enable', which will overwrite the servlet currently
;; installed in `httpd/', probably the simple-httpd file server.
;; Finally, `skewer-proxy-disable' will restore the original `httpd/'
;; servlet.

;;; Code:

(require 'skewer-mode)

(defvar skewer-proxy-url "http://example.com"
  "Remote server for the silent proxy.")

(defvar skewer--orig-httpd/ (symbol-function 'httpd/)
  "Original definition of the servlet at httpd/.")

(defvar skewer--enabled nil
  "Current status of skewer-proxy.")

(defvar skewer-inject t
  "When T, skewer will attempt to modify pages to insert its script.")

(defun skewer-proxy-servlet (proc p &rest args)
  (with-current-buffer (url-retrieve-synchronously (concat skewer-proxy-url p))
    (let* ((header-string (car (split-string (buffer-string) "\n\r?\n\r?")))
           (header (httpd-parse header-string)))
      (if skewer-inject (skewer-inject))
      (process-send-region proc (point-min) (point-max))
      (if (equal "close" (cadr (assoc "Connection" header)))
          (process-send-eof proc)))))

(defun skewer-proxy-enable ()
  "Enable the skewer proxy, overwriting the httpd/ servlet."
  (interactive)
  (unless skewer--enabled
    (setq skewer--enabled t)
    (setq skewer--orig-httpd/ (symbol-function 'httpd/))
    (fset 'httpd/ (symbol-function 'skewer-proxy-servlet))))

(defun skewer-proxy-disable ()
  "Disable the skewer proxy, restoring the original httpd/ function."
  (interactive)
  (fset 'httpd/ skewer--orig-httpd/)
  (setq skewer--enabled nil))

;; Page manipulation

(defvar skewer-inject-string
  "<script src=\"//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js\"></script><script src=\"/skewer\"></script>"
  "String to inject to skewer a page.")

(defun skewer--skeweredp ()
  "Determine if the current page has been skewered already."
  (save-excursion
    (beginning-of-buffer)
    (re-search-forward "\"/skewer\"" nil t)))

(defun skewer--content-type ()
  "Determine the content type of the current page."
  (save-excursion
    (beginning-of-buffer)
    (re-search-forward "Content-Type: " nil t)
    (read (current-buffer))))

(defun skewer-inject ()
  "Inject the skewer script into the current page."
  (when (and (not (skewer--skeweredp)) (eq (skewer--content-type) 'text/html))
    (save-excursion
      (beginning-of-buffer)
      (let ((case-fold-search t))
        (when (re-search-forward "</head>" nil t)
          (backward-char (length "</head>"))
          (insert skewer-inject-string)
          (beginning-of-buffer)
          (when (re-search-forward "Content-Length: " nil t)
            (let* ((length (read (current-buffer)))
                   (fixed (+ length (length skewer-inject-string))))
              (backward-kill-word 1)
              (insert (number-to-string fixed)))))))))

(provide 'skewer-proxy)

;;; skewer-proxy.el ends here
{'fraction_non_alphanumeric': 0.14138118542686243, 'fraction_numerical': 0.0007250317201377561, 'mean_word_length': 3.500815660685155, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2526015', 'n_tokens_mistral': 1774, 'n_tokens_neox': 1748, 'n_words': 552}