Git-Raw

 view release on metacpan or  search on metacpan

deps/libgit2/src/libgit2/transports/winhttp.c  view on Meta::CPAN

	int default_timeout = TIMEOUT_INFINITE;
	int default_connect_timeout = DEFAULT_CONNECT_TIMEOUT;
	DWORD autologon_policy = WINHTTP_AUTOLOGON_SECURITY_LEVEL_HIGH;

	const char *service_url = s->service_url;
	size_t i;
	const git_proxy_options *proxy_opts;

	/* If path already ends in /, remove the leading slash from service_url */
	if ((git__suffixcmp(t->server.url.path, "/") == 0) && (git__prefixcmp(service_url, "/") == 0))
		service_url++;
	/* Prepare URL */
	git_str_printf(&buf, "%s%s", t->server.url.path, service_url);

	if (git_str_oom(&buf))
		return -1;

	/* Convert URL to wide characters */
	if (git__utf8_to_16_alloc(&s->request_uri, git_str_cstr(&buf)) < 0) {
		git_error_set(GIT_ERROR_OS, "failed to convert string to wide form");
		goto on_error;
	}

	/* Establish request */
	s->request = WinHttpOpenRequest(
			t->connection,
			s->verb,
			s->request_uri,
			NULL,
			WINHTTP_NO_REFERER,
			types,
			git__strcmp(t->server.url.scheme, "https") == 0 ? WINHTTP_FLAG_SECURE : 0);

	if (!s->request) {
		git_error_set(GIT_ERROR_OS, "failed to open request");
		goto on_error;
	}

	/* Never attempt default credentials; we'll provide them explicitly. */
	if (!WinHttpSetOption(s->request, WINHTTP_OPTION_AUTOLOGON_POLICY, &autologon_policy, sizeof(DWORD)))
		return -1;

	if (!WinHttpSetTimeouts(s->request, default_timeout, default_connect_timeout, default_timeout, default_timeout)) {
		git_error_set(GIT_ERROR_OS, "failed to set timeouts for WinHTTP");
		goto on_error;
	}

	proxy_opts = &t->owner->connect_opts.proxy_opts;
	if (proxy_opts->type == GIT_PROXY_AUTO) {
		/* Set proxy if necessary */
		if (git_remote__http_proxy(&proxy_url, t->owner->owner, &t->server.url) < 0)
			goto on_error;
	}
	else if (proxy_opts->type == GIT_PROXY_SPECIFIED) {
		proxy_url = git__strdup(proxy_opts->url);
		GIT_ERROR_CHECK_ALLOC(proxy_url);
	}

	if (proxy_url) {
		git_str processed_url = GIT_STR_INIT;
		WINHTTP_PROXY_INFO proxy_info;
		wchar_t *proxy_wide;

		git_net_url_dispose(&t->proxy.url);

		if ((error = git_net_url_parse(&t->proxy.url, proxy_url)) < 0)
			goto on_error;

		if (strcmp(t->proxy.url.scheme, "http") != 0 && strcmp(t->proxy.url.scheme, "https") != 0) {
			git_error_set(GIT_ERROR_HTTP, "invalid URL: '%s'", proxy_url);
			error = -1;
			goto on_error;
		}

		git_str_puts(&processed_url, t->proxy.url.scheme);
		git_str_PUTS(&processed_url, "://");

		if (git_net_url_is_ipv6(&t->proxy.url))
			git_str_putc(&processed_url, '[');

		git_str_puts(&processed_url, t->proxy.url.host);

		if (git_net_url_is_ipv6(&t->proxy.url))
			git_str_putc(&processed_url, ']');

		if (!git_net_url_is_default_port(&t->proxy.url))
			git_str_printf(&processed_url, ":%s", t->proxy.url.port);

		if (git_str_oom(&processed_url)) {
			error = -1;
			goto on_error;
		}

		/* Convert URL to wide characters */
		error = git__utf8_to_16_alloc(&proxy_wide, processed_url.ptr);
		git_str_dispose(&processed_url);
		if (error < 0)
			goto on_error;

		proxy_info.dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
		proxy_info.lpszProxy = proxy_wide;
		proxy_info.lpszProxyBypass = NULL;

		if (!WinHttpSetOption(s->request,
			WINHTTP_OPTION_PROXY,
			&proxy_info,
			sizeof(WINHTTP_PROXY_INFO))) {
			git_error_set(GIT_ERROR_OS, "failed to set proxy");
			git__free(proxy_wide);
			goto on_error;
		}

		git__free(proxy_wide);

		if ((error = apply_credentials(s->request, &t->proxy.url, WINHTTP_AUTH_TARGET_PROXY, t->proxy.cred, t->proxy.auth_mechanisms)) < 0)
			goto on_error;
	}

	/* Disable WinHTTP redirects so we can handle them manually. Why, you ask?
	 * http://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/b2ff8879-ab9f-4218-8f09-16d25dff87ae
	 */
	if (!WinHttpSetOption(s->request,
		WINHTTP_OPTION_DISABLE_FEATURE,
		&disable_redirects,
		sizeof(disable_redirects))) {
			git_error_set(GIT_ERROR_OS, "failed to disable redirects");
			error = -1;
			goto on_error;
	}

	/* Strip unwanted headers (X-P2P-PeerDist, X-P2P-PeerDistEx) that WinHTTP
	 * adds itself. This option may not be supported by the underlying
	 * platform, so we do not error-check it */
	WinHttpSetOption(s->request,
		WINHTTP_OPTION_PEERDIST_EXTENSION_STATE,
		&peerdist,
		sizeof(peerdist));

	/* Send Pragma: no-cache header */
	if (!WinHttpAddRequestHeaders(s->request, pragma_nocache, (ULONG) -1L, WINHTTP_ADDREQ_FLAG_ADD)) {
		git_error_set(GIT_ERROR_OS, "failed to add a header to the request");
		goto on_error;
	}

	if (post_verb == s->verb) {
		/* Send Content-Type and Accept headers -- only necessary on a POST */
		git_str_clear(&buf);
		if (git_str_printf(&buf,
			"Content-Type: application/x-git-%s-request",
			s->service) < 0)
			goto on_error;

		if (git__utf8_to_16(ct, MAX_CONTENT_TYPE_LEN, git_str_cstr(&buf)) < 0) {
			git_error_set(GIT_ERROR_OS, "failed to convert content-type to wide characters");
			goto on_error;
		}

		if (!WinHttpAddRequestHeaders(s->request, ct, (ULONG)-1L,
			WINHTTP_ADDREQ_FLAG_ADD | WINHTTP_ADDREQ_FLAG_REPLACE)) {
			git_error_set(GIT_ERROR_OS, "failed to add a header to the request");
			goto on_error;
		}

		git_str_clear(&buf);
		if (git_str_printf(&buf,
			"Accept: application/x-git-%s-result",
			s->service) < 0)



( run in 0.908 second using v1.01-cache-2.11-cpan-d7a12ab2c7f )