From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Joseph Turner Newsgroups: gmane.emacs.devel Subject: Re: How to get buffer byte length (not number of characters)? Date: Fri, 23 Aug 2024 00:58:47 -0700 Message-ID: <87ed6ffz60.fsf@ushin.org> References: <87wmkbekjp.fsf@ushin.org> <86o75nwilg.fsf@gnu.org> <87bk1lhkvg.fsf@ushin.org> <86y14pu5rp.fsf@gnu.org> <871q2hfn7c.fsf@ushin.org> <86plq1td4n.fsf@gnu.org> <87ed6hdnpe.fsf@ushin.org> <86o75ksm2q.fsf@gnu.org> <203a830d-356e-4c0a-83c6-c2aeb8d1e135@alphapapa.net> <87ttfbg0a0.fsf@ushin.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="25351"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Eli Zaretskii , emacs-devel@gnu.org, schwab@suse.de To: Adam Porter Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Fri Aug 23 14:27:23 2024 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1shTNy-0006QZ-Fg for ged-emacs-devel@m.gmane-mx.org; Fri, 23 Aug 2024 14:27:23 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1shTNm-0003Ct-Qc; Fri, 23 Aug 2024 08:27:10 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1shPCE-0002Jr-Sm for emacs-devel@gnu.org; Fri, 23 Aug 2024 03:58:58 -0400 Original-Received: from out-180.mta0.migadu.com ([91.218.175.180]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1shPCC-0002fn-9e for emacs-devel@gnu.org; Fri, 23 Aug 2024 03:58:58 -0400 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ushin.org; s=key1; t=1724399932; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=tdYCo30cAa//cL/3ljfGXZ9zrvlF/inFaKjlBj2P7Zo=; b=noNNWgcVzo/3zb7HK/XR8id1EjjjidFMwjpm0BudSy4c18GbuiMNQ/vwcM2Jup74/KRA9I 03AKzyQ6BInqQn2NTYo72Md8l7AdmLO/i0FxgMtuf+LIIpoOVMKGhG7F4UOUKp+sb3YCbN fZxI2J2CGrIxOb2sKWjA2WJ6DXhJkDNq02CPOhLX5KPk3M80EuiZ3YGwiigG/o1zWisRxr rtlo13z1nC6Yr02N15IDzueI1vDeL/7Hv4/CDjQsAkrI4Pvmf1Crg8xBRNRY8YvjzvvJVO F7OXift60xd5KStHlAtBp7fKdedwbMeJB4fj/NndhiKEW39VZpUQ2HaJjS1GcQ== In-Reply-To: <87ttfbg0a0.fsf@ushin.org> (Joseph Turner's message of "Fri, 23 Aug 2024 00:07:54 -0700") X-Migadu-Flow: FLOW_OUT Received-SPF: pass client-ip=91.218.175.180; envelope-from=joseph@ushin.org; helo=out-180.mta0.migadu.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Fri, 23 Aug 2024 08:26:10 -0400 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:323077 Archived-At: --=-=-= Content-Type: text/plain Joseph Turner writes: > Please see patch! (not tested yet) I made a typo in the last patch. Here's a new one. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-Add-plz-ENCODE-argument-Add-default-encoding.patch >From 9ff971c6bbf00ebfe33a6e8993a006a168b4c6cb Mon Sep 17 00:00:00 2001 From: Joseph Turner Date: Thu, 22 Aug 2024 00:02:19 -0700 Subject: [PATCH] Add: (plz) ENCODE argument; Add default encoding Previously, PUT requests which pass an unencoded string or buffer with multibyte characters sent the internal Emacs multibyte representation. Now strings and buffers are encoded by default, and the ENCODE nil argument (or :BODY-TYPE 'binary) can used when the user wants to handle encoding. WIP --- plz.el | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/plz.el b/plz.el index 903d71e..2a2077d 100644 --- a/plz.el +++ b/plz.el @@ -325,7 +325,8 @@ (defalias 'plz--generate-new-buffer (cl-defun plz (method url &rest rest &key headers body else filter finally noquery timeout (as 'string) (then 'sync) - (body-type 'text) (decode t decode-s) + (body-type 'text) (encode t encode-s) + (decode t decode-s) (connect-timeout plz-connect-timeout)) "Request METHOD from URL with curl. Return the curl process object or, for a synchronous request, the @@ -340,6 +341,11 @@ (cl-defun plz (method url &rest rest &key headers body else filter finally noque BODY-TYPE may be `text' to send BODY as text, or `binary' to send it as binary. +If ENCODE is non-nil, BODY is encoded automatically. For binary +content, it should be nil. When BODY-TYPE is `binary', ENCODE is +automatically set to nil. ENCODE has no effect when BODY is a +list like `(file FILENAME)'. + AS selects the kind of result to pass to the callback function THEN, or the kind of result to return for synchronous requests. It may be: @@ -416,6 +422,8 @@ (cl-defun plz (method url &rest rest &key headers body else filter finally noque (declare (indent defun)) (setf decode (if (and decode-s (not decode)) nil decode)) + (setf encode (if (and encode-s (not encode)) + nil encode)) ;; NOTE: By default, for PUT requests and POST requests >1KB, curl sends an ;; "Expect:" header, which causes servers to send a "100 Continue" response, which ;; we don't want to have to deal with, so we disable it by setting the header to @@ -473,6 +481,9 @@ (cl-defun plz (method url &rest rest &key headers body else filter finally noque (decode (pcase as ('binary nil) (_ decode))) + (encode (pcase body-type + ('binary nil) + (_ encode))) (default-directory ;; Avoid making process in a nonexistent directory (in case the current ;; default-directory has since been removed). It's unclear what the best @@ -553,9 +564,19 @@ (cl-defun plz (method url &rest rest &key headers body else filter finally noque (process-send-string process curl-config) (when body (cl-typecase body - (string (process-send-string process body)) - (buffer (with-current-buffer body - (process-send-region process (point-min) (point-max)))))) + (string (process-send-string + process (if encode + (encode-coding-string + body (cdr default-process-coding-system)) + body))) + (buffer (if encode + (with-temp-buffer + (insert-buffer-substring-no-properties body) + (encode-coding-region + (point-min) (point-max) (cdr default-process-coding-system)) + (process-send-region process (point-min) (point-max))) + (with-current-buffer body + (process-send-region process (point-min) (point-max))))))) (process-send-eof process) (if sync-p (unwind-protect -- 2.41.0 --=-=-=--