annotate dpi/vsource.c @ 1568:96e28c8697ad

Copy the hello dpi as a basis for the "view source" dpi
author Jorge Arellano Cid <jcid@dillo.org>
date Wed, 17 Feb 2010 13:53:36 -0300
parents
children f275bd2cdeed
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
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
32
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
33 /*
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
34 *
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
35 */
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
36 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
37 {
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
38 Dsh *sh;
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
39 int data_size, bytes_read = 0;
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
40 char *dpip_tag, *cmd = NULL, *url = NULL, *size_str = NULL;
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
41 char *d_cmd, *src_str;
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
42
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
43 MSG("starting...\n");
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
44 /* sleep(20) */
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
45
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
46 /* 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
47 * 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
48 * 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
49 * 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
50 * (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
51 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
52
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
53 /* 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
54 * 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
55 * 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
56 * 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
57 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
58 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
59 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
60 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
61 return 1;
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
62 }
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
63 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
64
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
65 /* 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
66 * 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
67 * 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
68 * 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
69 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
70 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
71
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
72 /* 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
73 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
74 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
75
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
76 /* 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
77 * (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
78 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
79 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
80 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
81
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
82 a_Dpip_dsh_printf(sh, 0,
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
83 "Content-type: text/plain\n\n"
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
84 ".----------------.\n"
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
85 "| Hello World! |\n"
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
86 "'----------------'\n\n");
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
87
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
88 /* Show the dpip tag we received */
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
89 a_Dpip_dsh_printf(sh, 0, "Dpip tag received: ");
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
90 a_Dpip_dsh_printf(sh, 0, dpip_tag);
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
91 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
92
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
93 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
94 a_Dpip_dsh_printf(sh, 0, "\nDpip tag received: ");
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
95 a_Dpip_dsh_printf(sh, 0, dpip_tag ? dpip_tag : "None");
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
96 a_Dpip_dsh_printf(sh, 1, "\n\n");
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
97 //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
98 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
99 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
100
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
101 while (bytes_read < data_size &&
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
102 (src_str = 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
103 bytes_read += strlen(src_str);
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
104 //a_Dpip_dsh_write_str(sh, 0, src_str);
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
105 a_Dpip_dsh_write_str(sh, 1, src_str);
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
106 dFree(src_str);
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
107 }
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
108
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
109 dFree(cmd);
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
110 dFree(url);
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
111 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
112 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
113
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
114 /* 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
115 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
116 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
117
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
118 return 0;
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
119 }
96e28c8697ad Copy the hello dpi as a basis for the "view source" dpi
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
120