gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnunet-scheme] 48/49: examples/web (search-dht): Fail gracefully in cas


From: gnunet
Subject: [gnunet-scheme] 48/49: examples/web (search-dht): Fail gracefully in case of invalid input
Date: Sat, 25 Dec 2021 23:00:25 +0100

This is an automated email from the git hooks/post-receive script.

maxime-devos pushed a commit to branch master
in repository gnunet-scheme.

commit 0d948086726e851939adfd0bf8b1b0d6d386e022
Author: Maxime Devos <maximedevos@telenet.be>
AuthorDate: Sat Dec 25 21:31:37 2021 +0000

    examples/web (search-dht): Fail gracefully in case of invalid input
    
    * examples/web.scm
      (respond/html): Add an optional 'status-code' argument.
      (process-search-dht): Validate input parameters somewhat, and on
      failure, message the client appropriately.
---
 examples/web.scm | 67 +++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 39 insertions(+), 28 deletions(-)

diff --git a/examples/web.scm b/examples/web.scm
index 7311376..2564dc4 100644
--- a/examples/web.scm
+++ b/examples/web.scm
@@ -45,9 +45,11 @@
 
 (define config (load-configuration))
 
-(define (respond/html body)
+(define* (respond/html body #:key (status-code 200))
+  "@var{status-code}: the HTTP status code to return. By default, the status 
code
+for success is used."
   (values (build-response
-          #:code 200
+          #:code status-code
           #:headers `((content-type application/xhtml+xml) (charset . 
"utf-8")))
          (lambda (port)
            (display "<!DOCTYPE html>\n" port)
@@ -148,32 +150,41 @@
                     (slice-copy get-path)
                     (slice-copy put-path)))
     (signal-condition! found?))
-  (dht:start-get! dht-server
-                 (string->number (assoc-ref parameters "type"))
-                 (decode/key (assoc-ref parameters "key-encoding")
-                             (assoc-ref parameters "key"))
-                 found
-                 #:desired-replication-level
-                 (string->number (assoc-ref parameters "replication-level")))
-  (wait found?)
-  ;; TODO: properly format the result, streaming, stop searching
-  ;; after something has been found or if the client closes the connection ...
-  (respond/html `(div (p "Found! ")
-                     ;; TODO: better output, determine why the data is bogus
-                     (dl ,@(match what
-                             ((type key data expiration get-path put-path)
-                              `((dt "Type: ")
-                                (dd ,type)
-                                (dt "Key: ")
-                                (dd ,(data->string key))
-                                (dt "Data: ")
-                                (dd ,(data->string data))
-                                (dt "Expiration: ")
-                                (dd ,(object->string expiration))
-                                (dt "Get path: ") ; TODO as list
-                                (dd ,(object->string get-path))
-                                (dt "Put path: ")
-                                (dd ,(object->string put-path)))))))))
+  ;; Perform rudimentary input parameter validation (TODO: more validation).
+  (let* ((type (and=> (assoc-ref parameters "type") string->number))
+        (key-encoding (assoc-ref parameters "key-encoding"))
+        (key (assoc-ref parameters "key"))
+        (replication-level (assoc-ref parameters "key"))
+        (desired-replication-level
+         (and=> (assoc-ref parameters "replication-level") string->number)))
+    (if (and type key-encoding key replication-level desired-replication-level)
+       (begin
+         (dht:start-get! dht-server type
+                         (decode/key key-encoding key)
+                         found
+                         #:desired-replication-level
+                         desired-replication-level)
+         (wait found?)
+         ;; TODO: properly format the result, streaming, stop searching
+         ;; after something has been found or if the client closes the 
connection ...
+         (respond/html `(div (p "Found! ")
+                             ;; TODO: better output, determine why the data is 
bogus
+                             (dl ,@(match what
+                                     ((type key data expiration get-path 
put-path)
+                                      `((dt "Type: ")
+                                        (dd ,type)
+                                        (dt "Key: ")
+                                        (dd ,(data->string key))
+                                        (dt "Data: ")
+                                        (dd ,(data->string data))
+                                        (dt "Expiration: ")
+                                        (dd ,(object->string expiration))
+                                        (dt "Get path: ") ; TODO as list
+                                        (dd ,(object->string get-path))
+                                        (dt "Put path: ")
+                                        (dd ,(object->string put-path)))))))))
+       (respond/html `(p "Some fields were missing / invalid")
+                     #:status-code 400))))
 
 (define-once started? #f)
 

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]