Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Add proxy support for HTTPClient and the editor #55988

Merged
merged 1 commit into from
Dec 16, 2021

Conversation

timothyqiu
Copy link
Member

3.x backport of #47257 and #55747.

  • Adds proxy related methods for HTTPClient and HTTPRequest
  • Adds network/http_proxy/{host,port} editor settings
  • Makes AssetLib and Export Template Manager proxy aware
Test code for HTTPClient
extends SceneTree

func _init():
	var err = 0
	var http = HTTPClient.new() # Create the Client.

	http.set_http_proxy("localhost", 7890)
	http.set_https_proxy("127.0.0.1", 7890)

	print("----- http with proxy -----")
	err = http.connect_to_host("httpbin.org", -1, false) # Connect to host/port.
	assert(err == OK) # Make sure connection was OK.
	do_http(http)
	http.close()

	print("----- https with proxy -----")
	err = http.connect_to_host("httpbin.org", -1, true) # Connect to host/port.
	assert(err == OK) # Make sure connection was OK.
	do_http(http)
	http.close()

	http.set_http_proxy("127.0.0.1", -1)
	http.set_https_proxy("127.0.0.1", -1)

	print("----- http without proxy -----")
	err = http.connect_to_host("httpbin.org", -1, false) # Connect to host/port.
	assert(err == OK) # Make sure connection was OK.
	do_http(http)
	http.close()

	print("----- https without proxy -----")
	err = http.connect_to_host("httpbin.org", -1, true) # Connect to host/port.
	assert(err == OK) # Make sure connection was OK.
	do_http(http)
	http.close()

	quit()


func do_http(http):
	var err = 0
	# Wait until resolved and connected.
	while http.get_status() == HTTPClient.STATUS_CONNECTING or http.get_status() == HTTPClient.STATUS_RESOLVING:
		http.poll()
		print("Connecting...")
		OS.delay_msec(500)

	assert(http.get_status() == HTTPClient.STATUS_CONNECTED) # Could not connect

	# Some headers
	var headers = [
			"User-Agent: Pirulo/1.0 (Godot)",
			"Accept: */*"
			]

	err = http.request(HTTPClient.METHOD_GET, "/get", headers) # Request a page from the site (this one was chunked..)
	assert(err == OK) # Make sure all is OK.

	while http.get_status() == HTTPClient.STATUS_REQUESTING:
		# Keep polling for as long as the request is being processed.
		http.poll()
		print("Requesting...")
		OS.delay_msec(500)

	assert(http.get_status() == HTTPClient.STATUS_BODY or http.get_status() == HTTPClient.STATUS_CONNECTED) # Make sure request finished well.

	print("response? ", http.has_response()) # Site might not have a response.

	if http.has_response():
		# If there is a response...

		headers = http.get_response_headers_as_dictionary() # Get response headers.
		print("code: ", http.get_response_code()) # Show response code.
		print("**headers:\\n", headers) # Show headers.

		# Getting the HTTP Body

		if http.is_response_chunked():
			# Does it use chunks?
			print("Response is Chunked!")
		else:
			# Or just plain Content-Length
			var bl = http.get_response_body_length()
			print("Response Length: ", bl)

		# This method works for both anyway

		var rb = PoolByteArray() # Array that will hold the data.

		while http.get_status() == HTTPClient.STATUS_BODY:
			# Get a chunk.
			var chunk = http.read_response_body_chunk()
			if chunk.size() == 0:
				OS.delay_usec(1000)
			else:
				rb = rb + chunk # Append to read buffer.
			http.poll()

		print("bytes got: ", rb.size())
		var text = rb.get_string_from_ascii()
		print("Text: ", text)

* Adds proxy related methods for `HTTPClient` and `HTTPRequest`
* Adds `network/http_proxy/{host,port}` editor settings
* Makes AssetLib and Export Template Manager proxy aware
@akien-mga akien-mga merged commit a90cac7 into godotengine:3.x Dec 16, 2021
@akien-mga
Copy link
Member

Thanks!

@almusx
Copy link

almusx commented Jun 4, 2022

This support authentication?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants