[9] | 1 | /* urlget.c Copyright 2000 by Michael Temari All Rights Reserved */
|
---|
| 2 | /* 04/05/2000 Michael Temari <Michael@TemWare.Com> */
|
---|
| 3 |
|
---|
| 4 | #include <sys/types.h>
|
---|
| 5 | #include <sys/ioctl.h>
|
---|
| 6 | #include <sys/wait.h>
|
---|
| 7 | #include <stdio.h>
|
---|
| 8 | #include <ctype.h>
|
---|
| 9 | #include <stdlib.h>
|
---|
| 10 | #include <string.h>
|
---|
| 11 | #include <errno.h>
|
---|
| 12 | #include <fcntl.h>
|
---|
| 13 | #include <signal.h>
|
---|
| 14 | #include <unistd.h>
|
---|
| 15 | #include <time.h>
|
---|
| 16 |
|
---|
| 17 | #include "net.h"
|
---|
| 18 |
|
---|
| 19 | _PROTOTYPE(char *unesc, (char *s));
|
---|
| 20 | _PROTOTYPE(void encode64, (char **pp, char *s));
|
---|
| 21 | _PROTOTYPE(char *auth, (char *user, char *pass));
|
---|
| 22 | _PROTOTYPE(int skipit, (char *buf, int len, int *skip));
|
---|
| 23 | _PROTOTYPE(int httpget, (char *host, int port, char *user, char *pass, char *path, int headers, int discard, int post));
|
---|
| 24 | _PROTOTYPE(void ftppasv, (char *reply));
|
---|
| 25 | _PROTOTYPE(int ftpreply, (FILE *fpr));
|
---|
| 26 | _PROTOTYPE(int ftpcmd, (FILE *fpw, FILE *fpr, char *cmd, char *arg));
|
---|
| 27 | _PROTOTYPE(int ftpget, (char *host, int port, char *user, char *pass, char *path, int type));
|
---|
| 28 | _PROTOTYPE(int tcpget, (char *host, int port, char *user, char *pass, char *path));
|
---|
| 29 | _PROTOTYPE(int main, (int argc, char *argv[]));
|
---|
| 30 |
|
---|
| 31 | char ftpphost[15+1];
|
---|
| 32 | unsigned int ftppport;
|
---|
| 33 |
|
---|
| 34 | #define SCHEME_HTTP 1
|
---|
| 35 | #define SCHEME_FTP 2
|
---|
| 36 | #define SCHEME_TCP 3
|
---|
| 37 | #define SCHEME_NNTP 4
|
---|
| 38 |
|
---|
| 39 | char buffer[16000];
|
---|
| 40 |
|
---|
| 41 | #if 0
|
---|
| 42 | _PROTOTYPE(int strncasecmp, (const char *s1, const char *s2, size_t len));
|
---|
| 43 | int
|
---|
| 44 | strncasecmp(s1, s2, len)
|
---|
| 45 | const char *s1, *s2;
|
---|
| 46 | size_t len;
|
---|
| 47 | {
|
---|
| 48 | int c1, c2;
|
---|
| 49 | do {
|
---|
| 50 | if (len == 0)
|
---|
| 51 | return 0;
|
---|
| 52 | len--;
|
---|
| 53 | } while (c1= toupper(*s1++), c2= toupper(*s2++), c1 == c2 && (c1 & c2))
|
---|
| 54 | ;
|
---|
| 55 | if (c1 & c2)
|
---|
| 56 | return c1 < c2 ? -1 : 1;
|
---|
| 57 | return c1 ? 1 : (c2 ? -1 : 0);
|
---|
| 58 | }
|
---|
| 59 | #endif
|
---|
| 60 |
|
---|
| 61 | char *unesc(s)
|
---|
| 62 | char *s;
|
---|
| 63 | {
|
---|
| 64 | char *p;
|
---|
| 65 | char *p2;
|
---|
| 66 | unsigned char c;
|
---|
| 67 |
|
---|
| 68 | p = s;
|
---|
| 69 | p2 = s;
|
---|
| 70 | while(*p) {
|
---|
| 71 | if(*p != '%') {
|
---|
| 72 | *p2++ = *p++;
|
---|
| 73 | continue;
|
---|
| 74 | }
|
---|
| 75 | p++;
|
---|
| 76 | if(*p == '%') {
|
---|
| 77 | *p2++ = *p++;
|
---|
| 78 | continue;
|
---|
| 79 | }
|
---|
| 80 | if(*p >= '0' && *p <= '9') c = *p++ - '0'; else
|
---|
| 81 | if(*p >= 'a' && *p <= 'f') c = *p++ - 'a' + 10; else
|
---|
| 82 | if(*p >= 'A' && *p <= 'F') c = *p++ - 'A' + 10; else
|
---|
| 83 | break;
|
---|
| 84 | if(*p >= '0' && *p <= '9') c = c << 4 | (*p++ - '0'); else
|
---|
| 85 | if(*p >= 'a' && *p <= 'f') c = c << 4 | (*p++ - 'a') + 10; else
|
---|
| 86 | if(*p >= 'A' && *p <= 'F') c = c << 4 | (*p++ - 'A') + 10; else
|
---|
| 87 | break;
|
---|
| 88 | *p2++ = c;
|
---|
| 89 | }
|
---|
| 90 | *p2 = '\0';
|
---|
| 91 | return(s);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | void encode64(pp, s)
|
---|
| 95 | char **pp;
|
---|
| 96 | char *s;
|
---|
| 97 | {
|
---|
| 98 | char *p;
|
---|
| 99 | char c[3];
|
---|
| 100 | int i;
|
---|
| 101 | int len;
|
---|
| 102 | static char e64[64] = {
|
---|
| 103 | 'A','B','C','D','E','F','G','H','I','J','K','L','M',
|
---|
| 104 | 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
|
---|
| 105 | 'a','b','c','d','e','f','g','h','i','j','k','l','m',
|
---|
| 106 | 'n','o','p','q','r','s','t','u','v','w','x','y','z',
|
---|
| 107 | '0','1','2','3','4','5','6','7','8','9','+','/' };
|
---|
| 108 |
|
---|
| 109 | p = *pp;
|
---|
| 110 | len = strlen(s);
|
---|
| 111 | for(i=0; i < len; i += 3) {
|
---|
| 112 | c[0] = *s++;
|
---|
| 113 | c[1] = *s++;
|
---|
| 114 | c[2] = *s++;
|
---|
| 115 | *p++ = e64[ c[0] >> 2];
|
---|
| 116 | *p++ = e64[((c[0] << 4) & 0x30) | ((c[1] >> 4) & 0x0f)];
|
---|
| 117 | *p++ = e64[((c[1] << 2) & 0x3c) | ((c[2] >> 6) & 0x03)];
|
---|
| 118 | *p++ = e64[ c[2] & 0x3f];
|
---|
| 119 | }
|
---|
| 120 | if(i == len+1)
|
---|
| 121 | p[-1] = '=';
|
---|
| 122 | else
|
---|
| 123 | if(i == len+2) {
|
---|
| 124 | p[-1] = '=';
|
---|
| 125 | p[-2] = '=';
|
---|
| 126 | }
|
---|
| 127 | *p = '\0';
|
---|
| 128 | *pp = p;
|
---|
| 129 | return;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | char *auth(user, pass)
|
---|
| 133 | char *user;
|
---|
| 134 | char *pass;
|
---|
| 135 | {
|
---|
| 136 | static char a[128];
|
---|
| 137 | char up[128];
|
---|
| 138 | char *p;
|
---|
| 139 |
|
---|
| 140 | strcpy(a, "BASIC ");
|
---|
| 141 | p = a + 6;
|
---|
| 142 | sprintf(up, "%s:%s", user, pass);
|
---|
| 143 | encode64(&p, up);
|
---|
| 144 |
|
---|
| 145 | return(a);
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | int skipit(buf, len, skip)
|
---|
| 149 | char *buf;
|
---|
| 150 | int len;
|
---|
| 151 | int *skip;
|
---|
| 152 | {
|
---|
| 153 | static int lf = 0;
|
---|
| 154 | static int crlf = 0;
|
---|
| 155 | char *p;
|
---|
| 156 |
|
---|
| 157 | p = buf;
|
---|
| 158 |
|
---|
| 159 | while(--len >= 0) {
|
---|
| 160 | if((crlf == 0 || crlf == 2) && *p == '\r')
|
---|
| 161 | crlf++;
|
---|
| 162 | else
|
---|
| 163 | if((crlf == 1 || crlf == 3) && *p == '\n')
|
---|
| 164 | crlf++;
|
---|
| 165 | else
|
---|
| 166 | crlf = 0;
|
---|
| 167 | if(*p == '\n')
|
---|
| 168 | lf++;
|
---|
| 169 | else
|
---|
| 170 | lf = 0;
|
---|
| 171 | if(crlf == 4 || lf == 2) {
|
---|
| 172 | *skip = 0;
|
---|
| 173 | return(len);
|
---|
| 174 | }
|
---|
| 175 | p++;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | return(0);
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | int httpget(host, port, user, pass, path, headers, discard, post)
|
---|
| 182 | char *host;
|
---|
| 183 | int port;
|
---|
| 184 | char *user;
|
---|
| 185 | char *pass;
|
---|
| 186 | char *path;
|
---|
| 187 | int headers;
|
---|
| 188 | int discard;
|
---|
| 189 | int post;
|
---|
| 190 | {
|
---|
| 191 | int fd;
|
---|
| 192 | int skip;
|
---|
| 193 | int s;
|
---|
| 194 | int s2;
|
---|
| 195 | char *a;
|
---|
| 196 | char *qs;
|
---|
| 197 | int len;
|
---|
| 198 |
|
---|
| 199 | if(port == 0)
|
---|
| 200 | port = 80;
|
---|
| 201 |
|
---|
| 202 | fd = connect(host, port);
|
---|
| 203 | if(fd < 0) {
|
---|
| 204 | fprintf(stderr, "httpget: Could not connect to %s:%d\n", host, port);
|
---|
| 205 | return(-1);
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | if(post) {
|
---|
| 209 | qs = strrchr(path, '?');
|
---|
| 210 | if(qs != (char *)NULL) {
|
---|
| 211 | *qs++ = '\0';
|
---|
| 212 | len = strlen(qs);
|
---|
| 213 | } else
|
---|
| 214 | len = 0;
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | if(post && len > 0)
|
---|
| 218 | write(fd, "POST ", 5);
|
---|
| 219 | else
|
---|
| 220 | write(fd, "GET ", 4);
|
---|
| 221 | write(fd, path, strlen(path));
|
---|
| 222 | write(fd, " HTTP/1.0\r\n", 11);
|
---|
| 223 | write(fd, "User-Agent: urlget\r\n", 20);
|
---|
| 224 | write(fd, "Connection: Close\r\n", 19);
|
---|
| 225 | if(*user) {
|
---|
| 226 | write(fd, "Authorization: ", 15);
|
---|
| 227 | a = auth(user, pass);
|
---|
| 228 | write(fd, a, strlen(a));
|
---|
| 229 | write(fd, "\r\n", 2);
|
---|
| 230 | }
|
---|
| 231 | if(post && len > 0) {
|
---|
| 232 | sprintf(buffer, "Content-Length: %u\r\n", len);
|
---|
| 233 | write(fd, buffer, strlen(buffer));
|
---|
| 234 | }
|
---|
| 235 | write(fd, "Host: ", 6);
|
---|
| 236 | write(fd, host, strlen(host));
|
---|
| 237 | write(fd, "\r\n", 2);
|
---|
| 238 | write(fd, "\r\n", 2);
|
---|
| 239 | if(post && len > 0)
|
---|
| 240 | write(fd, qs, len);
|
---|
| 241 |
|
---|
| 242 | skip = 1;
|
---|
| 243 | while((s = read(fd, buffer, sizeof(buffer)-1)) > 0) {
|
---|
| 244 | buffer[s] = '\0';
|
---|
| 245 | if(skip) {
|
---|
| 246 | static int firstline = 1;
|
---|
| 247 | if(firstline) {
|
---|
| 248 | static char linebuf[1000];
|
---|
| 249 | int l = 0;
|
---|
| 250 | int c, v1, v2, e;
|
---|
| 251 | if(s >= sizeof(linebuf)-l)
|
---|
| 252 | c = sizeof(linebuf)-1-l;
|
---|
| 253 | else c = s;
|
---|
| 254 | memcpy(linebuf+l, buffer, c);
|
---|
| 255 | linebuf[l+c] = '\0';
|
---|
| 256 | if(strchr(buffer, '\n') || strchr(buffer, '\r'))
|
---|
| 257 | firstline = 0;
|
---|
| 258 | if(sscanf(linebuf, "HTTP/%d.%d %d ", &v1, &v2, &e) == 3
|
---|
| 259 | && e != 200) {
|
---|
| 260 | fprintf(stderr, "HTTP error %d\n", e);
|
---|
| 261 | return -1;
|
---|
| 262 | }
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | s2 = skipit(buffer, s, &skip);
|
---|
| 266 | if(headers)
|
---|
| 267 | write(1, buffer, s - s2);
|
---|
| 268 | } else
|
---|
| 269 | s2 = s;
|
---|
| 270 | if(s2 && !discard)
|
---|
| 271 | if(write(1, &buffer[s - s2], s2) != s2) {
|
---|
| 272 | perror("write");
|
---|
| 273 | return(-1);
|
---|
| 274 | }
|
---|
| 275 | }
|
---|
| 276 | if(s < 0) {
|
---|
| 277 | fprintf(stderr, "httpget: Read error\n");
|
---|
| 278 | return(-1);
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | close(fd);
|
---|
| 282 |
|
---|
| 283 | return(0);
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | void ftppasv(reply)
|
---|
| 287 | char *reply;
|
---|
| 288 | {
|
---|
| 289 | char *p;
|
---|
| 290 | unsigned char n[6];
|
---|
| 291 | int i;
|
---|
| 292 |
|
---|
| 293 | ftppport = 0;
|
---|
| 294 |
|
---|
| 295 | p = reply;
|
---|
| 296 | while(*p && *p != '(') p++;
|
---|
| 297 | if(!*p) return;
|
---|
| 298 | p++;
|
---|
| 299 | i = 0;
|
---|
| 300 | while(1) {
|
---|
| 301 | n[i++] = atoi(p);
|
---|
| 302 | if(i == 6) break;
|
---|
| 303 | p = strchr(p, ',');
|
---|
| 304 | if(p == (char *)NULL) return;
|
---|
| 305 | p++;
|
---|
| 306 | }
|
---|
| 307 | sprintf(ftpphost, "%d.%d.%d.%d", n[0], n[1], n[2], n[3]);
|
---|
| 308 | ftppport = n[4] * 256 + n[5];
|
---|
| 309 | return;
|
---|
| 310 | }
|
---|
| 311 |
|
---|
| 312 | int ftpreply(fpr)
|
---|
| 313 | FILE *fpr;
|
---|
| 314 | {
|
---|
| 315 | static char reply[256];
|
---|
| 316 | int s;
|
---|
| 317 | char code[4];
|
---|
| 318 | int ft;
|
---|
| 319 |
|
---|
| 320 | do {
|
---|
| 321 | ft = 1;
|
---|
| 322 | do {
|
---|
| 323 | if(fgets(reply, sizeof(reply), fpr) == (char *)NULL)
|
---|
| 324 | return(-1);
|
---|
| 325 | if(ft) {
|
---|
| 326 | ft = 0;
|
---|
| 327 | strncpy(code, reply, 3);
|
---|
| 328 | code[3] = '\0';
|
---|
| 329 | }
|
---|
| 330 | } while(strncmp(reply, code, 3) || reply[3] == '-');
|
---|
| 331 | s = atoi(code);
|
---|
| 332 | } while(s < 200 && s != 125 && s != 150);
|
---|
| 333 | if(s == 227) ftppasv(reply);
|
---|
| 334 | return(s);
|
---|
| 335 | }
|
---|
| 336 |
|
---|
| 337 | int ftpcmd(fpw, fpr, cmd, arg)
|
---|
| 338 | FILE *fpw;
|
---|
| 339 | FILE *fpr;
|
---|
| 340 | char *cmd;
|
---|
| 341 | char *arg;
|
---|
| 342 | {
|
---|
| 343 | fprintf(fpw, "%s%s%s\r\n", cmd, *arg ? " " : "", arg);
|
---|
| 344 | fflush(fpw);
|
---|
| 345 | return(ftpreply(fpr));
|
---|
| 346 | }
|
---|
| 347 |
|
---|
| 348 | int ftpget(host, port, user, pass, path, type)
|
---|
| 349 | char *host;
|
---|
| 350 | int port;
|
---|
| 351 | char *user;
|
---|
| 352 | char *pass;
|
---|
| 353 | char *path;
|
---|
| 354 | int type;
|
---|
| 355 | {
|
---|
| 356 | int fd;
|
---|
| 357 | int fd2;
|
---|
| 358 | FILE *fpr;
|
---|
| 359 | FILE *fpw;
|
---|
| 360 | int s;
|
---|
| 361 | int s2;
|
---|
| 362 | char *p;
|
---|
| 363 | char *p2;
|
---|
| 364 | char typec[2];
|
---|
| 365 |
|
---|
| 366 | if(port == 0)
|
---|
| 367 | port = 21;
|
---|
| 368 |
|
---|
| 369 | if(type == '\0')
|
---|
| 370 | type = 'i';
|
---|
| 371 |
|
---|
| 372 | fd = connect(host, port);
|
---|
| 373 | if(fd < 0) {
|
---|
| 374 | fprintf(stderr, "ftpget: Could not connect to %s:%d\n", host, port);
|
---|
| 375 | return(-1);
|
---|
| 376 | }
|
---|
| 377 | fpr = fdopen(fd, "r");
|
---|
| 378 | fpw = fdopen(fd, "w");
|
---|
| 379 |
|
---|
| 380 | s = ftpreply(fpr);
|
---|
| 381 | if(s / 100 != 2) goto error;
|
---|
| 382 | s = ftpcmd(fpw, fpr, "USER", *user ? user : "ftp");
|
---|
| 383 | if(s / 100 == 3)
|
---|
| 384 | s = ftpcmd(fpw, fpr, "PASS", *pass ? pass : "urlget@");
|
---|
| 385 |
|
---|
| 386 | if(s / 100 != 2) goto error;
|
---|
| 387 |
|
---|
| 388 | p = path;
|
---|
| 389 | if(*p == '/') p++;
|
---|
| 390 | while((p2 = strchr(p, '/')) != (char *)NULL) {
|
---|
| 391 | *p2++ = '\0';
|
---|
| 392 | s = ftpcmd(fpw, fpr, "CWD", unesc(p));
|
---|
| 393 | p = p2;
|
---|
| 394 | }
|
---|
| 395 | sprintf(typec, "%c", type == 'd' ? 'A' : type);
|
---|
| 396 | s = ftpcmd(fpw, fpr, "TYPE", typec);
|
---|
| 397 | if(s / 100 != 2) goto error;
|
---|
| 398 | s = ftpcmd(fpw, fpr, "PASV", "");
|
---|
| 399 | if(s != 227) goto error;
|
---|
| 400 | fd2 = connect(ftpphost, ftppport);
|
---|
| 401 | if(fd2 < 0) goto error;
|
---|
| 402 | s = ftpcmd(fpw, fpr, type == 'd' ? "NLST" : "RETR", unesc(p));
|
---|
| 403 | if(s / 100 != 1) goto error;
|
---|
| 404 | while((s = read(fd2, buffer, sizeof(buffer))) > 0) {
|
---|
| 405 | s2 = write(1, buffer, s);
|
---|
| 406 | if(s2 != s) break;
|
---|
| 407 | }
|
---|
| 408 | if(s2 != s && s != 0) s = -1;
|
---|
| 409 | close(fd2);
|
---|
| 410 |
|
---|
| 411 | s = ftpreply(fpr);
|
---|
| 412 | if(s / 100 == 2) s = 0;
|
---|
| 413 |
|
---|
| 414 | error:
|
---|
| 415 | (void) ftpcmd(fpw, fpr, "QUIT", "");
|
---|
| 416 |
|
---|
| 417 | fclose(fpr);
|
---|
| 418 | fclose(fpw);
|
---|
| 419 | close(fd);
|
---|
| 420 |
|
---|
| 421 | return(s == 0 ? 0 : -1);
|
---|
| 422 | }
|
---|
| 423 |
|
---|
| 424 | int tcpget(host, port, user, pass, path)
|
---|
| 425 | char *host;
|
---|
| 426 | int port;
|
---|
| 427 | char *user;
|
---|
| 428 | char *pass;
|
---|
| 429 | char *path;
|
---|
| 430 | {
|
---|
| 431 | int fd;
|
---|
| 432 | int s;
|
---|
| 433 | int s2;
|
---|
| 434 |
|
---|
| 435 | if(port == 0) {
|
---|
| 436 | fprintf(stderr, "tcpget: No port specified\n");
|
---|
| 437 | return(-1);
|
---|
| 438 | }
|
---|
| 439 |
|
---|
| 440 | fd = connect(host, port);
|
---|
| 441 | if(fd < 0) {
|
---|
| 442 | fprintf(stderr, "httpget: Could not connect to %s:%d\n", host, port);
|
---|
| 443 | return(-1);
|
---|
| 444 | }
|
---|
| 445 | if(*path == '\/')
|
---|
| 446 | path++;
|
---|
| 447 |
|
---|
| 448 | write(fd, path, strlen(path));
|
---|
| 449 | write(fd, "\n", 1);
|
---|
| 450 | while((s = read(fd, buffer, sizeof(buffer))) > 0) {
|
---|
| 451 | s2 = write(1, buffer, s);
|
---|
| 452 | if(s2 != s) break;
|
---|
| 453 | }
|
---|
| 454 | close(fd);
|
---|
| 455 | return(0);
|
---|
| 456 | }
|
---|
| 457 |
|
---|
| 458 | int main(argc, argv)
|
---|
| 459 | int argc;
|
---|
| 460 | char *argv[];
|
---|
| 461 | {
|
---|
| 462 | char *prog;
|
---|
| 463 | char *url;
|
---|
| 464 | char scheme;
|
---|
| 465 | char user[64];
|
---|
| 466 | char pass[64];
|
---|
| 467 | char host[64];
|
---|
| 468 | int port;
|
---|
| 469 | char *path;
|
---|
| 470 | int type;
|
---|
| 471 | char *ps;
|
---|
| 472 | char *p;
|
---|
| 473 | char *at;
|
---|
| 474 | int s, c;
|
---|
| 475 | int opt_h = 0;
|
---|
| 476 | int opt_d = 0;
|
---|
| 477 | int opt_p = 0;
|
---|
| 478 |
|
---|
| 479 | prog = strrchr(*argv, '/');
|
---|
| 480 | if(prog == (char *)NULL)
|
---|
| 481 | prog = *argv;
|
---|
| 482 | argv++;
|
---|
| 483 | argc--;
|
---|
| 484 |
|
---|
| 485 | while(argc && argv[0][0] == '-') {
|
---|
| 486 | char *opt = *argv++ + 1;
|
---|
| 487 | argc--;
|
---|
| 488 |
|
---|
| 489 | if (opt[0] == '-' && opt[1] == 0) break;
|
---|
| 490 |
|
---|
| 491 | while (*opt) switch (*opt++) {
|
---|
| 492 | case 'h': opt_h = -1; break;
|
---|
| 493 | case 'd': opt_d = -1; break;
|
---|
| 494 | case 'p': opt_p = -1; break;
|
---|
| 495 | default: argc = 0; break;
|
---|
| 496 | }
|
---|
| 497 | }
|
---|
| 498 |
|
---|
| 499 | if(strcmp(prog, "ftpget") == 0) {
|
---|
| 500 | if(argc < 2 || argc > 4) {
|
---|
| 501 | fprintf(stderr, "Usage: %s host path [user [pass]]\n", prog);
|
---|
| 502 | return(-1);
|
---|
| 503 | }
|
---|
| 504 | strncpy(host, *argv++, sizeof(host));
|
---|
| 505 | port = 21;
|
---|
| 506 | path = *argv++;
|
---|
| 507 | if(argc) {
|
---|
| 508 | strncpy(user, *argv++, sizeof(user));
|
---|
| 509 | argc++;
|
---|
| 510 | } else
|
---|
| 511 | *user = '\0';
|
---|
| 512 | if(argc) {
|
---|
| 513 | strncpy(pass, *argv++, sizeof(pass));
|
---|
| 514 | argc++;
|
---|
| 515 | } else
|
---|
| 516 | *pass = '\0';
|
---|
| 517 | s = ftpget(host, port, user, path, path, 'i');
|
---|
| 518 | return(s);
|
---|
| 519 | }
|
---|
| 520 | if(strcmp(prog, "httpget") == 0) {
|
---|
| 521 | if(argc != 2) {
|
---|
| 522 | fprintf(stderr, "Usage: %s [-h] [-d] [-p] host path\n", prog);
|
---|
| 523 | return(-1);
|
---|
| 524 | }
|
---|
| 525 | strncpy(host, *argv++, sizeof(host));
|
---|
| 526 | port = 80;
|
---|
| 527 | path = *argv++;
|
---|
| 528 | s = httpget(host, port, user, path, path, opt_h, opt_d, opt_p);
|
---|
| 529 | return(s);
|
---|
| 530 | }
|
---|
| 531 |
|
---|
| 532 | if(argc != 1) {
|
---|
| 533 | usage:
|
---|
| 534 | fprintf(stderr, "Usage: %s [-h] [-p] url\n", prog);
|
---|
| 535 | return(-1);
|
---|
| 536 | }
|
---|
| 537 |
|
---|
| 538 | url = *argv++;
|
---|
| 539 | argc--;
|
---|
| 540 |
|
---|
| 541 | if(strncasecmp(url, "http://", 7) == 0) {
|
---|
| 542 | scheme = SCHEME_HTTP;
|
---|
| 543 | ps = url + 7;
|
---|
| 544 | } else
|
---|
| 545 | if(strncasecmp(url, "ftp://", 6) == 0) {
|
---|
| 546 | scheme = SCHEME_FTP;
|
---|
| 547 | ps = url + 6;
|
---|
| 548 | } else
|
---|
| 549 | if(strncasecmp(url, "tcp://", 6) == 0) {
|
---|
| 550 | scheme = SCHEME_TCP;
|
---|
| 551 | ps = url + 6;
|
---|
| 552 | } else {
|
---|
| 553 | fprintf(stderr, "%s: I do not handle this scheme\n", prog);
|
---|
| 554 | return(-1);
|
---|
| 555 | }
|
---|
| 556 |
|
---|
| 557 | user[0] = '\0';
|
---|
| 558 | pass[0] = '\0';
|
---|
| 559 | host[0] = '\0';
|
---|
| 560 | port = 0;
|
---|
| 561 |
|
---|
| 562 | p = ps;
|
---|
| 563 | while(*p && *p != '/') p++;
|
---|
| 564 | path = p;
|
---|
| 565 | c = *path;
|
---|
| 566 | *path = '\0';
|
---|
| 567 |
|
---|
| 568 | at = strchr(ps, '@');
|
---|
| 569 | if(at != (char *)NULL) {
|
---|
| 570 | *at = '\0';
|
---|
| 571 | p = ps;
|
---|
| 572 | while(*p && *p != ':') p++;
|
---|
| 573 | if(*p)
|
---|
| 574 | *p++ = '\0';
|
---|
| 575 | strcpy(user, ps);
|
---|
| 576 | strcpy(pass, p);
|
---|
| 577 | ps = at + 1;
|
---|
| 578 | }
|
---|
| 579 |
|
---|
| 580 | *path = c;
|
---|
| 581 | p = ps;
|
---|
| 582 | while(*p && *p != '/' && *p != ':') p++;
|
---|
| 583 | strncpy(host, ps, p - ps);
|
---|
| 584 | host[p - ps] = '\0';
|
---|
| 585 | if(*p == ':') {
|
---|
| 586 | p++;
|
---|
| 587 | ps = p;
|
---|
| 588 | while(*p && *p != '/')
|
---|
| 589 | port = port * 10 + (*p++ - '0');
|
---|
| 590 | }
|
---|
| 591 | if(*p == '/')
|
---|
| 592 | path = p;
|
---|
| 593 | else
|
---|
| 594 | path = "/";
|
---|
| 595 | if(scheme == SCHEME_FTP) {
|
---|
| 596 | p = path;
|
---|
| 597 | while(*p && *p != ';') p++;
|
---|
| 598 | if(*p) {
|
---|
| 599 | *p++ = '\0';
|
---|
| 600 | if(strncasecmp(p, "type=", 5) == 0) {
|
---|
| 601 | p += 5;
|
---|
| 602 | type = tolower(*p);
|
---|
| 603 | }
|
---|
| 604 | }
|
---|
| 605 | }
|
---|
| 606 |
|
---|
| 607 | #if 0
|
---|
| 608 | fprintf(stderr, "Host: %s\n", host);
|
---|
| 609 | fprintf(stderr, "Port: %d\n", port);
|
---|
| 610 | fprintf(stderr, "User: %s\n", user);
|
---|
| 611 | fprintf(stderr, "Pass: %s\n", pass);
|
---|
| 612 | fprintf(stderr, "Path: %s\n", path);
|
---|
| 613 | fprintf(stderr, "Type: %c\n", type);
|
---|
| 614 | #endif
|
---|
| 615 |
|
---|
| 616 | switch(scheme) {
|
---|
| 617 | case SCHEME_HTTP:
|
---|
| 618 | s = httpget(host, port, user, pass, path, opt_h, opt_d, opt_p);
|
---|
| 619 | break;
|
---|
| 620 | case SCHEME_FTP:
|
---|
| 621 | s = ftpget(host, port, user, pass, path, type);
|
---|
| 622 | break;
|
---|
| 623 | case SCHEME_TCP:
|
---|
| 624 | s = tcpget(host, port, user, pass, path);
|
---|
| 625 | break;
|
---|
| 626 | }
|
---|
| 627 |
|
---|
| 628 | return(s);
|
---|
| 629 | }
|
---|