Ejemplo n.º 1
0
static int finalize() {
    static bool finalized = false;
    if (finalized) return 0;
    finalized = true;
    gstate.quit_activities();

#ifdef _WIN32
    shutdown_idle_monitor();

#ifdef USE_WINSOCK
    if (WinsockCleanup()) {
        log_message_error("WinSockCleanup() failed");
        return ERR_IO;
    }
#endif

    cleanup_system_monitor();

#endif

    curl_cleanup();

#ifdef _DEBUG
    gstate.free_mem();
#endif

    diagnostics_finish();
    gstate.cleanup_completed = true;
    return 0;
}
void *janus_source_keepalive(void *data) {
	JANUS_LOG(LOG_INFO, "SourcePlugin keepalive started\n");

	CURL *curl = curl_init();
	gchar *body_str = g_strdup_printf("{\"pid\": \"%s\", \"dly\": \"%lu\"}", PID, (uint64_t)(keepalive_interval/G_USEC_PER_SEC));
	json_t *res_json_object = NULL;	

	while (g_atomic_int_get(&initialized) && !g_atomic_int_get(&stopping)) {
		janus_mutex_lock(&keepalive_mutex);
		
		gboolean retCode = curl_request(curl, keepalive_service_url, body_str, "POST", &res_json_object);
		if (retCode != TRUE) {
			JANUS_LOG(LOG_ERR, "Could not send the request to the server.\n");
		}else{
			if (json_is_object(res_json_object)) json_decref(res_json_object);
			else JANUS_LOG(LOG_ERR, "Not valid json object.\n");
		}

		janus_mutex_unlock(&keepalive_mutex);
		g_usleep(keepalive_interval);
	}

	if (body_str) {
		g_free(body_str);
	}

	curl_cleanup(curl);

	JANUS_LOG(LOG_INFO, "SourcePlugin keepalive stopped\n");
	return NULL;
}
Ejemplo n.º 3
0
static int finalize() {
    static bool finalized = false;
    if (finalized) return 0;
    finalized = true;
    gstate.quit_activities();
    daily_xfer_history.write_state();

#ifdef _WIN32
    shutdown_idle_monitor();

#ifdef USE_WINSOCK
    if (WinsockCleanup()) {
        log_message_error("Failed to cleanup the Windows Sockets interface");
        return ERR_IO;
    }
#endif

    cleanup_system_monitor();

#endif

	curl_cleanup();

    gstate.free_mem();

    gstate.cleanup_completed = true;
    return 0;
}
Ejemplo n.º 4
0
int main(int argc, char* argv[]) {
    std::vector<std::string> args(argv, argv+argc);

    std::string url;
    size_t iterations = 1000;
    try {
        url = args.at(1);
        if (args.size() > 2)
            iterations = atoi(args.at(2).c_str());
    } catch (std::exception& e) {
        std::cerr << "exception: " << e.what() << std::endl;
        std::cerr << "usage: " << args.at(0) << " url iterations" << std::endl;
        return 1;
    }

    curl_init();

    CURL* curl = curl_easy_init();
    if (!curl) {
        logError("curl_easy_init");
        return 1;
    }

    setopt(curl, CURLOPT_URL, url.c_str());
    setopt(curl, CURLOPT_TIMEOUT, 5L);
    setopt(curl, CURLOPT_CONNECTTIMEOUT, 5L);
    setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
    setopt(curl, CURLOPT_HEADER, 1L);
    setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);

    char send_data[] = "words, words, words";

    for (size_t i = 0; i < iterations; i++) {
        ReadState read_state(send_data, strlen(send_data));
        setopt(curl, CURLOPT_POST, 1L);
        setopt(curl, CURLOPT_READFUNCTION, read_callback);
        setopt(curl, CURLOPT_READDATA, &read_state);
        setopt(curl, CURLOPT_POSTFIELDSIZE, read_state.remains);

        bool sent = false;
        for (size_t i = 0; i < 3; i++) {
            if (perform(curl)) {
                std::cout << "^";
                sent = true;
                break;
            }
            std::cout << ".";
        }
        if (!sent) {
            std::cout << "!";
        }
    }

    curl_easy_cleanup(curl);
    curl_cleanup();

    return 0;
}
Ejemplo n.º 5
0
int http_download(lua_State* L)
{
	curl_state state;
	CURL* curl;
	CURLcode code = CURLE_FAILED_INIT;
	long responseCode = 0;

	FILE* fp;
	const char* file = luaL_checkstring(L, 2);

	fp = fopen(file, "wb");
	if (!fp)
	{
		lua_pushstring(L, "Unable to open file.");
		lua_pushnumber(L, -1);
		return 2;
	}

	if (lua_istable(L, 3))
	{
		// http.download(source, destination, { options })
		curl = curl_request(L, &state, /*optionsIndex=*/3, /*progressFnIndex=*/0, /*headersIndex=*/0);
	}
	else
	{
		// backward compatible function signature
		// http.download(source, destination, progressFunction, { headers })
		curl = curl_request(L, &state, /*optionsIndex=*/0, /*progressFnIndex=*/3, /*headersIndex=*/4);
	}

	if (curl)
	{
		curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_file_cb);

		code = curl_easy_perform(curl);
		curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &responseCode);
		curl_cleanup(curl, &state);
	}

	fclose(fp);

	if (code != CURLE_OK)
	{
		char errorBuf[1024];
		snprintf(errorBuf, sizeof(errorBuf) - 1, "%s\n%s\n", curl_easy_strerror(code), state.errorBuffer);
		lua_pushstring(L, errorBuf);
	}
	else
	{
		lua_pushstring(L, "OK");
	}

	lua_pushnumber(L, (lua_Number)responseCode);
	return 2;
}
Ejemplo n.º 6
0
int http_get(lua_State* L)
{
	curl_state state;
	CURL* curl;
	CURLcode code = CURLE_FAILED_INIT;
	long responseCode = 0;

	if (lua_istable(L, 2))
	{
		// http.get(source, { options })
		curl = curl_request(L, &state, /*optionsIndex=*/2, /*progressFnIndex=*/0, /*headersIndex=*/0);
	}
	else
	{
		// backward compatible function signature
		// http.get(source, progressFunction, { headers })
		curl = curl_request(L, &state, /*optionsIndex=*/0, /*progressFnIndex=*/2, /*headersIndex=*/3);
	}

	string_init(&state.S);
	if (curl)
	{
		curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);

		code = curl_easy_perform(curl);
		curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &responseCode);
		curl_cleanup(curl, &state);
	}

	if (code != CURLE_OK)
	{
		char errorBuf[1024];

		lua_pushnil(L);
		snprintf(errorBuf, sizeof(errorBuf) - 1, "%s\n%s\n", curl_easy_strerror(code), state.errorBuffer);
		lua_pushstring(L, errorBuf);
	}
	else
	{
		lua_pushlstring(L, state.S.ptr, state.S.len);
		lua_pushstring(L, "OK");
	}

	string_free(&state.S);
	lua_pushnumber(L, (lua_Number)responseCode);
	return 3;
}
Ejemplo n.º 7
0
int http_post(lua_State* L)
{
	curl_state state;
	CURL* curl;
	CURLcode code = CURLE_FAILED_INIT;
	long responseCode = 0;

	// http.post(source, postdata, { options })
	curl = curl_request(L, &state, /*optionsIndex=*/3, /*progressFnIndex=*/0, /*headersIndex=*/0);

	string_init(&state.S);
	if (curl)
	{
		size_t dataSize;
		const char* data = luaL_checklstring(L, 2, &dataSize);

		curl_easy_setopt(curl, CURLOPT_POST, 1);
		if (data && dataSize > 0)
		{
			curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)dataSize);
			curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
		}

		code = curl_easy_perform(curl);
		curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &responseCode);
		curl_cleanup(curl, &state);
	}

	if (code != CURLE_OK)
	{
		char errorBuf[1024];

		lua_pushnil(L);
		snprintf(errorBuf, sizeof(errorBuf) - 1, "%s\n%s\n", curl_easy_strerror(code), state.errorBuffer);
		lua_pushstring(L, errorBuf);
	}
	else
	{
		lua_pushlstring(L, state.S.ptr, state.S.len);
		lua_pushstring(L, "OK");
	}

	string_free(&state.S);
	lua_pushnumber(L, (lua_Number)responseCode);
	return 3;
}
Ejemplo n.º 8
0
/* unis_curl_free : Free the curl context, in case of persistent connection it does curl clean up
 * context : curl context
 */
void unis_curl_free(curl_context *context){
	
	if(context == NULL){
		return;
	}
	
	if(context->certfile)
		free(context->certfile);
	
	if(context->keyfile)
		free(context->keyfile);

	if(context->keypass)
		free(context->keypass);
	
	if(context->cacerts)
		free(context->cacerts);

	curl_cleanup(context);

	free(context);
}
Ejemplo n.º 9
0
Archivo: burp.c Proyecto: cinelli/burp
int main(int argc, char **argv) {
  long cookie_expire;
  int ret = 1;

  config = config_new();

  if (curl_init() != 0) {
    fprintf(stderr, "Error: An error occurred while initializing curl\n");
    goto finish;
  }

  ret = parseargs(argc, argv);
  if (ret != 0) {
    return 1;
  }

  if (config->category) {
    config->catnum = category_is_valid(config->category);
    if (config->catnum < 0) {
      usage_categories();
      goto finish;
    }
  } else {
    config->catnum = 1;
  }

  if (optind == argc) {
    fprintf(stderr, "error: no packages specified (use -h for help)\n");
    goto finish;
  }

  /* We can't read the config file without having verbosity set, but the
   * command line options need to take precedence over the config.  file.
   * Therefore, if ((user && pass) || cookie file) is supplied on the command
   * line, we won't read the config file.
   */
  if (!(config->user || config->cookie_file)) {
    read_config_file();
  }

  if (cookie_setup() != 0) {
    goto finish;
  }

  cookie_expire = cookie_expire_time(config->cookie_file, AUR_DOMAIN, AUR_COOKIE_NAME);
  if (cookie_expire > 0) {
    if (time(NULL) < cookie_expire) {
      config->cookie_valid = 1;
    } else {
      fprintf(stderr, "Your cookie has expired. Gathering user and password...\n");
    }
  }

  if (!config->cookie_valid) {
    if (!config->cmdline_user && !config->user) {
      config->user = read_stdin("Enter username", AUR_USER_MAX, 1);
      if (!config->user || !strlen(config->user)) {
        fprintf(stderr, "error: invalid username supplied\n");
        goto finish;
      }
    }

    if (!config->password || (config->cmdline_user && !config->cmdline_passwd)) {
      printf("[%s] ", config->user);
      config->password = read_stdin("Enter password", AUR_PASSWORD_MAX, 0);
    }
  }

  if (config->cookie_valid || aur_login() == 0) {
    char *csrf_token;

    /* booo, stupid hacks. curl doesn't prime curl_slist of cookies
     * we want via CURLINFO_COOKIELIST until we call perform at least
     * once. */
    prime_cookielist();

    csrf_token = get_csrf_token();
    if (csrf_token == NULL) {
      fprintf(stderr, "failed to obtain CSRF token for uploading\n");
      goto finish;
    }

    ret = 0;

    while (optind < argc) {
      int r = aur_upload(argv[optind++], csrf_token);
      if (r != 0) {
        ret = r;
      }
    }
    free(csrf_token);
  }

finish:
  if (config->cookie_file && !config->cookie_persist) {
    debug("Deleting file %s\n", config->cookie_file);
    unlink(config->cookie_file);
  }

  config_free(config);
  curl_cleanup();

  return ret;
}
void janus_source_destroy(void) {
	if (!g_atomic_int_get(&initialized))
		return;
	g_atomic_int_set(&stopping, 1);

	g_async_queue_push(messages, &exit_message);
	if (handler_thread != NULL) {
		g_thread_join(handler_thread);
		handler_thread = NULL;
	}

	g_hash_table_foreach(sessions, janus_source_close_session_func, NULL);
	socket_utils_destroy();

	janus_source_deattach_rtsp_queue_callback(rtsp_server_data);
	
	janus_source_rtsp_clean_and_quit_main_loop(rtsp_server_data);
	
	if (handler_rtsp_thread != NULL) {
		g_thread_join(handler_rtsp_thread);
		handler_rtsp_thread = NULL;
	}
  
	g_free(rtsp_server_data);
	rtsp_server_data = NULL;

	if (keepalive != NULL) {
		g_thread_join(keepalive);
		keepalive = NULL;
	}

	if(keepalive == NULL){
		janus_source_remove_pid_from_registry();
	}

	if (watchdog != NULL) {
		g_thread_join(watchdog);
		watchdog = NULL;
	}

	/* FIXME We should destroy the sessions cleanly */
	janus_mutex_lock(&sessions_mutex);
	g_hash_table_destroy(sessions);
	janus_mutex_unlock(&sessions_mutex);
	g_async_queue_unref(messages);
	messages = NULL;
	sessions = NULL;
	
    /* Free configuration fields */
  if (keepalive_service_url) {
    g_free(keepalive_service_url);
    keepalive_service_url = NULL;
  }
  
	g_free(status_service_url);
	status_service_url = NULL;
 
	g_free(rtsp_interface_ip);
	rtsp_interface_ip = NULL;
 
	curl_cleanup(curl_handle);
	
	g_atomic_int_set(&initialized, 0);
	g_atomic_int_set(&stopping, 0);
	JANUS_LOG(LOG_INFO, "%s destroyed!\n", JANUS_SOURCE_NAME);
}
Ejemplo n.º 11
0
Archivo: saldl.c Proyecto: saldl/saldl
void saldl(saldl_params *params_ptr) {
  /* Definitions */
  info_s info = DEF_INFO_S;
  info.params = params_ptr;

  /* Handle signals */
  info_global = &info;
  saldl_handle_signals();

  /* Need to be set as early as possible */
  set_color(&params_ptr->no_color);
  set_verbosity(&params_ptr->verbosity, &params_ptr->libcurl_verbosity);

  /* Check if loaded libcurl is recent enough */
  info.curl_info = curl_version_info(CURLVERSION_NOW);
  check_libcurl(info.curl_info);

  /* Library initializations, should run only once */
  SALDL_ASSERT(!curl_global_init(CURL_GLOBAL_ALL));
  SALDL_ASSERT(!evthread_use_pthreads());

  /* get/set initial info */
  main_msg("URL", "%s", params_ptr->start_url);
  check_url(params_ptr->start_url);
  get_info(&info);
  set_info(&info);
  check_remote_file_size(&info);

  /* initialize chunks early for extra_resume() */
  chunks_init(&info);

  if (params_ptr->resume) {
    check_resume(&info);
  }

  print_chunk_info(&info);
  global_progress_init(&info);

  /* exit here if dry_run was set */
  if ( params_ptr->dry_run  ) {
    saldl_free_all(&info);
    finish_msg_and_exit("Dry-run done.");
  }

  check_files_and_dirs(&info);

  /* Check if download was interrupted after all data was merged */
  if (info.already_finished) {
    goto saldl_all_data_merged;
  }

  /* threads, needed by set_modes() */
  info.threads = saldl_calloc(params_ptr->num_connections, sizeof(thread_s));
  set_modes(&info);

  /* 1st iteration */
  for (size_t counter = 0; counter < params_ptr->num_connections; counter++) {
    queue_next_chunk(&info, counter, 1);
  }

  /* Create event pthreads */
  saldl_pthread_create(&info.trigger_events_pth, NULL, events_trigger_thread, &info);

  if (!params_ptr->read_only && !params_ptr->to_stdout) {
    saldl_pthread_create(&info.sync_ctrl_pth, NULL, sync_ctrl, &info);
  }

  if (info.chunk_count != 1) {
    saldl_pthread_create(&info.status_display_pth, NULL, status_display, &info);
    saldl_pthread_create(&info.queue_next_pth, NULL, queue_next_thread, &info);
    saldl_pthread_create(&info.merger_pth, NULL, merger_thread, &info);
  }

  /* Now that everything is initialized */
  info.session_status = SESSION_IN_PROGRESS;

  /* Avoid race in joining event threads if the session was interrupted, or finishing without downloading if single_mode */
  do {
    usleep(100000);
  } while (params_ptr->single_mode ? info.chunks[0].progress != PRG_FINISHED : info.global_progress.complete_size != info.file_size);

  /* Join event pthreads */
  if (!params_ptr->read_only && !params_ptr->to_stdout) {
    join_event_pth(&info.ev_ctrl ,&info.sync_ctrl_pth);
  }

  if (info.chunk_count !=1) {
    join_event_pth(&info.ev_status, &info.status_display_pth);
    join_event_pth(&info.ev_queue, &info.queue_next_pth);
    join_event_pth(&info.ev_merge, &info.merger_pth);
  }

  info.events_queue_done = true;
  event_queue(&info.ev_trigger, NULL);
  join_event_pth(&info.ev_trigger ,&info.trigger_events_pth);

saldl_all_data_merged:

  /* Remove tmp_dirname */
  if (!params_ptr->read_only && !params_ptr->mem_bufs && !params_ptr->single_mode) {
    if ( rmdir(info.tmp_dirname) ) {
      err_msg(FN, "Failed to delete %s: %s", info.tmp_dirname, strerror(errno) );
    }
  }

  /*** Final Steps ***/

  /* One last check  */
  if (info.file_size && !params_ptr->no_remote_info &&
      !params_ptr->read_only && !params_ptr->to_stdout &&
      (!info.remote_info.content_encoded || params_ptr->no_decompress)) {
    off_t saved_file_size = saldl_fsizeo(info.part_filename, info.file);
    if (saved_file_size != info.file_size) {
      pre_fatal(FN, "Unexpected saved file size (%"SAL_JU"!=%"SAL_JU").", saved_file_size, info.file_size);
      pre_fatal(FN, "This could happen if you're downloading from a dynamic site.");
      pre_fatal(FN, "If that's the case and the download is small, retry with --no-remote-info");
      fatal(FN, "If you think that's a bug in saldl, report it: https://github.com/saldl/saldl/issues");
    }
  }
  else {
    debug_msg(FN, "Strict check for finished file size skipped.");
  }

  if (!params_ptr->read_only && !params_ptr->to_stdout) {
    saldl_fclose(info.part_filename, info.file);
    if (rename(info.part_filename, params_ptr->filename) ) {
      err_msg(FN, "Failed to rename now-complete %s to %s: %s", info.part_filename, params_ptr->filename, strerror(errno));
    }

    saldl_fclose(info.ctrl_filename, info.ctrl_file);
    if ( remove(info.ctrl_filename) ) {
      err_msg(FN, "Failed to remove %s: %s", info.ctrl_filename, strerror(errno));
    }
  }

  /* cleanups */
  curl_cleanup(&info);
  saldl_free_all(&info);

  finish_msg_and_exit("Download Finished.");
}
Ejemplo n.º 12
0
interface void lt_cleanup(void)
{
    curl_cleanup();
}