/* create a connection for given file/URL */ int cache_connect(Connection *cnx, const char *file, const char *url, char *firstblock, unsigned int size) { char buffer[4096], *cvt; mylog("cache_connect(%p, %s, %s)\n", cnx, file, url); cvt = wget_encode(url, file); sprintf(buffer, "%s", cvt); /* create CURL connection (checks validity) */ if (!wget_connect(buffer, cnx, firstblock, size)) { mylog("cache_connect: wget_connect(%s, -) failed\n", buffer); return(0); } mylog("cache_connect: wget_connect ok\n"); cnx->target = strdup(buffer); if (cnx->target == NULL) { mylog("cache_connect: failed to copy target name"); wget_disconnect(cnx); /* not needed, but... */ return(0); } cnx->type = CNX_URL; cnx->data = NULL; /* not used */ cnx->idata = 0; /* not used */ return(1); }
int wget(const char *url, int redirect_no) { extern struct cfg global_cfg; struct wgetfile finfo; int ret; char *outname = NULL; char *host, *file, *proto, *port; char *urlcpy = alloca(strlen(url) + 2); strcpy(urlcpy, url); memset(&finfo, 0, sizeof finfo); finfo.redirect_no = redirect_no; if(!strchr(urlcpy, '/')) strcat(urlcpy, "/"); if(parseurl(url, &host, &file, &proto, &port)) return 1; if(!host || !port){ output_err(OUT_ERR, "invalid url \"%s\"", url); goto bail; } if( !strcmp(proto, "http")) finfo.proto = HTTP; else if(!strcmp(proto, "ftp")) finfo.proto = FTP; else if(!strcmp(proto, "gopher")) finfo.proto = GOPHER; else{ ret = 1; output_err(OUT_ERR, "unknown protocol: %s", proto); goto bail; } free(proto); if(global_cfg.out_fname){ if(!strcmp(global_cfg.out_fname, "-")) outname = NULL; else outname = xstrdup(global_cfg.out_fname); finfo.namemode = NAME_FORCE; }else{ char *the_last_slash_rated_pg_for_parental_guidance; the_last_slash_rated_pg_for_parental_guidance = strrchr(file, '/'); if(the_last_slash_rated_pg_for_parental_guidance) outname = xstrdup(the_last_slash_rated_pg_for_parental_guidance + 1); else outname = xstrdup(file); finfo.namemode = NAME_GUESS; /* TODO: urldecode outname (except for %3f) */ } finfo.sock = -1; finfo.host_file = file; finfo.host_name = host; finfo.host_port = port; finfo.outname = outname; if(wget_connect(&finfo)) goto bail; switch(finfo.proto) { default: case HTTP: ret = http_GET(&finfo); break; case FTP: ret = ftp_RETR(&finfo); break; case GOPHER: ret = gopher_retrieve(&finfo); break; } fin: /* don't close the connection - let connection_* handle it */ /* free the struct's members, since they might be changed */ free(finfo.host_file); free(finfo.host_name); free(finfo.host_port); free(finfo.outname ); return ret; bail: ret = 1; goto fin; }