Skip to content
Newer
Older
100644 532 lines (468 sloc) 12.9 KB
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
1 /* url.c */
2
3 #include "elvis.h"
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
4 #ifdef FEATURE_RCSID
2fe6e17 @mbert Import Elvis 2.2_1 (written by Steve Kirkendall)
authored Dec 10, 2011
5 char id_url[] = "$Id: url.c,v 2.25 2004/03/07 20:10:28 steve Exp $";
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
6 #endif
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
7
8 static char url_protocol[10];
9 static char url_site_port[50];
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
10 static char url_resource[1000];
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
11
12 #if USE_PROTOTYPES
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
13 static ELVBOOL parseurl(char *url);
14 # if defined(PROTOCOL_HTTP) || defined(PROTOCOL_FTP)
15 static char *findproxy(ELVBOOL never_direct);
16 # endif
17 #endif
18
19 #ifdef FEATURE_PROTO
20 /* This indicates whether we're current running a URL protocol alias. This is
21 * important because URL protocol aliases such as readMAILTO aren't allowed
22 * to switch buffers.
23 */
24 ELVBOOL urlprotobusy;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
25 #endif
26
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
27 /* Parse an URL, and return ElvTrue if successful. */
28 static ELVBOOL parseurl(url)
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
29 char *url;
30 {
31 int i;
32
33 /* Copy the protocol into url_protocol[], converting to lowercase */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
34 for (i = 0; i < QTY(url_protocol) - 1 && elvalpha(url[i]); i++)
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
35 {
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
36 url_protocol[i] = elvtolower(url[i]);
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
37 }
38 url_protocol[i] = '\0';
39
40 /* We expect a colon after the protocol. If we don't have that, then
41 * fail. Also fail if the protocol is less than two characters long,
42 * so we don't mistake the "C:" drive letter for a protocol.
43 */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
44 if (url[i++] != ':' || i < 3)
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
45 {
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
46 /* Directories are treated like "dir:" protocol */
47 if (dirperm(url) == DIR_DIRECTORY)
48 {
49 strcpy(url_protocol, "dir");
50 *url_site_port = '\0';
51 strcpy(url_resource, url);
52 return ElvTrue;
53 }
54
55 /* else non-file or a normal file */
56 return ElvFalse;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
57 }
58
59 /* Check for a host. Not all protocols require one. */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
60 url += i;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
61 url_site_port[0] = '\0';
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
62 if ((url[0] == '/' || url[0] == '\\') && url[1] == url[0])
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
63 {
64 url += 2;
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
65 for (i = 0; i < QTY(url_site_port) - 1 && *url && *url != '/' && *url != '\\'; i++, url++)
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
66 {
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
67 url_site_port[i] = elvtolower(*url);
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
68 }
69 url_site_port[i] = '\0';
70 }
71
72 /* The rest of the URL is assumed to be a resource name. If it is a
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
73 * null string, then assume it should be "/". Convert backslashes to
74 * forward slashes.
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
75 */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
76 if (!*url)
77 strcpy(url_resource, "/");
78 else
79 {
80 for (i = 0; i < QTY(url_resource) - 1 && *url; url++, i++)
81 if (*url == '\\')
82 url_resource[i] = '/';
83 else
84 url_resource[i] = *url;
85 url_resource[i] = '\0';
86 }
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
87
88 /* some debugging code */
3a9bb55 @mbert Import Elvis 2.1_3 (written by Steve Kirkendall)
authored Dec 10, 2011
89 if (o_verbose >= 4)
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
90 msg(MSG_INFO, "[sss]protocol=$1, site_port=$2, resource=$3",
91 url_protocol, url_site_port, url_resource);
92
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
93 return ElvTrue;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
94 }
95
96 /* If the URL refers to a local file, this returns the name of the
97 * file. This handles "file:" protocol and names which have no
98 * protocol; for all others it returns NULL. It is also smart enough
99 * to realize that "C:" is a drive letter rather than a protocol.
100 */
101 char *urllocal(url)
102 char *url;
103 {
104 if (!parseurl(url))
105 return url;
106 else if (!strcmp(url_protocol, "file"))
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
107 return url + 5;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
108 return NULL;
109 }
110
111 /* Check the permissions on a URL. */
112 DIRPERM urlperm(url)
113 char *url;
114 {
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
115 /* "-" is used for stdio -- not an actual filename */
116 if (!strcmp(url, "-"))
117 return DIR_READWRITE;
118
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
119 #if defined(PROTOCOL_HTTP) || defined(PROTOCOL_FTP)
120 /* Verify that this is a remote URL, other than mailto:. This also
121 * has the side-effect of parsing the URL. For local URLs try using
122 * the dirperm() function.
123 */
124 if (!urlremote(url))
125 return dirperm(url);
126
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
127 # ifdef PROTOCOL_FTP
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
128 /* If the protocol is anything other than "ftp" then assume the URL
129 * exists and is readonly.
130 */
131 if (strcmp(url_protocol, "ftp"))
132 return DIR_READONLY;
133
134 /* Else use FTP to inspect the file (not easy!) and then return
135 * whatever permissions it found.
136 */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
137 (void)ftpopen(url_site_port, url_resource, ElvFalse, 'p');
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
138 return ftpperms;
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
139 # else /* PROTOCOL_HTTP but not PROTOCOL_FTP */
140 /* it must be HTTP, which is readonly */
141 return DIR_READONLY;
142 # endif
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
143
144 #else /* no network protocols */
145
146 return dirperm(url);
147 #endif
148 }
149
150
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
151 #ifdef FEATURE_PROTO
152
153 /* If this is a URL and its protocol is the same as a readXXX or writeXXX
154 * alias, then run the alias and return RESULT_COMPLETE or RESULT_ERROR
155 * to indicate the result. Otherwise return RESULT_MORE.
156 */
157 RESULT urlalias(from, to, url)
158 MARK from; /* starting mark */
159 MARK to; /* ending mark when writing, or NULL when reading */
160 char *url; /* URL to read */
161 {
162 char name[20];/* name of a possible alias */
163 EXINFO xinf; /* a constructed ex command line */
164 RESULT result;
165 int i, j;
166
167 /* try to extract the protocol */
168 if (!parseurl(url))
169 return RESULT_MORE;
170
171 /* is there an alias with this name? */
172 strcpy(name, to ? "write" : "read");
173 for (i=0, j=strlen(name); (name[j++] = elvtoupper(url_protocol[i++])); )
174 {
175 }
176 memset(&xinf, 0, sizeof xinf);
177 xinf.cmdname = exisalias(name, ElvFalse);
178 if (!xinf.cmdname)
179 return RESULT_MORE;
180
181 /* turn on the buffer's userprotocol option */
182 o_userprotocol(markbuffer(from)) = ElvTrue;
183
184 /* construct an ex command, and execute it */
185 xinf.command = EX_DOPROTO;
186 xinf.window = windefault;
187 xinf.defaddr = *from;
188 xinf.fromaddr = from;
189 xinf.toaddr = to ? to : from;
190 xinf.from = markline(from);
191 xinf.to = markline(xinf.toaddr) - 1;
192 xinf.anyaddr = (ELVBOOL)(markoffset(xinf.toaddr) > 0);
193 (void)buildstr(&xinf.rhs, url);
194 urlprotobusy = ElvTrue;
195 result = ex_doalias(&xinf);
196 urlprotobusy = ElvFalse;
197 safefree(xinf.rhs);
198
199 /* return the result */
200 return result;
201 }
202
203 #endif /* FEATURE_PROTO */
204
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
205
206 #if defined(PROTOCOL_HTTP) || defined(PROTOCOL_FTP)
207
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
208 /* Return ElvTrue if the url uses a protocol other than "mailto:" or
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
209 * "file:".
210 */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
211 ELVBOOL urlremote(url)
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
212 char *url;
213 {
214 /* Parse the url. If parsing fails, then it isn't remote. Also,
215 * check for a few specific protocols which aren't supported.
216 */
217 if (!parseurl(url)
218 || !strcmp(url_protocol, "mailto")
2fe6e17 @mbert Import Elvis 2.2_1 (written by Steve Kirkendall)
authored Dec 10, 2011
219 || !strcmp(url_protocol, "file")
220 || !strcmp(url_protocol, "dir"))
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
221 return ElvFalse;
222 return ElvTrue;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
223 }
224
225 static long totalbytes;
226 static long expectbytes;
227 static long oldpollfreq;
228 #ifdef PROTOCOL_FTP
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
229 static ELVBOOL useftp;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
230 #endif
231
232 /* Scan through the elvis.net file for this URL's domain. Return the proxy
233 * site's name if found, or NULL if the URL should be accessed directly.
234 * The proxy name will be in a dynamically-allocated buffer, which must be
235 * freed by the calling function.
236 *
237 * This assumes that parseurl() was recently called on the URL, so the site
238 * name can be found in url_site_port[].
239 */
240 static char *findproxy(never_direct)
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
241 ELVBOOL never_direct; /* protocol isn't supported directly by elvis */
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
242 {
243 CHAR *word, *proxy; /* words from file */
244 int sitelen, len; /* lengths of site name and current word */
245 FILE *fp; /* stream used for reading from file */
246 int ch; /* character from file */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
247 ELVBOOL expect_proxy; /* is next word expected to be proxy name? */
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
248 char *tmp;
249
250 /* compute the length of the site name, excluding the port number */
251 tmp = strchr(url_site_port, ':');
252 if (tmp)
253 sitelen = (int)(tmp - url_site_port);
254 else
255 sitelen = strlen(url_site_port);
256
257 /* find the "elvis.net" file (actually the buffer containing it) */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
258 tmp = iopath(tochar8(o_elvispath), NET_FILE, ElvFalse);
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
259 if (!tmp)
260 return NULL;
261
262 /* scan for the domain in the "elvis.net" file */
263 fp = fopen(tmp, "r");
264 proxy = word = NULL;
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
265 expect_proxy = ElvFalse;
266 if (fp != NULL)
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
267 {
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
268 while ((ch = getc(fp)) != EOF)
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
269 {
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
270 /* If '#' is encountered, then skip ahead to the next
271 * newline. Later code will then see the newline and
272 * hence treat the entire comment like whitespace.
273 */
274 if (ch == '#')
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
275 {
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
276 do
277 {
278 if ((ch = getc(fp)) == EOF)
279 goto End;
280 } while (ch != '\n');
281 }
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
282
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
283 /* non-whitespace just adds the character to the current word */
284 if (!elvspace(ch))
285 {
286 buildCHAR(&word, ch);
287 continue;
288 }
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
289
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
290 /* if we have no word (multiple spaces) then continue */
291 if (!word)
292 continue;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
293
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
294 /* if this word is supposed to be a proxy name, then use it */
295 if (expect_proxy)
296 {
297 expect_proxy = ElvFalse;
298 assert(!proxy);
299 proxy = word;
300 word = NULL;
301 continue;
302 }
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
303
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
304 /* Handle the "direct" keyword */
305 if (!strcmp(tochar8(word), "direct"))
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
306 {
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
307 /* forget the current proxy */
308 if (proxy && !never_direct)
309 {
310 safefree(proxy);
311 proxy = NULL;
312 }
313 goto NextWord;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
314 }
315
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
316 /* handle the "proxy" keyword */
317 if (!strcmp(tochar8(word), "proxy"))
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
318 {
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
319 if (proxy)
320 {
321 safefree(proxy);
322 proxy = NULL;
323 }
324 expect_proxy = ElvTrue;
325 goto NextWord;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
326 }
327
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
328 /* Then it must be a domain name. Is the the URL's? */
329 len = CHARlen(word);
330 if ((len < sitelen && *word == '.' && !strncmp(&url_site_port[len - sitelen], tochar8(word), len))
331 || (len == sitelen && !strncmp(url_site_port, tochar8(word), len)))
332 {
333 /* Found it! Stop looking! */
334 break;
335 }
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
336
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
337 /* otherwise we can forget the current word */
338 NextWord:
339 safefree(word);
340 word = NULL;
341 }
342 fclose(fp);
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
343 }
344
345 /* if there was a last word, free it */
346 End:
347 if (word)
348 safefree(word);
349
350 /* return the proxy (which may be NULL) */
351 return tochar8(proxy);
352 }
353
354
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
355 /* Open a remote URL for reading or writing. Returns ElvTrue if successful. If
356 * error, it gives an error message and returns ElvFalse.
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
357 */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
358 ELVBOOL urlopen(url, force, rwa)
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
359 char *url;
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
360 ELVBOOL force;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
361 _char_ rwa;
362 {
363 char *proxy;
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
364 ELVBOOL unsupported;
365 ELVBOOL reading;
366 ELVBOOL retval;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
367
368 /* reset the expectbytes/totalbytes values to an impossible value */
369 expectbytes = -1L;
370 totalbytes = 0;
371
372 /* Verify that this is a remote URL, other than mailto:. This also
373 * has the side-effect of parsing the URL.
374 */
375 if (!urlremote(url))
376 {
377 msg(MSG_ERROR, "[s]$1 not a remote url", url);
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
378 return ElvFalse;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
379 }
380
381 /* set "unsupported" if this isn't a built-in protocol */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
382 unsupported = (ELVBOOL)!(
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
383 #ifdef PROTOCOL_FTP
384 !strcmp(url_protocol, "ftp") ||
385 #endif
386 !strcmp(url_protocol, "http"));
387
388 /* If a proxy is specified in "elvis.net", then use it. Otherwise
389 * we'll access the site/port directly.
390 */
391 proxy = findproxy(unsupported);
392 if (proxy)
393 {
394 /* Use the proxy. Assume it uses the HTTP protocol */
395 strcpy(url_protocol, "http");
396 strcpy(url_site_port, proxy);
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
397 strcpy(url_resource, url);
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
398
399 /* Free the proxy name, but don't zero the proxy pointer.
400 * Later, we can still compare proxt against NULL, to detect
401 * whether we're using a proxy.
402 */
403 safefree(proxy);
404 }
405 else
406 {
407 /* We'll access it directly... unless we don't know how! */
408 if (unsupported)
409 {
410 msg(MSG_ERROR, "[s]unsupported protocol $1", url_protocol);
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
411 return ElvFalse;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
412 }
413 }
414
415 /* Will we be reading or writing? */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
416 reading = (ELVBOOL)(rwa == 'r');
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
417
418 /* arrange for frequent polling during the network operation */
419 oldpollfreq = o_pollfrequency;
420 o_pollfrequency = 1L;
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
421 (void)guipoll(ElvTrue);
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
422
423 /* At this point, we know we have a supported protocol. Which one? */
424 #ifdef PROTOCOL_FTP
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
425 useftp = (ELVBOOL)!strcmp(url_protocol, "ftp");
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
426 if (useftp)
427 retval = ftpopen(url_site_port, url_resource, force, rwa);
428 else
429 #endif
430 {
431 if (!reading)
432 {
433 msg(MSG_ERROR, "[s]can only READ from http $1", proxy ? "proxy" : "server");
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
434 retval = ElvFalse;
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
435 }
436 else
437 retval = httpopen(url_site_port, url_resource);
438 }
439
440 /* restore pollfrequency */
441 o_pollfrequency = oldpollfreq;
442 return retval;
443 }
444
445 /* During a "write" operation, this value will be displayed to the
446 * user as the total number of bytes to be transferred. You should
447 * call urlbytes() after calling urlopen() with 'w' or 'a' as the
448 * rwa parameter. Alternatively, you may omit calling this function
449 * in which case elvis simply won't display the total number of bytes
450 * to the user. When rwa is 'r', this function does nothing.
451 *
452 * Is is also harmless to call urlbytes() after other flavors of ioopen()
453 * for writing; i.e., you can safely call urlbytes() after any ioopen() which
454 * has an rwa argument of anything except 'r'. The httpopen() and ftpopen()
455 * functions call urlbytes() for 'r' operations.
456 */
457 void urlbytes(totbytes)
458 long totbytes; /* number of bytes we expect to write */
459 {
460 expectbytes = totbytes;
461 }
462
463 /* Read data from an URL. Returns the number of bytes read (0 at EOF);
464 * if error or user-abort, it given an error message and returns -1.
465 */
466 int urlread(buf, bytes)
467 CHAR *buf;
468 int bytes;
469 {
470 #ifdef PROTOCOL_FTP
471 if (useftp)
472 bytes = ftpread(buf, bytes);
473 else
474 #endif
475 bytes = httpread(buf, bytes);
476 if (bytes > 0)
477 {
478 totalbytes += bytes;
479 if (windefault)
480 {
481 if (totalbytes <= expectbytes)
482 msg(MSG_STATUS, "[dd]receiving... $1 of $2 bytes", totalbytes, expectbytes);
483 else
484 msg(MSG_STATUS, "[d]receiving... $1 bytes", totalbytes);
485 }
486 }
487 return bytes;
488 }
489
490 /* Write data to an URL. Returns the number of bytes written;
491 * if error or user-abort, it given an error message and returns -1.
492 */
493 int urlwrite(buf, bytes)
494 CHAR *buf;
495 int bytes;
496 {
497 #ifdef PROTOCOL_FTP
498 if (useftp)
499 bytes = ftpwrite(buf, bytes);
500 /* there is no httpwrite() */
501
502 if (bytes > 0)
503 {
504 totalbytes += bytes;
505 if (windefault)
506 {
507 if (totalbytes <= expectbytes)
508 msg(MSG_STATUS, "[dd]sending... $1 of $2 bytes", totalbytes, expectbytes);
509 else
510 msg(MSG_STATUS, "[d]sending... $1 bytes", totalbytes);
511 }
512 }
513 #endif
514 return bytes;
515 }
516
517 /* Close an URL. */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored Dec 10, 2011
518 void urlclose()
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored Dec 10, 2011
519 {
520 #ifdef PROTOCOL_FTP
521 if (useftp)
522 ftpclose();
523 else
524 #endif
525 httpclose();
526
527 /* restore the old poll frequency */
528 o_pollfrequency = oldpollfreq;
529 }
530
531 #endif /* PROTOCOL_HTTP || PROTOCOL_FTP */
Something went wrong with that request. Please try again.