changeset 1568:96e28c8697ad

Copy the hello dpi as a basis for the "view source" dpi
author Jorge Arellano Cid <jcid@dillo.org>
date Wed, 17 Feb 2010 13:53:36 -0300
parents cb334b6576d4
children 24a5d37b09e1
files dpi/Makefile.am dpi/vsource.c
diffstat 2 files changed, 124 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/dpi/Makefile.am	Tue Feb 16 11:54:18 2010 -0300
+++ b/dpi/Makefile.am	Wed Feb 17 13:53:36 2010 -0300
@@ -3,6 +3,7 @@
 ftpdir = $(libdir)/dillo/dpi/ftp
 httpsdir = $(libdir)/dillo/dpi/https
 hellodir = $(libdir)/dillo/dpi/hello
+vsourcedir = $(libdir)/dillo/dpi/vsource
 filedir = $(libdir)/dillo/dpi/file
 cookiesdir = $(libdir)/dillo/dpi/cookies
 datauridir = $(libdir)/dillo/dpi/datauri
@@ -11,6 +12,7 @@
 ftp_PROGRAMS = ftp.filter.dpi
 https_PROGRAMS = https.filter.dpi
 hello_PROGRAMS = hello.filter.dpi
+vsource_PROGRAMS = vsource.filter.dpi
 file_PROGRAMS = file.dpi
 cookies_PROGRAMS = cookies.dpi
 datauri_PROGRAMS = datauri.filter.dpi
@@ -20,6 +22,7 @@
 ftp_filter_dpi_LDADD = ../dpip/libDpip.a ../dlib/libDlib.a
 https_filter_dpi_LDADD = @LIBSSL_LIBS@ ../dpip/libDpip.a ../dlib/libDlib.a
 hello_filter_dpi_LDADD = ../dpip/libDpip.a ../dlib/libDlib.a
+vsource_filter_dpi_LDADD = ../dpip/libDpip.a ../dlib/libDlib.a
 file_dpi_LDADD = ../dpip/libDpip.a ../dlib/libDlib.a
 cookies_dpi_LDADD = ../dpip/libDpip.a ../dlib/libDlib.a
 datauri_filter_dpi_LDADD = ../dpip/libDpip.a ../dlib/libDlib.a
@@ -31,6 +34,7 @@
 ftp_filter_dpi_SOURCES = ftp.c dpiutil.c dpiutil.h
 https_filter_dpi_SOURCES = https.c dpiutil.c dpiutil.h
 hello_filter_dpi_SOURCES = hello.c dpiutil.c dpiutil.h
+vsource_filter_dpi_SOURCES = vsource.c dpiutil.c dpiutil.h
 file_dpi_SOURCES = file.c dpiutil.c dpiutil.h
 cookies_dpi_SOURCES = cookies.c dpiutil.c dpiutil.h
 datauri_filter_dpi_SOURCES = datauri.c dpiutil.c dpiutil.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dpi/vsource.c	Wed Feb 17 13:53:36 2010 -0300
@@ -0,0 +1,120 @@
+/*
+ * Dpi for "View source".
+ *
+ * This server is an example. Play with it and modify to your taste.
+ *
+ * Copyright 2010 Jorge Arellano Cid <jcid@dillo.org>
+ *
+ * This program 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.
+ *
+ */
+
+#include <unistd.h>
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include "../dpip/dpip.h"
+#include "dpiutil.h"
+
+/*
+ * Debugging macros
+ */
+#define _MSG(...)
+#define MSG(...)  printf("[vsource dpi]: " __VA_ARGS__)
+
+/*---------------------------------------------------------------------------*/
+
+
+/*
+ *
+ */
+int main(void)
+{
+   Dsh *sh;
+   int data_size, bytes_read = 0;
+   char *dpip_tag, *cmd = NULL, *url = NULL, *size_str = NULL;
+   char *d_cmd, *src_str;
+
+   MSG("starting...\n");
+   /* sleep(20) */
+
+   /* Initialize the SockHandler.
+    * This means we'll use stdin for input and stdout for output.
+    * In case of a server dpi, we'd use a socket and pass its file descriptor
+    * twice (e.g. a_Dpip_dsh_new(sock_fd, sock_fd, 1024).
+    * (Note: by now the last parameter is not used) */
+   sh = a_Dpip_dsh_new(STDIN_FILENO, STDOUT_FILENO, 2*1024);
+
+   /* Authenticate our client...
+    * As we're using Internet domain sockets, DPIP checks whether the client
+    * runs with the user's ID, by means of a shared secret. The DPIP API does
+    * the work for us. */
+   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
+    * Now we're past the authentication phase, let's see what's dillo
+    * asking from us. a_Dpip_dsh_read_token() will block and return
+    * a full dpip token or null on error (it's commented in dpip.c) */
+   dpip_tag = a_Dpip_dsh_read_token(sh, 1);
+   MSG("tag = [%s]\n", dpip_tag);
+
+   /* Now that we have the dpip_tag, let's isolate the command and url */
+   cmd = a_Dpip_get_attr(dpip_tag, "cmd");
+   url = a_Dpip_get_attr(dpip_tag, "url");
+
+   /* Start sending our answer.
+    * (You can read the comments for DPIP API functions in dpip/dpip.c) */
+   d_cmd = a_Dpip_build_cmd("cmd=%s url=%s", "start_send_page", url);
+   a_Dpip_dsh_write_str(sh, 0, d_cmd);
+   dFree(d_cmd);
+
+   a_Dpip_dsh_printf(sh, 0,
+      "Content-type: text/plain\n\n"
+      ".----------------.\n"
+      "|  Hello World!  |\n"
+      "'----------------'\n\n");
+
+   /* Show the dpip tag we received */
+   a_Dpip_dsh_printf(sh, 0, "Dpip tag received: ");
+   a_Dpip_dsh_printf(sh, 0, dpip_tag);
+   dFree(dpip_tag);
+
+   dpip_tag = a_Dpip_dsh_read_token(sh, 1);
+   a_Dpip_dsh_printf(sh, 0, "\nDpip tag received: ");
+   a_Dpip_dsh_printf(sh, 0, dpip_tag ? dpip_tag : "None");
+   a_Dpip_dsh_printf(sh, 1, "\n\n");
+   //dFree(dpip_tag);
+   size_str = a_Dpip_get_attr(dpip_tag, "data_size");
+   data_size = strtol(size_str, NULL, 10);
+
+   while (bytes_read < data_size &&
+          (src_str = a_Dpip_dsh_read_token(sh, 1))) {
+      bytes_read += strlen(src_str);
+      //a_Dpip_dsh_write_str(sh, 0, src_str);
+      a_Dpip_dsh_write_str(sh, 1, src_str);
+      dFree(src_str);
+   }
+
+   dFree(cmd);
+   dFree(url);
+   dFree(size_str);
+   dFree(dpip_tag);
+
+   /* Finish the SockHandler */
+   a_Dpip_dsh_close(sh);
+   a_Dpip_dsh_free(sh);
+
+   return 0;
+}
+