diff options
author | Jorge Arellano Cid <jcid@dillo.org> | 2010-02-17 13:55:26 -0300 |
---|---|---|
committer | Jorge Arellano Cid <jcid@dillo.org> | 2010-02-17 13:55:26 -0300 |
commit | 9ec7bf0cf9413566ebb50fd5a3d388cbfaaaa013 (patch) | |
tree | 410dc4d5b71aeca4d0a0aac28f79a2dcb21c3af7 | |
parent | f8d6d864ed3b9bd82564b73ef264bd834f2e1bf2 (diff) |
Implement the view source functionality as a dpi
-rw-r--r-- | dpi/vsource.c | 147 | ||||
-rw-r--r-- | src/capi.c | 24 | ||||
-rw-r--r-- | src/capi.h | 6 | ||||
-rw-r--r-- | src/nav.c | 20 | ||||
-rw-r--r-- | src/nav.h | 1 | ||||
-rw-r--r-- | src/uicmd.cc | 6 |
6 files changed, 167 insertions, 37 deletions
diff --git a/dpi/vsource.c b/dpi/vsource.c index aa132722..e7c89ab2 100644 --- a/dpi/vsource.c +++ b/dpi/vsource.c @@ -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 <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, "<"); + } + } 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 @@ int main(void) 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); @@ -85,7 +85,7 @@ void a_Capi_init(void) * 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 @@ const char *a_Capi_set_content_type(const DilloUrl *url, const char *ctype, * 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 @@ int a_Capi_dpi_send_data(DilloUrl *url, void *bw, char *data, int data_sz, 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 @@ int a_Capi_dpi_send_cmd(DilloUrl *url, void *bw, char *cmd, char *server, } /* + * 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. */ @@ -31,10 +31,12 @@ const char *a_Capi_set_content_type(const DilloUrl *url, const char *ctype, 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); @@ -583,3 +583,23 @@ void a_Nav_unref_buf(const DilloUrl *Url) { 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); + } +} @@ -34,6 +34,7 @@ void a_Nav_save_url(BrowserWindow *bw, 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 } diff --git a/src/uicmd.cc b/src/uicmd.cc index 3bf7ddd8..b57e456f 100644 --- a/src/uicmd.cc +++ b/src/uicmd.cc @@ -1002,11 +1002,7 @@ void a_UIcmd_copy_urlstr(BrowserWindow *bw, const char *urlstr) */ 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); } /* |