annotate dpi/hello.c @ 1397:a9662d8ada3f

hello dpi: convert to dsh API (with auth)
author Jorge Arellano Cid <jcid@dillo.org>
date Sun, 01 Nov 2009 16:31:59 -0300
parents b912173aecd1
children c2fc4d2510d0
rev   line source
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
2 * Dpi for "Hello World".
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
3 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
4 * This server is an example. Play with it and modify to your taste.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
5 *
35
d9e7b35430de Updated copyright lines
jcid
parents: 0
diff changeset
6 * Copyright 2003-2007 Jorge Arellano Cid <jcid@dillo.org>
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
7 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
8 * This program is free software; you can redistribute it and/or modify
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
9 * it under the terms of the GNU General Public License as published by
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
10 * the Free Software Foundation; either version 3 of the License, or
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
11 * (at your option) any later version.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
12 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
13 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
14
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
15 #include <unistd.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
16 #include <sys/types.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
17 #include <stdio.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
18 #include <stdlib.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
19 #include <string.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
20 #include <errno.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
21 #include "../dpip/dpip.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
22 #include "dpiutil.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
23
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
24 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
25 * Debugging macros
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
26 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
27 #define _MSG(...)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
28 #define MSG(...) printf("[hello dpi]: " __VA_ARGS__)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
29
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
30 /*---------------------------------------------------------------------------*/
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
31
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
32
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
33 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
34 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
35 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
36 int main(void)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
37 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
38 FILE *in_stream;
1397
a9662d8ada3f hello dpi: convert to dsh API (with auth)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
39 Dsh *sh;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
40 char *dpip_tag, *cmd = NULL, *url = NULL, *child_cmd = NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
41 char *esc_tag, *d_cmd;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
42 size_t n;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
43 int ret;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
44 char buf[4096];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
45 char *choice[] = {"Window was closed", "Yes", "No",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
46 "Could be", "It's OK", "Cancel"};
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
47 /* "Could>be", ">It's OK", "Can'>cel"}; --for testing */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
48 int choice_num;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
49
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
50 MSG("starting...\n");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
51
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
52 /* Initialize the SockHandler */
1397
a9662d8ada3f hello dpi: convert to dsh API (with auth)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
53 sh = a_Dpip_dsh_new(STDIN_FILENO, STDOUT_FILENO, 2*1024);
a9662d8ada3f hello dpi: convert to dsh API (with auth)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
54
a9662d8ada3f hello dpi: convert to dsh API (with auth)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
55 /* Authenticate our client... */
a9662d8ada3f hello dpi: convert to dsh API (with auth)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
56 if (!(dpip_tag = a_Dpip_dsh_read_token(sh, 1)) ||
a9662d8ada3f hello dpi: convert to dsh API (with auth)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
57 a_Dpip_check_auth(dpip_tag) < 0) {
a9662d8ada3f hello dpi: convert to dsh API (with auth)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
58 MSG("can't authenticate request: %s\n", dStrerror(errno));
a9662d8ada3f hello dpi: convert to dsh API (with auth)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
59 a_Dpip_dsh_close(sh);
a9662d8ada3f hello dpi: convert to dsh API (with auth)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
60 return 1;
a9662d8ada3f hello dpi: convert to dsh API (with auth)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
61 }
a9662d8ada3f hello dpi: convert to dsh API (with auth)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
62 dFree(dpip_tag);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
63
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
64 /* Read the dpi command from STDIN */
1397
a9662d8ada3f hello dpi: convert to dsh API (with auth)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
65 dpip_tag = a_Dpip_dsh_read_token(sh, 1);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
66 MSG("tag = [%s]\n", dpip_tag);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
67
1236
b912173aecd1 Added a_Dpip_get_attr_l() to DPIP's API
Jorge Arellano Cid <jcid@dillo.org>
parents: 35
diff changeset
68 cmd = a_Dpip_get_attr(dpip_tag, "cmd");
b912173aecd1 Added a_Dpip_get_attr_l() to DPIP's API
Jorge Arellano Cid <jcid@dillo.org>
parents: 35
diff changeset
69 url = a_Dpip_get_attr(dpip_tag, "url");
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
70
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
71 /*-- Dialog part */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
72 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
73 char *dpip_tag2, *dialog_msg;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
74
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
75 /* Let's confirm the request */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
76 /* NOTE: you can send less alternatives (e.g. only alt1 and alt2) */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
77 d_cmd = a_Dpip_build_cmd(
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
78 "cmd=%s msg=%s alt1=%s alt2=%s alt3=%s alt4=%s alt5=%s",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
79 "dialog", "Do you want to see the hello page?",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
80 choice[1], choice[2], choice[3], choice[4], choice[5]);
1397
a9662d8ada3f hello dpi: convert to dsh API (with auth)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
81 a_Dpip_dsh_write_str(sh, 1, d_cmd);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
82 dFree(d_cmd);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
83
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
84 /* Get the answer */
1397
a9662d8ada3f hello dpi: convert to dsh API (with auth)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
85 dpip_tag2 = a_Dpip_dsh_read_token(sh, 1);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
86 MSG("tag = [%s]\n", dpip_tag2);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
87
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
88 /* Get "msg" value */
1236
b912173aecd1 Added a_Dpip_get_attr_l() to DPIP's API
Jorge Arellano Cid <jcid@dillo.org>
parents: 35
diff changeset
89 dialog_msg = a_Dpip_get_attr(dpip_tag2, "msg");
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
90 choice_num = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
91 if (dialog_msg)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
92 choice_num = *dialog_msg - '0';
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
93
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
94 dFree(dialog_msg);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
95 dFree(dpip_tag2);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
96 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
97 /*-- EOD part */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
98
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
99 /* Start sending our answer */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
100 d_cmd = a_Dpip_build_cmd("cmd=%s url=%s", "start_send_page", url);
1397
a9662d8ada3f hello dpi: convert to dsh API (with auth)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
101 a_Dpip_dsh_write_str(sh, 0, d_cmd);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
102 dFree(d_cmd);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
103
1397
a9662d8ada3f hello dpi: convert to dsh API (with auth)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
104 a_Dpip_dsh_printf(sh, 0,
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
105 "Content-type: text/html\n\n"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
106 "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>\n"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
107 "<html>\n"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
108 "<head><title>Simple dpi test page (hello.dpi)</title></head>\n"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
109 "<body><hr><h1>Hello world!</h1><hr>\n<br><br>\n");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
110
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
111 /* Show the choice received with the dialog */
1397
a9662d8ada3f hello dpi: convert to dsh API (with auth)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
112 a_Dpip_dsh_printf(sh, 0,
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
113 "<hr>\n"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
114 "<table width='100%%' border='1' bgcolor='burlywood'><tr><td>\n"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
115 "<big><em>Dialog question:</em> Do you want to see the hello page?<br>\n"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
116 "<em>Answer received:</em> <b>%s</b></big> </table>\n"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
117 "<hr>\n",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
118 choice[choice_num]);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
119
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
120 /* Show the dpip tag we received */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
121 esc_tag = Escape_html_str(dpip_tag);
1397
a9662d8ada3f hello dpi: convert to dsh API (with auth)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
122 a_Dpip_dsh_printf(sh, 0,
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
123 "<h3>dpip tag received:</h3>\n"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
124 "<pre>\n%s</pre>\n"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
125 "<br><small>(<b>dpip:</b> dpi protocol)</small><br><br><br>\n",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
126 esc_tag);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
127 dFree(esc_tag);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
128
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
129
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
130 /* Now something more interesting,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
131 * fork a command and show its feedback */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
132 if (cmd && url) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
133 child_cmd = dStrdup("date -R");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
134 MSG("[%s]\n", child_cmd);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
135
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
136 /* Fork, exec command, get its output and answer */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
137 if ((in_stream = popen(child_cmd, "r")) == NULL) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
138 perror("popen");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
139 return EXIT_FAILURE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
140 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
141
1397
a9662d8ada3f hello dpi: convert to dsh API (with auth)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
142 a_Dpip_dsh_write_str(sh, 0, "<h3>date:</h3>\n");
a9662d8ada3f hello dpi: convert to dsh API (with auth)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
143 a_Dpip_dsh_write_str(sh, 0, "<pre>\n");
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
144
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
145 /* Read/Write */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
146 while ((n = fread (buf, 1, 4096, in_stream)) > 0) {
1397
a9662d8ada3f hello dpi: convert to dsh API (with auth)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
147 a_Dpip_dsh_write(sh, 0, buf, n);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
148 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
149
1397
a9662d8ada3f hello dpi: convert to dsh API (with auth)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
150 a_Dpip_dsh_write_str(sh, 0, "</pre>\n");
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
151
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
152 if ((ret = pclose(in_stream)) != 0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
153 MSG("popen: [%d]\n", ret);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
154
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
155 dFree(child_cmd);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
156 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
157
1397
a9662d8ada3f hello dpi: convert to dsh API (with auth)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
158 a_Dpip_dsh_write_str(sh, 1, "</body></html>\n");
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
159
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
160 dFree(cmd);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
161 dFree(url);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
162 dFree(dpip_tag);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
163
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
164 /* Finish the SockHandler */
1397
a9662d8ada3f hello dpi: convert to dsh API (with auth)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
165 a_Dpip_dsh_close(sh);
a9662d8ada3f hello dpi: convert to dsh API (with auth)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1236
diff changeset
166 a_Dpip_dsh_free(sh);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
167
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
168 return 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
169 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
170