Example #1
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;
}
Example #2
0
int http_get(lua_State* L)
{
	CurlCallbackState state = { 0, 0, {NULL, 0} };

	CURL* curl = curl_request(L, &state, /*fp=*/NULL, /*progressFnIndex=*/2);
	CURLcode code;

	string_init(&state.S);

	if (!curl)
	{
		lua_pushnil(L);
		return 1;
	}

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

		lua_pushnil(L);
		lua_pushfstring(L, errorBuf);
		return 2;
	}

	curl_easy_cleanup(curl);

	lua_pushlstring(L, state.S.ptr, state.S.len);
	return 1;
}
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;
}
Example #4
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;
}
void janus_source_remove_pid_from_registry(void){

	gchar *curl_str = g_strdup_printf("%s/%s", keepalive_service_url, PID);	
	gboolean retCode = curl_request(curl_handle, keepalive_service_url, "{}", "DELETE", NULL);		    	

	if (curl_str) {
		g_free(curl_str);
	}

	if (retCode != TRUE) {
	    JANUS_LOG(LOG_ERR, "Could not send the request to the server\n"); 
	}
}
Example #6
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;
}
Example #7
0
int http_download(lua_State* L)
{
	CurlCallbackState state = { 0, 0, {NULL, 0} };

	CURL* curl;
	CURLcode code = CURLE_FAILED_INIT;

	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;
	}

	curl = curl_request(L, &state, fp, /*progressFnIndex=*/3);
	if (curl)
	{
		code = curl_easy_perform(curl);
		curl_easy_cleanup(curl);
	}

	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, code);
	return 2;
}
Example #8
0
File: query.c Project: m-sche/tiret
static char *get_random_list(CURL *curl, const char *base_url)
{
	char *url = strdup(base_url);
	char *str;

	str_append(&url, "?action=query&list=random"
			 "&format=json&utf8&rnnamespace=0"
			 "&rnlimit=" RANDOM_LIMIT_STR);

#ifdef FAKE_RANDOM_LIST
	str = read_file(FAKE_RANDOM_LIST);
#else
	str = curl_request(curl, url, NULL, 0);
#endif

	free(url);
	if (str == NULL)
		return NULL;
	//printf("%s\n", str);

	return str;
}
static void janus_source_close_session(janus_source_session * session) {
	JANUS_LOG(LOG_INFO, "Closing source session: %s\n", session->id);

	gchar *session_id = g_strdup(session->db_entry_session_id);
	gchar *curl_str = g_strdup_printf("%s/%s", status_service_url, session_id);	

#ifdef USE_REGISTRY_SERVICE
	curl_request(curl_handle, curl_str, "{}", "DELETE", NULL);		    
#endif	    

	if(rtsp_server_data && session->callback_data)	
		janus_source_rtsp_remove_mountpoint(rtsp_server_data, session->id, session->callback_data);

	if (session->sockets) {
		JANUS_LOG(LOG_VERB, "Closing session sockets\n");
		g_hash_table_foreach_remove(session->sockets, (GHRFunc)close_and_destroy_sockets, NULL);
		g_hash_table_destroy(session->sockets);
		session->sockets = NULL;
	}

	g_free(session_id);
	session_id = NULL;

	g_free(curl_str);
	curl_str = NULL;

	g_free(session->id);
	session->id = NULL;

	g_free(session->db_entry_session_id);
	session->db_entry_session_id = NULL;

	g_free(session->rtsp_url);
	session->rtsp_url = NULL;

}
Example #10
0
File: query.c Project: m-sche/tiret
/* Download a given page */
struct page query(CURL *curl, const char *base_url, const char *title)
{
	char *url = strdup(base_url);
	char *title_urlencoded;
	char *str;
	struct page page = {NULL};

	title_urlencoded = curl_easy_escape(curl, title, 0);

	str_append(&url, "?action=query&prop=revisions"
			"&format=json&utf8"
			"&rvprop=content|timestamp"
			"&redirects&titles=");
	str_append(&url, title_urlencoded);
	curl_free(title_urlencoded);

#ifdef FAKE_QUERY
	str = read_file(FAKE_QUERY);
#else
	str = curl_request(curl, url, NULL, 0);
#endif

	free(url);
	if (str == NULL)
		return page;

	/* For debugging */
	//puts(str); exit(0);

	/* TODO Prettify */
	if (strchr(str, '\n')) {
		//fprintf(run_log, "(!) query: LF found in '%s'\n", title);
		char *tmp = str;
		str = replace(tmp, "\n", "");
		free(tmp);
	}
	if (strchr(str, '\r')){
		//fprintf(run_log, "(!) query: CR found in '%s'\n", title);
		char *tmp = str;
		str = replace(tmp, "\r", "");
		free(tmp);
	}

	page.title = strdup(title);
	page.content = find_value(str, "query", "pages", "", "revisions", "1", "*", NULL);
	page.timestamp = find_value(str, "query", "pages", "", "revisions", "1", "timestamp", NULL);
	free(str);
	if (page.content == NULL)
		return page;

	unescape(&page.content);

#ifdef CHECK_IO
	char *fn = NULL;
	str_append(&fn, "tmp/");
	str_append(&fn, page.title);
	FILE *f = fopen(fn, "w");
	fputs(page.content, f);
	fclose(f);
#endif

	/* Initialize the rest of 'struct page' */
	page.forbidden = calloc(strlen(page.content), sizeof(int));
	heap_init(&page.err_found, 16, cmp_error);
	queue_init(&page.err_approved);

	return page;
}