annotate dpi/ftp.c @ 1200:6bdf6ea2637a

Removed compiler warnings for unused return values
author Michal Nowak newman.x@gmail.com
date Fri, 26 Jun 2009 22:41:45 -0400
parents ba5eed484075
children f34b803b8639
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 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
59 static SockHandler *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 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
100 for (i = 0; i < Size && isspace(p[i]); ++i);
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];
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
132 if ((ch < 32 || ch > 126) && !isspace(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.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
174 * Return: Number of bytes transfered.
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 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
178 #define MinSZ 256
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
179
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
180 ssize_t n;
212
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
181 int nb, has_mime_type, has_html_header;
184
e9c8fd71790a - Improved parsing of collapsing white space.
jcid
parents: 83
diff changeset
182 const char *mime_type = "application/octet-stream";
212
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
183 char buf[4096], *d_cmd;
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
184 Dstr *dbuf = dStr_sized_new(4096);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
185 pid_t ch_pid;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
186 int aborted = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
187 int DataPipe[2];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
188
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
189 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
190 MSG("pipe, %s\n", dStrerror(errno));
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
191 return 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
192 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
193
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
194 /* Prepare args for execvp() */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
195 make_wget_argv(url);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
196
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
197 /* Start the child process */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
198 if ((ch_pid = fork()) == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
199 /* child */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
200 /* start wget */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
201 close(DataPipe[0]);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
202 dup2(DataPipe[1], 1); /* stdout */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
203 execvp(dl_argv[0], dl_argv);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
204 _exit(1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
205 } else if (ch_pid < 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
206 perror("fork, ");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
207 exit(1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
208 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
209 /* father continues below */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
210 close(DataPipe[1]);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
211 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
212
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
213 /* Read/Write the real data */
212
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
214 nb = 0;
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
215 has_mime_type = 0;
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
216 has_html_header = 0;
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
217 do {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
218 while ((n = read(DataPipe[0], buf, 4096)) < 0 && errno == EINTR);
212
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
219 if (n > 0) {
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
220 dStr_append_l(dbuf, buf, n);
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
221 if (!has_mime_type && dbuf->len < MinSZ)
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
222 continue;
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
223 } else if (n < 0)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
224 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
225
212
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
226 if (!has_mime_type) {
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
227 if (dbuf->len > 0)
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
228 a_Misc_get_content_type_from_data2(dbuf->str,dbuf->len,&mime_type);
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
229 has_mime_type = 1;
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
230
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
231 if (strcmp(mime_type, "application/octet-stream") == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
232 /* abort transfer */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
233 kill(ch_pid, SIGTERM);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
234 /* The "application/octet-stream" MIME type will be sent and
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
235 * Dillo will offer a download dialog */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
236 aborted = 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
237 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
238 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
239
212
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
240 if (!has_html_header && dbuf->len) {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
241 /* Send dpip tag */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
242 d_cmd = a_Dpip_build_cmd("cmd=%s url=%s", "start_send_page", url);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
243 sock_handler_write_str(sh, 1, d_cmd);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
244 dFree(d_cmd);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
245
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
246 /* Send HTTP header. */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
247 sock_handler_write_str(sh, 0, "Content-type: ");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
248 sock_handler_write_str(sh, 0, mime_type);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
249 sock_handler_write_str(sh, 1, "\n\n");
212
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
250 has_html_header = 1;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
251 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
252
212
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
253 if (!aborted && dbuf->len) {
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
254 sock_handler_write(sh, 0, dbuf->str, dbuf->len);
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
255 nb += dbuf->len;
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
256 dStr_truncate(dbuf, 0);
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
257 }
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
258 } while (n > 0 && !aborted);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
259
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
260 return nb;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
261 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
262
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
263 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
264 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
265 */
212
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
266 int main(int argc, char **argv)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
267 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
268 char *dpip_tag = NULL, *cmd = NULL, *url = NULL, *url2 = NULL;
1200
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 483
diff changeset
269 int nb, rc;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
270 char *p, *d_cmd;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
271
212
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
272 /* Debugging with a command line argument */
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
273 if (argc == 2)
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
274 dpip_tag = dStrdup(argv[1]);
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
275
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
276 /* Initialize the SockHandler */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
277 sh = sock_handler_new(STDIN_FILENO, STDOUT_FILENO, 8*1024);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
278
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
279 /* 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
280 rc = chdir("/tmp");
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 483
diff changeset
281 if (rc == -1) {
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 483
diff changeset
282 MSG("paths: error changing directory to /tmp: %s\n",
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 483
diff changeset
283 dStrerror(errno));
6bdf6ea2637a Removed compiler warnings for unused return values
Michal Nowak newman.x@gmail.com
parents: 483
diff changeset
284 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
285
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
286 /* Read the dpi command from STDIN */
212
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
287 if (!dpip_tag)
06b2b2fa6bac - FTP dpi: Fixed algorithm bugs and improved the mime-type detector.
jcid
parents: 184
diff changeset
288 dpip_tag = sock_handler_read(sh);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
289 MSG("tag=[%s]\n", dpip_tag);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
290
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
291 cmd = a_Dpip_get_attr(dpip_tag, strlen(dpip_tag), "cmd");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
292 url = a_Dpip_get_attr(dpip_tag, strlen(dpip_tag), "url");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
293 if (!cmd || !url) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
294 MSG("ERROR, cmd=%s, url=%s\n", cmd, url);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
295 exit (EXIT_FAILURE);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
296 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
297
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
298 if ((nb = try_ftp_transfer(url)) == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
299 /* Transfer failed, the requested file may not exist or be a symlink
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
300 * to a directory. Try again... */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
301 if ((p = strrchr(url, '/')) && p[1]) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
302 url2 = dStrconcat(url, "/", NULL);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
303 nb = try_ftp_transfer(url2);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
304 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
305 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
306
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
307 if (nb == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
308 /* The transfer failed, let dillo know... */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
309 d_cmd = a_Dpip_build_cmd("cmd=%s to_cmd=%s msg=%s",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
310 "answer", "open_url", "not a directory");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
311 sock_handler_write_str(sh, 1, d_cmd);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
312 dFree(d_cmd);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
313 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
314
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
315 dFree(cmd);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
316 dFree(url);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
317 dFree(url2);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
318 dFree(dpip_tag);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
319
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
320 /* Finish the SockHandler */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
321 sock_handler_close(sh);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
322 sock_handler_free(sh);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
323
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
324 return 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
325 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
326