Exemple #1
0
Error HTTPRequest::request(const String &p_url, const Vector<String> &p_custom_headers, bool p_ssl_validate_domain, HTTPClient::Method p_method, const String &p_request_data) {

	ERR_FAIL_COND_V(!is_inside_tree(), ERR_UNCONFIGURED);
	if (requesting) {
		ERR_EXPLAIN("HTTPRequest is processing a request. Wait for completion or cancel it before attempting a new one.");
		ERR_FAIL_V(ERR_BUSY);
	}

	method = p_method;

	Error err = _parse_url(p_url);
	if (err)
		return err;

	validate_ssl = p_ssl_validate_domain;

	bool has_user_agent = false;
	bool has_accept = false;
	headers = p_custom_headers;

	request_data = p_request_data;

	for (int i = 0; i < headers.size(); i++) {

		if (headers[i].findn("user-agent:") == 0)
			has_user_agent = true;
		if (headers[i].findn("Accept:") == 0)
			has_accept = true;
	}

	if (!has_user_agent) {
		headers.push_back("User-Agent: GodotEngine/" + String(VERSION_MKSTRING) + " (" + OS::get_singleton()->get_name() + ")");
	}

	if (!has_accept) {
		headers.push_back("Accept: */*");
	}

	requesting = true;

	if (use_threads) {

		thread_done = false;
		thread_request_quit = false;
		client->set_blocking_mode(true);
		thread = Thread::create(_thread_func, this);
	} else {
		client->set_blocking_mode(false);
		err = _request();
		if (err != OK) {
			call_deferred("_request_done", RESULT_CANT_CONNECT, 0, PoolStringArray(), PoolByteArray());
			return ERR_CANT_CONNECT;
		}

		set_process_internal(true);
	}

	return OK;
}
END_TEST
#endif

#ifdef ECORE_CON_HTTP_TEST_URL
START_TEST(ecore_con_test_ecore_con_url_post)
{
   Ecore_Con_Url *ec_url;
   url_test *info;
   int ret;
   char link[] = ECORE_CON_HTTP_TEST_URL;
   char url_data[] = "test";
   char *username = NULL, *password = NULL;
   char url[4096];

   ret = eina_init();
   fail_if(ret != 1);
   ret = ecore_con_url_init();
   fail_if(ret != 1);

   fail_unless(_parse_url(link, url, &username, &password, NULL, NULL));

   fprintf (stderr, "HTTP: \n url = %s \n username = %s \n password = %s \n", url, username, password);

   ecore_con_url_pipeline_set(EINA_TRUE);
   fail_unless (ecore_con_url_pipeline_get());

   ec_url = ecore_con_url_custom_new(url, "POST");
   fail_unless (ec_url);

   ecore_con_url_additional_header_add(ec_url, "User-Agent", "blablabla");
   ecore_con_url_verbose_set(ec_url, EINA_TRUE);

   ecore_con_url_httpauth_set(ec_url, username, password, EINA_FALSE);
   ecore_con_url_time(ec_url, ECORE_CON_URL_TIME_IFMODSINCE, 0);

   fail_unless(ecore_con_url_post(ec_url, url_data, 4, NULL));

   ecore_event_handler_add(ECORE_CON_EVENT_URL_COMPLETE,
                           _url_compl_cb, info);

   ret = ecore_con_url_shutdown();
   fail_if(ret != 0);
   ret = eina_shutdown();
}
Exemple #3
0
Error HTTPRequest::request(const String& p_url, const Vector<String>& p_custom_headers, bool p_ssl_validate_domain) {

	ERR_FAIL_COND_V(!is_inside_tree(),ERR_UNCONFIGURED);
	if ( requesting ) {
		ERR_EXPLAIN("HTTPRequest is processing a request. Wait for completion or cancel it before attempting a new one.");
		ERR_FAIL_V(ERR_BUSY);
	}

	Error err = _parse_url(p_url);
	validate_ssl=p_ssl_validate_domain;

	bool has_user_agent=false;
	bool has_accept=false;
	headers=p_custom_headers;

	for(int i=0;i<headers.size();i++) {

		if (headers[i].findn("user-agent:")==0)
			has_user_agent=true;
		if (headers[i].findn("Accept:")==0)
			has_accept=true;
	}

	if (!has_user_agent) {
		headers.push_back("User-Agent: GodotEngine/"+String(VERSION_MKSTRING)+" ("+OS::get_singleton()->get_name()+")");
	}

	if (!has_accept) {
		headers.push_back("Accept: */*");
	}


	err = _request();

	if (err==OK) {
		set_process(true);
		requesting=true;
	}


	return err;
}
Exemple #4
0
static int _http_request_on_headers_complete(http_parser* inner) {
  shttp_connection_internal_t* conn;
  int                          rc;

  conn = (shttp_connection_internal_t*)inner->data;
  conn_incomming(conn).status = shttp_request_parse_headers_ok;

  conn_request(conn).headers.array = conn_incomming(conn).headers.array;
  conn_request(conn).headers.length = conn_incomming(conn).headers.length;
  conn_request(conn).http_major = inner->http_major;
  conn_request(conn).http_minor = inner->http_minor;
  conn_request(conn).method = inner->method;
  conn_request(conn).content_length = inner->content_length;

  rc = _parse_url(conn);
  if(0 != rc) {
    return rc;
  }
  return _shttp_connection_on_headers_complete(conn);
}
Exemple #5
0
Error HTTPRequest::request(const String &p_url, const Vector<String> &p_custom_headers, bool p_ssl_validate_domain, HTTPClient::Method p_method, const String &p_request_data) {

	ERR_FAIL_COND_V(!is_inside_tree(), ERR_UNCONFIGURED);
	if (requesting) {
		ERR_EXPLAIN("HTTPRequest is processing a request. Wait for completion or cancel it before attempting a new one.");
		ERR_FAIL_V(ERR_BUSY);
	}

	method = p_method;

	Error err = _parse_url(p_url);
	if (err)
		return err;

	validate_ssl = p_ssl_validate_domain;

	headers = p_custom_headers;

	request_data = p_request_data;

	requesting = true;

	if (use_threads) {

		thread_done = false;
		thread_request_quit = false;
		client->set_blocking_mode(true);
		thread = Thread::create(_thread_func, this);
	} else {
		client->set_blocking_mode(false);
		err = _request();
		if (err != OK) {
			call_deferred("_request_done", RESULT_CANT_CONNECT, 0, PoolStringArray(), PoolByteArray());
			return ERR_CANT_CONNECT;
		}

		set_process_internal(true);
	}

	return OK;
}
Exemple #6
0
bool HTTPRequest::_handle_response(bool *ret_value) {

	if (!client->has_response()) {
		call_deferred("emit_signal","request_completed",RESULT_NO_RESPONSE,0,StringArray(),ByteArray());
		*ret_value=true;
		return true;
	}

	got_response=true;
	response_code=client->get_response_code();
	List<String> rheaders;
	client->get_response_headers(&rheaders);
	response_headers.resize(0);
	downloaded=0;
	for (List<String>::Element *E=rheaders.front();E;E=E->next()) {
		print_line("HEADER: "+E->get());
		response_headers.push_back(E->get());
	}

	if (response_code==301 || response_code==302) {
		//redirect
		if (max_redirects>=0 && redirections>=max_redirects) {

			call_deferred("emit_signal","request_completed",RESULT_REDIRECT_LIMIT_REACHED,response_code,response_headers,ByteArray());
			*ret_value=true;
			return true;
		}

		String new_request;

		for (List<String>::Element *E=rheaders.front();E;E=E->next()) {
			if (E->get().findn("Location: ")!=-1) {
				new_request=E->get().substr(9,E->get().length()).strip_edges();
			}
		}

		print_line("NEW LOCATION: "+new_request);

		if (new_request!="") {
			//process redirect
			client->close();
			int new_redirs=redirections+1; //because _request() will clear it
			Error err;
			if (new_request.begins_with("http")) {
				//new url, request all again
				err=_parse_url(new_request);
			} else {
				request_string=new_request;
			}

			err = _request();

			print_line("new connection: "+itos(err));
			if (err==OK) {
				request_sent=false;
				got_response=false;
				body_len=-1;
				body.resize(0);
				downloaded=0;
				redirections=new_redirs;
				*ret_value=false;
				return true;

			}
		}
	}

	return false;
}