Blame src/transports/winhttp.c

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_WINHTTP
Packit Service 20376f
Packit Service 20376f
#include "git2.h"
Packit Service 20376f
#include "git2/transport.h"
Packit Service 20376f
#include "buffer.h"
Packit Service 20376f
#include "posix.h"
Packit Service 20376f
#include "netops.h"
Packit Service 20376f
#include "smart.h"
Packit Service 20376f
#include "remote.h"
Packit Service 20376f
#include "repository.h"
Packit Service 20376f
#include "global.h"
Packit Service 20376f
Packit Service 20376f
#include <wincrypt.h>
Packit Service 20376f
#include <winhttp.h>
Packit Service 20376f
Packit Service 20376f
/* For IInternetSecurityManager zone check */
Packit Service 20376f
#include <objbase.h>
Packit Service 20376f
#include <urlmon.h>
Packit Service 20376f
Packit Service 20376f
#define WIDEN2(s) L ## s
Packit Service 20376f
#define WIDEN(s) WIDEN2(s)
Packit Service 20376f
Packit Service 20376f
#define MAX_CONTENT_TYPE_LEN	100
Packit Service 20376f
#define WINHTTP_OPTION_PEERDIST_EXTENSION_STATE	109
Packit Service 20376f
#define CACHED_POST_BODY_BUF_SIZE	4096
Packit Service 20376f
#define UUID_LENGTH_CCH	32
Packit Service 20376f
#define TIMEOUT_INFINITE -1
Packit Service 20376f
#define DEFAULT_CONNECT_TIMEOUT 60000
Packit Service 20376f
#ifndef WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH
Packit Service 20376f
#define WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH 0
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
#ifndef WINHTTP_FLAG_SECURE_PROTOCOL_TLS_1_1
Packit Service 20376f
# define WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1 0x00000200
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
#ifndef WINHTTP_FLAG_SECURE_PROTOCOL_TLS_1_2
Packit Service 20376f
# define WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2 0x00000800
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
static const char *prefix_https = "https://";
Packit Service 20376f
static const char *upload_pack_service = "upload-pack";
Packit Service 20376f
static const char *upload_pack_ls_service_url = "/info/refs?service=git-upload-pack";
Packit Service 20376f
static const char *upload_pack_service_url = "/git-upload-pack";
Packit Service 20376f
static const char *receive_pack_service = "receive-pack";
Packit Service 20376f
static const char *receive_pack_ls_service_url = "/info/refs?service=git-receive-pack";
Packit Service 20376f
static const char *receive_pack_service_url = "/git-receive-pack";
Packit Service 20376f
static const wchar_t *get_verb = L"GET";
Packit Service 20376f
static const wchar_t *post_verb = L"POST";
Packit Service 20376f
static const wchar_t *pragma_nocache = L"Pragma: no-cache";
Packit Service 20376f
static const wchar_t *transfer_encoding = L"Transfer-Encoding: chunked";
Packit Service 20376f
static const int no_check_cert_flags = SECURITY_FLAG_IGNORE_CERT_CN_INVALID |
Packit Service 20376f
	SECURITY_FLAG_IGNORE_CERT_DATE_INVALID |
Packit Service 20376f
	SECURITY_FLAG_IGNORE_UNKNOWN_CA;
Packit Service 20376f
Packit Service 20376f
#if defined(__MINGW32__)
Packit Service 20376f
static const CLSID CLSID_InternetSecurityManager_mingw =
Packit Service 20376f
	{ 0x7B8A2D94, 0x0AC9, 0x11D1,
Packit Service 20376f
	{ 0x89, 0x6C, 0x00, 0xC0, 0x4F, 0xB6, 0xBF, 0xC4 } };
Packit Service 20376f
static const IID IID_IInternetSecurityManager_mingw =
Packit Service 20376f
	{ 0x79EAC9EE, 0xBAF9, 0x11CE,
Packit Service 20376f
	{ 0x8C, 0x82, 0x00, 0xAA, 0x00, 0x4B, 0xA9, 0x0B } };
Packit Service 20376f
Packit Service 20376f
# define CLSID_InternetSecurityManager CLSID_InternetSecurityManager_mingw
Packit Service 20376f
# define IID_IInternetSecurityManager IID_IInternetSecurityManager_mingw
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
#define OWNING_SUBTRANSPORT(s) ((winhttp_subtransport *)(s)->parent.subtransport)
Packit Service 20376f
Packit Service 20376f
typedef enum {
Packit Service 20376f
	GIT_WINHTTP_AUTH_BASIC = 1,
Packit Service 20376f
	GIT_WINHTTP_AUTH_NTLM = 2,
Packit Service 20376f
	GIT_WINHTTP_AUTH_NEGOTIATE = 4,
Packit Service 20376f
	GIT_WINHTTP_AUTH_DIGEST = 8,
Packit Service 20376f
} winhttp_authmechanism_t;
Packit Service 20376f
Packit Service 20376f
typedef struct {
Packit Service 20376f
	git_smart_subtransport_stream parent;
Packit Service 20376f
	const char *service;
Packit Service 20376f
	const char *service_url;
Packit Service 20376f
	const wchar_t *verb;
Packit Service 20376f
	HINTERNET request;
Packit Service 20376f
	wchar_t *request_uri;
Packit Service 20376f
	char *chunk_buffer;
Packit Service 20376f
	unsigned chunk_buffer_len;
Packit Service 20376f
	HANDLE post_body;
Packit Service 20376f
	DWORD post_body_len;
Packit Service 20376f
	unsigned sent_request : 1,
Packit Service 20376f
		received_response : 1,
Packit Service 20376f
		chunked : 1;
Packit Service 20376f
} winhttp_stream;
Packit Service 20376f
Packit Service 20376f
typedef struct {
Packit Service 20376f
	git_smart_subtransport parent;
Packit Service 20376f
	transport_smart *owner;
Packit Service 20376f
	gitno_connection_data connection_data;
Packit Service 20376f
	gitno_connection_data proxy_connection_data;
Packit Service 20376f
	git_cred *cred;
Packit Service 20376f
	git_cred *url_cred;
Packit Service 20376f
	git_cred *proxy_cred;
Packit Service 20376f
	int auth_mechanisms;
Packit Service 20376f
	HINTERNET session;
Packit Service 20376f
	HINTERNET connection;
Packit Service 20376f
} winhttp_subtransport;
Packit Service 20376f
Packit Service 20376f
static int _apply_userpass_credential(HINTERNET request, DWORD target, DWORD scheme, git_cred *cred)
Packit Service 20376f
{
Packit Service 20376f
	git_cred_userpass_plaintext *c = (git_cred_userpass_plaintext *)cred;
Packit Service 20376f
	wchar_t *user, *pass;
Packit Service 20376f
	int user_len = 0, pass_len = 0, error = 0;
Packit Service 20376f
Packit Service 20376f
	if ((error = user_len = git__utf8_to_16_alloc(&user, c->username)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	if ((error = pass_len = git__utf8_to_16_alloc(&pass, c->password)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	if (!WinHttpSetCredentials(request, target, scheme, user, pass, NULL)) {
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to set credentials");
Packit Service 20376f
		error = -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	if (user_len > 0)
Packit Service 20376f
		git__memzero(user, user_len * sizeof(wchar_t));
Packit Service 20376f
Packit Service 20376f
	if (pass_len > 0)
Packit Service 20376f
		git__memzero(pass, pass_len * sizeof(wchar_t));
Packit Service 20376f
Packit Service 20376f
	git__free(user);
Packit Service 20376f
	git__free(pass);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int apply_userpass_credential_proxy(HINTERNET request, git_cred *cred, int mechanisms)
Packit Service 20376f
{
Packit Service 20376f
	if (GIT_WINHTTP_AUTH_DIGEST & mechanisms) {
Packit Service 20376f
		return _apply_userpass_credential(request, WINHTTP_AUTH_TARGET_PROXY,
Packit Service 20376f
			WINHTTP_AUTH_SCHEME_DIGEST, cred);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return _apply_userpass_credential(request, WINHTTP_AUTH_TARGET_PROXY,
Packit Service 20376f
		WINHTTP_AUTH_SCHEME_BASIC, cred);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int apply_userpass_credential(HINTERNET request, int mechanisms, git_cred *cred)
Packit Service 20376f
{
Packit Service 20376f
	DWORD native_scheme;
Packit Service 20376f
Packit Service 20376f
	if ((mechanisms & GIT_WINHTTP_AUTH_NTLM) ||
Packit Service 20376f
		(mechanisms & GIT_WINHTTP_AUTH_NEGOTIATE)) {
Packit Service 20376f
		native_scheme = WINHTTP_AUTH_SCHEME_NTLM;
Packit Service 20376f
	} else if (mechanisms & GIT_WINHTTP_AUTH_BASIC) {
Packit Service 20376f
		native_scheme = WINHTTP_AUTH_SCHEME_BASIC;
Packit Service 20376f
	} else {
Packit Service 20376f
		giterr_set(GITERR_NET, "invalid authentication scheme");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return _apply_userpass_credential(request, WINHTTP_AUTH_TARGET_SERVER,
Packit Service 20376f
		native_scheme, cred);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int apply_default_credentials(HINTERNET request, int mechanisms)
Packit Service 20376f
{
Packit Service 20376f
	/* Either the caller explicitly requested that default credentials be passed,
Packit Service 20376f
	 * or our fallback credential callback was invoked and checked that the target
Packit Service 20376f
	 * URI was in the appropriate Internet Explorer security zone. By setting this
Packit Service 20376f
	 * flag, we guarantee that the credentials are delivered by WinHTTP. The default
Packit Service 20376f
	 * is "medium" which applies to the intranet and sounds like it would correspond
Packit Service 20376f
	 * to Internet Explorer security zones, but in fact does not. */
Packit Service 20376f
	DWORD data = WINHTTP_AUTOLOGON_SECURITY_LEVEL_LOW;
Packit Service 20376f
Packit Service 20376f
	if ((mechanisms & GIT_WINHTTP_AUTH_NTLM) == 0 &&
Packit Service 20376f
		(mechanisms & GIT_WINHTTP_AUTH_NEGOTIATE) == 0) {
Packit Service 20376f
		giterr_set(GITERR_NET, "invalid authentication scheme");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (!WinHttpSetOption(request, WINHTTP_OPTION_AUTOLOGON_POLICY, &data, sizeof(DWORD)))
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int fallback_cred_acquire_cb(
Packit Service 20376f
	git_cred **cred,
Packit Service 20376f
	const char *url,
Packit Service 20376f
	const char *username_from_url,
Packit Service 20376f
	unsigned int allowed_types,
Packit Service 20376f
	void *payload)
Packit Service 20376f
{
Packit Service 20376f
	int error = 1;
Packit Service 20376f
Packit Service 20376f
	GIT_UNUSED(username_from_url);
Packit Service 20376f
	GIT_UNUSED(payload);
Packit Service 20376f
Packit Service 20376f
	/* If the target URI supports integrated Windows authentication
Packit Service 20376f
	 * as an authentication mechanism */
Packit Service 20376f
	if (GIT_CREDTYPE_DEFAULT & allowed_types) {
Packit Service 20376f
		wchar_t *wide_url;
Packit Service 20376f
Packit Service 20376f
		/* Convert URL to wide characters */
Packit Service 20376f
		if (git__utf8_to_16_alloc(&wide_url, url) < 0) {
Packit Service 20376f
			giterr_set(GITERR_OS, "failed to convert string to wide form");
Packit Service 20376f
			return -1;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (SUCCEEDED(CoInitializeEx(NULL, COINIT_MULTITHREADED))) {
Packit Service 20376f
			IInternetSecurityManager* pISM;
Packit Service 20376f
Packit Service 20376f
			/* And if the target URI is in the My Computer, Intranet, or Trusted zones */
Packit Service 20376f
			if (SUCCEEDED(CoCreateInstance(&CLSID_InternetSecurityManager, NULL,
Packit Service 20376f
				CLSCTX_ALL, &IID_IInternetSecurityManager, (void **)&pISM))) {
Packit Service 20376f
				DWORD dwZone;
Packit Service 20376f
Packit Service 20376f
				if (SUCCEEDED(pISM->lpVtbl->MapUrlToZone(pISM, wide_url, &dwZone, 0)) &&
Packit Service 20376f
					(URLZONE_LOCAL_MACHINE == dwZone ||
Packit Service 20376f
					URLZONE_INTRANET == dwZone ||
Packit Service 20376f
					URLZONE_TRUSTED == dwZone)) {
Packit Service 20376f
					git_cred *existing = *cred;
Packit Service 20376f
Packit Service 20376f
					if (existing)
Packit Service 20376f
						existing->free(existing);
Packit Service 20376f
Packit Service 20376f
					/* Then use default Windows credentials to authenticate this request */
Packit Service 20376f
					error = git_cred_default_new(cred);
Packit Service 20376f
				}
Packit Service 20376f
Packit Service 20376f
				pISM->lpVtbl->Release(pISM);
Packit Service 20376f
			}
Packit Service 20376f
Packit Service 20376f
			CoUninitialize();
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		git__free(wide_url);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int certificate_check(winhttp_stream *s, int valid)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
	winhttp_subtransport *t = OWNING_SUBTRANSPORT(s);
Packit Service 20376f
	PCERT_CONTEXT cert_ctx;
Packit Service 20376f
	DWORD cert_ctx_size = sizeof(cert_ctx);
Packit Service 20376f
	git_cert_x509 cert;
Packit Service 20376f
Packit Service 20376f
	/* If there is no override, we should fail if WinHTTP doesn't think it's fine */
Packit Service 20376f
	if (t->owner->certificate_check_cb == NULL && !valid) {
Packit Service 20376f
		if (!giterr_last())
Packit Service 20376f
			giterr_set(GITERR_NET, "unknown certificate check failure");
Packit Service 20376f
Packit Service 20376f
		return GIT_ECERTIFICATE;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (t->owner->certificate_check_cb == NULL || !t->connection_data.use_ssl)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	if (!WinHttpQueryOption(s->request, WINHTTP_OPTION_SERVER_CERT_CONTEXT, &cert_ctx, &cert_ctx_size)) {
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to get server certificate");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	giterr_clear();
Packit Service 20376f
	cert.parent.cert_type = GIT_CERT_X509;
Packit Service 20376f
	cert.data = cert_ctx->pbCertEncoded;
Packit Service 20376f
	cert.len = cert_ctx->cbCertEncoded;
Packit Service 20376f
	error = t->owner->certificate_check_cb((git_cert *) &cert, valid, t->connection_data.host, t->owner->cred_acquire_payload);
Packit Service 20376f
	CertFreeCertificateContext(cert_ctx);
Packit Service 20376f
Packit Service 20376f
	if (error < 0 && !giterr_last())
Packit Service 20376f
		giterr_set(GITERR_NET, "user cancelled certificate check");
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void winhttp_stream_close(winhttp_stream *s)
Packit Service 20376f
{
Packit Service 20376f
	if (s->chunk_buffer) {
Packit Service 20376f
		git__free(s->chunk_buffer);
Packit Service 20376f
		s->chunk_buffer = NULL;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (s->post_body) {
Packit Service 20376f
		CloseHandle(s->post_body);
Packit Service 20376f
		s->post_body = NULL;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (s->request_uri) {
Packit Service 20376f
		git__free(s->request_uri);
Packit Service 20376f
		s->request_uri = NULL;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (s->request) {
Packit Service 20376f
		WinHttpCloseHandle(s->request);
Packit Service 20376f
		s->request = NULL;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	s->sent_request = 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Extract the url and password from a URL. The outputs are pointers
Packit Service 20376f
 * into the input.
Packit Service 20376f
 */
Packit Service 20376f
static int userpass_from_url(wchar_t **user, int *user_len,
Packit Service 20376f
			     wchar_t **pass, int *pass_len,
Packit Service 20376f
			     const wchar_t *url, int url_len)
Packit Service 20376f
{
Packit Service 20376f
	URL_COMPONENTS components = { 0 };
Packit Service 20376f
Packit Service 20376f
	components.dwStructSize = sizeof(components);
Packit Service 20376f
	/* These tell WinHttpCrackUrl that we're interested in the fields */
Packit Service 20376f
	components.dwUserNameLength = 1;
Packit Service 20376f
	components.dwPasswordLength = 1;
Packit Service 20376f
Packit Service 20376f
	if (!WinHttpCrackUrl(url, url_len, 0, &components)) {
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to extract user/pass from url");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	*user     = components.lpszUserName;
Packit Service 20376f
	*user_len = components.dwUserNameLength;
Packit Service 20376f
	*pass     = components.lpszPassword;
Packit Service 20376f
	*pass_len = components.dwPasswordLength;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#define SCHEME_HTTP  "http://"
Packit Service 20376f
#define SCHEME_HTTPS "https://"
Packit Service 20376f
Packit Service 20376f
static int winhttp_stream_connect(winhttp_stream *s)
Packit Service 20376f
{
Packit Service 20376f
	winhttp_subtransport *t = OWNING_SUBTRANSPORT(s);
Packit Service 20376f
	git_buf buf = GIT_BUF_INIT;
Packit Service 20376f
	char *proxy_url = NULL;
Packit Service 20376f
	wchar_t ct[MAX_CONTENT_TYPE_LEN];
Packit Service 20376f
	LPCWSTR types[] = { L"*/*", NULL };
Packit Service 20376f
	BOOL peerdist = FALSE;
Packit Service 20376f
	int error = -1;
Packit Service 20376f
	unsigned long disable_redirects = WINHTTP_DISABLE_REDIRECTS;
Packit Service 20376f
	int default_timeout = TIMEOUT_INFINITE;
Packit Service 20376f
	int default_connect_timeout = DEFAULT_CONNECT_TIMEOUT;
Packit Service 20376f
	size_t i;
Packit Service 20376f
	const git_proxy_options *proxy_opts;
Packit Service 20376f
Packit Service 20376f
	/* Prepare URL */
Packit Service 20376f
	git_buf_printf(&buf, "%s%s", t->connection_data.path, s->service_url);
Packit Service 20376f
Packit Service 20376f
	if (git_buf_oom(&buf))
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	/* Convert URL to wide characters */
Packit Service 20376f
	if (git__utf8_to_16_alloc(&s->request_uri, git_buf_cstr(&buf)) < 0) {
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to convert string to wide form");
Packit Service 20376f
		goto on_error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Establish request */
Packit Service 20376f
	s->request = WinHttpOpenRequest(
Packit Service 20376f
			t->connection,
Packit Service 20376f
			s->verb,
Packit Service 20376f
			s->request_uri,
Packit Service 20376f
			NULL,
Packit Service 20376f
			WINHTTP_NO_REFERER,
Packit Service 20376f
			types,
Packit Service 20376f
			t->connection_data.use_ssl ? WINHTTP_FLAG_SECURE : 0);
Packit Service 20376f
Packit Service 20376f
	if (!s->request) {
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to open request");
Packit Service 20376f
		goto on_error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (!WinHttpSetTimeouts(s->request, default_timeout, default_connect_timeout, default_timeout, default_timeout)) {
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to set timeouts for WinHTTP");
Packit Service 20376f
		goto on_error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	proxy_opts = &t->owner->proxy;
Packit Service 20376f
	if (proxy_opts->type == GIT_PROXY_AUTO) {
Packit Service 20376f
		/* Set proxy if necessary */
Packit Service 20376f
		if (git_remote__get_http_proxy(t->owner->owner, !!t->connection_data.use_ssl, &proxy_url) < 0)
Packit Service 20376f
			goto on_error;
Packit Service 20376f
	}
Packit Service 20376f
	else if (proxy_opts->type == GIT_PROXY_SPECIFIED) {
Packit Service 20376f
		proxy_url = git__strdup(proxy_opts->url);
Packit Service 20376f
		GITERR_CHECK_ALLOC(proxy_url);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (proxy_url) {
Packit Service 20376f
		git_buf processed_url = GIT_BUF_INIT;
Packit Service 20376f
		WINHTTP_PROXY_INFO proxy_info;
Packit Service 20376f
		wchar_t *proxy_wide;
Packit Service 20376f
Packit Service 20376f
		if (!git__prefixcmp(proxy_url, SCHEME_HTTP)) {
Packit Service 20376f
			t->proxy_connection_data.use_ssl = false;
Packit Service 20376f
		} else if (!git__prefixcmp(proxy_url, SCHEME_HTTPS)) {
Packit Service 20376f
			t->proxy_connection_data.use_ssl = true;
Packit Service 20376f
		} else {
Packit Service 20376f
			giterr_set(GITERR_NET, "invalid URL: '%s'", proxy_url);
Packit Service 20376f
			return -1;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		gitno_connection_data_free_ptrs(&t->proxy_connection_data);
Packit Service 20376f
Packit Service 20376f
		if ((error = gitno_extract_url_parts(&t->proxy_connection_data.host, &t->proxy_connection_data.port, NULL,
Packit Service 20376f
				&t->proxy_connection_data.user, &t->proxy_connection_data.pass, proxy_url, NULL)) < 0)
Packit Service 20376f
			goto on_error;
Packit Service 20376f
Packit Service 20376f
		if (t->proxy_connection_data.user && t->proxy_connection_data.pass) {
Packit Service 20376f
			if (t->proxy_cred) {
Packit Service 20376f
				t->proxy_cred->free(t->proxy_cred);
Packit Service 20376f
			}
Packit Service 20376f
Packit Service 20376f
			if ((error = git_cred_userpass_plaintext_new(&t->proxy_cred, t->proxy_connection_data.user, t->proxy_connection_data.pass)) < 0)
Packit Service 20376f
				goto on_error;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (t->proxy_connection_data.use_ssl)
Packit Service 20376f
			git_buf_PUTS(&processed_url, SCHEME_HTTPS);
Packit Service 20376f
		else
Packit Service 20376f
			git_buf_PUTS(&processed_url, SCHEME_HTTP);
Packit Service 20376f
Packit Service 20376f
		git_buf_puts(&processed_url, t->proxy_connection_data.host);
Packit Service 20376f
		if (t->proxy_connection_data.port)
Packit Service 20376f
			git_buf_printf(&processed_url, ":%s", t->proxy_connection_data.port);
Packit Service 20376f
Packit Service 20376f
		if (git_buf_oom(&processed_url)) {
Packit Service 20376f
			error = -1;
Packit Service 20376f
			goto on_error;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/* Convert URL to wide characters */
Packit Service 20376f
		error = git__utf8_to_16_alloc(&proxy_wide, processed_url.ptr);
Packit Service 20376f
		git_buf_free(&processed_url);
Packit Service 20376f
		if (error < 0)
Packit Service 20376f
			goto on_error;
Packit Service 20376f
Packit Service 20376f
		proxy_info.dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
Packit Service 20376f
		proxy_info.lpszProxy = proxy_wide;
Packit Service 20376f
		proxy_info.lpszProxyBypass = NULL;
Packit Service 20376f
Packit Service 20376f
		if (!WinHttpSetOption(s->request,
Packit Service 20376f
			WINHTTP_OPTION_PROXY,
Packit Service 20376f
			&proxy_info,
Packit Service 20376f
			sizeof(WINHTTP_PROXY_INFO))) {
Packit Service 20376f
			giterr_set(GITERR_OS, "failed to set proxy");
Packit Service 20376f
			git__free(proxy_wide);
Packit Service 20376f
			goto on_error;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		git__free(proxy_wide);
Packit Service 20376f
Packit Service 20376f
		if (t->proxy_cred) {
Packit Service 20376f
			if (t->proxy_cred->credtype == GIT_CREDTYPE_USERPASS_PLAINTEXT) {
Packit Service 20376f
				if ((error = apply_userpass_credential_proxy(s->request, t->proxy_cred, t->auth_mechanisms)) < 0)
Packit Service 20376f
					goto on_error;
Packit Service 20376f
			}
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Disable WinHTTP redirects so we can handle them manually. Why, you ask?
Packit Service 20376f
	 * http://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/b2ff8879-ab9f-4218-8f09-16d25dff87ae
Packit Service 20376f
	 */
Packit Service 20376f
	if (!WinHttpSetOption(s->request,
Packit Service 20376f
		WINHTTP_OPTION_DISABLE_FEATURE,
Packit Service 20376f
		&disable_redirects,
Packit Service 20376f
		sizeof(disable_redirects))) {
Packit Service 20376f
			giterr_set(GITERR_OS, "failed to disable redirects");
Packit Service 20376f
			goto on_error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Strip unwanted headers (X-P2P-PeerDist, X-P2P-PeerDistEx) that WinHTTP
Packit Service 20376f
	 * adds itself. This option may not be supported by the underlying
Packit Service 20376f
	 * platform, so we do not error-check it */
Packit Service 20376f
	WinHttpSetOption(s->request,
Packit Service 20376f
		WINHTTP_OPTION_PEERDIST_EXTENSION_STATE,
Packit Service 20376f
		&peerdist,
Packit Service 20376f
		sizeof(peerdist));
Packit Service 20376f
Packit Service 20376f
	/* Send Pragma: no-cache header */
Packit Service 20376f
	if (!WinHttpAddRequestHeaders(s->request, pragma_nocache, (ULONG) -1L, WINHTTP_ADDREQ_FLAG_ADD)) {
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to add a header to the request");
Packit Service 20376f
		goto on_error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (post_verb == s->verb) {
Packit Service 20376f
		/* Send Content-Type and Accept headers -- only necessary on a POST */
Packit Service 20376f
		git_buf_clear(&buf;;
Packit Service 20376f
		if (git_buf_printf(&buf,
Packit Service 20376f
			"Content-Type: application/x-git-%s-request",
Packit Service 20376f
			s->service) < 0)
Packit Service 20376f
			goto on_error;
Packit Service 20376f
Packit Service 20376f
		if (git__utf8_to_16(ct, MAX_CONTENT_TYPE_LEN, git_buf_cstr(&buf)) < 0) {
Packit Service 20376f
			giterr_set(GITERR_OS, "failed to convert content-type to wide characters");
Packit Service 20376f
			goto on_error;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (!WinHttpAddRequestHeaders(s->request, ct, (ULONG)-1L,
Packit Service 20376f
			WINHTTP_ADDREQ_FLAG_ADD | WINHTTP_ADDREQ_FLAG_REPLACE)) {
Packit Service 20376f
			giterr_set(GITERR_OS, "failed to add a header to the request");
Packit Service 20376f
			goto on_error;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		git_buf_clear(&buf;;
Packit Service 20376f
		if (git_buf_printf(&buf,
Packit Service 20376f
			"Accept: application/x-git-%s-result",
Packit Service 20376f
			s->service) < 0)
Packit Service 20376f
			goto on_error;
Packit Service 20376f
Packit Service 20376f
		if (git__utf8_to_16(ct, MAX_CONTENT_TYPE_LEN, git_buf_cstr(&buf)) < 0) {
Packit Service 20376f
			giterr_set(GITERR_OS, "failed to convert accept header to wide characters");
Packit Service 20376f
			goto on_error;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (!WinHttpAddRequestHeaders(s->request, ct, (ULONG)-1L,
Packit Service 20376f
			WINHTTP_ADDREQ_FLAG_ADD | WINHTTP_ADDREQ_FLAG_REPLACE)) {
Packit Service 20376f
			giterr_set(GITERR_OS, "failed to add a header to the request");
Packit Service 20376f
			goto on_error;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	for (i = 0; i < t->owner->custom_headers.count; i++) {
Packit Service 20376f
		if (t->owner->custom_headers.strings[i]) {
Packit Service 20376f
			git_buf_clear(&buf;;
Packit Service 20376f
			git_buf_puts(&buf, t->owner->custom_headers.strings[i]);
Packit Service 20376f
			if (git__utf8_to_16(ct, MAX_CONTENT_TYPE_LEN, git_buf_cstr(&buf)) < 0) {
Packit Service 20376f
				giterr_set(GITERR_OS, "failed to convert custom header to wide characters");
Packit Service 20376f
				goto on_error;
Packit Service 20376f
			}
Packit Service 20376f
Packit Service 20376f
			if (!WinHttpAddRequestHeaders(s->request, ct, (ULONG)-1L,
Packit Service 20376f
				WINHTTP_ADDREQ_FLAG_ADD | WINHTTP_ADDREQ_FLAG_REPLACE)) {
Packit Service 20376f
				giterr_set(GITERR_OS, "failed to add a header to the request");
Packit Service 20376f
				goto on_error;
Packit Service 20376f
			}
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* If requested, disable certificate validation */
Packit Service 20376f
	if (t->connection_data.use_ssl) {
Packit Service 20376f
		int flags;
Packit Service 20376f
Packit Service 20376f
		if (t->owner->parent.read_flags(&t->owner->parent, &flags) < 0)
Packit Service 20376f
			goto on_error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* If we have a credential on the subtransport, apply it to the request */
Packit Service 20376f
	if (t->cred &&
Packit Service 20376f
		t->cred->credtype == GIT_CREDTYPE_USERPASS_PLAINTEXT &&
Packit Service 20376f
		apply_userpass_credential(s->request, t->auth_mechanisms, t->cred) < 0)
Packit Service 20376f
		goto on_error;
Packit Service 20376f
	else if (t->cred &&
Packit Service 20376f
		t->cred->credtype == GIT_CREDTYPE_DEFAULT &&
Packit Service 20376f
		apply_default_credentials(s->request, t->auth_mechanisms) < 0)
Packit Service 20376f
		goto on_error;
Packit Service 20376f
Packit Service 20376f
	/* If no other credentials have been applied and the URL has username and
Packit Service 20376f
	 * password, use those */
Packit Service 20376f
	if (!t->cred && t->connection_data.user && t->connection_data.pass) {
Packit Service 20376f
		if (!t->url_cred &&
Packit Service 20376f
			git_cred_userpass_plaintext_new(&t->url_cred, t->connection_data.user, t->connection_data.pass) < 0)
Packit Service 20376f
			goto on_error;
Packit Service 20376f
		if (apply_userpass_credential(s->request, GIT_WINHTTP_AUTH_BASIC, t->url_cred) < 0)
Packit Service 20376f
			goto on_error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* We've done everything up to calling WinHttpSendRequest. */
Packit Service 20376f
Packit Service 20376f
	error = 0;
Packit Service 20376f
Packit Service 20376f
on_error:
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		winhttp_stream_close(s);
Packit Service 20376f
Packit Service 20376f
	git__free(proxy_url);
Packit Service 20376f
	git_buf_free(&buf;;
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int parse_unauthorized_response(
Packit Service 20376f
	HINTERNET request,
Packit Service 20376f
	int *allowed_types,
Packit Service 20376f
	int *allowed_mechanisms)
Packit Service 20376f
{
Packit Service 20376f
	DWORD supported, first, target;
Packit Service 20376f
Packit Service 20376f
	*allowed_types = 0;
Packit Service 20376f
	*allowed_mechanisms = 0;
Packit Service 20376f
Packit Service 20376f
	/* WinHttpQueryHeaders() must be called before WinHttpQueryAuthSchemes().
Packit Service 20376f
	 * We can assume this was already done, since we know we are unauthorized.
Packit Service 20376f
	 */
Packit Service 20376f
	if (!WinHttpQueryAuthSchemes(request, &supported, &first, &target)) {
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to parse supported auth schemes");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (WINHTTP_AUTH_SCHEME_NTLM & supported) {
Packit Service 20376f
		*allowed_types |= GIT_CREDTYPE_USERPASS_PLAINTEXT;
Packit Service 20376f
		*allowed_types |= GIT_CREDTYPE_DEFAULT;
Packit Service 20376f
		*allowed_mechanisms = GIT_WINHTTP_AUTH_NEGOTIATE;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (WINHTTP_AUTH_SCHEME_NEGOTIATE & supported) {
Packit Service 20376f
		*allowed_types |= GIT_CREDTYPE_DEFAULT;
Packit Service 20376f
		*allowed_mechanisms = GIT_WINHTTP_AUTH_NEGOTIATE;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (WINHTTP_AUTH_SCHEME_BASIC & supported) {
Packit Service 20376f
		*allowed_types |= GIT_CREDTYPE_USERPASS_PLAINTEXT;
Packit Service 20376f
		*allowed_mechanisms |= GIT_WINHTTP_AUTH_BASIC;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (WINHTTP_AUTH_SCHEME_DIGEST & supported) {
Packit Service 20376f
		*allowed_types |= GIT_CREDTYPE_USERPASS_PLAINTEXT;
Packit Service 20376f
		*allowed_mechanisms |= GIT_WINHTTP_AUTH_DIGEST;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int write_chunk(HINTERNET request, const char *buffer, size_t len)
Packit Service 20376f
{
Packit Service 20376f
	DWORD bytes_written;
Packit Service 20376f
	git_buf buf = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	/* Chunk header */
Packit Service 20376f
	git_buf_printf(&buf, "%X\r\n", len);
Packit Service 20376f
Packit Service 20376f
	if (git_buf_oom(&buf))
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (!WinHttpWriteData(request,
Packit Service 20376f
		git_buf_cstr(&buf),	(DWORD)git_buf_len(&buf),
Packit Service 20376f
		&bytes_written)) {
Packit Service 20376f
		git_buf_free(&buf;;
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to write chunk header");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&buf;;
Packit Service 20376f
Packit Service 20376f
	/* Chunk body */
Packit Service 20376f
	if (!WinHttpWriteData(request,
Packit Service 20376f
		buffer, (DWORD)len,
Packit Service 20376f
		&bytes_written)) {
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to write chunk");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Chunk footer */
Packit Service 20376f
	if (!WinHttpWriteData(request,
Packit Service 20376f
		"\r\n", 2,
Packit Service 20376f
		&bytes_written)) {
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to write chunk footer");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int winhttp_close_connection(winhttp_subtransport *t)
Packit Service 20376f
{
Packit Service 20376f
	int ret = 0;
Packit Service 20376f
Packit Service 20376f
	if (t->connection) {
Packit Service 20376f
		if (!WinHttpCloseHandle(t->connection)) {
Packit Service 20376f
			giterr_set(GITERR_OS, "unable to close connection");
Packit Service 20376f
			ret = -1;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		t->connection = NULL;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (t->session) {
Packit Service 20376f
		if (!WinHttpCloseHandle(t->session)) {
Packit Service 20376f
			giterr_set(GITERR_OS, "unable to close session");
Packit Service 20376f
			ret = -1;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		t->session = NULL;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return ret;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int user_agent(git_buf *ua)
Packit Service 20376f
{
Packit Service 20376f
	const char *custom = git_libgit2__user_agent();
Packit Service 20376f
Packit Service 20376f
	git_buf_clear(ua);
Packit Service 20376f
	git_buf_PUTS(ua, "git/1.0 (");
Packit Service 20376f
Packit Service 20376f
	if (custom)
Packit Service 20376f
		git_buf_puts(ua, custom);
Packit Service 20376f
	else
Packit Service 20376f
		git_buf_PUTS(ua, "libgit2 " LIBGIT2_VERSION);
Packit Service 20376f
Packit Service 20376f
	return git_buf_putc(ua, ')');
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void CALLBACK winhttp_status(
Packit Service 20376f
	HINTERNET connection,
Packit Service 20376f
	DWORD_PTR ctx,
Packit Service 20376f
	DWORD code,
Packit Service 20376f
	LPVOID info,
Packit Service 20376f
	DWORD info_len)
Packit Service 20376f
{
Packit Service 20376f
	DWORD status;
Packit Service 20376f
Packit Service 20376f
	if (code != WINHTTP_CALLBACK_STATUS_SECURE_FAILURE)
Packit Service 20376f
		return;
Packit Service 20376f
Packit Service 20376f
	status = *((DWORD *)info);
Packit Service 20376f
Packit Service 20376f
	if ((status & WINHTTP_CALLBACK_STATUS_FLAG_CERT_CN_INVALID))
Packit Service 20376f
		giterr_set(GITERR_NET, "SSL certificate issued for different common name");
Packit Service 20376f
	else if ((status & WINHTTP_CALLBACK_STATUS_FLAG_CERT_DATE_INVALID))
Packit Service 20376f
		giterr_set(GITERR_NET, "SSL certificate has expired");
Packit Service 20376f
	else if ((status & WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CA))
Packit Service 20376f
		giterr_set(GITERR_NET, "SSL certificate signed by unknown CA");
Packit Service 20376f
	else if ((status & WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CERT))
Packit Service 20376f
		giterr_set(GITERR_NET, "SSL certificate is invalid");
Packit Service 20376f
	else if ((status & WINHTTP_CALLBACK_STATUS_FLAG_CERT_REV_FAILED))
Packit Service 20376f
		giterr_set(GITERR_NET, "certificate revocation check failed");
Packit Service 20376f
	else if ((status & WINHTTP_CALLBACK_STATUS_FLAG_CERT_REVOKED))
Packit Service 20376f
		giterr_set(GITERR_NET, "SSL certificate was revoked");
Packit Service 20376f
	else if ((status & WINHTTP_CALLBACK_STATUS_FLAG_SECURITY_CHANNEL_ERROR))
Packit Service 20376f
		giterr_set(GITERR_NET, "security libraries could not be loaded");
Packit Service 20376f
	else
Packit Service 20376f
		giterr_set(GITERR_NET, "unknown security error %d", status);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int winhttp_connect(
Packit Service 20376f
	winhttp_subtransport *t)
Packit Service 20376f
{
Packit Service 20376f
	wchar_t *wide_host;
Packit Service 20376f
	int32_t port;
Packit Service 20376f
	wchar_t *wide_ua;
Packit Service 20376f
	git_buf ua = GIT_BUF_INIT;
Packit Service 20376f
	int error = -1;
Packit Service 20376f
	int default_timeout = TIMEOUT_INFINITE;
Packit Service 20376f
	int default_connect_timeout = DEFAULT_CONNECT_TIMEOUT;
Packit Service 20376f
	DWORD protocols =
Packit Service 20376f
		WINHTTP_FLAG_SECURE_PROTOCOL_TLS1 |
Packit Service 20376f
		WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1 |
Packit Service 20376f
		WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2;
Packit Service 20376f
Packit Service 20376f
	t->session = NULL;
Packit Service 20376f
	t->connection = NULL;
Packit Service 20376f
Packit Service 20376f
	/* Prepare port */
Packit Service 20376f
	if (git__strntol32(&port, t->connection_data.port,
Packit Service 20376f
			   strlen(t->connection_data.port), NULL, 10) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	/* Prepare host */
Packit Service 20376f
	if (git__utf8_to_16_alloc(&wide_host, t->connection_data.host) < 0) {
Packit Service 20376f
		giterr_set(GITERR_OS, "unable to convert host to wide characters");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((error = user_agent(&ua)) < 0) {
Packit Service 20376f
		git__free(wide_host);
Packit Service 20376f
		return error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (git__utf8_to_16_alloc(&wide_ua, git_buf_cstr(&ua)) < 0) {
Packit Service 20376f
		giterr_set(GITERR_OS, "unable to convert host to wide characters");
Packit Service 20376f
		git__free(wide_host);
Packit Service 20376f
		git_buf_free(&ua);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&ua);
Packit Service 20376f
Packit Service 20376f
	/* Establish session */
Packit Service 20376f
	t->session = WinHttpOpen(
Packit Service 20376f
		wide_ua,
Packit Service 20376f
		WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
Packit Service 20376f
		WINHTTP_NO_PROXY_NAME,
Packit Service 20376f
		WINHTTP_NO_PROXY_BYPASS,
Packit Service 20376f
		0);
Packit Service 20376f
Packit Service 20376f
	if (!t->session) {
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to init WinHTTP");
Packit Service 20376f
		goto on_error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/*
Packit Service 20376f
	 * Do a best-effort attempt to enable TLS 1.2 but allow this to
Packit Service 20376f
	 * fail; if TLS 1.2 support is not available for some reason,
Packit Service 20376f
	 * ignore the failure (it will keep the default protocols).
Packit Service 20376f
	 */
Packit Service 20376f
	WinHttpSetOption(t->session,
Packit Service 20376f
		WINHTTP_OPTION_SECURE_PROTOCOLS,
Packit Service 20376f
		&protocols,
Packit Service 20376f
		sizeof(protocols));
Packit Service 20376f
Packit Service 20376f
	if (!WinHttpSetTimeouts(t->session, default_timeout, default_connect_timeout, default_timeout, default_timeout)) {
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to set timeouts for WinHTTP");
Packit Service 20376f
		goto on_error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
	/* Establish connection */
Packit Service 20376f
	t->connection = WinHttpConnect(
Packit Service 20376f
		t->session,
Packit Service 20376f
		wide_host,
Packit Service 20376f
		(INTERNET_PORT) port,
Packit Service 20376f
		0);
Packit Service 20376f
Packit Service 20376f
	if (!t->connection) {
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to connect to host");
Packit Service 20376f
		goto on_error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (WinHttpSetStatusCallback(t->connection, winhttp_status, WINHTTP_CALLBACK_FLAG_SECURE_FAILURE, 0) == WINHTTP_INVALID_STATUS_CALLBACK) {
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to set status callback");
Packit Service 20376f
		goto on_error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	error = 0;
Packit Service 20376f
Packit Service 20376f
on_error:
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		winhttp_close_connection(t);
Packit Service 20376f
Packit Service 20376f
	git__free(wide_host);
Packit Service 20376f
	git__free(wide_ua);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int do_send_request(winhttp_stream *s, size_t len, int ignore_length)
Packit Service 20376f
{
Packit Service 20376f
	if (ignore_length) {
Packit Service 20376f
		if (!WinHttpSendRequest(s->request,
Packit Service 20376f
			WINHTTP_NO_ADDITIONAL_HEADERS, 0,
Packit Service 20376f
			WINHTTP_NO_REQUEST_DATA, 0,
Packit Service 20376f
			WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH, 0)) {
Packit Service 20376f
			return -1;
Packit Service 20376f
		}
Packit Service 20376f
	} else {
Packit Service 20376f
		if (!WinHttpSendRequest(s->request,
Packit Service 20376f
			WINHTTP_NO_ADDITIONAL_HEADERS, 0,
Packit Service 20376f
			WINHTTP_NO_REQUEST_DATA, 0,
Packit Service 20376f
			len, 0)) {
Packit Service 20376f
			return -1;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int send_request(winhttp_stream *s, size_t len, int ignore_length)
Packit Service 20376f
{
Packit Service 20376f
	int request_failed = 0, cert_valid = 1, error = 0;
Packit Service 20376f
	DWORD ignore_flags;
Packit Service 20376f
Packit Service 20376f
	giterr_clear();
Packit Service 20376f
	if ((error = do_send_request(s, len, ignore_length)) < 0) {
Packit Service 20376f
		if (GetLastError() != ERROR_WINHTTP_SECURE_FAILURE) {
Packit Service 20376f
			giterr_set(GITERR_OS, "failed to send request");
Packit Service 20376f
			return -1;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		request_failed = 1;
Packit Service 20376f
		cert_valid = 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	giterr_clear();
Packit Service 20376f
	if ((error = certificate_check(s, cert_valid)) < 0) {
Packit Service 20376f
		if (!giterr_last())
Packit Service 20376f
			giterr_set(GITERR_OS, "user cancelled certificate check");
Packit Service 20376f
Packit Service 20376f
		return error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* if neither the request nor the certificate check returned errors, we're done */
Packit Service 20376f
	if (!request_failed)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	ignore_flags = no_check_cert_flags;
Packit Service 20376f
Packit Service 20376f
	if (!WinHttpSetOption(s->request, WINHTTP_OPTION_SECURITY_FLAGS, &ignore_flags, sizeof(ignore_flags))) {
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to set security options");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((error = do_send_request(s, len, ignore_length)) < 0)
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to send request with unchecked certificate");
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int winhttp_stream_read(
Packit Service 20376f
	git_smart_subtransport_stream *stream,
Packit Service 20376f
	char *buffer,
Packit Service 20376f
	size_t buf_size,
Packit Service 20376f
	size_t *bytes_read)
Packit Service 20376f
{
Packit Service 20376f
	winhttp_stream *s = (winhttp_stream *)stream;
Packit Service 20376f
	winhttp_subtransport *t = OWNING_SUBTRANSPORT(s);
Packit Service 20376f
	DWORD dw_bytes_read;
Packit Service 20376f
	char replay_count = 0;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
replay:
Packit Service 20376f
	/* Enforce a reasonable cap on the number of replays */
Packit Service 20376f
	if (++replay_count >= 7) {
Packit Service 20376f
		giterr_set(GITERR_NET, "too many redirects or authentication replays");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Connect if necessary */
Packit Service 20376f
	if (!s->request && winhttp_stream_connect(s) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (!s->received_response) {
Packit Service 20376f
		DWORD status_code, status_code_length, content_type_length, bytes_written;
Packit Service 20376f
		char expected_content_type_8[MAX_CONTENT_TYPE_LEN];
Packit Service 20376f
		wchar_t expected_content_type[MAX_CONTENT_TYPE_LEN], content_type[MAX_CONTENT_TYPE_LEN];
Packit Service 20376f
Packit Service 20376f
		if (!s->sent_request) {
Packit Service 20376f
Packit Service 20376f
			if ((error = send_request(s, s->post_body_len, 0)) < 0)
Packit Service 20376f
				return error;
Packit Service 20376f
Packit Service 20376f
			s->sent_request = 1;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (s->chunked) {
Packit Service 20376f
			assert(s->verb == post_verb);
Packit Service 20376f
Packit Service 20376f
			/* Flush, if necessary */
Packit Service 20376f
			if (s->chunk_buffer_len > 0 &&
Packit Service 20376f
				write_chunk(s->request, s->chunk_buffer, s->chunk_buffer_len) < 0)
Packit Service 20376f
				return -1;
Packit Service 20376f
Packit Service 20376f
			s->chunk_buffer_len = 0;
Packit Service 20376f
Packit Service 20376f
			/* Write the final chunk. */
Packit Service 20376f
			if (!WinHttpWriteData(s->request,
Packit Service 20376f
				"0\r\n\r\n", 5,
Packit Service 20376f
				&bytes_written)) {
Packit Service 20376f
				giterr_set(GITERR_OS, "failed to write final chunk");
Packit Service 20376f
				return -1;
Packit Service 20376f
			}
Packit Service 20376f
		}
Packit Service 20376f
		else if (s->post_body) {
Packit Service 20376f
			char *buffer;
Packit Service 20376f
			DWORD len = s->post_body_len, bytes_read;
Packit Service 20376f
Packit Service 20376f
			if (INVALID_SET_FILE_POINTER == SetFilePointer(s->post_body,
Packit Service 20376f
					0, 0, FILE_BEGIN) &&
Packit Service 20376f
				NO_ERROR != GetLastError()) {
Packit Service 20376f
				giterr_set(GITERR_OS, "failed to reset file pointer");
Packit Service 20376f
				return -1;
Packit Service 20376f
			}
Packit Service 20376f
Packit Service 20376f
			buffer = git__malloc(CACHED_POST_BODY_BUF_SIZE);
Packit Service 20376f
Packit Service 20376f
			while (len > 0) {
Packit Service 20376f
				DWORD bytes_written;
Packit Service 20376f
Packit Service 20376f
				if (!ReadFile(s->post_body, buffer,
Packit Service 20376f
					min(CACHED_POST_BODY_BUF_SIZE, len),
Packit Service 20376f
					&bytes_read, NULL) ||
Packit Service 20376f
					!bytes_read) {
Packit Service 20376f
					git__free(buffer);
Packit Service 20376f
					giterr_set(GITERR_OS, "failed to read from temp file");
Packit Service 20376f
					return -1;
Packit Service 20376f
				}
Packit Service 20376f
Packit Service 20376f
				if (!WinHttpWriteData(s->request, buffer,
Packit Service 20376f
					bytes_read, &bytes_written)) {
Packit Service 20376f
					git__free(buffer);
Packit Service 20376f
					giterr_set(GITERR_OS, "failed to write data");
Packit Service 20376f
					return -1;
Packit Service 20376f
				}
Packit Service 20376f
Packit Service 20376f
				len -= bytes_read;
Packit Service 20376f
				assert(bytes_read == bytes_written);
Packit Service 20376f
			}
Packit Service 20376f
Packit Service 20376f
			git__free(buffer);
Packit Service 20376f
Packit Service 20376f
			/* Eagerly close the temp file */
Packit Service 20376f
			CloseHandle(s->post_body);
Packit Service 20376f
			s->post_body = NULL;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (!WinHttpReceiveResponse(s->request, 0)) {
Packit Service 20376f
			giterr_set(GITERR_OS, "failed to receive response");
Packit Service 20376f
			return -1;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/* Verify that we got a 200 back */
Packit Service 20376f
		status_code_length = sizeof(status_code);
Packit Service 20376f
Packit Service 20376f
		if (!WinHttpQueryHeaders(s->request,
Packit Service 20376f
			WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER,
Packit Service 20376f
			WINHTTP_HEADER_NAME_BY_INDEX,
Packit Service 20376f
			&status_code, &status_code_length,
Packit Service 20376f
			WINHTTP_NO_HEADER_INDEX)) {
Packit Service 20376f
				giterr_set(GITERR_OS, "failed to retrieve status code");
Packit Service 20376f
				return -1;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/* The implementation of WinHTTP prior to Windows 7 will not
Packit Service 20376f
		 * redirect to an identical URI. Some Git hosters use self-redirects
Packit Service 20376f
		 * as part of their DoS mitigation strategy. Check first to see if we
Packit Service 20376f
		 * have a redirect status code, and that we haven't already streamed
Packit Service 20376f
		 * a post body. (We can't replay a streamed POST.) */
Packit Service 20376f
		if (!s->chunked &&
Packit Service 20376f
			(HTTP_STATUS_MOVED == status_code ||
Packit Service 20376f
			 HTTP_STATUS_REDIRECT == status_code ||
Packit Service 20376f
			 (HTTP_STATUS_REDIRECT_METHOD == status_code &&
Packit Service 20376f
			  get_verb == s->verb) ||
Packit Service 20376f
			 HTTP_STATUS_REDIRECT_KEEP_VERB == status_code)) {
Packit Service 20376f
Packit Service 20376f
			/* Check for Windows 7. This workaround is only necessary on
Packit Service 20376f
			 * Windows Vista and earlier. Windows 7 is version 6.1. */
Packit Service 20376f
			wchar_t *location;
Packit Service 20376f
			DWORD location_length;
Packit Service 20376f
			char *location8;
Packit Service 20376f
Packit Service 20376f
			/* OK, fetch the Location header from the redirect. */
Packit Service 20376f
			if (WinHttpQueryHeaders(s->request,
Packit Service 20376f
				WINHTTP_QUERY_LOCATION,
Packit Service 20376f
				WINHTTP_HEADER_NAME_BY_INDEX,
Packit Service 20376f
				WINHTTP_NO_OUTPUT_BUFFER,
Packit Service 20376f
				&location_length,
Packit Service 20376f
				WINHTTP_NO_HEADER_INDEX) ||
Packit Service 20376f
				GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
Packit Service 20376f
				giterr_set(GITERR_OS, "failed to read Location header");
Packit Service 20376f
				return -1;
Packit Service 20376f
			}
Packit Service 20376f
Packit Service 20376f
			location = git__malloc(location_length);
Packit Service 20376f
			GITERR_CHECK_ALLOC(location);
Packit Service 20376f
Packit Service 20376f
			if (!WinHttpQueryHeaders(s->request,
Packit Service 20376f
				WINHTTP_QUERY_LOCATION,
Packit Service 20376f
				WINHTTP_HEADER_NAME_BY_INDEX,
Packit Service 20376f
				location,
Packit Service 20376f
				&location_length,
Packit Service 20376f
				WINHTTP_NO_HEADER_INDEX)) {
Packit Service 20376f
				giterr_set(GITERR_OS, "failed to read Location header");
Packit Service 20376f
				git__free(location);
Packit Service 20376f
				return -1;
Packit Service 20376f
			}
Packit Service 20376f
Packit Service 20376f
			/* Convert the Location header to UTF-8 */
Packit Service 20376f
			if (git__utf16_to_8_alloc(&location8, location) < 0) {
Packit Service 20376f
				giterr_set(GITERR_OS, "failed to convert Location header to UTF-8");
Packit Service 20376f
				git__free(location);
Packit Service 20376f
				return -1;
Packit Service 20376f
			}
Packit Service 20376f
Packit Service 20376f
			git__free(location);
Packit Service 20376f
Packit Service 20376f
			/* Replay the request */
Packit Service 20376f
			winhttp_stream_close(s);
Packit Service 20376f
Packit Service 20376f
			if (!git__prefixcmp_icase(location8, prefix_https)) {
Packit Service 20376f
				/* Upgrade to secure connection; disconnect and start over */
Packit Service 20376f
				if (gitno_connection_data_from_url(&t->connection_data, location8, s->service_url) < 0) {
Packit Service 20376f
					git__free(location8);
Packit Service 20376f
					return -1;
Packit Service 20376f
				}
Packit Service 20376f
Packit Service 20376f
				winhttp_close_connection(t);
Packit Service 20376f
Packit Service 20376f
				if (winhttp_connect(t) < 0)
Packit Service 20376f
					return -1;
Packit Service 20376f
			}
Packit Service 20376f
Packit Service 20376f
			git__free(location8);
Packit Service 20376f
			goto replay;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/* Handle proxy authentication failures */
Packit Service 20376f
		if (status_code == HTTP_STATUS_PROXY_AUTH_REQ) {
Packit Service 20376f
			int allowed_types;
Packit Service 20376f
Packit Service 20376f
			if (parse_unauthorized_response(s->request, &allowed_types, &t->auth_mechanisms) < 0)
Packit Service 20376f
				return -1;
Packit Service 20376f
Packit Service 20376f
			/* TODO: extract the username from the url, no payload? */
Packit Service 20376f
			if (t->owner->proxy.credentials) {
Packit Service 20376f
				int cred_error = 1;
Packit Service 20376f
				cred_error = t->owner->proxy.credentials(&t->proxy_cred, t->owner->proxy.url, NULL, allowed_types, t->owner->proxy.payload);
Packit Service 20376f
Packit Service 20376f
				if (cred_error < 0)
Packit Service 20376f
					return cred_error;
Packit Service 20376f
			}
Packit Service 20376f
Packit Service 20376f
			winhttp_stream_close(s);
Packit Service 20376f
			goto replay;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/* Handle authentication failures */
Packit Service 20376f
		if (HTTP_STATUS_DENIED == status_code && get_verb == s->verb) {
Packit Service 20376f
			int allowed_types;
Packit Service 20376f
Packit Service 20376f
			if (parse_unauthorized_response(s->request, &allowed_types, &t->auth_mechanisms) < 0)
Packit Service 20376f
				return -1;
Packit Service 20376f
Packit Service 20376f
			if (allowed_types) {
Packit Service 20376f
				int cred_error = 1;
Packit Service 20376f
Packit Service 20376f
				git_cred_free(t->cred);
Packit Service 20376f
				t->cred = NULL;
Packit Service 20376f
				/* Start with the user-supplied credential callback, if present */
Packit Service 20376f
				if (t->owner->cred_acquire_cb) {
Packit Service 20376f
					cred_error = t->owner->cred_acquire_cb(&t->cred, t->owner->url,
Packit Service 20376f
						t->connection_data.user, allowed_types,	t->owner->cred_acquire_payload);
Packit Service 20376f
Packit Service 20376f
					/* Treat GIT_PASSTHROUGH as though git_cred_acquire_cb isn't set */
Packit Service 20376f
					if (cred_error == GIT_PASSTHROUGH)
Packit Service 20376f
						cred_error = 1;
Packit Service 20376f
					else if (cred_error < 0)
Packit Service 20376f
						return cred_error;
Packit Service 20376f
				}
Packit Service 20376f
Packit Service 20376f
				/* Invoke the fallback credentials acquisition callback if necessary */
Packit Service 20376f
				if (cred_error > 0) {
Packit Service 20376f
					cred_error = fallback_cred_acquire_cb(&t->cred, t->owner->url,
Packit Service 20376f
						t->connection_data.user, allowed_types, NULL);
Packit Service 20376f
Packit Service 20376f
					if (cred_error < 0)
Packit Service 20376f
						return cred_error;
Packit Service 20376f
				}
Packit Service 20376f
Packit Service 20376f
				if (!cred_error) {
Packit Service 20376f
					assert(t->cred);
Packit Service 20376f
Packit Service 20376f
					winhttp_stream_close(s);
Packit Service 20376f
Packit Service 20376f
					/* Successfully acquired a credential */
Packit Service 20376f
					goto replay;
Packit Service 20376f
				}
Packit Service 20376f
			}
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (HTTP_STATUS_OK != status_code) {
Packit Service 20376f
			giterr_set(GITERR_NET, "request failed with status code: %d", status_code);
Packit Service 20376f
			return -1;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/* Verify that we got the correct content-type back */
Packit Service 20376f
		if (post_verb == s->verb)
Packit Service 20376f
			p_snprintf(expected_content_type_8, MAX_CONTENT_TYPE_LEN, "application/x-git-%s-result", s->service);
Packit Service 20376f
		else
Packit Service 20376f
			p_snprintf(expected_content_type_8, MAX_CONTENT_TYPE_LEN, "application/x-git-%s-advertisement", s->service);
Packit Service 20376f
Packit Service 20376f
		if (git__utf8_to_16(expected_content_type, MAX_CONTENT_TYPE_LEN, expected_content_type_8) < 0) {
Packit Service 20376f
			giterr_set(GITERR_OS, "failed to convert expected content-type to wide characters");
Packit Service 20376f
			return -1;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		content_type_length = sizeof(content_type);
Packit Service 20376f
Packit Service 20376f
		if (!WinHttpQueryHeaders(s->request,
Packit Service 20376f
			WINHTTP_QUERY_CONTENT_TYPE,
Packit Service 20376f
			WINHTTP_HEADER_NAME_BY_INDEX,
Packit Service 20376f
			&content_type, &content_type_length,
Packit Service 20376f
			WINHTTP_NO_HEADER_INDEX)) {
Packit Service 20376f
				giterr_set(GITERR_OS, "failed to retrieve response content-type");
Packit Service 20376f
				return -1;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (wcscmp(expected_content_type, content_type)) {
Packit Service 20376f
			giterr_set(GITERR_NET, "received unexpected content-type");
Packit Service 20376f
			return -1;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		s->received_response = 1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (!WinHttpReadData(s->request,
Packit Service 20376f
		(LPVOID)buffer,
Packit Service 20376f
		(DWORD)buf_size,
Packit Service 20376f
		&dw_bytes_read))
Packit Service 20376f
	{
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to read data");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	*bytes_read = dw_bytes_read;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int winhttp_stream_write_single(
Packit Service 20376f
	git_smart_subtransport_stream *stream,
Packit Service 20376f
	const char *buffer,
Packit Service 20376f
	size_t len)
Packit Service 20376f
{
Packit Service 20376f
	winhttp_stream *s = (winhttp_stream *)stream;
Packit Service 20376f
	DWORD bytes_written;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	if (!s->request && winhttp_stream_connect(s) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	/* This implementation of write permits only a single call. */
Packit Service 20376f
	if (s->sent_request) {
Packit Service 20376f
		giterr_set(GITERR_NET, "subtransport configured for only one write");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((error = send_request(s, len, 0)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	s->sent_request = 1;
Packit Service 20376f
Packit Service 20376f
	if (!WinHttpWriteData(s->request,
Packit Service 20376f
			(LPCVOID)buffer,
Packit Service 20376f
			(DWORD)len,
Packit Service 20376f
			&bytes_written)) {
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to write data");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	assert((DWORD)len == bytes_written);
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int put_uuid_string(LPWSTR buffer, size_t buffer_len_cch)
Packit Service 20376f
{
Packit Service 20376f
	UUID uuid;
Packit Service 20376f
	RPC_STATUS status = UuidCreate(&uuid);
Packit Service 20376f
	int result;
Packit Service 20376f
Packit Service 20376f
	if (RPC_S_OK != status &&
Packit Service 20376f
		RPC_S_UUID_LOCAL_ONLY != status &&
Packit Service 20376f
		RPC_S_UUID_NO_ADDRESS != status) {
Packit Service 20376f
		giterr_set(GITERR_NET, "unable to generate name for temp file");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (buffer_len_cch < UUID_LENGTH_CCH + 1) {
Packit Service 20376f
		giterr_set(GITERR_NET, "buffer too small for name of temp file");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
#if !defined(__MINGW32__) || defined(MINGW_HAS_SECURE_API)
Packit Service 20376f
	result = swprintf_s(buffer, buffer_len_cch,
Packit Service 20376f
#else
Packit Service 20376f
	result = wsprintfW(buffer,
Packit Service 20376f
#endif
Packit Service 20376f
		L"%08x%04x%04x%02x%02x%02x%02x%02x%02x%02x%02x",
Packit Service 20376f
		uuid.Data1, uuid.Data2, uuid.Data3,
Packit Service 20376f
		uuid.Data4[0], uuid.Data4[1], uuid.Data4[2], uuid.Data4[3],
Packit Service 20376f
		uuid.Data4[4], uuid.Data4[5], uuid.Data4[6], uuid.Data4[7]);
Packit Service 20376f
Packit Service 20376f
	if (result < UUID_LENGTH_CCH) {
Packit Service 20376f
		giterr_set(GITERR_OS, "unable to generate name for temp file");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int get_temp_file(LPWSTR buffer, DWORD buffer_len_cch)
Packit Service 20376f
{
Packit Service 20376f
	size_t len;
Packit Service 20376f
Packit Service 20376f
	if (!GetTempPathW(buffer_len_cch, buffer)) {
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to get temp path");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	len = wcslen(buffer);
Packit Service 20376f
Packit Service 20376f
	if (buffer[len - 1] != '\\' && len < buffer_len_cch)
Packit Service 20376f
		buffer[len++] = '\\';
Packit Service 20376f
Packit Service 20376f
	if (put_uuid_string(&buffer[len], (size_t)buffer_len_cch - len) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int winhttp_stream_write_buffered(
Packit Service 20376f
	git_smart_subtransport_stream *stream,
Packit Service 20376f
	const char *buffer,
Packit Service 20376f
	size_t len)
Packit Service 20376f
{
Packit Service 20376f
	winhttp_stream *s = (winhttp_stream *)stream;
Packit Service 20376f
	DWORD bytes_written;
Packit Service 20376f
Packit Service 20376f
	if (!s->request && winhttp_stream_connect(s) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	/* Buffer the payload, using a temporary file so we delegate
Packit Service 20376f
	 * memory management of the data to the operating system. */
Packit Service 20376f
	if (!s->post_body) {
Packit Service 20376f
		wchar_t temp_path[MAX_PATH + 1];
Packit Service 20376f
Packit Service 20376f
		if (get_temp_file(temp_path, MAX_PATH + 1) < 0)
Packit Service 20376f
			return -1;
Packit Service 20376f
Packit Service 20376f
		s->post_body = CreateFileW(temp_path,
Packit Service 20376f
			GENERIC_READ | GENERIC_WRITE,
Packit Service 20376f
			FILE_SHARE_DELETE, NULL,
Packit Service 20376f
			CREATE_NEW,
Packit Service 20376f
			FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE | FILE_FLAG_SEQUENTIAL_SCAN,
Packit Service 20376f
			NULL);
Packit Service 20376f
Packit Service 20376f
		if (INVALID_HANDLE_VALUE == s->post_body) {
Packit Service 20376f
			s->post_body = NULL;
Packit Service 20376f
			giterr_set(GITERR_OS, "failed to create temporary file");
Packit Service 20376f
			return -1;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (!WriteFile(s->post_body, buffer, (DWORD)len, &bytes_written, NULL)) {
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to write to temporary file");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	assert((DWORD)len == bytes_written);
Packit Service 20376f
Packit Service 20376f
	s->post_body_len += bytes_written;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int winhttp_stream_write_chunked(
Packit Service 20376f
	git_smart_subtransport_stream *stream,
Packit Service 20376f
	const char *buffer,
Packit Service 20376f
	size_t len)
Packit Service 20376f
{
Packit Service 20376f
	winhttp_stream *s = (winhttp_stream *)stream;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	if (!s->request && winhttp_stream_connect(s) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (!s->sent_request) {
Packit Service 20376f
		/* Send Transfer-Encoding: chunked header */
Packit Service 20376f
		if (!WinHttpAddRequestHeaders(s->request,
Packit Service 20376f
			transfer_encoding, (ULONG) -1L,
Packit Service 20376f
			WINHTTP_ADDREQ_FLAG_ADD)) {
Packit Service 20376f
			giterr_set(GITERR_OS, "failed to add a header to the request");
Packit Service 20376f
			return -1;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if ((error = send_request(s, 0, 1)) < 0)
Packit Service 20376f
			return error;
Packit Service 20376f
Packit Service 20376f
		s->sent_request = 1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (len > CACHED_POST_BODY_BUF_SIZE) {
Packit Service 20376f
		/* Flush, if necessary */
Packit Service 20376f
		if (s->chunk_buffer_len > 0) {
Packit Service 20376f
			if (write_chunk(s->request, s->chunk_buffer, s->chunk_buffer_len) < 0)
Packit Service 20376f
				return -1;
Packit Service 20376f
Packit Service 20376f
			s->chunk_buffer_len = 0;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/* Write chunk directly */
Packit Service 20376f
		if (write_chunk(s->request, buffer, len) < 0)
Packit Service 20376f
			return -1;
Packit Service 20376f
	}
Packit Service 20376f
	else {
Packit Service 20376f
		/* Append as much to the buffer as we can */
Packit Service 20376f
		int count = (int)min(CACHED_POST_BODY_BUF_SIZE - s->chunk_buffer_len, len);
Packit Service 20376f
Packit Service 20376f
		if (!s->chunk_buffer)
Packit Service 20376f
			s->chunk_buffer = git__malloc(CACHED_POST_BODY_BUF_SIZE);
Packit Service 20376f
Packit Service 20376f
		memcpy(s->chunk_buffer + s->chunk_buffer_len, buffer, count);
Packit Service 20376f
		s->chunk_buffer_len += count;
Packit Service 20376f
		buffer += count;
Packit Service 20376f
		len -= count;
Packit Service 20376f
Packit Service 20376f
		/* Is the buffer full? If so, then flush */
Packit Service 20376f
		if (CACHED_POST_BODY_BUF_SIZE == s->chunk_buffer_len) {
Packit Service 20376f
			if (write_chunk(s->request, s->chunk_buffer, s->chunk_buffer_len) < 0)
Packit Service 20376f
				return -1;
Packit Service 20376f
Packit Service 20376f
			s->chunk_buffer_len = 0;
Packit Service 20376f
Packit Service 20376f
			/* Is there any remaining data from the source? */
Packit Service 20376f
			if (len > 0) {
Packit Service 20376f
				memcpy(s->chunk_buffer, buffer, len);
Packit Service 20376f
				s->chunk_buffer_len = (unsigned int)len;
Packit Service 20376f
			}
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void winhttp_stream_free(git_smart_subtransport_stream *stream)
Packit Service 20376f
{
Packit Service 20376f
	winhttp_stream *s = (winhttp_stream *)stream;
Packit Service 20376f
Packit Service 20376f
	winhttp_stream_close(s);
Packit Service 20376f
	git__free(s);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int winhttp_stream_alloc(winhttp_subtransport *t, winhttp_stream **stream)
Packit Service 20376f
{
Packit Service 20376f
	winhttp_stream *s;
Packit Service 20376f
Packit Service 20376f
	if (!stream)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	s = git__calloc(1, sizeof(winhttp_stream));
Packit Service 20376f
	GITERR_CHECK_ALLOC(s);
Packit Service 20376f
Packit Service 20376f
	s->parent.subtransport = &t->parent;
Packit Service 20376f
	s->parent.read = winhttp_stream_read;
Packit Service 20376f
	s->parent.write = winhttp_stream_write_single;
Packit Service 20376f
	s->parent.free = winhttp_stream_free;
Packit Service 20376f
Packit Service 20376f
	*stream = s;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int winhttp_uploadpack_ls(
Packit Service 20376f
	winhttp_subtransport *t,
Packit Service 20376f
	winhttp_stream *s)
Packit Service 20376f
{
Packit Service 20376f
	GIT_UNUSED(t);
Packit Service 20376f
Packit Service 20376f
	s->service = upload_pack_service;
Packit Service 20376f
	s->service_url = upload_pack_ls_service_url;
Packit Service 20376f
	s->verb = get_verb;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int winhttp_uploadpack(
Packit Service 20376f
	winhttp_subtransport *t,
Packit Service 20376f
	winhttp_stream *s)
Packit Service 20376f
{
Packit Service 20376f
	GIT_UNUSED(t);
Packit Service 20376f
Packit Service 20376f
	s->service = upload_pack_service;
Packit Service 20376f
	s->service_url = upload_pack_service_url;
Packit Service 20376f
	s->verb = post_verb;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int winhttp_receivepack_ls(
Packit Service 20376f
	winhttp_subtransport *t,
Packit Service 20376f
	winhttp_stream *s)
Packit Service 20376f
{
Packit Service 20376f
	GIT_UNUSED(t);
Packit Service 20376f
Packit Service 20376f
	s->service = receive_pack_service;
Packit Service 20376f
	s->service_url = receive_pack_ls_service_url;
Packit Service 20376f
	s->verb = get_verb;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int winhttp_receivepack(
Packit Service 20376f
	winhttp_subtransport *t,
Packit Service 20376f
	winhttp_stream *s)
Packit Service 20376f
{
Packit Service 20376f
	GIT_UNUSED(t);
Packit Service 20376f
Packit Service 20376f
	/* WinHTTP only supports Transfer-Encoding: chunked
Packit Service 20376f
	 * on Windows Vista (NT 6.0) and higher. */
Packit Service 20376f
	s->chunked = git_has_win32_version(6, 0, 0);
Packit Service 20376f
Packit Service 20376f
	if (s->chunked)
Packit Service 20376f
		s->parent.write = winhttp_stream_write_chunked;
Packit Service 20376f
	else
Packit Service 20376f
		s->parent.write = winhttp_stream_write_buffered;
Packit Service 20376f
Packit Service 20376f
	s->service = receive_pack_service;
Packit Service 20376f
	s->service_url = receive_pack_service_url;
Packit Service 20376f
	s->verb = post_verb;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int winhttp_action(
Packit Service 20376f
	git_smart_subtransport_stream **stream,
Packit Service 20376f
	git_smart_subtransport *subtransport,
Packit Service 20376f
	const char *url,
Packit Service 20376f
	git_smart_service_t action)
Packit Service 20376f
{
Packit Service 20376f
	winhttp_subtransport *t = (winhttp_subtransport *)subtransport;
Packit Service 20376f
	winhttp_stream *s;
Packit Service 20376f
	int ret = -1;
Packit Service 20376f
Packit Service 20376f
	if (!t->connection)
Packit Service 20376f
		if ((ret = gitno_connection_data_from_url(&t->connection_data, url, NULL)) < 0 ||
Packit Service 20376f
			 (ret = winhttp_connect(t)) < 0)
Packit Service 20376f
			return ret;
Packit Service 20376f
Packit Service 20376f
	if (winhttp_stream_alloc(t, &s) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (!stream)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	switch (action)
Packit Service 20376f
	{
Packit Service 20376f
		case GIT_SERVICE_UPLOADPACK_LS:
Packit Service 20376f
			ret = winhttp_uploadpack_ls(t, s);
Packit Service 20376f
			break;
Packit Service 20376f
Packit Service 20376f
		case GIT_SERVICE_UPLOADPACK:
Packit Service 20376f
			ret = winhttp_uploadpack(t, s);
Packit Service 20376f
			break;
Packit Service 20376f
Packit Service 20376f
		case GIT_SERVICE_RECEIVEPACK_LS:
Packit Service 20376f
			ret = winhttp_receivepack_ls(t, s);
Packit Service 20376f
			break;
Packit Service 20376f
Packit Service 20376f
		case GIT_SERVICE_RECEIVEPACK:
Packit Service 20376f
			ret = winhttp_receivepack(t, s);
Packit Service 20376f
			break;
Packit Service 20376f
Packit Service 20376f
		default:
Packit Service 20376f
			assert(0);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (!ret)
Packit Service 20376f
		*stream = &s->parent;
Packit Service 20376f
Packit Service 20376f
	return ret;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int winhttp_close(git_smart_subtransport *subtransport)
Packit Service 20376f
{
Packit Service 20376f
	winhttp_subtransport *t = (winhttp_subtransport *)subtransport;
Packit Service 20376f
Packit Service 20376f
	gitno_connection_data_free_ptrs(&t->connection_data);
Packit Service 20376f
	memset(&t->connection_data, 0x0, sizeof(gitno_connection_data));
Packit Service 20376f
	gitno_connection_data_free_ptrs(&t->proxy_connection_data);
Packit Service 20376f
	memset(&t->proxy_connection_data, 0x0, sizeof(gitno_connection_data));
Packit Service 20376f
Packit Service 20376f
	if (t->cred) {
Packit Service 20376f
		t->cred->free(t->cred);
Packit Service 20376f
		t->cred = NULL;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (t->proxy_cred) {
Packit Service 20376f
		t->proxy_cred->free(t->proxy_cred);
Packit Service 20376f
		t->proxy_cred = NULL;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (t->url_cred) {
Packit Service 20376f
		t->url_cred->free(t->url_cred);
Packit Service 20376f
		t->url_cred = NULL;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return winhttp_close_connection(t);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void winhttp_free(git_smart_subtransport *subtransport)
Packit Service 20376f
{
Packit Service 20376f
	winhttp_subtransport *t = (winhttp_subtransport *)subtransport;
Packit Service 20376f
Packit Service 20376f
	winhttp_close(subtransport);
Packit Service 20376f
Packit Service 20376f
	git__free(t);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_smart_subtransport_http(git_smart_subtransport **out, git_transport *owner, void *param)
Packit Service 20376f
{
Packit Service 20376f
	winhttp_subtransport *t;
Packit Service 20376f
Packit Service 20376f
	GIT_UNUSED(param);
Packit Service 20376f
Packit Service 20376f
	if (!out)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	t = git__calloc(1, sizeof(winhttp_subtransport));
Packit Service 20376f
	GITERR_CHECK_ALLOC(t);
Packit Service 20376f
Packit Service 20376f
	t->owner = (transport_smart *)owner;
Packit Service 20376f
	t->parent.action = winhttp_action;
Packit Service 20376f
	t->parent.close = winhttp_close;
Packit Service 20376f
	t->parent.free = winhttp_free;
Packit Service 20376f
Packit Service 20376f
	*out = (git_smart_subtransport *) t;
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#endif /* GIT_WINHTTP */