annotate dpi/vsource.c @ 2048:5060d415a85a

clickable menu items (even those introducing submenus) MUST have callbacks I clicked on the "Panel size" item itself instead of any of the options in its submenu, and: Segfault!
author corvid <corvid@lavabit.com>
date Thu, 26 May 2011 02:51:18 +0000
parents b974e9823aa1
children
rev   line source
1568
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
1 /*
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
2 * Dpi for "View source".
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
3 *
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
4 * This server is an example. Play with it and modify to your taste.
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
5 *
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
6 * Copyright 2010 Jorge Arellano Cid <jcid@dillo.org>
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
7 *
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
8 * This program is free software; you can redistribute it and/or modify
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
9 * it under the terms of the GNU General Public License as published by
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
10 * the Free Software Foundation; either version 3 of the License, or
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
11 * (at your option) any later version.
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
12 *
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
13 */
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
14
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
15 #include <unistd.h>
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
16 #include <sys/types.h>
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
17 #include <stdio.h>
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
18 #include <stdlib.h>
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
19 #include <string.h>
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
20 #include <errno.h>
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
21 #include "../dpip/dpip.h"
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
22 #include "dpiutil.h"
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
23
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
24 /*
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
25 * Debugging macros
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
26 */
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
27 #define _MSG(...)
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
28 #define MSG(...) printf("[vsource dpi]: " __VA_ARGS__)
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
29
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
30 /*---------------------------------------------------------------------------*/
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
31
1570
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
32 const char *DOCTYPE=
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
33 "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>";
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
34
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
35
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
36 void send_dpip_tag(Dsh *sh, char *dpip_tag)
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
37 {
1679
b974e9823aa1 vsource use a_Dpip_dsh_write_str when possible
corvid <corvid@lavabit.com>
parents: 1678
diff changeset
38 a_Dpip_dsh_write_str(sh, 0, "\nDpip tag received: ");
b974e9823aa1 vsource use a_Dpip_dsh_write_str when possible
corvid <corvid@lavabit.com>
parents: 1678
diff changeset
39 a_Dpip_dsh_write_str(sh, 0, dpip_tag ? dpip_tag : "None");
b974e9823aa1 vsource use a_Dpip_dsh_write_str when possible
corvid <corvid@lavabit.com>
parents: 1678
diff changeset
40 a_Dpip_dsh_write_str(sh, 1, "\n\n");
1570
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
41 }
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
42
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
43 /*
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
44 * Send source as plain text
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
45 */
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
46 void send_plain_text(Dsh *sh, int data_size)
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
47 {
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
48 int bytes_read = 0;
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
49 char *src_str;
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
50
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
51 /* Send HTTP header for plain text MIME type */
1679
b974e9823aa1 vsource use a_Dpip_dsh_write_str when possible
corvid <corvid@lavabit.com>
parents: 1678
diff changeset
52 a_Dpip_dsh_write_str(sh, 0, "Content-type: text/plain\n\n");
1570
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
53
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
54 while (bytes_read < data_size &&
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
55 (src_str = a_Dpip_dsh_read_token(sh, 1))) {
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
56 bytes_read += strlen(src_str);
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
57 a_Dpip_dsh_write_str(sh, 1, src_str);
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
58 dFree(src_str);
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
59 }
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
60 }
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
61
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
62 /*
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
63 * Send source as plain text with line numbers
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
64 */
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
65 void send_numbered_text(Dsh *sh, int data_size)
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
66 {
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
67 int bytes_read = 0, line = 1;
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
68 char *p, *q, *src_str, line_str[32];
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
69
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
70 /* Send HTTP header for plain text MIME type */
1679
b974e9823aa1 vsource use a_Dpip_dsh_write_str when possible
corvid <corvid@lavabit.com>
parents: 1678
diff changeset
71 a_Dpip_dsh_write_str(sh, 0, "Content-type: text/plain\n\n");
1570
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
72
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
73 while (bytes_read < data_size &&
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
74 (src_str = a_Dpip_dsh_read_token(sh, 1))) {
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
75 bytes_read += strlen(src_str);
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
76 p = q = src_str;
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
77
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
78 while (*p) {
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
79 snprintf(line_str, 32, "%2d: ", line);
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
80 a_Dpip_dsh_write_str(sh, 0, line_str);
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
81 if ((p = strchr(q, '\n'))) {
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
82 a_Dpip_dsh_write(sh, 0, q, p - q + 1);
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
83 if (p[1] == '\r')
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
84 ++p;
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
85 ++line;
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
86 } else {
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
87 a_Dpip_dsh_write_str(sh, 1, q);
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
88 break;
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
89 }
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
90 q = ++p;
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
91 }
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
92 dFree(src_str);
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
93 }
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
94 }
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
95
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
96 /*
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
97 * Send source as html text with line numbers
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
98 */
1678
2b3cba462b23 vsource page add title element
corvid <corvid@lavabit.com>
parents: 1677
diff changeset
99 void send_html_text(Dsh *sh, const char *url, int data_size)
1570
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
100 {
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
101 int bytes_read = 0, old_line = 0, line = 1;
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
102 char *p, *q, *src_str, line_str[128];
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
103
1678
2b3cba462b23 vsource page add title element
corvid <corvid@lavabit.com>
parents: 1677
diff changeset
104 if (strncmp(url, "dpi:/vsource/:", 14) == 0)
2b3cba462b23 vsource page add title element
corvid <corvid@lavabit.com>
parents: 1677
diff changeset
105 url += 14;
2b3cba462b23 vsource page add title element
corvid <corvid@lavabit.com>
parents: 1677
diff changeset
106
1570
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
107 /* Send HTTP header for plain text MIME type */
1679
b974e9823aa1 vsource use a_Dpip_dsh_write_str when possible
corvid <corvid@lavabit.com>
parents: 1678
diff changeset
108 a_Dpip_dsh_write_str(sh, 0, "Content-type: text/html\n\n");
1570
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
109
1679
b974e9823aa1 vsource use a_Dpip_dsh_write_str when possible
corvid <corvid@lavabit.com>
parents: 1678
diff changeset
110 a_Dpip_dsh_write_str(sh, 0, DOCTYPE);
1591
cf7f2d3312fb trim some spaces
corvid <corvid@lavabit.com>
parents: 1578
diff changeset
111 a_Dpip_dsh_printf(sh, 0,
1640
ac1a5c7d4c9b use pre-wrap in vsource
corvid <corvid@lavabit.com>
parents: 1619
diff changeset
112 "\n"
ac1a5c7d4c9b use pre-wrap in vsource
corvid <corvid@lavabit.com>
parents: 1619
diff changeset
113 "<html><head>\n"
1678
2b3cba462b23 vsource page add title element
corvid <corvid@lavabit.com>
parents: 1677
diff changeset
114 "<title>Source for %s</title>\n"
1640
ac1a5c7d4c9b use pre-wrap in vsource
corvid <corvid@lavabit.com>
parents: 1619
diff changeset
115 "<style type=\"text/css\">PRE {white-space: pre-wrap}\n"
ac1a5c7d4c9b use pre-wrap in vsource
corvid <corvid@lavabit.com>
parents: 1619
diff changeset
116 "</style>\n"
1677
2adf63a0999b vsource fix extraneous </pre> with empty source file
corvid <corvid@lavabit.com>
parents: 1643
diff changeset
117 "</head>\n"
1678
2b3cba462b23 vsource page add title element
corvid <corvid@lavabit.com>
parents: 1677
diff changeset
118 "<body id=\"dillo_vs\">\n<table cellpadding='0'>\n", url);
1570
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
119
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
120 while (bytes_read < data_size &&
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
121 (src_str = a_Dpip_dsh_read_token(sh, 1))) {
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
122 bytes_read += strlen(src_str);
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
123 p = q = src_str;
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
124
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
125 while (*p) {
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
126 if (line > old_line) {
1591
cf7f2d3312fb trim some spaces
corvid <corvid@lavabit.com>
parents: 1578
diff changeset
127 snprintf(line_str, 128,
1578
31b53c47cf74 Fix: vsource html, fix entities display, indentation and removed \r check.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1577
diff changeset
128 "%s<tr><td bgcolor='%s'>%d%s<td><pre>",
31b53c47cf74 Fix: vsource html, fix entities display, indentation and removed \r check.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1577
diff changeset
129 (line > 1) ? "</pre>" : "",
31b53c47cf74 Fix: vsource html, fix entities display, indentation and removed \r check.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1577
diff changeset
130 (line & 1) ? "#B87333" : "#DD7F32", line,
31b53c47cf74 Fix: vsource html, fix entities display, indentation and removed \r check.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1577
diff changeset
131 (line == 1 || (line % 10) == 0) ? "&nbsp;&nbsp;" : "");
1570
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
132 a_Dpip_dsh_write_str(sh, 0, line_str);
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
133 old_line = line;
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
134 }
1602
3d9a1b463163 view source: CR separates lines
corvid <corvid@lavabit.com>
parents: 1596
diff changeset
135 if ((p = strpbrk(q, "\r\n<&"))) {
3d9a1b463163 view source: CR separates lines
corvid <corvid@lavabit.com>
parents: 1596
diff changeset
136 if (*p == '\r' || *p == '\n') {
1570
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
137 a_Dpip_dsh_write(sh, 0, q, p - q + 1);
1602
3d9a1b463163 view source: CR separates lines
corvid <corvid@lavabit.com>
parents: 1596
diff changeset
138 if (*p == '\r' && p[1] == '\n')
3d9a1b463163 view source: CR separates lines
corvid <corvid@lavabit.com>
parents: 1596
diff changeset
139 p++;
1570
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
140 ++line;
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
141 } else {
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
142 a_Dpip_dsh_write(sh, 0, q, p - q);
1578
31b53c47cf74 Fix: vsource html, fix entities display, indentation and removed \r check.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1577
diff changeset
143 a_Dpip_dsh_write_str(sh, 0, (*p == '<') ? "&lt;" : "&amp;");
1570
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
144 }
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
145 } else {
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
146 a_Dpip_dsh_write_str(sh, 1, q);
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
147 break;
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
148 }
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
149 q = ++p;
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
150 }
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
151 dFree(src_str);
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
152 }
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
153
1677
2adf63a0999b vsource fix extraneous </pre> with empty source file
corvid <corvid@lavabit.com>
parents: 1643
diff changeset
154 if (data_size > 0)
1679
b974e9823aa1 vsource use a_Dpip_dsh_write_str when possible
corvid <corvid@lavabit.com>
parents: 1678
diff changeset
155 a_Dpip_dsh_write_str(sh, 0, "</pre>");
b974e9823aa1 vsource use a_Dpip_dsh_write_str when possible
corvid <corvid@lavabit.com>
parents: 1678
diff changeset
156 a_Dpip_dsh_write_str(sh, 1, "</table></body></html>");
1570
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
157 }
1568
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
158
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
159 /*
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
160 *
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
161 */
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
162 int main(void)
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
163 {
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
164 Dsh *sh;
1570
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
165 int data_size;
1577
404899a37226 Handle vsource dpi inside a_Capi_open_url(); more orthogonality.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1570
diff changeset
166 char *dpip_tag, *cmd = NULL, *cmd2 = NULL, *url = NULL, *size_str = NULL;
1570
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
167 char *d_cmd;
1568
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
168
1570
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
169 _MSG("starting...\n");
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
170 //sleep(20);
1568
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
171
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
172 /* Initialize the SockHandler.
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
173 * This means we'll use stdin for input and stdout for output.
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
174 * In case of a server dpi, we'd use a socket and pass its file descriptor
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
175 * twice (e.g. a_Dpip_dsh_new(sock_fd, sock_fd, 1024).
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
176 * (Note: by now the last parameter is not used) */
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
177 sh = a_Dpip_dsh_new(STDIN_FILENO, STDOUT_FILENO, 2*1024);
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
178
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
179 /* Authenticate our client...
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
180 * As we're using Internet domain sockets, DPIP checks whether the client
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
181 * runs with the user's ID, by means of a shared secret. The DPIP API does
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
182 * the work for us. */
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
183 if (!(dpip_tag = a_Dpip_dsh_read_token(sh, 1)) ||
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
184 a_Dpip_check_auth(dpip_tag) < 0) {
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
185 MSG("can't authenticate request: %s\n", dStrerror(errno));
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
186 a_Dpip_dsh_close(sh);
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
187 return 1;
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
188 }
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
189 dFree(dpip_tag);
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
190
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
191 /* Read the dpi command from STDIN
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
192 * Now we're past the authentication phase, let's see what's dillo
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
193 * asking from us. a_Dpip_dsh_read_token() will block and return
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
194 * a full dpip token or null on error (it's commented in dpip.c) */
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
195 dpip_tag = a_Dpip_dsh_read_token(sh, 1);
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
196 MSG("tag = [%s]\n", dpip_tag);
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
197
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
198 /* Now that we have the dpip_tag, let's isolate the command and url */
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
199 cmd = a_Dpip_get_attr(dpip_tag, "cmd");
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
200 url = a_Dpip_get_attr(dpip_tag, "url");
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
201
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
202 /* Start sending our answer.
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
203 * (You can read the comments for DPIP API functions in dpip/dpip.c) */
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
204 d_cmd = a_Dpip_build_cmd("cmd=%s url=%s", "start_send_page", url);
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
205 a_Dpip_dsh_write_str(sh, 0, d_cmd);
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
206 dFree(d_cmd);
1596
c4502225d4de free dpip_tag
corvid <corvid@lavabit.com>
parents: 1591
diff changeset
207 dFree(dpip_tag);
1568
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
208
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
209 dpip_tag = a_Dpip_dsh_read_token(sh, 1);
1577
404899a37226 Handle vsource dpi inside a_Capi_open_url(); more orthogonality.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1570
diff changeset
210 cmd2 = a_Dpip_get_attr(dpip_tag, "cmd");
404899a37226 Handle vsource dpi inside a_Capi_open_url(); more orthogonality.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1570
diff changeset
211 if (cmd2) {
404899a37226 Handle vsource dpi inside a_Capi_open_url(); more orthogonality.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1570
diff changeset
212 if (strcmp(cmd2, "start_send_page") == 0 &&
404899a37226 Handle vsource dpi inside a_Capi_open_url(); more orthogonality.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1570
diff changeset
213 (size_str = a_Dpip_get_attr(dpip_tag, "data_size"))) {
404899a37226 Handle vsource dpi inside a_Capi_open_url(); more orthogonality.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1570
diff changeset
214 data_size = strtol(size_str, NULL, 10);
404899a37226 Handle vsource dpi inside a_Capi_open_url(); more orthogonality.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1570
diff changeset
215 /* Choose your flavour */
404899a37226 Handle vsource dpi inside a_Capi_open_url(); more orthogonality.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1570
diff changeset
216 //send_plain_text(sh, data_size);
404899a37226 Handle vsource dpi inside a_Capi_open_url(); more orthogonality.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1570
diff changeset
217 //send_numbered_text(sh, data_size);
1678
2b3cba462b23 vsource page add title element
corvid <corvid@lavabit.com>
parents: 1677
diff changeset
218 send_html_text(sh, url, data_size);
1577
404899a37226 Handle vsource dpi inside a_Capi_open_url(); more orthogonality.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1570
diff changeset
219 } else if (strcmp(cmd2, "DpiError") == 0) {
404899a37226 Handle vsource dpi inside a_Capi_open_url(); more orthogonality.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1570
diff changeset
220 /* Dillo detected an error (other failures just close the socket) */
1679
b974e9823aa1 vsource use a_Dpip_dsh_write_str when possible
corvid <corvid@lavabit.com>
parents: 1678
diff changeset
221 a_Dpip_dsh_write_str(sh, 0, "Content-type: text/plain\n\n");
b974e9823aa1 vsource use a_Dpip_dsh_write_str when possible
corvid <corvid@lavabit.com>
parents: 1678
diff changeset
222 a_Dpip_dsh_write_str(sh, 1, "[vsource dpi]: "
b974e9823aa1 vsource use a_Dpip_dsh_write_str when possible
corvid <corvid@lavabit.com>
parents: 1678
diff changeset
223 "ERROR: Page not cached.\n");
1577
404899a37226 Handle vsource dpi inside a_Capi_open_url(); more orthogonality.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1570
diff changeset
224 }
1619
83a52e3a7ef2 fix attr leak
corvid <corvid@lavabit.com>
parents: 1602
diff changeset
225 dFree(cmd2);
1577
404899a37226 Handle vsource dpi inside a_Capi_open_url(); more orthogonality.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1570
diff changeset
226 }
1568
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
227
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
228 dFree(cmd);
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
229 dFree(url);
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
230 dFree(size_str);
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
231 dFree(dpip_tag);
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
232
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
233 /* Finish the SockHandler */
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
234 a_Dpip_dsh_close(sh);
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
235 a_Dpip_dsh_free(sh);
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
236
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
237 return 0;
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
238 }
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
239