changeset 1570:f275bd2cdeed

Implement the view source functionality as a dpi
author Jorge Arellano Cid <jcid@dillo.org>
date Wed, 17 Feb 2010 13:55:26 -0300
parents 24a5d37b09e1
children d0cbd6d81205
files dpi/vsource.c src/capi.c src/capi.h src/nav.c src/nav.h src/uicmd.cc
diffstat 6 files changed, 167 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/dpi/vsource.c	Wed Feb 17 13:54:58 2010 -0300
+++ b/dpi/vsource.c	Wed Feb 17 13:55:26 2010 -0300
@@ -29,6 +29,119 @@
 
 /*---------------------------------------------------------------------------*/
 
+const char *DOCTYPE=
+ "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>";
+
+
+void send_dpip_tag(Dsh *sh, char *dpip_tag)
+{
+   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");
+}
+
+/*
+ * Send source as plain text
+ */
+void send_plain_text(Dsh *sh, int data_size)
+{
+   int bytes_read = 0;
+   char *src_str;
+
+   /* Send HTTP header for plain text MIME type */
+   a_Dpip_dsh_printf(sh, 0, "Content-type: text/plain\n\n");
+
+   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, 1, src_str);
+      dFree(src_str);
+   }
+}
+
+/*
+ * Send source as plain text with line numbers
+ */
+void send_numbered_text(Dsh *sh, int data_size)
+{
+   int bytes_read = 0, line = 1;
+   char *p, *q, *src_str, line_str[32];
+
+   /* Send HTTP header for plain text MIME type */
+   a_Dpip_dsh_printf(sh, 0, "Content-type: text/plain\n\n");
+
+   while (bytes_read < data_size &&
+          (src_str = a_Dpip_dsh_read_token(sh, 1))) {
+      bytes_read += strlen(src_str);
+      p = q = src_str;
+
+      while (*p) {
+         snprintf(line_str, 32, "%2d: ", line);
+         a_Dpip_dsh_write_str(sh, 0, line_str);
+         if ((p = strchr(q, '\n'))) {
+            a_Dpip_dsh_write(sh, 0, q, p - q + 1);
+            if (p[1] == '\r')
+               ++p;
+            ++line;
+         } else {
+            a_Dpip_dsh_write_str(sh, 1, q);
+            break;
+         }
+         q = ++p;
+      }
+      dFree(src_str);
+   }
+}
+
+/*
+ * Send source as html text with line numbers
+ */
+void send_html_text(Dsh *sh, int data_size)
+{
+   int bytes_read = 0, old_line = 0, line = 1;
+   char *p, *q, *src_str, line_str[128];
+
+   /* Send HTTP header for plain text MIME type */
+   a_Dpip_dsh_printf(sh, 0, "Content-type: text/html\n\n");
+
+   a_Dpip_dsh_printf(sh, 0, DOCTYPE);
+   a_Dpip_dsh_printf(sh, 0, 
+                     "<html><body>\n<table width='100%%' cellpadding='0'>\n");
+
+   while (bytes_read < data_size &&
+          (src_str = a_Dpip_dsh_read_token(sh, 1))) {
+      bytes_read += strlen(src_str);
+      p = q = src_str;
+
+      while (*p) {
+         if (line > old_line) {
+            snprintf(line_str, 128, 
+                     "<tr><td bgcolor='%s'>%d&nbsp;<td>",
+                     (line & 1) ? "#B87333" : "#DD7F32", line);
+            a_Dpip_dsh_write_str(sh, 0, line_str);
+            old_line = line;
+         }
+         if ((p = strpbrk(q, "\n<"))) {
+            if (*p == '\n') {
+               a_Dpip_dsh_write(sh, 0, q, p - q + 1);
+               if (p[1] == '\r')
+                  ++p;
+               ++line;
+            } else {
+               a_Dpip_dsh_write(sh, 0, q, p - q);
+               a_Dpip_dsh_write_str(sh, 0, "&lt;");
+            }
+         } else {
+            a_Dpip_dsh_write_str(sh, 1, q);
+            break;
+         }
+         q = ++p;
+      }
+      dFree(src_str);
+   }
+
+   a_Dpip_dsh_printf(sh, 1, "</table></body></html>");
+}
 
 /*
  *
@@ -36,12 +149,12 @@
 int main(void)
 {
    Dsh *sh;
-   int data_size, bytes_read = 0;
+   int data_size;
    char *dpip_tag, *cmd = NULL, *url = NULL, *size_str = NULL;
-   char *d_cmd, *src_str;
+   char *d_cmd;
 
-   MSG("starting...\n");
-   /* sleep(20) */
+   _MSG("starting...\n");
+   //sleep(20);
 
    /* Initialize the SockHandler.
     * This means we'll use stdin for input and stdout for output.
@@ -79,32 +192,14 @@
    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);
-   }
+   /* Choose your flavour */
+   //send_plain_text(sh, data_size);
+   //send_numbered_text(sh, data_size);
+   send_html_text(sh, data_size);
 
    dFree(cmd);
    dFree(url);
--- a/src/capi.c	Wed Feb 17 13:54:58 2010 -0300
+++ b/src/capi.c	Wed Feb 17 13:55:26 2010 -0300
@@ -85,7 +85,7 @@
  * Create a new connection data structure
  */
 static capi_conn_t *
- Capi_conn_new(DilloUrl *url, void *bw, char *server, char *datastr)
+ Capi_conn_new(const DilloUrl *url, void *bw, char *server, char *datastr)
 {
    capi_conn_t *conn;
 
@@ -478,8 +478,8 @@
  * Most of the time we send dpi commands, but it also serves for raw data
  * as with "view source".
  */
-int a_Capi_dpi_send_data(DilloUrl *url, void *bw, char *data, int data_sz,
-                         char *server, int flags)
+int a_Capi_dpi_send_data(const DilloUrl *url, void *bw, 
+                         char *data, int data_sz, char *server, int flags)
 {
    capi_conn_t *conn;
    DataBuf *dbuf;
@@ -502,7 +502,7 @@
          a_Capi_ccc(OpSend, 1, BCK, conn->InfoSend, dbuf, NULL);
          dFree(dbuf);
       } else {
-         MSG(" ERROR: [a_Capi_dpi_send_cmd] No open connection found\n");
+         MSG(" ERROR: [a_Capi_dpi_send_data] No open connection found\n");
       }
    }
 
@@ -520,6 +520,22 @@
 }
 
 /*
+ * Send this url's source to the "view source" dpi
+ */
+void a_Capi_dpi_send_source(BrowserWindow *bw, const DilloUrl *url,
+                            char *buf, int buf_size)
+{
+   char *cmd, size_str[32];
+
+   /* send the page's source to this dpi connection */
+   snprintf(size_str, 32, "%d", buf_size);
+   cmd = a_Dpip_build_cmd("cmd=%s url=%s data_size=%s",
+                          "start_send_page", URL_STR(url), size_str);
+   a_Capi_dpi_send_cmd(NULL, bw, cmd, "vsource", 0);
+   a_Capi_dpi_send_data(url, bw, buf, buf_size, "vsource", 0);
+}
+
+/*
  * Remove a client from the cache client queue.
  * force = also abort the CCC if this is the last client.
  */
--- a/src/capi.h	Wed Feb 17 13:54:58 2010 -0300
+++ b/src/capi.h	Wed Feb 17 13:55:26 2010 -0300
@@ -31,10 +31,12 @@
 int a_Capi_get_flags(const DilloUrl *Url);
 int a_Capi_get_flags_with_redirection(const DilloUrl *Url);
 int a_Capi_dpi_verify_request(BrowserWindow *bw, DilloUrl *url);
-int a_Capi_dpi_send_data(DilloUrl *url, void *bw, char *data, int data_sz,
-                         char *server, int flags);
+int a_Capi_dpi_send_data(const DilloUrl *url, void *bw, 
+                         char *data, int data_sz, char *server, int flags);
 int a_Capi_dpi_send_cmd(DilloUrl *url, void *bw, char *cmd, char *server,
                          int flags);
+void a_Capi_dpi_send_source(BrowserWindow *bw, const DilloUrl *url,
+                            char *buf, int buf_size);
 void a_Capi_stop_client(int Key, int force);
 void a_Capi_conn_abort_by_url(const DilloUrl *url);
 
--- a/src/nav.c	Wed Feb 17 13:54:58 2010 -0300
+++ b/src/nav.c	Wed Feb 17 13:55:26 2010 -0300
@@ -583,3 +583,23 @@
 {
    a_Capi_unref_buf(Url);
 }
+
+/*
+ * Send source to a dpi
+ */
+void a_Nav_send_source(BrowserWindow *bw, const DilloUrl *url)
+{
+   char *buf;
+   int buf_size;
+   DilloUrl *vs_url;
+
+   if (a_Nav_get_buf(url, &buf, &buf_size)) {
+      vs_url = a_Url_new("dpi:/vsource/", NULL);
+      a_UIcmd_open_url_nt(bw, vs_url, 1);
+      a_Url_free(vs_url);
+
+      /* send the page's source to this dpi connection */
+      a_Capi_dpi_send_source(bw, url, buf, buf_size);
+      a_Nav_unref_buf(url);
+   }
+}
--- a/src/nav.h	Wed Feb 17 13:54:58 2010 -0300
+++ b/src/nav.h	Wed Feb 17 13:55:26 2010 -0300
@@ -34,6 +34,7 @@
                     const DilloUrl *url, const char *filename);
 int a_Nav_get_buf(const DilloUrl *Url, char **PBuf, int *BufSize);
 void a_Nav_unref_buf(const DilloUrl *Url);
+void a_Nav_send_source(BrowserWindow *bw, const DilloUrl *url);
 
 #ifdef __cplusplus
 }
--- a/src/uicmd.cc	Wed Feb 17 13:54:58 2010 -0300
+++ b/src/uicmd.cc	Wed Feb 17 13:55:26 2010 -0300
@@ -1002,11 +1002,7 @@
  */
 void a_UIcmd_view_page_source(BrowserWindow *bw, const DilloUrl *url)
 {
-   DilloUrl *vs_url;
-
-   vs_url = a_Url_new("dpi:/vsource/", NULL);
-   a_UIcmd_open_url_nt(bw, vs_url, 1);
-   a_Url_free(vs_url);
+   a_Nav_send_source(bw, url);
 }
 
 /*