changeset 1397:a9662d8ada3f

hello dpi: convert to dsh API (with auth)
author Jorge Arellano Cid <jcid@dillo.org>
date Sun, 01 Nov 2009 16:31:59 -0300
parents 7d57f3528d20
children b0aec637cdc0
files dpi/hello.c
diffstat 1 files changed, 25 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/dpi/hello.c	Sun Nov 01 16:31:59 2009 -0300
+++ b/dpi/hello.c	Sun Nov 01 16:31:59 2009 -0300
@@ -36,7 +36,7 @@
 int main(void)
 {
    FILE *in_stream;
-   SockHandler *sh;
+   Dsh *sh;
    char *dpip_tag, *cmd = NULL, *url = NULL, *child_cmd = NULL;
    char *esc_tag, *d_cmd;
    size_t n;
@@ -50,10 +50,19 @@
    MSG("starting...\n");
 
    /* Initialize the SockHandler */
-   sh = sock_handler_new(STDIN_FILENO, STDOUT_FILENO, 2*1024);
+   sh = a_Dpip_dsh_new(STDIN_FILENO, STDOUT_FILENO, 2*1024);
+
+   /* Authenticate our client... */
+   if (!(dpip_tag = a_Dpip_dsh_read_token(sh, 1)) ||
+       a_Dpip_check_auth(dpip_tag) < 0) {
+      MSG("can't authenticate request: %s\n", dStrerror(errno));
+      a_Dpip_dsh_close(sh);
+      return 1;
+   }
+   dFree(dpip_tag);
 
    /* Read the dpi command from STDIN */
-   dpip_tag = sock_handler_read(sh);
+   dpip_tag = a_Dpip_dsh_read_token(sh, 1);
    MSG("tag = [%s]\n", dpip_tag);
 
    cmd = a_Dpip_get_attr(dpip_tag, "cmd");
@@ -69,11 +78,11 @@
               "cmd=%s msg=%s alt1=%s alt2=%s alt3=%s alt4=%s alt5=%s",
               "dialog", "Do you want to see the hello page?",
               choice[1], choice[2], choice[3], choice[4], choice[5]);
-   sock_handler_write_str(sh, 1, d_cmd);
+   a_Dpip_dsh_write_str(sh, 1, d_cmd);
    dFree(d_cmd);
 
    /* Get the answer */
-   dpip_tag2 = sock_handler_read(sh);
+   dpip_tag2 = a_Dpip_dsh_read_token(sh, 1);
    MSG("tag = [%s]\n", dpip_tag2);
 
    /* Get "msg" value */
@@ -89,10 +98,10 @@
 
    /* Start sending our answer */
    d_cmd = a_Dpip_build_cmd("cmd=%s url=%s", "start_send_page", url);
-   sock_handler_write_str(sh, 0, d_cmd);
+   a_Dpip_dsh_write_str(sh, 0, d_cmd);
    dFree(d_cmd);
 
-   sock_handler_printf(sh, 0,
+   a_Dpip_dsh_printf(sh, 0,
       "Content-type: text/html\n\n"
       "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>\n"
       "<html>\n"
@@ -100,7 +109,7 @@
       "<body><hr><h1>Hello world!</h1><hr>\n<br><br>\n");
 
    /* Show the choice received with the dialog */
-   sock_handler_printf(sh, 0,
+   a_Dpip_dsh_printf(sh, 0,
       "<hr>\n"
       "<table width='100%%' border='1' bgcolor='burlywood'><tr><td>\n"
       "<big><em>Dialog question:</em> Do you want to see the hello page?<br>\n"
@@ -110,7 +119,7 @@
 
    /* Show the dpip tag we received */
    esc_tag = Escape_html_str(dpip_tag);
-   sock_handler_printf(sh, 0,
+   a_Dpip_dsh_printf(sh, 0,
       "<h3>dpip tag received:</h3>\n"
       "<pre>\n%s</pre>\n"
       "<br><small>(<b>dpip:</b> dpi protocol)</small><br><br><br>\n",
@@ -130,15 +139,15 @@
          return EXIT_FAILURE;
       }
 
-      sock_handler_printf(sh, 0, "<h3>date:</h3>\n");
-      sock_handler_printf(sh, 0, "<pre>\n");
+      a_Dpip_dsh_write_str(sh, 0, "<h3>date:</h3>\n");
+      a_Dpip_dsh_write_str(sh, 0, "<pre>\n");
 
       /* Read/Write */
       while ((n = fread (buf, 1, 4096, in_stream)) > 0) {
-         sock_handler_write(sh, 0, buf, n);
+         a_Dpip_dsh_write(sh, 0, buf, n);
       }
 
-      sock_handler_printf(sh, 0, "</pre>\n");
+      a_Dpip_dsh_write_str(sh, 0, "</pre>\n");
 
       if ((ret = pclose(in_stream)) != 0)
          MSG("popen: [%d]\n", ret);
@@ -146,15 +155,15 @@
       dFree(child_cmd);
    }
 
-   sock_handler_printf(sh, 1, "</body></html>\n");
+   a_Dpip_dsh_write_str(sh, 1, "</body></html>\n");
 
    dFree(cmd);
    dFree(url);
    dFree(dpip_tag);
 
    /* Finish the SockHandler */
-   sock_handler_close(sh);
-   sock_handler_free(sh);
+   a_Dpip_dsh_close(sh);
+   a_Dpip_dsh_free(sh);
 
    return 0;
 }