annotate dpi/ftp.c @ 2048:5060d415a85a

clickable menu items (even those introducing submenus) MUST have callbacks I clicked on the "Panel size" item itself instead of any of the options in its submenu, and: Segfault!
author corvid <corvid@lavabit.com>
date Thu, 26 May 2011 02:51:18 +0000
parents 3f1501ae35c4
children
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
231
64ec10ca6d51 - Minor spelling fixes.
jcid
parents: 212
diff changeset
6 * a dpip message (which is caught by dillo who redirects the ftp URL
0
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 *
35
d9e7b35430de Updated copyright lines
jcid
parents: 0
diff changeset
11 * Copyright 2003-2007 Jorge Arellano Cid <jcid@dillo.org>
0
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
212
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
50 * (Set debugging messages to stderr, to see them)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
51 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
52 #define _MSG(...)
212
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
53 //#define MSG(...) fprintf(stderr, "[ftp dpi]: " __VA_ARGS__)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
54 #define MSG(...) printf("[ftp dpi]: " __VA_ARGS__)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
55
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
56 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
57 * Global variables
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
58 */
1396
7d57f3528d20 ftp dpi: convert to dsh API (with auth)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1326
diff changeset
59 static Dsh *sh = NULL;
83
866d074448f6 - Added more statics in dpi, const in pixmaps, and removed redundant includes.
jcid
parents: 35
diff changeset
60 static char **dl_argv = NULL;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
61
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
62 /*---------------------------------------------------------------------------*/
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
63
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
64 /* TODO: could use dStr ADT! */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
65 typedef struct ContentType_ {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
66 const char *str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
67 int len;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
68 } ContentType_t;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
69
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
70 static const ContentType_t MimeTypes[] = {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
71 { "application/octet-stream", 24 },
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
72 { "text/html", 9 },
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
73 { "text/plain", 10 },
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
74 { "image/gif", 9 },
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
75 { "image/png", 9 },
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
76 { "image/jpeg", 10 },
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
77 { NULL, 0 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
78 };
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
79
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
80 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
81 * Detects 'Content-Type' from a data stream sample.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
82 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
83 * It uses the magic(5) logic from file(1). Currently, it
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
84 * only checks the few mime types that Dillo supports.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
85 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
86 * 'Data' is a pointer to the first bytes of the raw data.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
87 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
88 * Return value: (0 on success, 1 on doubt, 2 on lack of data).
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
89 */
212
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
90 static int a_Misc_get_content_type_from_data2(void *Data, size_t Size,
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
91 const char **PT)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
92 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
93 int st = 1; /* default to "doubt' */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
94 int Type = 0; /* default to "application/octet-stream" */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
95 char *p = Data;
212
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
96 uchar_t ch;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
97 size_t i, non_ascci;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
98
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
99 /* HTML try */
1221
f34b803b8639 Handle signed chars. Aadded dIsspace() and dIsalnum() to dlib
Jorge Arellano Cid <jcid@dillo.org>
parents: 1200
diff changeset
100 for (i = 0; i < Size && dIsspace(p[i]); ++i);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
101 if ((Size - i >= 5 && !dStrncasecmp(p+i, "<html", 5)) ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
102 (Size - i >= 5 && !dStrncasecmp(p+i, "<head", 5)) ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
103 (Size - i >= 6 && !dStrncasecmp(p+i, "<title", 6)) ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
104 (Size - i >= 14 && !dStrncasecmp(p+i, "<!doctype html", 14)) ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
105 /* this line is workaround for FTP through the Squid proxy */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
106 (Size - i >= 17 && !dStrncasecmp(p+i, "<!-- HTML listing", 17))) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
107
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
108 Type = 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
109 st = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
110 /* Images */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
111 } else if (Size >= 4 && !dStrncasecmp(p, "GIF8", 4)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
112 Type = 3;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
113 st = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
114 } else if (Size >= 4 && !dStrncasecmp(p, "\x89PNG", 4)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
115 Type = 4;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
116 st = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
117 } else if (Size >= 2 && !dStrncasecmp(p, "\xff\xd8", 2)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
118 /* JPEG has the first 2 bytes set to 0xffd8 in BigEndian - looking
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
119 * at the character representation should be machine independent. */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
120 Type = 5;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
121 st = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
122
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
123 /* Text */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
124 } else {
212
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
125 /* We'll assume "text/plain" if the set of chars above 127 is <= 10%
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
126 * of the sample. This helps to catch ASCII, LATIN1 and UTF-8 as text.
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
127 * Better heuristics are welcomed! :-) */
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
128 non_ascci = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
129 Size = MIN (Size, 256);
212
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
130 for (i = 0; i < Size; i++) {
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
131 ch = (uchar_t) p[i];
1221
f34b803b8639 Handle signed chars. Aadded dIsspace() and dIsalnum() to dlib
Jorge Arellano Cid <jcid@dillo.org>
parents: 1200
diff changeset
132 if ((ch < 32 || ch > 126) && !dIsspace(ch))
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
133 ++non_ascci;
212
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
134 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
135 if (Size == 256) {
212
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
136 Type = (non_ascci > Size/10) ? 0 : 2;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
137 st = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
138 } else {
212
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
139 Type = (non_ascci > Size/10) ? 0 : 2;
0
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 *PT = MimeTypes[Type].str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
144 return st;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
145 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
146
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
147 /*---------------------------------------------------------------------------*/
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
148
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
149 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
150 * Build a shell command using wget for this URL.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
151 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
152 static void make_wget_argv(char *url)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
153 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
154 char *esc_url;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
155
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
156 if (dl_argv) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
157 dFree(dl_argv[2]);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
158 dFree(dl_argv);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
159 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
160 dl_argv = dNew(char*, 10);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
161
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
162 esc_url = Escape_uri_str(url, "'");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
163 /* avoid malicious SMTP relaying with FTP urls */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
164 Filter_smtp_hack(esc_url);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
165
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
166 dl_argv[0] = "wget";
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
167 dl_argv[1] = "-O-";
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
168 dl_argv[2] = esc_url;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
169 dl_argv[3] = NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
170 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
171
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
172 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
173 * Fork, exec command, get its output and send via stdout.
1401
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
174 * Return: Number of bytes transfered, -1 if file-not_found, -2 if aborted.
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
175 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
176 static int try_ftp_transfer(char *url)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
177 {
1401
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
178 #define MIN_SZ 256
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
179 #define READ_SZ 16*1024
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
180
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
181 ssize_t n;
1401
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
182 int nb, has_mime_type, has_html_header, no_such_file, offer_download;
184
e9c8fd71790a - Improved parsing of collapsing white space.
jcid
parents: 83
diff changeset
183 const char *mime_type = "application/octet-stream";
1401
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
184 char buf[READ_SZ], *d_cmd;
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
185 Dstr *dbuf = dStr_sized_new(READ_SZ);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
186 pid_t ch_pid;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
187 int aborted = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
188 int DataPipe[2];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
189
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
190 if (pipe(DataPipe) < 0) {
483
ba5eed484075 Ensure that the dlib dStr* functions are used everywhere.
Jorge Arellano Cid <jcid@dillo.org>
parents: 231
diff changeset
191 MSG("pipe, %s\n", dStrerror(errno));
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
192 return 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
193 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
194
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
195 /* Prepare args for execvp() */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
196 make_wget_argv(url);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
197
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
198 /* Start the child process */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
199 if ((ch_pid = fork()) == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
200 /* child */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
201 /* start wget */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
202 close(DataPipe[0]);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
203 dup2(DataPipe[1], 1); /* stdout */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
204 execvp(dl_argv[0], dl_argv);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
205 _exit(1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
206 } else if (ch_pid < 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
207 perror("fork, ");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
208 exit(1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
209 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
210 /* father continues below */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
211 close(DataPipe[1]);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
212 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
213
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
214 /* Read/Write the real data */
212
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
215 nb = 0;
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
216 has_mime_type = 0;
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
217 has_html_header = 0;
1401
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
218 no_such_file = 0;
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
219 offer_download = 0;
212
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
220 do {
1401
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
221 while ((n = read(DataPipe[0], buf, READ_SZ)) < 0 && errno == EINTR);
212
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
222 if (n > 0) {
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
223 dStr_append_l(dbuf, buf, n);
1401
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
224 if (!has_mime_type && dbuf->len < MIN_SZ)
212
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
225 continue;
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
226 } else if (n < 0)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
227 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
228
212
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
229 if (!has_mime_type) {
1401
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
230 if (dbuf->len == 0) {
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
231 /* When the file doesn't exist, the transfer size is zero */
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
232 no_such_file = 1;
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
233 break;
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
234 }
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
235 a_Misc_get_content_type_from_data2(dbuf->str, dbuf->len, &mime_type);
212
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
236 has_mime_type = 1;
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
237
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
238 if (strcmp(mime_type, "application/octet-stream") == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
239 /* abort transfer */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
240 kill(ch_pid, SIGTERM);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
241 /* The "application/octet-stream" MIME type will be sent and
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
242 * Dillo will offer a download dialog */
1401
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
243 offer_download = 1;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
244 aborted = 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
245 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
246 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
247
1401
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
248 if (offer_download || (!aborted && !has_html_header && dbuf->len)) {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
249 /* Send dpip tag */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
250 d_cmd = a_Dpip_build_cmd("cmd=%s url=%s", "start_send_page", url);
1396
7d57f3528d20 ftp dpi: convert to dsh API (with auth)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1326
diff changeset
251 a_Dpip_dsh_write_str(sh, 1, d_cmd);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
252 dFree(d_cmd);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
253
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
254 /* Send HTTP header. */
1396
7d57f3528d20 ftp dpi: convert to dsh API (with auth)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1326
diff changeset
255 a_Dpip_dsh_write_str(sh, 0, "Content-type: ");
7d57f3528d20 ftp dpi: convert to dsh API (with auth)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1326
diff changeset
256 a_Dpip_dsh_write_str(sh, 0, mime_type);
1401
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
257 a_Dpip_dsh_write_str(sh, 1, "\r\n\r\n");
212
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
258 has_html_header = 1;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
259 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
260
212
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
261 if (!aborted && dbuf->len) {
1401
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
262 a_Dpip_dsh_write(sh, 1, dbuf->str, dbuf->len);
212
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
263 nb += dbuf->len;
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
264 dStr_truncate(dbuf, 0);
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
265 }
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
266 } while (n > 0 && !aborted);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
267
1401
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
268 dStr_free(dbuf, 1);
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
269 return (no_such_file ? -1 : (aborted ? -2 : nb));
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
270 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
271
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
272 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
273 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
274 */
212
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
275 int main(int argc, char **argv)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
276 {
1401
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
277 const char *err_msg = "404 Not Found\nNo such file or directory";
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
278 char *dpip_tag = NULL, *cmd = NULL, *url = NULL, *url2 = NULL;
1401
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
279 int st, rc;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
280 char *p, *d_cmd;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
281
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
282 /* wget may need to write a temporary file... */
1200
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 483
diff changeset
283 rc = chdir("/tmp");
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 483
diff changeset
284 if (rc == -1) {
1326
02ef25700865 trim some spaces
corvid <corvid@lavabit.com>
parents: 1236
diff changeset
285 MSG("paths: error changing directory to /tmp: %s\n",
1200
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 483
diff changeset
286 dStrerror(errno));
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 483
diff changeset
287 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
288
1401
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
289 /* Initialize the SockHandler */
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
290 sh = a_Dpip_dsh_new(STDIN_FILENO, STDOUT_FILENO, 8*1024);
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
291
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
292 if (argc == 2) {
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
293 /* Debugging with a command line argument */
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
294 dpip_tag = dStrdup(argv[1]);
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
295 } else {
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
296 /* Authenticate our client... */
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
297 if (!(dpip_tag = a_Dpip_dsh_read_token(sh, 1)) ||
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
298 a_Dpip_check_auth(dpip_tag) < 0) {
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
299 MSG("can't authenticate request: %s\n", dStrerror(errno));
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
300 a_Dpip_dsh_close(sh);
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
301 return 1;
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
302 }
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
303 dFree(dpip_tag);
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
304 /* Read the dpi command from STDIN */
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
305 dpip_tag = a_Dpip_dsh_read_token(sh, 1);
1396
7d57f3528d20 ftp dpi: convert to dsh API (with auth)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1326
diff changeset
306 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
307 MSG("tag=[%s]\n", dpip_tag);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
308
1236
b912173aecd1 Added a_Dpip_get_attr_l() to DPIP's API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1221
diff changeset
309 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: 1221
diff changeset
310 url = a_Dpip_get_attr(dpip_tag, "url");
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
311 if (!cmd || !url) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
312 MSG("ERROR, cmd=%s, url=%s\n", cmd, url);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
313 exit (EXIT_FAILURE);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
314 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
315
1401
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
316 if ((st = try_ftp_transfer(url)) == -1) {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
317 /* Transfer failed, the requested file may not exist or be a symlink
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
318 * to a directory. Try again... */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
319 if ((p = strrchr(url, '/')) && p[1]) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
320 url2 = dStrconcat(url, "/", NULL);
1401
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
321 st = try_ftp_transfer(url2);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
322 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
323 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
324
1401
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
325 if (st == -1) {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
326 /* The transfer failed, let dillo know... */
1401
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
327 d_cmd = a_Dpip_build_cmd("cmd=%s url=%s", "start_send_page", url);
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
328 a_Dpip_dsh_write_str(sh, 0, d_cmd);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
329 dFree(d_cmd);
1401
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
330 a_Dpip_dsh_printf(sh, 1,
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
331 "HTTP/1.1 404 Not Found\r\n"
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
332 "Content-Type: text/plain\r\n"
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
333 "Content-Length: %d\r\n"
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
334 "\r\n"
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
335 "%s",
3f1501ae35c4 ftp dpi: fixed download offer, streamed transfer, added HTML feedback.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1396
diff changeset
336 strlen(err_msg), err_msg);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
337 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
338
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
339 dFree(cmd);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
340 dFree(url);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
341 dFree(url2);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
342 dFree(dpip_tag);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
343
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
344 /* Finish the SockHandler */
1396
7d57f3528d20 ftp dpi: convert to dsh API (with auth)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1326
diff changeset
345 a_Dpip_dsh_close(sh);
7d57f3528d20 ftp dpi: convert to dsh API (with auth)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1326
diff changeset
346 a_Dpip_dsh_free(sh);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
347
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
348 return 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
349 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
350