void HttpLoader::do_item(const std::shared_ptr<WorkItem> &item,
                         const std::shared_ptr<ICurlWrapper> &curl_wrapper)
{
    auto download_item = std::dynamic_pointer_cast<DownloadItem>(item);
    if (nullptr != download_item) {
        do_download(download_item, curl_wrapper);
        return;
    }

    auto upload_item = std::dynamic_pointer_cast<UploadItem>(item);
    if (nullptr != upload_item) {
        do_upload(upload_item, curl_wrapper);
        return;
    }
}
示例#2
0
文件: httpget.c 项目: csko/yaosp
int main( int argc, char** argv ) {
    int file;
    char* url;
    char* filename;

    if ( argc != 2 ) {
        fprintf( stderr, "%s url\n", argv[ 0 ] );
        return EXIT_FAILURE;
    }

    argv0 = argv[ 0 ];
    url = argv[ 1 ];

    if ( validate_url( url ) != 0 ) {
        fprintf( stderr, "%s: invalid url: %s.\n", argv0, url );
        fprintf( stderr, "%s: valid url format is http://domain/file\n", argv0 );
        return EXIT_FAILURE;
    }

    curl_global_init( CURL_GLOBAL_ALL );

    filename = strrchr( url, '/' ) + 1;

    file = open( filename, O_WRONLY | O_CREAT | O_TRUNC );

    if ( file < 0 ) {
        fprintf( stderr, "%s: failed to open %s: %s.\n", argv0, filename, strerror( errno ) );
        return EXIT_FAILURE;
    }

    do_download( url, file );

    close( file );

    curl_global_cleanup();

    return EXIT_SUCCESS;
}
示例#3
0
/*------------------------------------------------------------------------*/
int main(int argc, char **argv)
{
	int ret=0;
	
	while(1) {
		int ret = getopt(argc,argv, "U:X:Di:AlLI:E:Z:d:F:P:u:p:rRqK");
		if (ret==-1)
			break;
			
		char c=(char)ret;

		switch(c) {
		case 'F':
			strncpy(ftp_cmd,optarg,512);
			ftp_cmd[511]=0;
			break;
		case 'X':
			ret=do_update(uuids, num_uuids, device, optarg);
			if (ret==-2)
				exit(ret);
			break;
		case 'U':
			ret=do_upload(uuids, num_uuids, device, optarg);
			if (ret==-2)
				exit(ret);
			break;
		case 'D':
			ret|=do_download(uuids, num_uuids, device, NC_CONFPATH, NC_CONFFILE);
			break;
		case 'i':
			uuids[num_uuids]=strdup(optarg);
			num_uuids++;
			break;
		case 'A':
			num_uuids=get_uuids(uuids,255);
			break;
		case 'l':
			show_uuids();
			break;
		case 'd':
			strncpy(device,optarg,255);
			device[255]=0;
			break;
		case 'P':
			strncpy(socket_path,optarg,255);
			socket_path[255]=0;
			break;
		case 'p':
			strncpy(password,optarg,255);
			password[255]=0;
			break;
		case 'u':
			strncpy(username,optarg,255);
			username[255]=0;
			break;
		case 'r':
			no_reboot=1;
			break;
		case 'K':
			ret|=do_all_kill(uuids,num_uuids,device);
			break;	
		case 'R':
			ret|=do_all_reboot(uuids, num_uuids, device);
			break;
		case 'L':
			show_all_firmwares(uuids, num_uuids);
			break;
		case 'I':
			do_fw_actions(uuids, num_uuids, 0, optarg);
			break;
		case 'E':
			do_fw_actions(uuids, num_uuids, 1, optarg);
			break;
		case 'Z':
			do_fw_actions(uuids, num_uuids, 2, optarg);
			break;			
		case 'q':
			verbose=0;
			break;
		default:
			usage();
			break;
		}
	}
	exit(ret);
}
示例#4
0
/* 下载文件或目录 */
int command_download(int argc, char **argv) {
//{{{
    int option_overwrite    = 0; /* 覆盖同名文件        */
    int option_new          = 0; /* 创建新文件          */

    int ret = 0;
    char c;

    char *remote_path;
    char *local_path;
    
    opterr = 0;
    while ((c = getopt(argc, argv, "on")) != -1) {
        switch (c) {
            case 'o':
                option_overwrite = 1;
                break;
            case 'n':
                option_new = 1;
                break;
        }
    }
 
    if (option_overwrite && option_new) {
        color_log(COLOR_LOG_ERROR, "请不要同时指定-n -o\n");
        ret = 1;
        goto free;
    }

    if (optind < argc - 2) {
        color_log(COLOR_LOG_ERROR, "请指定路径\n");
        usage();
        ret = 1;
        goto free;
    }

    local_path = argv[argc - 1];
    if (local_path[strlen(local_path) - 1] == '/') {
        local_path[strlen(local_path) - 1] = '\0';
    }

    remote_path = argv[argc - 2];
    if (remote_path[strlen(remote_path) - 1] == '/') {
        remote_path[strlen(remote_path) - 1] = '\0';
    }

#ifdef DEBUG
    fprintf(stderr, "Download %s to %s\n", local_path, remote_path);
#endif

    if (!init_api()) {
        ret = 1;
        goto free;
    }
    
    ret = do_download(remote_path,
                    local_path,
                    option_overwrite, 
                    option_new);
free:
    return ret;
} 
示例#5
0
文件: main.c 项目: kl3mz/webserver
int
main (int argc, char **argv)
{
    int                    re;
    ret_t                  ret;
    cint_t                 val;
    cint_t                 param_num;
    cint_t                 long_index;
    cherokee_downloader_t *downloader;
    cherokee_buffer_t      proxy       = CHEROKEE_BUF_INIT;
    cuint_t                proxy_port;

    struct option long_options[] = {
        /* Options without arguments */
        {"help",          no_argument,       NULL, 'h'},
        {"version",       no_argument,       NULL, 'V'},
        {"quiet",         no_argument,       NULL, 'q'},
        {"save-headers",  no_argument,       NULL, 's'},
        {"header",        required_argument, NULL,  0 },
        {NULL, 0, NULL, 0}
    };

    /* Parse known parameters
     */
    while ((val = getopt_long (argc, argv, "VshqO:", long_options, &long_index)) != -1) {
        switch (val) {
        case 'V':
            printf ("Cherokee Downloader %s\n"
                    "Written by Alvaro Lopez Ortega <*****@*****.**>\n\n"
                    "Copyright (C) 2001-2014 Alvaro Lopez Ortega.\n"
                    "This is free software; see the source for copying conditions.  There is NO\n"
                    "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
                    PACKAGE_VERSION);
            return EXIT_OK;

        case 'O':
            if (global_fd != UNSET_FD) {
                close (global_fd);
            }

            if ((strlen(optarg) == 1) && (optarg[0] == '-')) {
                global_fd = fileno(stdout);
            } else {
                global_fd = open (optarg, O_WRONLY | O_CREAT, 0644);
            }

            if (global_fd < 0) {
                PRINT_MSG ("ERROR: Can not open %s\n", optarg);
                return EXIT_ERROR;
            }
            break;

        case 0:
            break;

        case 'q':
            quiet = true;
            break;

        case 's':
            save_headers = true;
            break;

        case 'h':
        case '?':
        default:
            print_help();
            return EXIT_OK;
        }
    }

    /* The rest..
     */
    param_num = argc - optind;

    if (param_num <= 0) {
        print_usage();
        return EXIT_OK;
    }

    /* Tracing and proxy discovering..
     */
    cherokee_init();
    cget_find_proxy (&proxy, &proxy_port);

    for (val=optind; val<optind+param_num; val++) {
        cherokee_buffer_t url = CHEROKEE_BUF_INIT;

        /* Build the url buffer
         */
        ret = cherokee_buffer_add_va (&url, "%s", argv[val]);
        if (ret != ret_ok)
            exit (EXIT_ERROR);

        /* Create the downloader object..
         */
        ret = cherokee_downloader_new (&downloader);
        if (ret != ret_ok)
            exit (EXIT_ERROR);

        ret = cherokee_downloader_init(downloader);
        if (ret != ret_ok)
            exit (EXIT_ERROR);

        if (! cherokee_buffer_is_empty (&proxy)) {
            ret = cherokee_downloader_set_proxy (downloader, &proxy, proxy_port);
            if (ret != ret_ok)
                exit (EXIT_ERROR);
        }

        ret = cherokee_downloader_set_url (downloader, &url);
        if (ret != ret_ok)
            exit (EXIT_ERROR);

        ret = cherokee_downloader_connect (downloader);
        if (ret != ret_ok)
            exit (EXIT_ERROR);

        /* Download it!
         */
        ret = do_download (downloader);
        if ((ret != ret_ok) && (ret != ret_eof)) {
            exit (EXIT_ERROR);
        }

        /* Free the objects..
         */
        cherokee_buffer_mrproper (&url);
        cherokee_downloader_free (downloader);
    }

    /* Close the output file
     */
    re = close (output_fd);
    if (re != 0)
        exit (EXIT_ERROR);

    cherokee_mrproper();
    return EXIT_OK;
}
bool HttpLoader::download_sync(const std::string &url, const std::string &local_path)
{
    auto work_item = std::make_shared<DownloadItem>(url, local_path, nullptr);
    bool success = do_download(work_item, _curl_wrapper);
    return success;
}
示例#7
0
文件: light_ftp.c 项目: onlyu/fvn
int main(int argc, char **argv)
{

  static const char *global_opt_string = "GPu:p:l:r:C:h?";

  static const struct option global_long_opts[] = {
    { "user", required_argument, NULL, 'u' }, // user name
    { "password", required_argument, NULL, 'p' }, // password
    { "local-file", required_argument, NULL, 'l' }, // local file name
    { "remote-file", required_argument, NULL, 'r' }, // remote file name
    { "url", required_argument, NULL, 'U'}, // remote url
    { "get", no_argument, NULL, 'G' }, // use get method
    { "put", no_argument, NULL, 'P' }, // use put method
    { "help", no_argument, NULL, 'h'}, // help
    { NULL, no_argument, NULL, 0 },
  };

  /* parse the arguments */
  int opt = 0;
  int long_index = 0;
  global_arg.usr = NULL;
  global_arg.pwd = NULL;
  global_arg.url = NULL;
  global_arg.local_file = NULL;
  global_arg.remote_file = NULL;
  global_arg.method = METHOD_GET;

  opt = getopt_long(argc, argv, global_opt_string, global_long_opts, &long_index);

  while(opt != -1){
    switch(opt){
    case 'u':
      global_arg.usr = optarg;
      break;
    case 'p':
      global_arg.pwd = optarg;
      break;
    case 'l':
      global_arg.local_file = optarg;
      break;
    case 'r':
      global_arg.remote_file = optarg;
      break;
    case 'U':
      global_arg.url = optarg;
      break;
    case 'G':
      global_arg.method = METHOD_GET;
      break;
    case 'P':
      global_arg.method = METHOD_PUT;
      break;
    case 'h':
    case '?':
      display_usage();
      break;
    default:
      printf("wrong option:%c\n",opt);
      break;
    }
    opt = getopt_long(argc, argv, global_opt_string, global_long_opts, &long_index);
  }

#ifdef _DEBUG
  printf("arguments:\n");
  printf("user name:%s\n", global_arg.usr);
  printf("password:%s\n", global_arg.pwd);
  printf("locale file:%s\n", global_arg.local_file);
  printf("remote file:%s\n", global_arg.remote_file);
  printf("remote url:%s\n", global_arg.url);
  printf("method:%s\n", global_arg.method == METHOD_GET?"get":"put");
#endif

  // check the arguments
  if(!global_arg.url || strlen(global_arg.url) == 0){
    printf("error:not remote url or bad url!!!\n");
    display_usage();
  }

  if(global_arg.usr && !global_arg.pwd){
    printf("error:not password provide!!!\n");
    display_usage();
  }

  if(!global_arg.usr && global_arg.pwd){
    printf("warning:no user, ignore password!!!\n");
  }
  
  if(global_arg.method == METHOD_PUT){
    if(!global_arg.local_file || strlen(global_arg.local_file) == 0){
      printf("error:put method must have local file name!!!\n");
      display_usage();
    }
    if(!global_arg.remote_file || strlen(global_arg.remote_file) == 0){
      global_arg.remote_file = global_arg.local_file;
      //printf("warning:no argument remote file!!!\n");
    }
  }else{
    if(!global_arg.remote_file || strlen(global_arg.remote_file) == 0){
      printf("error:get method must have remote file name!!!\n");
      display_usage();
    }
    if(!global_arg.local_file || strlen(global_arg.local_file) == 0){
      global_arg.local_file = global_arg.remote_file;
      //printf("warning:no argument local file!!!\n");
    }
  }

  if(global_arg.method == METHOD_GET){
    do_download(global_arg.url, 
		global_arg.usr,
		global_arg.pwd,
		global_arg.local_file,
		global_arg.remote_file);
  }else{
    // call the upload function
    do_upload(global_arg.url, 
	      global_arg.usr, 
	      global_arg.pwd, 
	      global_arg.local_file, 
	      global_arg.remote_file);
  }
	
  printf("\n");
  return SUCCESS;
}
示例#8
0
static int
process_get(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
{
	char *abs_src = NULL;
	char *abs_dst = NULL;
	char *tmp;
	glob_t g;
	int err = 0;
	int i;

	abs_src = xstrdup(src);
	abs_src = make_absolute(abs_src, pwd);

	memset(&g, 0, sizeof(g));
	debug3("Looking up %s", abs_src);
	if (remote_glob(conn, abs_src, 0, NULL, &g)) {
		error("File \"%s\" not found.", abs_src);
		err = -1;
		goto out;
	}

	/* If multiple matches, dst must be a directory or unspecified */
	if (g.gl_matchc > 1 && dst && !is_dir(dst)) {
		error("Multiple files match, but \"%s\" is not a directory",
		    dst);
		err = -1;
		goto out;
	}

	for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
		if (infer_path(g.gl_pathv[i], &tmp)) {
			err = -1;
			goto out;
		}

		if (g.gl_matchc == 1 && dst) {
			/* If directory specified, append filename */
			if (is_dir(dst)) {
				if (infer_path(g.gl_pathv[0], &tmp)) {
					err = 1;
					goto out;
				}
				abs_dst = path_append(dst, tmp);
				xfree(tmp);
			} else
				abs_dst = xstrdup(dst);
		} else if (dst) {
			abs_dst = path_append(dst, tmp);
			xfree(tmp);
		} else
			abs_dst = tmp;

		printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst);
		if (do_download(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
			err = -1;
		xfree(abs_dst);
		abs_dst = NULL;
	}

out:
	xfree(abs_src);
	if (abs_dst)
		xfree(abs_dst);
	globfree(&g);
	return(err);
}