annotate dpi/ftp.c @ 0:6ee11bf9e3ea

Initial revision
author jcid
date Sun, 07 Oct 2007 00:36:34 +0200
parents
children d9e7b35430de
rev   line source
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
2 * Dpi for FTP.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
3 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
4 * This server checks the ftp-URL to be a directory (requires wget).
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
5 * If true, it sends back an html representation of it, and if not
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
6 * a dpip message (which is catched by dillo who redirects the ftp URL
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
7 * to the downloads server).
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
8 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
9 * Feel free to polish!
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
10 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
11 * Copyright 2003-2005 Jorge Arellano Cid <jcid@dillo.org>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
12 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
13 * This program is free software; you can redistribute it and/or modify
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
14 * it under the terms of the GNU General Public License as published by
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
15 * the Free Software Foundation; either version 3 of the License, or
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
16 * (at your option) any later version.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
17 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
18 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
19
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
20 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
21 * TODO:
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
22 * - Send feedback about the FTP login process from wget's stderr.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
23 * i.e. capture our child's stderr, process it, and report back.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
24 * - Handle simultaneous connections.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
25 * If ftp.dpi is implemented with a low level ftp library, it becomes
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
26 * possible to keep the connection alive, and thus make browsing of ftp
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
27 * directories faster (this avoids one login per page, and forks). Perhaps
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
28 * it's not worth, but can be done.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
29 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
30
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
31 #include <unistd.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
32 #include <sys/types.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
33 #include <sys/socket.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
34 #include <sys/un.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
35 #include <stdio.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
36 #include <stdlib.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
37 #include <string.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
38 #include <signal.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
39 #include <sys/wait.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
40 #include <errno.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
41 #include <sys/time.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
42 #include <ctype.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
43
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
44 #include "../dpip/dpip.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
45 #include "dpiutil.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
46 #include "d_size.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
47
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
48 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
49 * Debugging macros
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
50 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
51 #define _MSG(...)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
52 #define MSG(...) printf("[ftp dpi]: " __VA_ARGS__)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
53
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
54 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
55 * Global variables
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
56 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
57 static SockHandler *sh = NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
58 char **dl_argv = NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
59
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
60 /*---------------------------------------------------------------------------*/
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
61
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
62 /* TODO: could use dStr ADT! */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
63 typedef struct ContentType_ {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
64 const char *str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
65 int len;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
66 } ContentType_t;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
67
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
68 static const ContentType_t MimeTypes[] = {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
69 { "application/octet-stream", 24 },
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
70 { "text/html", 9 },
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
71 { "text/plain", 10 },
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
72 { "image/gif", 9 },
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
73 { "image/png", 9 },
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
74 { "image/jpeg", 10 },
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
75 { NULL, 0 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
76 };
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
77
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
78 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
79 * Detects 'Content-Type' from a data stream sample.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
80 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
81 * It uses the magic(5) logic from file(1). Currently, it
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
82 * only checks the few mime types that Dillo supports.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
83 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
84 * 'Data' is a pointer to the first bytes of the raw data.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
85 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
86 * Return value: (0 on success, 1 on doubt, 2 on lack of data).
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
87 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
88 int a_Misc_get_content_type_from_data(void *Data, size_t Size,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
89 const char **PT)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
90 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
91 int st = 1; /* default to "doubt' */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
92 int Type = 0; /* default to "application/octet-stream" */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
93 char *p = Data;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
94 size_t i, non_ascci;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
95
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
96 /* HTML try */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
97 for (i = 0; i < Size && isspace(p[i]); ++i);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
98 if ((Size - i >= 5 && !dStrncasecmp(p+i, "<html", 5)) ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
99 (Size - i >= 5 && !dStrncasecmp(p+i, "<head", 5)) ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
100 (Size - i >= 6 && !dStrncasecmp(p+i, "<title", 6)) ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
101 (Size - i >= 14 && !dStrncasecmp(p+i, "<!doctype html", 14)) ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
102 /* this line is workaround for FTP through the Squid proxy */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
103 (Size - i >= 17 && !dStrncasecmp(p+i, "<!-- HTML listing", 17))) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
104
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
105 Type = 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
106 st = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
107 /* Images */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
108 } else if (Size >= 4 && !dStrncasecmp(p, "GIF8", 4)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
109 Type = 3;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
110 st = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
111 } else if (Size >= 4 && !dStrncasecmp(p, "\x89PNG", 4)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
112 Type = 4;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
113 st = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
114 } else if (Size >= 2 && !dStrncasecmp(p, "\xff\xd8", 2)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
115 /* JPEG has the first 2 bytes set to 0xffd8 in BigEndian - looking
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
116 * at the character representation should be machine independent. */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
117 Type = 5;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
118 st = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
119
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
120 /* Text */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
121 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
122 /* We'll assume "text/plain" if the set of chars above 127 is <= 10
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
123 * in a 256-bytes sample. Better heuristics are welcomed! :-) */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
124 non_ascci = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
125 Size = MIN (Size, 256);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
126 for (i = 0; i < Size; i++)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
127 if ((uchar_t) p[i] > 127)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
128 ++non_ascci;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
129 if (Size == 256) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
130 Type = (non_ascci > 10) ? 0 : 2;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
131 st = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
132 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
133 Type = (non_ascci > 0) ? 0 : 2;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
134 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
135 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
136
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
137 *PT = MimeTypes[Type].str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
138 return st;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
139 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
140
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
141 /*---------------------------------------------------------------------------*/
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
142
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
143 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
144 * Build a shell command using wget for this URL.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
145 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
146 static void make_wget_argv(char *url)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
147 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
148 char *esc_url;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
149
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
150 if (dl_argv) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
151 dFree(dl_argv[2]);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
152 dFree(dl_argv);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
153 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
154 dl_argv = dNew(char*, 10);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
155
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
156 esc_url = Escape_uri_str(url, "'");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
157 /* avoid malicious SMTP relaying with FTP urls */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
158 Filter_smtp_hack(esc_url);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
159
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
160 dl_argv[0] = "wget";
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
161 dl_argv[1] = "-O-";
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
162 dl_argv[2] = esc_url;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
163 dl_argv[3] = NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
164 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
165
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
166 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
167 * Fork, exec command, get its output and send via stdout.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
168 * Return: Number of bytes transfered.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
169 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
170 static int try_ftp_transfer(char *url)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
171 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
172 #define MinSZ 256
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
173
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
174 ssize_t n;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
175 int nb, minibuf_sz;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
176 const char *mime_type;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
177 char buf[4096], minibuf[MinSZ], *d_cmd;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
178 pid_t ch_pid;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
179 int aborted = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
180 int DataPipe[2];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
181
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
182 if (pipe(DataPipe) < 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
183 MSG("pipe, %s\n", strerror(errno));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
184 return 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
185 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
186
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
187 /* Prepare args for execvp() */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
188 make_wget_argv(url);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
189
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
190 /* Start the child process */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
191 if ((ch_pid = fork()) == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
192 /* child */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
193 /* start wget */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
194 close(DataPipe[0]);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
195 dup2(DataPipe[1], 1); /* stdout */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
196 execvp(dl_argv[0], dl_argv);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
197 _exit(1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
198 } else if (ch_pid < 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
199 perror("fork, ");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
200 exit(1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
201 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
202 /* father continues below */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
203 close(DataPipe[1]);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
204 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
205
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
206 /* Read/Write the real data */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
207 minibuf_sz = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
208 for (nb = 0; 1; nb += n) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
209 while ((n = read(DataPipe[0], buf, 4096)) < 0 && errno == EINTR);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
210 if (n <= 0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
211 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
212
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
213 if (minibuf_sz < MinSZ) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
214 memcpy(minibuf + minibuf_sz, buf, MIN(n, MinSZ - minibuf_sz));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
215 minibuf_sz += MIN(n, MinSZ - minibuf_sz);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
216 if (minibuf_sz < MinSZ)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
217 continue;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
218 a_Misc_get_content_type_from_data(minibuf, minibuf_sz, &mime_type);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
219 if (strcmp(mime_type, "application/octet-stream") == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
220 /* abort transfer */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
221 kill(ch_pid, SIGTERM);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
222 /* The "application/octet-stream" MIME type will be sent and
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
223 * Dillo will offer a download dialog */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
224 aborted = 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
225 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
226 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
227
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
228 if (nb == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
229 /* Send dpip tag */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
230 d_cmd = a_Dpip_build_cmd("cmd=%s url=%s", "start_send_page", url);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
231 sock_handler_write_str(sh, 1, d_cmd);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
232 dFree(d_cmd);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
233
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
234 /* Send HTTP header. */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
235 sock_handler_write_str(sh, 0, "Content-type: ");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
236 sock_handler_write_str(sh, 0, mime_type);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
237 sock_handler_write_str(sh, 1, "\n\n");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
238 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
239
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
240 if (!aborted)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
241 sock_handler_write(sh, 0, buf, n);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
242 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
243
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
244 return nb;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
245 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
246
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
247 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
248 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
249 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
250 int main(void)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
251 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
252 char *dpip_tag = NULL, *cmd = NULL, *url = NULL, *url2 = NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
253 int nb;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
254 char *p, *d_cmd;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
255
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
256 /* Initialize the SockHandler */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
257 sh = sock_handler_new(STDIN_FILENO, STDOUT_FILENO, 8*1024);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
258
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
259 /* wget may need to write a temporary file... */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
260 chdir("/tmp");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
261
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
262 /* Read the dpi command from STDIN */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
263 dpip_tag = sock_handler_read(sh);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
264 MSG("tag=[%s]\n", dpip_tag);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
265
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
266 cmd = a_Dpip_get_attr(dpip_tag, strlen(dpip_tag), "cmd");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
267 url = a_Dpip_get_attr(dpip_tag, strlen(dpip_tag), "url");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
268 if (!cmd || !url) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
269 MSG("ERROR, cmd=%s, url=%s\n", cmd, url);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
270 exit (EXIT_FAILURE);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
271 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
272
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
273 if ((nb = try_ftp_transfer(url)) == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
274 /* Transfer failed, the requested file may not exist or be a symlink
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
275 * to a directory. Try again... */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
276 if ((p = strrchr(url, '/')) && p[1]) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
277 url2 = dStrconcat(url, "/", NULL);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
278 nb = try_ftp_transfer(url2);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
279 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
280 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
281
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
282 if (nb == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
283 /* The transfer failed, let dillo know... */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
284 d_cmd = a_Dpip_build_cmd("cmd=%s to_cmd=%s msg=%s",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
285 "answer", "open_url", "not a directory");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
286 sock_handler_write_str(sh, 1, d_cmd);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
287 dFree(d_cmd);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
288 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
289
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
290 dFree(cmd);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
291 dFree(url);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
292 dFree(url2);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
293 dFree(dpip_tag);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
294
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
295 /* Finish the SockHandler */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
296 sock_handler_close(sh);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
297 sock_handler_free(sh);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
298
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
299 return 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
300 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
301