annotate dpi/file.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 0399d0ecb3b9
children
rev   line source
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
2 * File: file.c :)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
3 *
35
d9e7b35430de Updated copyright lines
jcid
parents: 0
diff changeset
4 * Copyright (C) 2000-2007 Jorge Arellano Cid <jcid@dillo.org>
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
5 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
6 * This program is free software; you can redistribute it and/or modify
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
7 * it under the terms of the GNU General Public License as published by
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
8 * the Free Software Foundation; either version 3 of the License, or
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
9 * (at your option) any later version.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
10 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
11
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
12 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
13 * Directory scanning is no longer streamed, but it gets sorted instead!
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
14 * Directory entries on top, files next.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
15 * With new HTML layout.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
16 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
17
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
18 #include <ctype.h> /* for tolower */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
19 #include <errno.h> /* for errno */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
20 #include <stdio.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
21 #include <stdlib.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
22 #include <string.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
23 #include <unistd.h>
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
24 #include <sys/select.h>
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
25 #include <sys/socket.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
26 #include <sys/stat.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
27 #include <sys/types.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
28 #include <sys/time.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
29 #include <sys/un.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
30 #include <dirent.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
31 #include <fcntl.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
32 #include <time.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
33 #include <signal.h>
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
34 #include <netinet/in.h>
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
35
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
36 #include "../dpip/dpip.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
37 #include "dpiutil.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
38 #include "d_size.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
39
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
40 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
41 * Debugging macros
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
42 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
43 #define _MSG(...)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
44 #define MSG(...) printf("[file dpi]: " __VA_ARGS__)
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
45 #define _MSG_RAW(...)
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
46 #define MSG_RAW(...) printf(__VA_ARGS__)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
47
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
48
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
49 #define MAXNAMESIZE 30
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
50 #define HIDE_DOTFILES TRUE
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
51
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
52 /*
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
53 * Communication flags
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
54 */
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
55 #define FILE_AUTH_OK 1 /* Authentication done */
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
56 #define FILE_READ 2 /* Waiting data */
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
57 #define FILE_WRITE 4 /* Sending data */
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
58 #define FILE_DONE 8 /* Operation done */
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
59 #define FILE_ERR 16 /* Operation error */
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
60
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
61
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
62 typedef enum {
1389
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
63 st_start = 10,
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
64 st_dpip,
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
65 st_http,
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
66 st_content,
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
67 st_done,
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
68 st_err
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
69 } FileState;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
70
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
71 typedef struct {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
72 char *full_path;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
73 const char *filename;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
74 off_t size;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
75 mode_t mode;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
76 time_t mtime;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
77 } FileInfo;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
78
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
79 typedef struct {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
80 char *dirname;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
81 Dlist *flist; /* List of files and subdirectories (for sorting) */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
82 } DilloDir;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
83
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
84 typedef struct {
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
85 Dsh *sh;
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
86 char *orig_url;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
87 char *filename;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
88 int file_fd;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
89 off_t file_sz;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
90 DilloDir *d_dir;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
91 FileState state;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
92 int err_code;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
93 int flags;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
94 int old_style;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
95 } ClientInfo;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
96
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
97 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
98 * Forward references
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
99 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
100 static const char *File_content_type(const char *filename);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
101
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
102 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
103 * Global variables
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
104 */
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
105 static int DPIBYE = 0;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
106 static int OLD_STYLE = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
107 /* A list for the clients we are serving */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
108 static Dlist *Clients;
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
109 /* Set of filedescriptors we're working on */
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
110 fd_set read_set, write_set;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
111
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
112
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
113 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
114 * Close a file descriptor, but handling EINTR
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
115 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
116 static void File_close(int fd)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
117 {
1389
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
118 while (fd >= 0 && close(fd) < 0 && errno == EINTR)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
119 ;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
120 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
121
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
122 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
123 * Detects 'Content-Type' when the server does not supply one.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
124 * It uses the magic(5) logic from file(1). Currently, it
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
125 * only checks the few mime types that Dillo supports.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
126 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
127 * 'Data' is a pointer to the first bytes of the raw data.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
128 * (this is based on a_Misc_get_content_type_from_data())
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
129 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
130 static const char *File_get_content_type_from_data(void *Data, size_t Size)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
131 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
132 static const char *Types[] = {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
133 "application/octet-stream",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
134 "text/html", "text/plain",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
135 "image/gif", "image/png", "image/jpeg",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
136 };
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
137 int Type = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
138 char *p = Data;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
139 size_t i, non_ascci;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
140
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
141 _MSG("File_get_content_type_from_data:: Size = %d\n", Size);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
142
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
143 /* HTML try */
1221
f34b803b8639 Handle signed chars. Aadded dIsspace() and dIsalnum() to dlib
Jorge Arellano Cid <jcid@dillo.org>
parents: 1062
diff changeset
144 for (i = 0; i < Size && dIsspace(p[i]); ++i);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
145 if ((Size - i >= 5 && !dStrncasecmp(p+i, "<html", 5)) ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
146 (Size - i >= 5 && !dStrncasecmp(p+i, "<head", 5)) ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
147 (Size - i >= 6 && !dStrncasecmp(p+i, "<title", 6)) ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
148 (Size - i >= 14 && !dStrncasecmp(p+i, "<!doctype html", 14)) ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
149 /* this line is workaround for FTP through the Squid proxy */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
150 (Size - i >= 17 && !dStrncasecmp(p+i, "<!-- HTML listing", 17))) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
151
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
152 Type = 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
153
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
154 /* Images */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
155 } else if (Size >= 4 && !dStrncasecmp(p, "GIF8", 4)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
156 Type = 3;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
157 } else if (Size >= 4 && !dStrncasecmp(p, "\x89PNG", 4)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
158 Type = 4;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
159 } else if (Size >= 2 && !dStrncasecmp(p, "\xff\xd8", 2)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
160 /* JPEG has the first 2 bytes set to 0xffd8 in BigEndian - looking
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
161 * at the character representation should be machine independent. */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
162 Type = 5;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
163
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
164 /* Text */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
165 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
166 /* We'll assume "text/plain" if the set of chars above 127 is <= 10
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
167 * in a 256-bytes sample. Better heuristics are welcomed! :-) */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
168 non_ascci = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
169 Size = MIN (Size, 256);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
170 for (i = 0; i < Size; i++)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
171 if ((uchar_t) p[i] > 127)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
172 ++non_ascci;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
173 if (Size == 256) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
174 Type = (non_ascci > 10) ? 0 : 2;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
175 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
176 Type = (non_ascci > 0) ? 0 : 2;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
177 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
178 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
179
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
180 return (Types[Type]);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
181 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
182
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
183 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
184 * Compare two FileInfo pointers
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
185 * This function is used for sorting directories
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
186 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
187 static int File_comp(const FileInfo *f1, const FileInfo *f2)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
188 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
189 if (S_ISDIR(f1->mode)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
190 if (S_ISDIR(f2->mode)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
191 return strcmp(f1->filename, f2->filename);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
192 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
193 return -1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
194 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
195 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
196 if (S_ISDIR(f2->mode)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
197 return 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
198 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
199 return strcmp(f1->filename, f2->filename);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
200 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
201 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
202 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
203
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
204 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
205 * Allocate a DilloDir structure, set safe values in it and sort the entries.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
206 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
207 static DilloDir *File_dillodir_new(char *dirname)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
208 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
209 struct stat sb;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
210 struct dirent *de;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
211 DIR *dir;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
212 DilloDir *Ddir;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
213 FileInfo *finfo;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
214 char *fname;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
215 int dirname_len;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
216
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
217 if (!(dir = opendir(dirname)))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
218 return NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
219
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
220 Ddir = dNew(DilloDir, 1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
221 Ddir->dirname = dStrdup(dirname);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
222 Ddir->flist = dList_new(512);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
223
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
224 dirname_len = strlen(Ddir->dirname);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
225
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
226 /* Scan every name and sort them */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
227 while ((de = readdir(dir)) != 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
228 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
229 continue; /* skip "." and ".." */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
230
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
231 if (HIDE_DOTFILES) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
232 /* Don't add hidden files or backup files to the list */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
233 if (de->d_name[0] == '.' ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
234 de->d_name[0] == '#' ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
235 (de->d_name[0] != '\0' &&
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
236 de->d_name[strlen(de->d_name) - 1] == '~'))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
237 continue;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
238 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
239
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
240 fname = dStrconcat(Ddir->dirname, de->d_name, NULL);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
241 if (stat(fname, &sb) == -1) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
242 dFree(fname);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
243 continue; /* ignore files we can't stat */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
244 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
245
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
246 finfo = dNew(FileInfo, 1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
247 finfo->full_path = fname;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
248 finfo->filename = fname + dirname_len;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
249 finfo->size = sb.st_size;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
250 finfo->mode = sb.st_mode;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
251 finfo->mtime = sb.st_mtime;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
252
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
253 dList_append(Ddir->flist, finfo);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
254 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
255
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
256 closedir(dir);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
257
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
258 /* sort the entries */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
259 dList_sort(Ddir->flist, (dCompareFunc)File_comp);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
260
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
261 return Ddir;
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 * Deallocate a DilloDir structure.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
266 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
267 static void File_dillodir_free(DilloDir *Ddir)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
268 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
269 int i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
270 FileInfo *finfo;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
271
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
272 dReturn_if (Ddir == NULL);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
273
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
274 for (i = 0; i < dList_length(Ddir->flist); ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
275 finfo = dList_nth_data(Ddir->flist, i);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
276 dFree(finfo->full_path);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
277 dFree(finfo);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
278 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
279
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
280 dList_free(Ddir->flist);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
281 dFree(Ddir->dirname);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
282 dFree(Ddir);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
283 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
284
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
285 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
286 * Output the string for parent directory
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
287 */
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
288 static void File_print_parent_dir(ClientInfo *client, const char *dirname)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
289 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
290 if (strcmp(dirname, "/") != 0) { /* Not the root dir */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
291 char *p, *parent, *HUparent, *Uparent;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
292
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
293 parent = dStrdup(dirname);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
294 /* cut trailing slash */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
295 parent[strlen(parent) - 1] = '\0';
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
296 /* make 'parent' have the parent dir path */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
297 if ((p = strrchr(parent, '/')))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
298 *(p + 1) = '\0';
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
299
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
300 Uparent = Escape_uri_str(parent, NULL);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
301 HUparent = Escape_html_str(Uparent);
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
302 a_Dpip_dsh_printf(client->sh, 0,
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
303 "<a href='file:%s'>Parent directory</a>", HUparent);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
304 dFree(HUparent);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
305 dFree(Uparent);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
306 dFree(parent);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
307 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
308 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
309
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
310 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
311 * Given a timestamp, output an HTML-formatted date string.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
312 */
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
313 static void File_print_mtime(ClientInfo *client, time_t mtime)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
314 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
315 char *ds = ctime(&mtime);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
316
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
317 /* Month, day and {hour or year} */
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
318 if (client->old_style) {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
319 a_Dpip_dsh_printf(client->sh, 0, " %.3s %.2s", ds + 4, ds + 8);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
320 if (time(NULL) - mtime > 15811200) {
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
321 a_Dpip_dsh_printf(client->sh, 0, " %.4s", ds + 20);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
322 } else {
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
323 a_Dpip_dsh_printf(client->sh, 0, " %.5s", ds + 11);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
324 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
325 } else {
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
326 a_Dpip_dsh_printf(client->sh, 0,
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
327 "<td>%.3s&nbsp;%.2s&nbsp;%.5s", ds + 4, ds + 8,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
328 /* (more than 6 months old) ? year : hour; */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
329 (time(NULL) - mtime > 15811200) ? ds + 20 : ds + 11);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
330 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
331 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
332
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
333 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
334 * Return a HTML-line from file info.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
335 */
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
336 static void File_info2html(ClientInfo *client, FileInfo *finfo, int n)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
337 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
338 int size;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
339 char *sizeunits;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
340 char namebuf[MAXNAMESIZE + 1];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
341 char *Uref, *HUref, *Hname;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
342 const char *ref, *filecont, *name = finfo->filename;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
343
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
344 if (finfo->size <= 9999) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
345 size = finfo->size;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
346 sizeunits = "bytes";
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
347 } else if (finfo->size / 1024 <= 9999) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
348 size = finfo->size / 1024 + (finfo->size % 1024 >= 1024 / 2);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
349 sizeunits = "Kb";
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
350 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
351 size = finfo->size / 1048576 + (finfo->size % 1048576 >= 1048576 / 2);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
352 sizeunits = "Mb";
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
353 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
354
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
355 /* we could note if it's a symlink... */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
356 if S_ISDIR (finfo->mode) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
357 filecont = "Directory";
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
358 } else if (finfo->mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
359 filecont = "Executable";
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
360 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
361 filecont = File_content_type(finfo->full_path);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
362 if (!filecont || !strcmp(filecont, "application/octet-stream"))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
363 filecont = "unknown";
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
364 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
365
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
366 ref = name;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
367
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
368 if (strlen(name) > MAXNAMESIZE) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
369 memcpy(namebuf, name, MAXNAMESIZE - 3);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
370 strcpy(namebuf + (MAXNAMESIZE - 3), "...");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
371 name = namebuf;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
372 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
373
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
374 /* escape problematic filenames */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
375 Uref = Escape_uri_str(ref, NULL);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
376 HUref = Escape_html_str(Uref);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
377 Hname = Escape_html_str(name);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
378
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
379 if (client->old_style) {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
380 char *dots = ".. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..";
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
381 int ndots = MAXNAMESIZE - strlen(name);
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
382 a_Dpip_dsh_printf(client->sh, 0,
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
383 "%s<a href='%s'>%s</a>"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
384 " %s"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
385 " %-11s%4d %-5s",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
386 S_ISDIR (finfo->mode) ? ">" : " ", HUref, Hname,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
387 dots + 50 - (ndots > 0 ? ndots : 0),
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
388 filecont, size, sizeunits);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
389
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
390 } else {
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
391 a_Dpip_dsh_printf(client->sh, 0,
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
392 "<tr align=center %s><td>%s<td align=left><a href='%s'>%s</a>"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
393 "<td>%s<td>%d&nbsp;%s",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
394 (n & 1) ? "bgcolor=#dcdcdc" : "",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
395 S_ISDIR (finfo->mode) ? ">" : " ", HUref, Hname,
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
396 filecont, size, sizeunits);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
397 }
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
398 File_print_mtime(client, finfo->mtime);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
399 a_Dpip_dsh_write_str(client->sh, 0, "\n");
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
400
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
401 dFree(Hname);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
402 dFree(HUref);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
403 dFree(Uref);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
404 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
405
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
406 /*
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
407 * Send the HTML directory page in HTTP.
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
408 */
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
409 static void File_send_dir(ClientInfo *client)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
410 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
411 int n;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
412 char *d_cmd, *Hdirname, *Udirname, *HUdirname;
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
413 DilloDir *Ddir = client->d_dir;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
414
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
415 if (client->state == st_start) {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
416 /* Send DPI command */
1591
cf7f2d3312fb trim some spaces
corvid <corvid@lavabit.com>
parents: 1439
diff changeset
417 d_cmd = a_Dpip_build_cmd("cmd=%s url=%s", "start_send_page",
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
418 client->orig_url);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
419 a_Dpip_dsh_write_str(client->sh, 1, d_cmd);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
420 dFree(d_cmd);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
421 client->state = st_dpip;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
422
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
423 } else if (client->state == st_dpip) {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
424 /* send HTTP header and HTML top part */
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
425
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
426 /* Send page title */
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
427 Udirname = Escape_uri_str(Ddir->dirname, NULL);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
428 HUdirname = Escape_html_str(Udirname);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
429 Hdirname = Escape_html_str(Ddir->dirname);
1591
cf7f2d3312fb trim some spaces
corvid <corvid@lavabit.com>
parents: 1439
diff changeset
430
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
431 a_Dpip_dsh_printf(client->sh, 0,
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
432 "HTTP/1.1 200 OK\r\n"
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
433 "Content-Type: text/html\r\n"
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
434 "\r\n"
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
435 "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>\n"
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
436 "<HTML>\n<HEAD>\n <BASE href='file:%s'>\n"
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
437 " <TITLE>file:%s</TITLE>\n</HEAD>\n"
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
438 "<BODY><H1>Directory listing of %s</H1>\n",
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
439 HUdirname, Hdirname, Hdirname);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
440 dFree(Hdirname);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
441 dFree(HUdirname);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
442 dFree(Udirname);
1591
cf7f2d3312fb trim some spaces
corvid <corvid@lavabit.com>
parents: 1439
diff changeset
443
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
444 if (client->old_style) {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
445 a_Dpip_dsh_write_str(client->sh, 0, "<pre>\n");
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
446 }
1591
cf7f2d3312fb trim some spaces
corvid <corvid@lavabit.com>
parents: 1439
diff changeset
447
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
448 /* Output the parent directory */
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
449 File_print_parent_dir(client, Ddir->dirname);
1591
cf7f2d3312fb trim some spaces
corvid <corvid@lavabit.com>
parents: 1439
diff changeset
450
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
451 /* HTML style toggle */
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
452 a_Dpip_dsh_write_str(client->sh, 0,
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
453 "&nbsp;&nbsp;<a href='dpi:/file/toggle'>%</a>\n");
1591
cf7f2d3312fb trim some spaces
corvid <corvid@lavabit.com>
parents: 1439
diff changeset
454
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
455 if (dList_length(Ddir->flist)) {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
456 if (client->old_style) {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
457 a_Dpip_dsh_write_str(client->sh, 0, "\n\n");
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
458 } else {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
459 a_Dpip_dsh_write_str(client->sh, 0,
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
460 "<br><br>\n"
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
461 "<table border=0 cellpadding=1 cellspacing=0"
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
462 " bgcolor=#E0E0E0 width=100%>\n"
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
463 "<tr align=center>\n"
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
464 "<td>\n"
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
465 "<td width=60%><b>Filename</b>"
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
466 "<td><b>Type</b>"
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
467 "<td><b>Size</b>"
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
468 "<td><b>Modified&nbsp;at</b>\n");
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
469 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
470 } else {
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
471 a_Dpip_dsh_write_str(client->sh, 0, "<br><br>Directory is empty...");
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
472 }
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
473 client->state = st_http;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
474
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
475 } else if (client->state == st_http) {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
476 /* send directories as HTML contents */
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
477 for (n = 0; n < dList_length(Ddir->flist); ++n) {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
478 File_info2html(client, dList_nth_data(Ddir->flist,n), n+1);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
479 }
1591
cf7f2d3312fb trim some spaces
corvid <corvid@lavabit.com>
parents: 1439
diff changeset
480
1692
0399d0ecb3b9 file dpi close pre tag for empty directory listing
corvid <corvid@lavabit.com>
parents: 1596
diff changeset
481 if (client->old_style) {
0399d0ecb3b9 file dpi close pre tag for empty directory listing
corvid <corvid@lavabit.com>
parents: 1596
diff changeset
482 a_Dpip_dsh_write_str(client->sh, 0, "</pre>\n");
0399d0ecb3b9 file dpi close pre tag for empty directory listing
corvid <corvid@lavabit.com>
parents: 1596
diff changeset
483 } else if (dList_length(Ddir->flist)) {
0399d0ecb3b9 file dpi close pre tag for empty directory listing
corvid <corvid@lavabit.com>
parents: 1596
diff changeset
484 a_Dpip_dsh_write_str(client->sh, 0, "</table>\n");
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
485 }
1591
cf7f2d3312fb trim some spaces
corvid <corvid@lavabit.com>
parents: 1439
diff changeset
486
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
487 a_Dpip_dsh_write_str(client->sh, 1, "</BODY></HTML>\n");
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
488 client->state = st_content;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
489 client->flags |= FILE_DONE;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
490 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
491 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
492
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
493 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
494 * Return a content type based on the extension of the filename.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
495 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
496 static const char *File_ext(const char *filename)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
497 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
498 char *e;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
499
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
500 if (!(e = strrchr(filename, '.')))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
501 return NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
502
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
503 e++;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
504
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
505 if (!dStrcasecmp(e, "gif")) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
506 return "image/gif";
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
507 } else if (!dStrcasecmp(e, "jpg") ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
508 !dStrcasecmp(e, "jpeg")) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
509 return "image/jpeg";
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
510 } else if (!dStrcasecmp(e, "png")) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
511 return "image/png";
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
512 } else if (!dStrcasecmp(e, "html") ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
513 !dStrcasecmp(e, "htm") ||
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
514 !dStrcasecmp(e, "shtml")) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
515 return "text/html";
1062
e6c69e8b7bfa Add ".txt" as plain/text file extension in the file dpi
corvid <corvid@lavabit.com>
parents: 1059
diff changeset
516 } else if (!dStrcasecmp(e, "txt")) {
e6c69e8b7bfa Add ".txt" as plain/text file extension in the file dpi
corvid <corvid@lavabit.com>
parents: 1059
diff changeset
517 return "text/plain";
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
518 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
519 return NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
520 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
521 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
522
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
523 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
524 * Based on the extension, return the content_type for the file.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
525 * (if there's no extension, analyze the data and try to figure it out)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
526 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
527 static const char *File_content_type(const char *filename)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
528 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
529 int fd;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
530 struct stat sb;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
531 const char *ct;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
532 char buf[256];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
533 ssize_t buf_size;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
534
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
535 if (!(ct = File_ext(filename))) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
536 /* everything failed, let's analyze the data... */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
537 if ((fd = open(filename, O_RDONLY | O_NONBLOCK)) != -1) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
538 if ((buf_size = read(fd, buf, 256)) == 256 ) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
539 ct = File_get_content_type_from_data(buf, (size_t)buf_size);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
540
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
541 } else if (stat(filename, &sb) != -1 &&
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
542 buf_size > 0 && buf_size == sb.st_size) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
543 ct = File_get_content_type_from_data(buf, (size_t)buf_size);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
544 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
545 File_close(fd);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
546 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
547 }
1389
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
548 _MSG("File_content_type: name=%s ct=%s\n", filename, ct);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
549 return ct;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
550 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
551
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
552 /*
473
adb344b84f10 Switched file dpi error messages to HTML.
corvid <corvid@lavabit.com>
parents: 371
diff changeset
553 * Send an error page
adb344b84f10 Switched file dpi error messages to HTML.
corvid <corvid@lavabit.com>
parents: 371
diff changeset
554 */
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
555 static void File_prepare_send_error_page(ClientInfo *client, int res,
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
556 const char *orig_url)
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
557 {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
558 client->state = st_err;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
559 client->err_code = res;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
560 client->orig_url = dStrdup(orig_url);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
561 client->flags &= ~FILE_READ;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
562 client->flags |= FILE_WRITE;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
563 }
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
564
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
565 /*
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
566 * Send an error page
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
567 */
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
568 static void File_send_error_page(ClientInfo *client)
473
adb344b84f10 Switched file dpi error messages to HTML.
corvid <corvid@lavabit.com>
parents: 371
diff changeset
569 {
adb344b84f10 Switched file dpi error messages to HTML.
corvid <corvid@lavabit.com>
parents: 371
diff changeset
570 const char *status;
adb344b84f10 Switched file dpi error messages to HTML.
corvid <corvid@lavabit.com>
parents: 371
diff changeset
571 char *d_cmd;
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
572 Dstr *body = dStr_sized_new(128);
473
adb344b84f10 Switched file dpi error messages to HTML.
corvid <corvid@lavabit.com>
parents: 371
diff changeset
573
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
574 if (client->err_code == EACCES) {
473
adb344b84f10 Switched file dpi error messages to HTML.
corvid <corvid@lavabit.com>
parents: 371
diff changeset
575 status = "403 Forbidden";
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
576 } else if (client->err_code == ENOENT) {
473
adb344b84f10 Switched file dpi error messages to HTML.
corvid <corvid@lavabit.com>
parents: 371
diff changeset
577 status = "404 Not Found";
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
578 } else {
473
adb344b84f10 Switched file dpi error messages to HTML.
corvid <corvid@lavabit.com>
parents: 371
diff changeset
579 /* good enough */
adb344b84f10 Switched file dpi error messages to HTML.
corvid <corvid@lavabit.com>
parents: 371
diff changeset
580 status = "500 Internal Server Error";
adb344b84f10 Switched file dpi error messages to HTML.
corvid <corvid@lavabit.com>
parents: 371
diff changeset
581 }
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
582 dStr_append(body, status);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
583 dStr_append(body, "\n");
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
584 dStr_append(body, dStrerror(client->err_code));
473
adb344b84f10 Switched file dpi error messages to HTML.
corvid <corvid@lavabit.com>
parents: 371
diff changeset
585
adb344b84f10 Switched file dpi error messages to HTML.
corvid <corvid@lavabit.com>
parents: 371
diff changeset
586 /* Send DPI command */
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
587 d_cmd = a_Dpip_build_cmd("cmd=%s url=%s", "start_send_page",
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
588 client->orig_url);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
589 a_Dpip_dsh_write_str(client->sh, 0, d_cmd);
473
adb344b84f10 Switched file dpi error messages to HTML.
corvid <corvid@lavabit.com>
parents: 371
diff changeset
590 dFree(d_cmd);
adb344b84f10 Switched file dpi error messages to HTML.
corvid <corvid@lavabit.com>
parents: 371
diff changeset
591
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
592 a_Dpip_dsh_printf(client->sh, 0,
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
593 "HTTP/1.1 %s\r\n"
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
594 "Content-Type: text/plain\r\n"
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
595 "Content-Length: %d\r\n"
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
596 "\r\n"
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
597 "%s",
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
598 status, body->len, body->str);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
599 dStr_free(body, TRUE);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
600
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
601 client->flags |= FILE_DONE;
473
adb344b84f10 Switched file dpi error messages to HTML.
corvid <corvid@lavabit.com>
parents: 371
diff changeset
602 }
adb344b84f10 Switched file dpi error messages to HTML.
corvid <corvid@lavabit.com>
parents: 371
diff changeset
603
adb344b84f10 Switched file dpi error messages to HTML.
corvid <corvid@lavabit.com>
parents: 371
diff changeset
604 /*
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
605 * Scan the directory, sort and prepare to send it enclosed in HTTP.
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
606 */
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
607 static int File_prepare_send_dir(ClientInfo *client,
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
608 const char *DirName, const char *orig_url)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
609 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
610 Dstr *ds_dirname;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
611 DilloDir *Ddir;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
612
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
613 /* Let's make sure this directory url has a trailing slash */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
614 ds_dirname = dStr_new(DirName);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
615 if (ds_dirname->str[ds_dirname->len - 1] != '/')
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
616 dStr_append(ds_dirname, "/");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
617
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
618 /* Let's get a structure ready for transfer */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
619 Ddir = File_dillodir_new(ds_dirname->str);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
620 dStr_free(ds_dirname, TRUE);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
621 if (Ddir) {
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
622 /* looks ok, set things accordingly */
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
623 client->orig_url = dStrdup(orig_url);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
624 client->d_dir = Ddir;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
625 client->state = st_start;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
626 client->flags &= ~FILE_READ;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
627 client->flags |= FILE_WRITE;
473
adb344b84f10 Switched file dpi error messages to HTML.
corvid <corvid@lavabit.com>
parents: 371
diff changeset
628 return 0;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
629 } else
473
adb344b84f10 Switched file dpi error messages to HTML.
corvid <corvid@lavabit.com>
parents: 371
diff changeset
630 return EACCES;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
631 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
632
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
633 /*
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
634 * Prepare to send HTTP headers and then the file itself.
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
635 */
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
636 static int File_prepare_send_file(ClientInfo *client,
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
637 const char *filename,
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
638 const char *orig_url)
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
639 {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
640 int fd, res = -1;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
641 struct stat sb;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
642
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
643 if (stat(filename, &sb) != 0) {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
644 /* prepare a file-not-found error */
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
645 res = ENOENT;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
646 } else if ((fd = open(filename, O_RDONLY | O_NONBLOCK)) < 0) {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
647 /* prepare an error message */
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
648 res = errno;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
649 } else {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
650 /* looks ok, set things accordingly */
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
651 client->file_fd = fd;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
652 client->file_sz = sb.st_size;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
653 client->d_dir = NULL;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
654 client->state = st_start;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
655 client->filename = dStrdup(filename);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
656 client->orig_url = dStrdup(orig_url);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
657 client->flags &= ~FILE_READ;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
658 client->flags |= FILE_WRITE;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
659 res = 0;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
660 }
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
661 return res;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
662 }
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
663
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
664 /*
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
665 * Try to stat the file and determine if it's readable.
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
666 */
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
667 static void File_get(ClientInfo *client, const char *filename,
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
668 const char *orig_url)
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
669 {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
670 int res;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
671 struct stat sb;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
672
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
673 if (stat(filename, &sb) != 0) {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
674 /* stat failed, prepare a file-not-found error. */
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
675 res = ENOENT;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
676 } else if (S_ISDIR(sb.st_mode)) {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
677 /* set up for reading directory */
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
678 res = File_prepare_send_dir(client, filename, orig_url);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
679 } else {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
680 /* set up for reading a file */
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
681 res = File_prepare_send_file(client, filename, orig_url);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
682 }
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
683 if (res != 0) {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
684 File_prepare_send_error_page(client, res, orig_url);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
685 }
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
686 }
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
687
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
688 /*
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
689 * Send HTTP headers and then the file itself.
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
690 */
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
691 static int File_send_file(ClientInfo *client)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
692 {
1390
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
693 //#define LBUF 1
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
694 #define LBUF 16*1024
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
695
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
696 const char *ct;
281
30963ebac871 - Enabled the file dpi to look inside gzipped files.
jcid
parents: 51
diff changeset
697 const char *unknown_type = "application/octet-stream";
30963ebac871 - Enabled the file dpi to look inside gzipped files.
jcid
parents: 51
diff changeset
698 char buf[LBUF], *d_cmd, *name;
1389
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
699 int st, st2, namelen;
281
30963ebac871 - Enabled the file dpi to look inside gzipped files.
jcid
parents: 51
diff changeset
700 bool_t gzipped = FALSE;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
701
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
702 if (client->state == st_start) {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
703 /* Send DPI command */
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
704 d_cmd = a_Dpip_build_cmd("cmd=%s url=%s", "start_send_page",
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
705 client->orig_url);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
706 a_Dpip_dsh_write_str(client->sh, 1, d_cmd);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
707 dFree(d_cmd);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
708 client->state = st_dpip;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
709
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
710 } else if (client->state == st_dpip) {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
711 /* send HTTP header */
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
712
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
713 /* Check for gzipped file */
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
714 namelen = strlen(client->filename);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
715 if (namelen > 3 && !dStrcasecmp(client->filename + namelen - 3, ".gz")) {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
716 gzipped = TRUE;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
717 namelen -= 3;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
718 }
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
719 /* Content-Type info is based on filename extension (with ".gz" removed).
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
720 * If there's no known extension, perform data sniffing.
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
721 * If this doesn't lead to a conclusion, use "application/octet-stream".
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
722 */
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
723 name = dStrndup(client->filename, namelen);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
724 if (!(ct = File_content_type(name)))
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
725 ct = unknown_type;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
726 dFree(name);
1591
cf7f2d3312fb trim some spaces
corvid <corvid@lavabit.com>
parents: 1439
diff changeset
727
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
728 /* Send HTTP headers */
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
729 a_Dpip_dsh_write_str(client->sh, 0, "HTTP/1.1 200 OK\r\n");
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
730 if (gzipped) {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
731 a_Dpip_dsh_write_str(client->sh, 0, "Content-Encoding: gzip\r\n");
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
732 }
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
733 if (!gzipped || strcmp(ct, unknown_type)) {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
734 a_Dpip_dsh_printf(client->sh, 0, "Content-Type: %s\r\n", ct);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
735 } else {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
736 /* If we don't know type for gzipped data, let dillo figure it out. */
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
737 }
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
738 a_Dpip_dsh_printf(client->sh, 1,
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
739 "Content-Length: %ld\r\n"
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
740 "\r\n",
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
741 client->file_sz);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
742 client->state = st_http;
281
30963ebac871 - Enabled the file dpi to look inside gzipped files.
jcid
parents: 51
diff changeset
743
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
744 } else if (client->state == st_http) {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
745 /* Send body -- raw file contents */
1390
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
746 if ((st = a_Dpip_dsh_tryflush(client->sh)) < 0) {
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
747 client->flags |= (st == -3) ? FILE_ERR : 0;
1389
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
748 } else {
1390
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
749 /* no pending data, let's send new data */
1389
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
750 do {
1390
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
751 st2 = read(client->file_fd, buf, LBUF);
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
752 } while (st2 < 0 && errno == EINTR);
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
753 if (st2 < 0) {
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
754 MSG("\nERROR while reading from file '%s': %s\n\n",
1389
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
755 client->filename, dStrerror(errno));
1390
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
756 client->flags |= FILE_ERR;
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
757 } else if (st2 == 0) {
1389
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
758 client->state = st_content;
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
759 client->flags |= FILE_DONE;
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
760 } else {
1390
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
761 /* ok to write */
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
762 st = a_Dpip_dsh_trywrite(client->sh, buf, st2);
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
763 client->flags |= (st == -3) ? FILE_ERR : 0;
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
764 }
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
765 }
281
30963ebac871 - Enabled the file dpi to look inside gzipped files.
jcid
parents: 51
diff changeset
766 }
930
b277eed3119c whitespace cleanup: 's/ +$//g'
Jorge Arellano Cid <jcid@dillo.org>
parents: 483
diff changeset
767
473
adb344b84f10 Switched file dpi error messages to HTML.
corvid <corvid@lavabit.com>
parents: 371
diff changeset
768 return 0;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
769 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
770
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
771 /*
473
adb344b84f10 Switched file dpi error messages to HTML.
corvid <corvid@lavabit.com>
parents: 371
diff changeset
772 * Given a hex octet (e3, 2F, 20), return the corresponding
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
773 * character if the octet is valid, and -1 otherwise
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
774 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
775 static int File_parse_hex_octet(const char *s)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
776 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
777 int hex_value;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
778 char *tail, hex[3];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
779
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
780 if ((hex[0] = s[0]) && (hex[1] = s[1])) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
781 hex[2] = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
782 hex_value = strtol(hex, &tail, 16);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
783 if (tail - hex == 2)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
784 return hex_value;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
785 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
786
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
787 return -1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
788 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
789
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
790 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
791 * Make a file URL into a human (and machine) readable path.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
792 * The idea is to always have a path that starts with only one slash.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
793 * Embedded slashes are ignored.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
794 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
795 static char *File_normalize_path(const char *orig)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
796 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
797 char *str = (char *) orig, *basename = NULL, *ret = NULL, *p;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
798
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
799 dReturn_val_if (orig == NULL, ret);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
800
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
801 /* Make sure the string starts with "file:/" */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
802 if (strncmp(str, "file:/", 5) != 0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
803 return ret;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
804 str += 5;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
805
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
806 /* Skip "localhost" */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
807 if (dStrncasecmp(str, "//localhost/", 12) == 0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
808 str += 11;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
809
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
810 /* Skip packed slashes, and leave just one */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
811 while (str[0] == '/' && str[1] == '/')
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
812 str++;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
813
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
814 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
815 int i, val;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
816 Dstr *ds = dStr_sized_new(32);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
817 dStr_sprintf(ds, "%s%s%s",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
818 basename ? basename : "",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
819 basename ? "/" : "",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
820 str);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
821 dFree(basename);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
822
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
823 /* Parse possible hexadecimal octets in the URI path */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
824 for (i = 0; ds->str[i]; ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
825 if (ds->str[i] == '%' &&
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
826 (val = File_parse_hex_octet(ds->str + i+1)) != -1) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
827 ds->str[i] = val;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
828 dStr_erase(ds, i+1, 2);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
829 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
830 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
831 /* Remove the fragment if not a part of the filename */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
832 if ((p = strrchr(ds->str, '#')) != NULL && access(ds->str, F_OK) != 0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
833 dStr_truncate(ds, p - ds->str);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
834 ret = ds->str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
835 dStr_free(ds, 0);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
836 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
837
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
838 return ret;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
839 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
840
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
841 /*
473
adb344b84f10 Switched file dpi error messages to HTML.
corvid <corvid@lavabit.com>
parents: 371
diff changeset
842 * Set the style flag and ask for a reload, so it shows immediately.
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
843 */
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
844 static void File_toggle_html_style(ClientInfo *client)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
845 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
846 char *d_cmd;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
847
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
848 OLD_STYLE = !OLD_STYLE;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
849 d_cmd = a_Dpip_build_cmd("cmd=%s", "reload_request");
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
850 a_Dpip_dsh_write_str(client->sh, 1, d_cmd);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
851 dFree(d_cmd);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
852 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
853
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
854 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
855 * Perform any necessary cleanups upon abnormal termination
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
856 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
857 static void termination_handler(int signum)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
858 {
1389
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
859 MSG("\nexit(signum), signum=%d\n\n", signum);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
860 exit(signum);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
861 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
862
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
863
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
864 /* Client handling ----------------------------------------------------------*/
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
865
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
866 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
867 * Add a new client to the list.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
868 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
869 static ClientInfo *File_add_client(int sock_fd)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
870 {
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
871 ClientInfo *new_client;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
872
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
873 new_client = dNew(ClientInfo, 1);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
874 new_client->sh = a_Dpip_dsh_new(sock_fd, sock_fd, 8*1024);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
875 new_client->orig_url = NULL;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
876 new_client->filename = NULL;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
877 new_client->file_fd = -1;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
878 new_client->file_sz = 0;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
879 new_client->d_dir = NULL;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
880 new_client->state = 0;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
881 new_client->err_code = 0;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
882 new_client->flags = FILE_READ;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
883 new_client->old_style = OLD_STYLE;
1389
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
884
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
885 dList_append(Clients, new_client);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
886 return new_client;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
887 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
888
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
889 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
890 * Remove a client from the list.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
891 */
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
892 static void File_remove_client(ClientInfo *client)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
893 {
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
894 dList_remove(Clients, (void *)client);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
895
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
896 _MSG("Closing Socket Handler\n");
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
897 a_Dpip_dsh_close(client->sh);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
898 a_Dpip_dsh_free(client->sh);
1389
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
899 File_close(client->file_fd);
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
900 dFree(client->orig_url);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
901 dFree(client->filename);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
902 File_dillodir_free(client->d_dir);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
903
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
904 dFree(client);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
905 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
906
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
907 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
908 * Serve this client.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
909 */
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
910 static void File_serve_client(void *data, int f_write)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
911 {
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
912 char *dpip_tag = NULL, *cmd = NULL, *url = NULL, *path;
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
913 ClientInfo *client = data;
1387
16cf380cd04c Convert dpid, file dpi and cookies dpi to dsh API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1384
diff changeset
914 int st;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
915
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
916 while (1) {
1389
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
917 _MSG("File_serve_client %p, flags=%d state=%d\n",
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
918 client, client->flags, client->state);
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
919 if (client->flags & (FILE_DONE | FILE_ERR))
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
920 break;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
921 if (client->flags & FILE_READ) {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
922 dpip_tag = a_Dpip_dsh_read_token(client->sh, 0);
1402
86cc2f286a7a file dpi: Used NONBLOCKING for accept() (to assert it never blocks)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
923 _MSG("dpip_tag={%s}\n", dpip_tag);
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
924 if (!dpip_tag)
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
925 break;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
926 }
1591
cf7f2d3312fb trim some spaces
corvid <corvid@lavabit.com>
parents: 1439
diff changeset
927
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
928 if (client->flags & FILE_READ) {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
929 if (!(client->flags & FILE_AUTH_OK)) {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
930 /* Authenticate our client... */
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
931 st = a_Dpip_check_auth(dpip_tag);
1402
86cc2f286a7a file dpi: Used NONBLOCKING for accept() (to assert it never blocks)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
932 _MSG("a_Dpip_check_auth returned %d\n", st);
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
933 client->flags |= (st == 1) ? FILE_AUTH_OK : FILE_ERR;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
934 } else {
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
935 /* Get file request */
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
936 cmd = a_Dpip_get_attr(dpip_tag, "cmd");
1236
b912173aecd1 Added a_Dpip_get_attr_l() to DPIP's API
Jorge Arellano Cid <jcid@dillo.org>
parents: 1221
diff changeset
937 url = a_Dpip_get_attr(dpip_tag, "url");
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
938 path = File_normalize_path(url);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
939 if (cmd) {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
940 if (strcmp(cmd, "DpiBye") == 0) {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
941 DPIBYE = 1;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
942 MSG("(pid %d): Got DpiBye.\n", (int)getpid());
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
943 client->flags |= FILE_DONE;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
944 } else if (url && strcmp(url, "dpi:/file/toggle") == 0) {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
945 File_toggle_html_style(client);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
946 } else if (path) {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
947 File_get(client, path, url);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
948 } else {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
949 client->flags |= FILE_ERR;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
950 MSG("ERROR: URL was %s\n", url);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
951 }
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
952 }
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
953 dFree(path);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
954 dFree(url);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
955 dFree(cmd);
1596
c4502225d4de free dpip_tag
corvid <corvid@lavabit.com>
parents: 1591
diff changeset
956 dFree(dpip_tag);
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
957 break;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
958 }
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
959 dFree(dpip_tag);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
960
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
961 } else if (f_write) {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
962 /* send our answer */
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
963 if (client->state == st_err)
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
964 File_send_error_page(client);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
965 else if (client->d_dir)
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
966 File_send_dir(client);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
967 else
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
968 File_send_file(client);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
969 break;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
970 }
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
971 } /*while*/
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
972
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
973 client->flags |= (client->sh->status & DPIP_ERROR) ? FILE_ERR : 0;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
974 client->flags |= (client->sh->status & DPIP_EOF) ? FILE_DONE : 0;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
975 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
976
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
977 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
978 * Serve the client queue.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
979 */
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
980 static void File_serve_clients()
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
981 {
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
982 int i, f_read, f_write;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
983 ClientInfo *client;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
984
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
985 for (i = 0; (client = dList_nth_data(Clients, i)); ++i) {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
986 f_read = FD_ISSET(client->sh->fd_in, &read_set);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
987 f_write = FD_ISSET(client->sh->fd_out, &write_set);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
988 if (!f_read && !f_write)
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
989 continue;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
990 File_serve_client(client, f_write);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
991 if (client->flags & (FILE_DONE | FILE_ERR)) {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
992 File_remove_client(client);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
993 --i;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
994 }
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
995 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
996 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
997
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
998 /* --------------------------------------------------------------------------*/
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
999
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1000 /*
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1001 * Check the fd sets for activity, with a max timeout.
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1002 * return value: 0 if timeout, 1 if input available, -1 if error.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1003 */
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1004 static int File_check_fds(uint_t seconds)
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1005 {
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1006 int i, st;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1007 ClientInfo *client;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1008 struct timeval timeout;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1009
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1010 /* initialize observed file descriptors */
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1011 FD_ZERO (&read_set);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1012 FD_ZERO (&write_set);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1013 FD_SET (STDIN_FILENO, &read_set);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1014 for (i = 0; (client = dList_nth_data(Clients, i)); ++i) {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1015 if (client->flags & FILE_READ)
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1016 FD_SET (client->sh->fd_in, &read_set);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1017 if (client->flags & FILE_WRITE)
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1018 FD_SET (client->sh->fd_out, &write_set);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1019 }
1402
86cc2f286a7a file dpi: Used NONBLOCKING for accept() (to assert it never blocks)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
1020 _MSG("Watching %d fds\n", dList_length(Clients) + 1);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1021
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1022 /* Initialize the timeout data structure. */
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1023 timeout.tv_sec = seconds;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1024 timeout.tv_usec = 0;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1025
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1026 do {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1027 st = select(FD_SETSIZE, &read_set, &write_set, NULL, &timeout);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1028 } while (st == -1 && errno == EINTR);
1389
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
1029 /*
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1030 MSG_RAW(" (%d%s%s)", STDIN_FILENO,
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1031 FD_ISSET(STDIN_FILENO, &read_set) ? "R" : "",
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1032 FD_ISSET(STDIN_FILENO, &write_set) ? "W" : "");
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1033 for (i = 0; (client = dList_nth_data(Clients, i)); ++i) {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1034 MSG_RAW(" (%d%s%s)", client->sh->fd_in,
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1035 FD_ISSET(client->sh->fd_in, &read_set) ? "R" : "",
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1036 FD_ISSET(client->sh->fd_out, &write_set) ? "W" : "");
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1037 }
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1038 MSG_RAW("\n");
1389
c1c822f70582 Removed the write/fwrite mix in dpip. Introduced a_Dpip_dsh_trywrite()
Jorge Arellano Cid <jcid@dillo.org>
parents: 1388
diff changeset
1039 */
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1040 return st;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1041 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1042
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1043
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1044 int main(void)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1045 {
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1046 struct sockaddr_in sin;
1390
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
1047 socklen_t sin_sz;
1439
79fcf67eeea0 Fixed bookmarks "cancel" link (Add/Mod URL, Add Sec). Also s/tmp_fd/sock_fd.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1402
diff changeset
1048 int sock_fd, c_st, st = 1;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1049
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1050 /* Arrange the cleanup function for abnormal terminations */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1051 if (signal (SIGINT, termination_handler) == SIG_IGN)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1052 signal (SIGINT, SIG_IGN);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1053 if (signal (SIGHUP, termination_handler) == SIG_IGN)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1054 signal (SIGHUP, SIG_IGN);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1055 if (signal (SIGTERM, termination_handler) == SIG_IGN)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1056 signal (SIGTERM, SIG_IGN);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1057
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1058 MSG("(v.2) accepting connections...\n");
1390
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
1059 //sleep(20);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1060
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1061 /* initialize observed file descriptors */
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1062 FD_ZERO (&read_set);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1063 FD_ZERO (&write_set);
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1064 FD_SET (STDIN_FILENO, &read_set);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1065
1402
86cc2f286a7a file dpi: Used NONBLOCKING for accept() (to assert it never blocks)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
1066 /* Set STDIN socket nonblocking (to ensure accept() never blocks) */
86cc2f286a7a file dpi: Used NONBLOCKING for accept() (to assert it never blocks)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
1067 fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK | fcntl(STDIN_FILENO, F_GETFL));
86cc2f286a7a file dpi: Used NONBLOCKING for accept() (to assert it never blocks)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
1068
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1069 /* initialize Clients list */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1070 Clients = dList_new(512);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1071
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1072 /* some OSes may need this... */
1390
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
1073 sin_sz = sizeof(sin);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1074
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1075 /* start the service loop */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1076 while (!DPIBYE) {
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1077 /* wait for activity */
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1078 do {
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1079 c_st = File_check_fds(10);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1080 } while (c_st == 0 && !DPIBYE);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1081 if (c_st < 0) {
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1082 MSG(" select() %s\n", dStrerror(errno));
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1083 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1084 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1085 if (DPIBYE)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1086 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1087
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1088 if (FD_ISSET(STDIN_FILENO, &read_set)) {
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1089 /* accept the incoming connection */
1390
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
1090 do {
1439
79fcf67eeea0 Fixed bookmarks "cancel" link (Add/Mod URL, Add Sec). Also s/tmp_fd/sock_fd.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1402
diff changeset
1091 sock_fd = accept(STDIN_FILENO, (struct sockaddr *)&sin, &sin_sz);
79fcf67eeea0 Fixed bookmarks "cancel" link (Add/Mod URL, Add Sec). Also s/tmp_fd/sock_fd.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1402
diff changeset
1092 } while (sock_fd < 0 && errno == EINTR);
79fcf67eeea0 Fixed bookmarks "cancel" link (Add/Mod URL, Add Sec). Also s/tmp_fd/sock_fd.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1402
diff changeset
1093 if (sock_fd == -1) {
1402
86cc2f286a7a file dpi: Used NONBLOCKING for accept() (to assert it never blocks)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
1094 if (errno == EAGAIN)
86cc2f286a7a file dpi: Used NONBLOCKING for accept() (to assert it never blocks)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1390
diff changeset
1095 continue;
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1096 MSG(" accept() %s\n", dStrerror(errno));
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1097 break;
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1098 } else {
1439
79fcf67eeea0 Fixed bookmarks "cancel" link (Add/Mod URL, Add Sec). Also s/tmp_fd/sock_fd.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1402
diff changeset
1099 _MSG(" accept() fd=%d\n", sock_fd);
1390
27911457287d Introduced the a_Dpip_dsh_tryflush() call
Jorge Arellano Cid <jcid@dillo.org>
parents: 1389
diff changeset
1100 /* Set nonblocking */
1439
79fcf67eeea0 Fixed bookmarks "cancel" link (Add/Mod URL, Add Sec). Also s/tmp_fd/sock_fd.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1402
diff changeset
1101 fcntl(sock_fd, F_SETFL, O_NONBLOCK | fcntl(sock_fd, F_GETFL));
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1102 /* Create and initialize a new client */
1439
79fcf67eeea0 Fixed bookmarks "cancel" link (Add/Mod URL, Add Sec). Also s/tmp_fd/sock_fd.
Jorge Arellano Cid <jcid@dillo.org>
parents: 1402
diff changeset
1103 File_add_client(sock_fd);
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1104 }
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1105 continue;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1106 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1107
1388
eb35203124e4 Implemented the file dpi based on select() (removed its pthreads dependency)
Jorge Arellano Cid <jcid@dillo.org>
parents: 1387
diff changeset
1108 File_serve_clients();
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1109 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1110
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1111 if (DPIBYE)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1112 st = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1113 return st;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1114 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1115