Subject: [bug#52555] [PATCH v3 3/8] publish: Add HTTP endpoint for resolving ERIS blocks.

From: pukkamustard <[email-masked]>
Date: Thu, 29 Dec 2022 18:13:22 +0000

* guix/eris/http.scm: New file.
* Makefile.am (MODULES): Add it.
* guix/scripts/publish.scm (make-request-handler): Add handler for RFC 2169
URN resolution endpoint.
---
 Makefile.am              |  1 +
 guix/eris/http.scm       | 40 ++++++++++++++++++++++++++++++++++++++++
 guix/scripts/publish.scm | 20 ++++++++++++++++++++
 3 files changed, 61 insertions(+)
 create mode 100644 guix/eris/http.scm

diff --git a/Makefile.am b/Makefile.am
index c549fc8580..373f6b7c27 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -134,6 +134,7 @@ MODULES =					\
   guix/ipfs.scm					\
   guix/eris.scm                                 \
   guix/eris/fs-store.scm                        \
+  guix/eris/http.scm                            \
   guix/platform.scm                             \
   guix/platforms/arm.scm                        \
   guix/platforms/mips.scm                       \
diff --git a/guix/eris/http.scm b/guix/eris/http.scm
new file mode 100644
index 0000000000..a8a9520197
--- /dev/null
+++ b/guix/eris/http.scm
@@ -0,0 +1,40 @@
+;;; GNU Guix --- Functional package management for GNU
+;;; Copyright © 2022 pukkamustard <[email-masked]>
+;;;
+;;; This file is part of GNU Guix.
+;;;
+;;; GNU Guix is free software; you can redistribute it and/or modify it
+;;; under the terms of the GNU General Public License as published by
+;;; the Free Software Foundation; either version 3 of the License, or (at
+;;; your option) any later version.
+;;;
+;;; GNU Guix is distributed in the hope that it will be useful, but
+;;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;;; GNU General Public License for more details.
+;;;
+;;; You should have received a copy of the GNU General Public License
+;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.
+
+(define-module (guix eris http)
+  #:use-module (eris utils base32)
+  #:use-module (web uri)
+
+  #:use-module (srfi srfi-2)
+
+  #:export (make-blake2b-urn-query-ref))
+
+(define blake2b-uri-path-regexp
+  (make-regexp "^blake2b:" regexp/icase))
+
+(define (make-blake2b-urn-query-ref block-ref)
+  (lambda (query)
+    (and-let* ((_ query) ; ensure query is not false
+               (urn (string->uri query))
+               (_ (uri? urn))
+               (_ (eqv? (uri-scheme urn) 'urn))
+               (_ (regexp-exec blake2b-uri-path-regexp
+                               (uri-path urn)))
+               (blake2b-ref (base32-decode
+                             (string-drop (uri-path urn) 8))))
+      (block-ref blake2b-ref))))
diff --git a/guix/scripts/publish.scm b/guix/scripts/publish.scm
index 7f14e4d4d4..15bdf02670 100644
--- a/guix/scripts/publish.scm
+++ b/guix/scripts/publish.scm
@@ -53,6 +53,8 @@ (define-module (guix scripts publish)
   #:use-module (guix config)
   #:use-module (guix derivations)
   #:use-module (guix eris)
+  #:use-module (guix eris fs-store)
+  #:use-module (guix eris http)
   #:use-module (gcrypt hash)
   #:use-module (guix pki)
   #:use-module (gcrypt pk-crypto)
@@ -1083,6 +1085,14 @@ (define nar-path?
     (let ((expected (split-and-decode-uri-path nar-path)))
       (cut equal? expected <>)))
 
+  ;; Get ERIS blocks directly from the filesystem store.
+  (define eris-block-ref
+    (eris-fs-store-ref (%eris-block-store-directory)))
+
+  ;; Create a handler for resolving blake2b URN queries.
+  (define blake2b-urn-query-ref
+    (make-blake2b-urn-query-ref eris-block-ref))
+
   (define (handle request body)
     (format #t "~a ~a~%"
             (request-method request)
@@ -1125,6 +1135,16 @@ (define (handle request body)
           (("log" name)
            (render-log-file store request name))
 
+          ;; /uri-res/N2R - RFC2169 URN resolution
+          (("uri-res" "N2R")
+           (let ((block (blake2b-urn-query-ref
+                         (uri-query (request-uri request)))))
+             (if block
+                 (values `((content-type . (application/octet-stream
+                                            (charset . "ISO-8859-1"))))
+                         block)
+                 (not-found request))))
+
           ;; Use different URLs depending on the compression type.  This
           ;; guarantees that /nar URLs remain valid even when 'guix publish'
           ;; is restarted with different compression parameters.
-- 
2.38.1