33#include <sys/socket.h>
44#include "../dpip/dpip.h"
54#define MSG(...) printf("[ftp dpi]: " __VA_ARGS__)
71 {
"application/octet-stream", 24 },
100 for (i = 0; i < Size &&
dIsspace(p[i]); ++i);
111 }
else if (Size >= 4 && !strncmp(p,
"GIF8", 4)) {
114 }
else if (Size >= 4 && !strncmp(p,
"\x89PNG", 4)) {
117 }
else if (Size >= 2 && !strncmp(p,
"\xff\xd8", 2)) {
129 Size =
MIN (Size, 256);
130 for (i = 0; i < Size; i++) {
132 if ((ch < 32 || ch > 126) && !
dIsspace(ch))
136 Type = (non_ascci > Size/10) ? 0 : 2;
139 Type = (non_ascci > Size/10) ? 0 : 2;
180#define READ_SZ 16*1024
183 int nb, has_mime_type, has_html_header, no_such_file, offer_download;
184 const char *mime_type =
"application/octet-stream";
191 MSG(
"try_ftp_transfer: url=%s\n", url);
193 if (pipe(DataPipe) < 0) {
202 if ((ch_pid = fork()) == 0) {
206 dup2(DataPipe[1], 1);
209 }
else if (ch_pid < 0) {
224 while ((n = read(DataPipe[0], buf,
READ_SZ)) < 0 && errno == EINTR);
227 if (!has_mime_type && dbuf->
len <
MIN_SZ)
232 if (!has_mime_type) {
233 if (dbuf->
len == 0) {
241 if (strcmp(mime_type,
"application/octet-stream") == 0) {
243 kill(ch_pid, SIGTERM);
251 if (offer_download || (!aborted && !has_html_header && dbuf->
len)) {
264 if (!aborted && dbuf->
len) {
269 }
while (n > 0 && !aborted);
272 return (no_such_file ? -1 : (aborted ? -2 : nb));
280 const char *err_msg =
"404 Not Found\nNo such file or directory";
281 char *dpip_tag = NULL, *cmd = NULL, *url = NULL, *url2 = NULL;
288 MSG(
"paths: error changing directory to /tmp: %s\n",
302 MSG(
"can't authenticate request: %s\n",
dStrerror(errno));
310 MSG(
"tag=[%s]\n", dpip_tag);
315 MSG(
"ERROR, cmd=%s, url=%s\n", cmd, url);
322 if ((p = strrchr(url,
'/')) && p[1] &&
323 p > url && p[-1] !=
'/') {
335 "HTTP/1.1 404 Not Found\r\n"
336 "Content-Type: text/plain\r\n"
337 "Content-Length: %d\r\n"
340 strlen(err_msg), err_msg);
char * dStrconcat(const char *s1,...)
Concatenate a NULL-terminated list of strings.
char * dStrdup(const char *s)
Dstr * dStr_sized_new(int sz)
Create a new string with a given size.
int dStrnAsciiCasecmp(const char *s1, const char *s2, size_t n)
void dStr_free(Dstr *ds, int all)
Free a dillo string.
void dStr_append_l(Dstr *ds, const char *s, int l)
Append a C string to a Dstr (providing length).
void dStr_truncate(Dstr *ds, int len)
Truncate a Dstr to be 'len' bytes long.
#define dNew(type, count)
void a_Dpip_dsh_free(Dsh *dsh)
Free the SockHandler structure.
char * a_Dpip_build_cmd(const char *format,...)
Printf like function for building dpip commands.
int a_Dpip_dsh_write_str(Dsh *dsh, int flush, const char *str)
Convenience function.
char * a_Dpip_get_attr(const char *tag, const char *attrname)
Task: given a tag and an attribute name, return its value.
char * a_Dpip_dsh_read_token(Dsh *dsh, int blocking)
Return a newlly allocated string with the next dpip token in the socket.
void a_Dpip_dsh_close(Dsh *dsh)
Close this socket for reading and writing.
int a_Dpip_check_auth(const char *auth_tag)
Check whether the given 'auth' string equals what dpid saved.
int a_Dpip_dsh_write(Dsh *dsh, int flush, const char *Data, int DataSize)
Streamed write to socket.
Dsh * a_Dpip_dsh_new(int fd_in, int fd_out, int flush_sz)
Create and initialize a dpip socket handler.
#define a_Dpip_dsh_printf(sh, flush,...)
char * Filter_smtp_hack(char *url)
char * Escape_uri_str(const char *str, const char *p_esc_set)
static void make_wget_argv(char *url)
static int try_ftp_transfer(char *url)
static const ContentType_t MimeTypes[]
static int a_Misc_get_content_type_from_data2(void *Data, size_t Size, const char **PT)
Dpip socket handler type.