annotate dpi/vsource.c @ 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 96e28c8697ad
children 404899a37226
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 {
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
38 a_Dpip_dsh_printf(sh, 0, "\nDpip tag received: ");
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
39 a_Dpip_dsh_printf(sh, 0, dpip_tag ? dpip_tag : "None");
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
40 a_Dpip_dsh_printf(sh, 1, "\n\n");
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 */
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
52 a_Dpip_dsh_printf(sh, 0, "Content-type: text/plain\n\n");
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 */
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
71 a_Dpip_dsh_printf(sh, 0, "Content-type: text/plain\n\n");
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 */
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
99 void send_html_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
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
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
104 /* Send HTTP header for plain text MIME type */
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
105 a_Dpip_dsh_printf(sh, 0, "Content-type: text/html\n\n");
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
106
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
107 a_Dpip_dsh_printf(sh, 0, DOCTYPE);
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
108 a_Dpip_dsh_printf(sh, 0,
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
109 "<html><body>\n<table width='100%%' cellpadding='0'>\n");
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
110
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
111 while (bytes_read < data_size &&
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
112 (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
113 bytes_read += strlen(src_str);
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
114 p = q = src_str;
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
115
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
116 while (*p) {
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
117 if (line > old_line) {
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
118 snprintf(line_str, 128,
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
119 "<tr><td bgcolor='%s'>%d&nbsp;<td>",
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
120 (line & 1) ? "#B87333" : "#DD7F32", line);
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
121 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
122 old_line = line;
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
123 }
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
124 if ((p = strpbrk(q, "\n<"))) {
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
125 if (*p == '\n') {
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
126 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
127 if (p[1] == '\r')
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
128 ++p;
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
129 ++line;
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
130 } else {
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
131 a_Dpip_dsh_write(sh, 0, q, p - q);
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, "&lt;");
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
133 }
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
134 } else {
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
135 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
136 break;
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
137 }
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
138 q = ++p;
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
139 }
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
140 dFree(src_str);
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
141 }
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
142
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
143 a_Dpip_dsh_printf(sh, 1, "</table></body></html>");
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
144 }
1568
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
145
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
146 /*
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
147 *
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
148 */
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
149 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
150 {
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
151 Dsh *sh;
1570
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
152 int data_size;
1568
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
153 char *dpip_tag, *cmd = 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
154 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
155
1570
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
156 _MSG("starting...\n");
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
157 //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
158
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
159 /* 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
160 * 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
161 * 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
162 * 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
163 * (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
164 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
165
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
166 /* 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
167 * 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
168 * 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
169 * 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
170 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
171 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
172 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
173 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
174 return 1;
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
175 }
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
176 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
177
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
178 /* 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
179 * 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
180 * 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
181 * 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
182 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
183 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
184
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
185 /* 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
186 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
187 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
188
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
189 /* 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
190 * (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
191 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
192 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
193 dFree(d_cmd);
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
194
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 size_str = a_Dpip_get_attr(dpip_tag, "data_size");
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
197 data_size = strtol(size_str, NULL, 10);
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
198
1570
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
199 /* Choose your flavour */
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
200 //send_plain_text(sh, data_size);
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
201 //send_numbered_text(sh, data_size);
f275bd2cdeed Implement the view source functionality as a dpi
Jorge Arellano Cid <jcid@dillo.org>
parents: 1568
diff changeset
202 send_html_text(sh, data_size);
1568
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
203
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
204 dFree(cmd);
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
205 dFree(url);
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
206 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
207 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
208
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
209 /* 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
210 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
211 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
212
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
213 return 0;
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
214 }
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
215