2 * Copyright (C) the libgit2 contributors. All rights reserved.
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
13 #include "git2/transport.h"
19 #include "repository.h"
22 #include "git2/sys/credential.h"
27 /* For IInternetSecurityManager zone check */
31 #define WIDEN2(s) L ## s
32 #define WIDEN(s) WIDEN2(s)
34 #define MAX_CONTENT_TYPE_LEN 100
35 #define WINHTTP_OPTION_PEERDIST_EXTENSION_STATE 109
36 #define CACHED_POST_BODY_BUF_SIZE 4096
37 #define UUID_LENGTH_CCH 32
38 #define TIMEOUT_INFINITE -1
39 #define DEFAULT_CONNECT_TIMEOUT 60000
40 #ifndef WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH
41 #define WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH 0
44 #ifndef WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1
45 # define WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1 0x00000200
48 #ifndef WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2
49 # define WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2 0x00000800
52 #ifndef WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_3
53 # define WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_3 0x00002000
56 #ifndef HTTP_STATUS_PERMANENT_REDIRECT
57 # define HTTP_STATUS_PERMANENT_REDIRECT 308
61 # define DWORD_MAX 0xffffffff
64 bool git_http__expect_continue
= false;
66 static const char *prefix_https
= "https://";
67 static const char *upload_pack_service
= "upload-pack";
68 static const char *upload_pack_ls_service_url
= "/info/refs?service=git-upload-pack";
69 static const char *upload_pack_service_url
= "/git-upload-pack";
70 static const char *receive_pack_service
= "receive-pack";
71 static const char *receive_pack_ls_service_url
= "/info/refs?service=git-receive-pack";
72 static const char *receive_pack_service_url
= "/git-receive-pack";
73 static const wchar_t *get_verb
= L
"GET";
74 static const wchar_t *post_verb
= L
"POST";
75 static const wchar_t *pragma_nocache
= L
"Pragma: no-cache";
76 static const wchar_t *transfer_encoding
= L
"Transfer-Encoding: chunked";
77 static const int no_check_cert_flags
= SECURITY_FLAG_IGNORE_CERT_CN_INVALID
|
78 SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
|
79 SECURITY_FLAG_IGNORE_UNKNOWN_CA
;
81 #if defined(__MINGW32__)
82 static const CLSID CLSID_InternetSecurityManager_mingw
=
83 { 0x7B8A2D94, 0x0AC9, 0x11D1,
84 { 0x89, 0x6C, 0x00, 0xC0, 0x4F, 0xB6, 0xBF, 0xC4 } };
85 static const IID IID_IInternetSecurityManager_mingw
=
86 { 0x79EAC9EE, 0xBAF9, 0x11CE,
87 { 0x8C, 0x82, 0x00, 0xAA, 0x00, 0x4B, 0xA9, 0x0B } };
89 # define CLSID_InternetSecurityManager CLSID_InternetSecurityManager_mingw
90 # define IID_IInternetSecurityManager IID_IInternetSecurityManager_mingw
93 #define OWNING_SUBTRANSPORT(s) ((winhttp_subtransport *)(s)->parent.subtransport)
96 GIT_WINHTTP_AUTH_BASIC
= 1,
97 GIT_WINHTTP_AUTH_NTLM
= 2,
98 GIT_WINHTTP_AUTH_NEGOTIATE
= 4,
99 GIT_WINHTTP_AUTH_DIGEST
= 8,
100 } winhttp_authmechanism_t
;
103 git_smart_subtransport_stream parent
;
105 const char *service_url
;
108 wchar_t *request_uri
;
110 unsigned chunk_buffer_len
;
113 unsigned sent_request
: 1,
114 received_response
: 1,
120 git_credential
*cred
;
122 bool url_cred_presented
;
126 git_smart_subtransport parent
;
127 transport_smart
*owner
;
129 winhttp_server server
;
130 winhttp_server proxy
;
133 HINTERNET connection
;
134 } winhttp_subtransport
;
136 static int apply_userpass_credentials(HINTERNET request
, DWORD target
, int mechanisms
, git_credential
*cred
)
138 git_credential_userpass_plaintext
*c
= (git_credential_userpass_plaintext
*)cred
;
139 wchar_t *user
= NULL
, *pass
= NULL
;
140 int user_len
= 0, pass_len
= 0, error
= 0;
143 if (mechanisms
& GIT_WINHTTP_AUTH_NEGOTIATE
) {
144 native_scheme
= WINHTTP_AUTH_SCHEME_NEGOTIATE
;
145 } else if (mechanisms
& GIT_WINHTTP_AUTH_NTLM
) {
146 native_scheme
= WINHTTP_AUTH_SCHEME_NTLM
;
147 } else if (mechanisms
& GIT_WINHTTP_AUTH_DIGEST
) {
148 native_scheme
= WINHTTP_AUTH_SCHEME_DIGEST
;
149 } else if (mechanisms
& GIT_WINHTTP_AUTH_BASIC
) {
150 native_scheme
= WINHTTP_AUTH_SCHEME_BASIC
;
152 git_error_set(GIT_ERROR_HTTP
, "invalid authentication scheme");
157 if ((error
= user_len
= git__utf8_to_16_alloc(&user
, c
->username
)) < 0)
160 if ((error
= pass_len
= git__utf8_to_16_alloc(&pass
, c
->password
)) < 0)
163 if (!WinHttpSetCredentials(request
, target
, native_scheme
, user
, pass
, NULL
)) {
164 git_error_set(GIT_ERROR_OS
, "failed to set credentials");
170 git__memzero(user
, user_len
* sizeof(wchar_t));
173 git__memzero(pass
, pass_len
* sizeof(wchar_t));
181 static int apply_default_credentials(HINTERNET request
, DWORD target
, int mechanisms
)
183 DWORD autologon_level
= WINHTTP_AUTOLOGON_SECURITY_LEVEL_LOW
;
184 DWORD native_scheme
= 0;
186 if ((mechanisms
& GIT_WINHTTP_AUTH_NEGOTIATE
) != 0) {
187 native_scheme
= WINHTTP_AUTH_SCHEME_NEGOTIATE
;
188 } else if ((mechanisms
& GIT_WINHTTP_AUTH_NTLM
) != 0) {
189 native_scheme
= WINHTTP_AUTH_SCHEME_NTLM
;
191 git_error_set(GIT_ERROR_HTTP
, "invalid authentication scheme");
196 * Autologon policy must be "low" to use default creds.
197 * This is safe as the user has explicitly requested it.
199 if (!WinHttpSetOption(request
, WINHTTP_OPTION_AUTOLOGON_POLICY
, &autologon_level
, sizeof(DWORD
))) {
200 git_error_set(GIT_ERROR_OS
, "could not configure logon policy");
204 if (!WinHttpSetCredentials(request
, target
, native_scheme
, NULL
, NULL
, NULL
)) {
205 git_error_set(GIT_ERROR_OS
, "could not configure credentials");
212 static int acquire_url_cred(
213 git_credential
**cred
,
214 unsigned int allowed_types
,
215 const char *username
,
216 const char *password
)
218 if (allowed_types
& GIT_CREDENTIAL_USERPASS_PLAINTEXT
)
219 return git_credential_userpass_plaintext_new(cred
, username
, password
);
221 if ((allowed_types
& GIT_CREDENTIAL_DEFAULT
) && *username
== '\0' && *password
== '\0')
222 return git_credential_default_new(cred
);
227 static int acquire_fallback_cred(
228 git_credential
**cred
,
230 unsigned int allowed_types
)
234 /* If the target URI supports integrated Windows authentication
235 * as an authentication mechanism */
236 if (GIT_CREDENTIAL_DEFAULT
& allowed_types
) {
238 HRESULT hCoInitResult
;
240 /* Convert URL to wide characters */
241 if (git__utf8_to_16_alloc(&wide_url
, url
) < 0) {
242 git_error_set(GIT_ERROR_OS
, "failed to convert string to wide form");
246 hCoInitResult
= CoInitializeEx(NULL
, COINIT_MULTITHREADED
);
248 if (SUCCEEDED(hCoInitResult
) || hCoInitResult
== RPC_E_CHANGED_MODE
) {
249 IInternetSecurityManager
* pISM
;
251 /* And if the target URI is in the My Computer, Intranet, or Trusted zones */
252 if (SUCCEEDED(CoCreateInstance(&CLSID_InternetSecurityManager
, NULL
,
253 CLSCTX_ALL
, &IID_IInternetSecurityManager
, (void **)&pISM
))) {
256 if (SUCCEEDED(pISM
->lpVtbl
->MapUrlToZone(pISM
, wide_url
, &dwZone
, 0)) &&
257 (URLZONE_LOCAL_MACHINE
== dwZone
||
258 URLZONE_INTRANET
== dwZone
||
259 URLZONE_TRUSTED
== dwZone
)) {
260 git_credential
*existing
= *cred
;
263 existing
->free(existing
);
265 /* Then use default Windows credentials to authenticate this request */
266 error
= git_credential_default_new(cred
);
269 pISM
->lpVtbl
->Release(pISM
);
272 /* Only unitialize if the call to CoInitializeEx was successful. */
273 if (SUCCEEDED(hCoInitResult
))
283 static int certificate_check(winhttp_stream
*s
, int valid
)
286 winhttp_subtransport
*t
= OWNING_SUBTRANSPORT(s
);
287 PCERT_CONTEXT cert_ctx
;
288 DWORD cert_ctx_size
= sizeof(cert_ctx
);
291 /* If there is no override, we should fail if WinHTTP doesn't think it's fine */
292 if (t
->owner
->certificate_check_cb
== NULL
&& !valid
) {
293 if (!git_error_last())
294 git_error_set(GIT_ERROR_HTTP
, "unknown certificate check failure");
296 return GIT_ECERTIFICATE
;
299 if (t
->owner
->certificate_check_cb
== NULL
|| git__strcmp(t
->server
.url
.scheme
, "https") != 0)
302 if (!WinHttpQueryOption(s
->request
, WINHTTP_OPTION_SERVER_CERT_CONTEXT
, &cert_ctx
, &cert_ctx_size
)) {
303 git_error_set(GIT_ERROR_OS
, "failed to get server certificate");
308 cert
.parent
.cert_type
= GIT_CERT_X509
;
309 cert
.data
= cert_ctx
->pbCertEncoded
;
310 cert
.len
= cert_ctx
->cbCertEncoded
;
311 error
= t
->owner
->certificate_check_cb((git_cert
*) &cert
, valid
, t
->server
.url
.host
, t
->owner
->message_cb_payload
);
312 CertFreeCertificateContext(cert_ctx
);
314 if (error
== GIT_PASSTHROUGH
)
315 error
= valid
? 0 : GIT_ECERTIFICATE
;
317 if (error
< 0 && !git_error_last())
318 git_error_set(GIT_ERROR_HTTP
, "user cancelled certificate check");
323 static void winhttp_stream_close(winhttp_stream
*s
)
325 if (s
->chunk_buffer
) {
326 git__free(s
->chunk_buffer
);
327 s
->chunk_buffer
= NULL
;
331 CloseHandle(s
->post_body
);
335 if (s
->request_uri
) {
336 git__free(s
->request_uri
);
337 s
->request_uri
= NULL
;
341 WinHttpCloseHandle(s
->request
);
348 static int apply_credentials(
352 git_credential
*creds
,
359 /* If we have creds, just apply them */
360 if (creds
&& creds
->credtype
== GIT_CREDENTIAL_USERPASS_PLAINTEXT
)
361 error
= apply_userpass_credentials(request
, target
, mechanisms
, creds
);
362 else if (creds
&& creds
->credtype
== GIT_CREDENTIAL_DEFAULT
)
363 error
= apply_default_credentials(request
, target
, mechanisms
);
368 static int winhttp_stream_connect(winhttp_stream
*s
)
370 winhttp_subtransport
*t
= OWNING_SUBTRANSPORT(s
);
371 git_buf buf
= GIT_BUF_INIT
;
372 char *proxy_url
= NULL
;
373 wchar_t ct
[MAX_CONTENT_TYPE_LEN
];
374 LPCWSTR types
[] = { L
"*/*", NULL
};
375 BOOL peerdist
= FALSE
;
377 unsigned long disable_redirects
= WINHTTP_DISABLE_REDIRECTS
;
378 int default_timeout
= TIMEOUT_INFINITE
;
379 int default_connect_timeout
= DEFAULT_CONNECT_TIMEOUT
;
380 DWORD autologon_policy
= WINHTTP_AUTOLOGON_SECURITY_LEVEL_HIGH
;
382 const char *service_url
= s
->service_url
;
384 const git_proxy_options
*proxy_opts
;
386 /* If path already ends in /, remove the leading slash from service_url */
387 if ((git__suffixcmp(t
->server
.url
.path
, "/") == 0) && (git__prefixcmp(service_url
, "/") == 0))
390 git_buf_printf(&buf
, "%s%s", t
->server
.url
.path
, service_url
);
392 if (git_buf_oom(&buf
))
395 /* Convert URL to wide characters */
396 if (git__utf8_to_16_alloc(&s
->request_uri
, git_buf_cstr(&buf
)) < 0) {
397 git_error_set(GIT_ERROR_OS
, "failed to convert string to wide form");
401 /* Establish request */
402 s
->request
= WinHttpOpenRequest(
409 git__strcmp(t
->server
.url
.scheme
, "https") == 0 ? WINHTTP_FLAG_SECURE
: 0);
412 git_error_set(GIT_ERROR_OS
, "failed to open request");
416 /* Never attempt default credentials; we'll provide them explicitly. */
417 if (!WinHttpSetOption(s
->request
, WINHTTP_OPTION_AUTOLOGON_POLICY
, &autologon_policy
, sizeof(DWORD
)))
420 if (!WinHttpSetTimeouts(s
->request
, default_timeout
, default_connect_timeout
, default_timeout
, default_timeout
)) {
421 git_error_set(GIT_ERROR_OS
, "failed to set timeouts for WinHTTP");
425 proxy_opts
= &t
->owner
->proxy
;
426 if (proxy_opts
->type
== GIT_PROXY_AUTO
) {
427 /* Set proxy if necessary */
428 if (git_remote__get_http_proxy(t
->owner
->owner
, (strcmp(t
->server
.url
.scheme
, "https") == 0), &proxy_url
) < 0)
431 else if (proxy_opts
->type
== GIT_PROXY_SPECIFIED
) {
432 proxy_url
= git__strdup(proxy_opts
->url
);
433 GIT_ERROR_CHECK_ALLOC(proxy_url
);
437 git_buf processed_url
= GIT_BUF_INIT
;
438 WINHTTP_PROXY_INFO proxy_info
;
441 git_net_url_dispose(&t
->proxy
.url
);
443 if ((error
= git_net_url_parse(&t
->proxy
.url
, proxy_url
)) < 0)
446 if (strcmp(t
->proxy
.url
.scheme
, "http") != 0 && strcmp(t
->proxy
.url
.scheme
, "https") != 0) {
447 git_error_set(GIT_ERROR_HTTP
, "invalid URL: '%s'", proxy_url
);
452 git_buf_puts(&processed_url
, t
->proxy
.url
.scheme
);
453 git_buf_PUTS(&processed_url
, "://");
455 git_buf_puts(&processed_url
, t
->proxy
.url
.host
);
457 if (!git_net_url_is_default_port(&t
->proxy
.url
))
458 git_buf_printf(&processed_url
, ":%s", t
->proxy
.url
.port
);
460 if (git_buf_oom(&processed_url
)) {
465 /* Convert URL to wide characters */
466 error
= git__utf8_to_16_alloc(&proxy_wide
, processed_url
.ptr
);
467 git_buf_dispose(&processed_url
);
471 proxy_info
.dwAccessType
= WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
472 proxy_info
.lpszProxy
= proxy_wide
;
473 proxy_info
.lpszProxyBypass
= NULL
;
475 if (!WinHttpSetOption(s
->request
,
476 WINHTTP_OPTION_PROXY
,
478 sizeof(WINHTTP_PROXY_INFO
))) {
479 git_error_set(GIT_ERROR_OS
, "failed to set proxy");
480 git__free(proxy_wide
);
484 git__free(proxy_wide
);
486 if ((error
= apply_credentials(s
->request
, &t
->proxy
.url
, WINHTTP_AUTH_TARGET_PROXY
, t
->proxy
.cred
, t
->proxy
.auth_mechanisms
)) < 0)
490 /* Disable WinHTTP redirects so we can handle them manually. Why, you ask?
491 * http://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/b2ff8879-ab9f-4218-8f09-16d25dff87ae
493 if (!WinHttpSetOption(s
->request
,
494 WINHTTP_OPTION_DISABLE_FEATURE
,
496 sizeof(disable_redirects
))) {
497 git_error_set(GIT_ERROR_OS
, "failed to disable redirects");
502 /* Strip unwanted headers (X-P2P-PeerDist, X-P2P-PeerDistEx) that WinHTTP
503 * adds itself. This option may not be supported by the underlying
504 * platform, so we do not error-check it */
505 WinHttpSetOption(s
->request
,
506 WINHTTP_OPTION_PEERDIST_EXTENSION_STATE
,
510 /* Send Pragma: no-cache header */
511 if (!WinHttpAddRequestHeaders(s
->request
, pragma_nocache
, (ULONG
) -1L, WINHTTP_ADDREQ_FLAG_ADD
)) {
512 git_error_set(GIT_ERROR_OS
, "failed to add a header to the request");
516 if (post_verb
== s
->verb
) {
517 /* Send Content-Type and Accept headers -- only necessary on a POST */
519 if (git_buf_printf(&buf
,
520 "Content-Type: application/x-git-%s-request",
524 if (git__utf8_to_16(ct
, MAX_CONTENT_TYPE_LEN
, git_buf_cstr(&buf
)) < 0) {
525 git_error_set(GIT_ERROR_OS
, "failed to convert content-type to wide characters");
529 if (!WinHttpAddRequestHeaders(s
->request
, ct
, (ULONG
)-1L,
530 WINHTTP_ADDREQ_FLAG_ADD
| WINHTTP_ADDREQ_FLAG_REPLACE
)) {
531 git_error_set(GIT_ERROR_OS
, "failed to add a header to the request");
536 if (git_buf_printf(&buf
,
537 "Accept: application/x-git-%s-result",
541 if (git__utf8_to_16(ct
, MAX_CONTENT_TYPE_LEN
, git_buf_cstr(&buf
)) < 0) {
542 git_error_set(GIT_ERROR_OS
, "failed to convert accept header to wide characters");
546 if (!WinHttpAddRequestHeaders(s
->request
, ct
, (ULONG
)-1L,
547 WINHTTP_ADDREQ_FLAG_ADD
| WINHTTP_ADDREQ_FLAG_REPLACE
)) {
548 git_error_set(GIT_ERROR_OS
, "failed to add a header to the request");
553 for (i
= 0; i
< t
->owner
->custom_headers
.count
; i
++) {
554 if (t
->owner
->custom_headers
.strings
[i
]) {
556 git_buf_puts(&buf
, t
->owner
->custom_headers
.strings
[i
]);
557 if (git__utf8_to_16(ct
, MAX_CONTENT_TYPE_LEN
, git_buf_cstr(&buf
)) < 0) {
558 git_error_set(GIT_ERROR_OS
, "failed to convert custom header to wide characters");
562 if (!WinHttpAddRequestHeaders(s
->request
, ct
, (ULONG
)-1L,
563 WINHTTP_ADDREQ_FLAG_ADD
| WINHTTP_ADDREQ_FLAG_REPLACE
)) {
564 git_error_set(GIT_ERROR_OS
, "failed to add a header to the request");
570 /* If requested, disable certificate validation */
571 if (strcmp(t
->server
.url
.scheme
, "https") == 0) {
574 if (t
->owner
->parent
.read_flags(&t
->owner
->parent
, &flags
) < 0)
578 if ((error
= apply_credentials(s
->request
, &t
->server
.url
, WINHTTP_AUTH_TARGET_SERVER
, t
->server
.cred
, t
->server
.auth_mechanisms
)) < 0)
581 /* We've done everything up to calling WinHttpSendRequest. */
587 winhttp_stream_close(s
);
589 git__free(proxy_url
);
590 git_buf_dispose(&buf
);
594 static int parse_unauthorized_response(
596 int *allowed_mechanisms
,
599 DWORD supported
, first
, target
;
602 *allowed_mechanisms
= 0;
604 /* WinHttpQueryHeaders() must be called before WinHttpQueryAuthSchemes().
605 * We can assume this was already done, since we know we are unauthorized.
607 if (!WinHttpQueryAuthSchemes(request
, &supported
, &first
, &target
)) {
608 git_error_set(GIT_ERROR_OS
, "failed to parse supported auth schemes");
612 if (WINHTTP_AUTH_SCHEME_NTLM
& supported
) {
613 *allowed_types
|= GIT_CREDENTIAL_USERPASS_PLAINTEXT
;
614 *allowed_types
|= GIT_CREDENTIAL_DEFAULT
;
615 *allowed_mechanisms
|= GIT_WINHTTP_AUTH_NTLM
;
618 if (WINHTTP_AUTH_SCHEME_NEGOTIATE
& supported
) {
619 *allowed_types
|= GIT_CREDENTIAL_DEFAULT
;
620 *allowed_mechanisms
|= GIT_WINHTTP_AUTH_NEGOTIATE
;
623 if (WINHTTP_AUTH_SCHEME_BASIC
& supported
) {
624 *allowed_types
|= GIT_CREDENTIAL_USERPASS_PLAINTEXT
;
625 *allowed_mechanisms
|= GIT_WINHTTP_AUTH_BASIC
;
628 if (WINHTTP_AUTH_SCHEME_DIGEST
& supported
) {
629 *allowed_types
|= GIT_CREDENTIAL_USERPASS_PLAINTEXT
;
630 *allowed_mechanisms
|= GIT_WINHTTP_AUTH_DIGEST
;
636 static int write_chunk(HINTERNET request
, const char *buffer
, size_t len
)
639 git_buf buf
= GIT_BUF_INIT
;
642 git_buf_printf(&buf
, "%"PRIXZ
"\r\n", len
);
644 if (git_buf_oom(&buf
))
647 if (!WinHttpWriteData(request
,
648 git_buf_cstr(&buf
), (DWORD
)git_buf_len(&buf
),
650 git_buf_dispose(&buf
);
651 git_error_set(GIT_ERROR_OS
, "failed to write chunk header");
655 git_buf_dispose(&buf
);
658 if (!WinHttpWriteData(request
,
661 git_error_set(GIT_ERROR_OS
, "failed to write chunk");
666 if (!WinHttpWriteData(request
,
669 git_error_set(GIT_ERROR_OS
, "failed to write chunk footer");
676 static int winhttp_close_connection(winhttp_subtransport
*t
)
681 if (!WinHttpCloseHandle(t
->connection
)) {
682 git_error_set(GIT_ERROR_OS
, "unable to close connection");
686 t
->connection
= NULL
;
690 if (!WinHttpCloseHandle(t
->session
)) {
691 git_error_set(GIT_ERROR_OS
, "unable to close session");
701 static void CALLBACK
winhttp_status(
702 HINTERNET connection
,
710 GIT_UNUSED(connection
);
712 GIT_UNUSED(info_len
);
714 if (code
!= WINHTTP_CALLBACK_STATUS_SECURE_FAILURE
)
717 status
= *((DWORD
*)info
);
719 if ((status
& WINHTTP_CALLBACK_STATUS_FLAG_CERT_CN_INVALID
))
720 git_error_set(GIT_ERROR_HTTP
, "SSL certificate issued for different common name");
721 else if ((status
& WINHTTP_CALLBACK_STATUS_FLAG_CERT_DATE_INVALID
))
722 git_error_set(GIT_ERROR_HTTP
, "SSL certificate has expired");
723 else if ((status
& WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CA
))
724 git_error_set(GIT_ERROR_HTTP
, "SSL certificate signed by unknown CA");
725 else if ((status
& WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CERT
))
726 git_error_set(GIT_ERROR_HTTP
, "SSL certificate is invalid");
727 else if ((status
& WINHTTP_CALLBACK_STATUS_FLAG_CERT_REV_FAILED
))
728 git_error_set(GIT_ERROR_HTTP
, "certificate revocation check failed");
729 else if ((status
& WINHTTP_CALLBACK_STATUS_FLAG_CERT_REVOKED
))
730 git_error_set(GIT_ERROR_HTTP
, "SSL certificate was revoked");
731 else if ((status
& WINHTTP_CALLBACK_STATUS_FLAG_SECURITY_CHANNEL_ERROR
))
732 git_error_set(GIT_ERROR_HTTP
, "security libraries could not be loaded");
734 git_error_set(GIT_ERROR_HTTP
, "unknown security error %lu", status
);
737 static int winhttp_connect(
738 winhttp_subtransport
*t
)
743 git_buf ua
= GIT_BUF_INIT
;
745 int default_timeout
= TIMEOUT_INFINITE
;
746 int default_connect_timeout
= DEFAULT_CONNECT_TIMEOUT
;
748 WINHTTP_FLAG_SECURE_PROTOCOL_TLS1
|
749 WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1
|
750 WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2
|
751 WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_3
;
754 t
->connection
= NULL
;
757 if (git__strntol32(&port
, t
->server
.url
.port
,
758 strlen(t
->server
.url
.port
), NULL
, 10) < 0)
762 if (git__utf8_to_16_alloc(&wide_host
, t
->server
.url
.host
) < 0) {
763 git_error_set(GIT_ERROR_OS
, "unable to convert host to wide characters");
768 if ((error
= git_http__user_agent(&ua
)) < 0) {
769 git__free(wide_host
);
773 if (git__utf8_to_16_alloc(&wide_ua
, git_buf_cstr(&ua
)) < 0) {
774 git_error_set(GIT_ERROR_OS
, "unable to convert host to wide characters");
775 git__free(wide_host
);
776 git_buf_dispose(&ua
);
780 git_buf_dispose(&ua
);
782 /* Establish session */
783 t
->session
= WinHttpOpen(
785 WINHTTP_ACCESS_TYPE_DEFAULT_PROXY
,
786 WINHTTP_NO_PROXY_NAME
,
787 WINHTTP_NO_PROXY_BYPASS
,
791 git_error_set(GIT_ERROR_OS
, "failed to init WinHTTP");
796 * Do a best-effort attempt to enable TLS 1.3 and 1.2 but allow this to
797 * fail; if TLS 1.2 or 1.3 support is not available for some reason,
798 * ignore the failure (it will keep the default protocols).
800 if (WinHttpSetOption(t
->session
,
801 WINHTTP_OPTION_SECURE_PROTOCOLS
,
803 sizeof(protocols
)) == FALSE
) {
804 protocols
&= ~WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_3
;
805 WinHttpSetOption(t
->session
,
806 WINHTTP_OPTION_SECURE_PROTOCOLS
,
811 if (!WinHttpSetTimeouts(t
->session
, default_timeout
, default_connect_timeout
, default_timeout
, default_timeout
)) {
812 git_error_set(GIT_ERROR_OS
, "failed to set timeouts for WinHTTP");
817 /* Establish connection */
818 t
->connection
= WinHttpConnect(
821 (INTERNET_PORT
) port
,
824 if (!t
->connection
) {
825 git_error_set(GIT_ERROR_OS
, "failed to connect to host");
829 if (WinHttpSetStatusCallback(t
->connection
, winhttp_status
, WINHTTP_CALLBACK_FLAG_SECURE_FAILURE
, 0) == WINHTTP_INVALID_STATUS_CALLBACK
) {
830 git_error_set(GIT_ERROR_OS
, "failed to set status callback");
838 winhttp_close_connection(t
);
840 git__free(wide_host
);
846 static int do_send_request(winhttp_stream
*s
, size_t len
, bool chunked
)
851 if (len
> DWORD_MAX
) {
852 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
856 for (attempts
= 0; attempts
< 5; attempts
++) {
858 success
= WinHttpSendRequest(s
->request
,
859 WINHTTP_NO_ADDITIONAL_HEADERS
, 0,
860 WINHTTP_NO_REQUEST_DATA
, 0,
861 WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH
, 0);
863 success
= WinHttpSendRequest(s
->request
,
864 WINHTTP_NO_ADDITIONAL_HEADERS
, 0,
865 WINHTTP_NO_REQUEST_DATA
, 0,
869 if (success
|| GetLastError() != (DWORD
)SEC_E_BUFFER_TOO_SMALL
)
873 return success
? 0 : -1;
876 static int send_request(winhttp_stream
*s
, size_t len
, bool chunked
)
878 int request_failed
= 0, cert_valid
= 1, error
= 0;
882 if ((error
= do_send_request(s
, len
, chunked
)) < 0) {
883 if (GetLastError() != ERROR_WINHTTP_SECURE_FAILURE
) {
884 git_error_set(GIT_ERROR_OS
, "failed to send request");
893 if ((error
= certificate_check(s
, cert_valid
)) < 0) {
894 if (!git_error_last())
895 git_error_set(GIT_ERROR_OS
, "user cancelled certificate check");
900 /* if neither the request nor the certificate check returned errors, we're done */
904 ignore_flags
= no_check_cert_flags
;
906 if (!WinHttpSetOption(s
->request
, WINHTTP_OPTION_SECURITY_FLAGS
, &ignore_flags
, sizeof(ignore_flags
))) {
907 git_error_set(GIT_ERROR_OS
, "failed to set security options");
911 if ((error
= do_send_request(s
, len
, chunked
)) < 0)
912 git_error_set(GIT_ERROR_OS
, "failed to send request with unchecked certificate");
917 static int acquire_credentials(
919 winhttp_server
*server
,
921 git_credential_acquire_cb cred_cb
,
922 void *cred_cb_payload
)
927 if (parse_unauthorized_response(&allowed_types
, &server
->auth_mechanisms
, request
) < 0)
931 git_credential_free(server
->cred
);
934 /* Start with URL-specified credentials, if there were any. */
935 if (!server
->url_cred_presented
&& server
->url
.username
&& server
->url
.password
) {
936 error
= acquire_url_cred(&server
->cred
, allowed_types
, server
->url
.username
, server
->url
.password
);
937 server
->url_cred_presented
= 1;
943 /* Next use the user-defined callback, if there is one. */
944 if (error
> 0 && cred_cb
) {
945 error
= cred_cb(&server
->cred
, url_str
, server
->url
.username
, allowed_types
, cred_cb_payload
);
947 /* Treat GIT_PASSTHROUGH as though git_credential_acquire_cb isn't set */
948 if (error
== GIT_PASSTHROUGH
)
954 /* Finally, invoke the fallback default credential lookup. */
956 error
= acquire_fallback_cred(&server
->cred
, url_str
, allowed_types
);
964 * No error occurred but we could not find appropriate credentials.
965 * This behaves like a pass-through.
970 static int winhttp_stream_read(
971 git_smart_subtransport_stream
*stream
,
976 winhttp_stream
*s
= (winhttp_stream
*)stream
;
977 winhttp_subtransport
*t
= OWNING_SUBTRANSPORT(s
);
979 char replay_count
= 0;
983 /* Enforce a reasonable cap on the number of replays */
984 if (replay_count
++ >= GIT_HTTP_REPLAY_MAX
) {
985 git_error_set(GIT_ERROR_HTTP
, "too many redirects or authentication replays");
989 /* Connect if necessary */
990 if (!s
->request
&& winhttp_stream_connect(s
) < 0)
993 if (!s
->received_response
) {
994 DWORD status_code
, status_code_length
, content_type_length
, bytes_written
;
995 char expected_content_type_8
[MAX_CONTENT_TYPE_LEN
];
996 wchar_t expected_content_type
[MAX_CONTENT_TYPE_LEN
], content_type
[MAX_CONTENT_TYPE_LEN
];
998 if (!s
->sent_request
) {
1000 if ((error
= send_request(s
, s
->post_body_len
, false)) < 0)
1003 s
->sent_request
= 1;
1007 assert(s
->verb
== post_verb
);
1009 /* Flush, if necessary */
1010 if (s
->chunk_buffer_len
> 0 &&
1011 write_chunk(s
->request
, s
->chunk_buffer
, s
->chunk_buffer_len
) < 0)
1014 s
->chunk_buffer_len
= 0;
1016 /* Write the final chunk. */
1017 if (!WinHttpWriteData(s
->request
,
1020 git_error_set(GIT_ERROR_OS
, "failed to write final chunk");
1024 else if (s
->post_body
) {
1026 DWORD len
= s
->post_body_len
, bytes_read
;
1028 if (INVALID_SET_FILE_POINTER
== SetFilePointer(s
->post_body
,
1029 0, 0, FILE_BEGIN
) &&
1030 NO_ERROR
!= GetLastError()) {
1031 git_error_set(GIT_ERROR_OS
, "failed to reset file pointer");
1035 buffer
= git__malloc(CACHED_POST_BODY_BUF_SIZE
);
1036 GIT_ERROR_CHECK_ALLOC(buffer
);
1039 DWORD bytes_written
;
1041 if (!ReadFile(s
->post_body
, buffer
,
1042 min(CACHED_POST_BODY_BUF_SIZE
, len
),
1043 &bytes_read
, NULL
) ||
1046 git_error_set(GIT_ERROR_OS
, "failed to read from temp file");
1050 if (!WinHttpWriteData(s
->request
, buffer
,
1051 bytes_read
, &bytes_written
)) {
1053 git_error_set(GIT_ERROR_OS
, "failed to write data");
1058 assert(bytes_read
== bytes_written
);
1063 /* Eagerly close the temp file */
1064 CloseHandle(s
->post_body
);
1065 s
->post_body
= NULL
;
1068 if (!WinHttpReceiveResponse(s
->request
, 0)) {
1069 git_error_set(GIT_ERROR_OS
, "failed to receive response");
1073 /* Verify that we got a 200 back */
1074 status_code_length
= sizeof(status_code
);
1076 if (!WinHttpQueryHeaders(s
->request
,
1077 WINHTTP_QUERY_STATUS_CODE
| WINHTTP_QUERY_FLAG_NUMBER
,
1078 WINHTTP_HEADER_NAME_BY_INDEX
,
1079 &status_code
, &status_code_length
,
1080 WINHTTP_NO_HEADER_INDEX
)) {
1081 git_error_set(GIT_ERROR_OS
, "failed to retrieve status code");
1085 /* The implementation of WinHTTP prior to Windows 7 will not
1086 * redirect to an identical URI. Some Git hosters use self-redirects
1087 * as part of their DoS mitigation strategy. Check first to see if we
1088 * have a redirect status code, and that we haven't already streamed
1089 * a post body. (We can't replay a streamed POST.) */
1091 (HTTP_STATUS_MOVED
== status_code
||
1092 HTTP_STATUS_REDIRECT
== status_code
||
1093 (HTTP_STATUS_REDIRECT_METHOD
== status_code
&&
1094 get_verb
== s
->verb
) ||
1095 HTTP_STATUS_REDIRECT_KEEP_VERB
== status_code
||
1096 HTTP_STATUS_PERMANENT_REDIRECT
== status_code
)) {
1098 /* Check for Windows 7. This workaround is only necessary on
1099 * Windows Vista and earlier. Windows 7 is version 6.1. */
1101 DWORD location_length
;
1104 /* OK, fetch the Location header from the redirect. */
1105 if (WinHttpQueryHeaders(s
->request
,
1106 WINHTTP_QUERY_LOCATION
,
1107 WINHTTP_HEADER_NAME_BY_INDEX
,
1108 WINHTTP_NO_OUTPUT_BUFFER
,
1110 WINHTTP_NO_HEADER_INDEX
) ||
1111 GetLastError() != ERROR_INSUFFICIENT_BUFFER
) {
1112 git_error_set(GIT_ERROR_OS
, "failed to read Location header");
1116 location
= git__malloc(location_length
);
1117 GIT_ERROR_CHECK_ALLOC(location
);
1119 if (!WinHttpQueryHeaders(s
->request
,
1120 WINHTTP_QUERY_LOCATION
,
1121 WINHTTP_HEADER_NAME_BY_INDEX
,
1124 WINHTTP_NO_HEADER_INDEX
)) {
1125 git_error_set(GIT_ERROR_OS
, "failed to read Location header");
1126 git__free(location
);
1130 /* Convert the Location header to UTF-8 */
1131 if (git__utf16_to_8_alloc(&location8
, location
) < 0) {
1132 git_error_set(GIT_ERROR_OS
, "failed to convert Location header to UTF-8");
1133 git__free(location
);
1137 git__free(location
);
1139 /* Replay the request */
1140 winhttp_stream_close(s
);
1142 if (!git__prefixcmp_icase(location8
, prefix_https
)) {
1143 /* Upgrade to secure connection; disconnect and start over */
1144 if (git_net_url_apply_redirect(&t
->server
.url
, location8
, s
->service_url
) < 0) {
1145 git__free(location8
);
1149 winhttp_close_connection(t
);
1151 if (winhttp_connect(t
) < 0)
1155 git__free(location8
);
1159 /* Handle authentication failures */
1160 if (status_code
== HTTP_STATUS_DENIED
) {
1161 int error
= acquire_credentials(s
->request
,
1164 t
->owner
->cred_acquire_cb
,
1165 t
->owner
->cred_acquire_payload
);
1169 } else if (!error
) {
1170 assert(t
->server
.cred
);
1171 winhttp_stream_close(s
);
1174 } else if (status_code
== HTTP_STATUS_PROXY_AUTH_REQ
) {
1175 int error
= acquire_credentials(s
->request
,
1177 t
->owner
->proxy
.url
,
1178 t
->owner
->proxy
.credentials
,
1179 t
->owner
->proxy
.payload
);
1183 } else if (!error
) {
1184 assert(t
->proxy
.cred
);
1185 winhttp_stream_close(s
);
1190 if (HTTP_STATUS_OK
!= status_code
) {
1191 git_error_set(GIT_ERROR_HTTP
, "request failed with status code: %lu", status_code
);
1195 /* Verify that we got the correct content-type back */
1196 if (post_verb
== s
->verb
)
1197 p_snprintf(expected_content_type_8
, MAX_CONTENT_TYPE_LEN
, "application/x-git-%s-result", s
->service
);
1199 p_snprintf(expected_content_type_8
, MAX_CONTENT_TYPE_LEN
, "application/x-git-%s-advertisement", s
->service
);
1201 if (git__utf8_to_16(expected_content_type
, MAX_CONTENT_TYPE_LEN
, expected_content_type_8
) < 0) {
1202 git_error_set(GIT_ERROR_OS
, "failed to convert expected content-type to wide characters");
1206 content_type_length
= sizeof(content_type
);
1208 if (!WinHttpQueryHeaders(s
->request
,
1209 WINHTTP_QUERY_CONTENT_TYPE
,
1210 WINHTTP_HEADER_NAME_BY_INDEX
,
1211 &content_type
, &content_type_length
,
1212 WINHTTP_NO_HEADER_INDEX
)) {
1213 git_error_set(GIT_ERROR_OS
, "failed to retrieve response content-type");
1217 if (wcscmp(expected_content_type
, content_type
)) {
1218 git_error_set(GIT_ERROR_HTTP
, "received unexpected content-type");
1222 s
->received_response
= 1;
1225 if (!WinHttpReadData(s
->request
,
1230 git_error_set(GIT_ERROR_OS
, "failed to read data");
1234 *bytes_read
= dw_bytes_read
;
1239 static int winhttp_stream_write_single(
1240 git_smart_subtransport_stream
*stream
,
1244 winhttp_stream
*s
= (winhttp_stream
*)stream
;
1245 DWORD bytes_written
;
1248 if (!s
->request
&& winhttp_stream_connect(s
) < 0)
1251 /* This implementation of write permits only a single call. */
1252 if (s
->sent_request
) {
1253 git_error_set(GIT_ERROR_HTTP
, "subtransport configured for only one write");
1257 if ((error
= send_request(s
, len
, false)) < 0)
1260 s
->sent_request
= 1;
1262 if (!WinHttpWriteData(s
->request
,
1266 git_error_set(GIT_ERROR_OS
, "failed to write data");
1270 assert((DWORD
)len
== bytes_written
);
1275 static int put_uuid_string(LPWSTR buffer
, size_t buffer_len_cch
)
1278 RPC_STATUS status
= UuidCreate(&uuid
);
1281 if (RPC_S_OK
!= status
&&
1282 RPC_S_UUID_LOCAL_ONLY
!= status
&&
1283 RPC_S_UUID_NO_ADDRESS
!= status
) {
1284 git_error_set(GIT_ERROR_HTTP
, "unable to generate name for temp file");
1288 if (buffer_len_cch
< UUID_LENGTH_CCH
+ 1) {
1289 git_error_set(GIT_ERROR_HTTP
, "buffer too small for name of temp file");
1293 #if !defined(__MINGW32__) || defined(MINGW_HAS_SECURE_API)
1294 result
= swprintf_s(buffer
, buffer_len_cch
,
1296 result
= wsprintfW(buffer
,
1298 L
"%08x%04x%04x%02x%02x%02x%02x%02x%02x%02x%02x",
1299 uuid
.Data1
, uuid
.Data2
, uuid
.Data3
,
1300 uuid
.Data4
[0], uuid
.Data4
[1], uuid
.Data4
[2], uuid
.Data4
[3],
1301 uuid
.Data4
[4], uuid
.Data4
[5], uuid
.Data4
[6], uuid
.Data4
[7]);
1303 if (result
< UUID_LENGTH_CCH
) {
1304 git_error_set(GIT_ERROR_OS
, "unable to generate name for temp file");
1311 static int get_temp_file(LPWSTR buffer
, DWORD buffer_len_cch
)
1315 if (!GetTempPathW(buffer_len_cch
, buffer
)) {
1316 git_error_set(GIT_ERROR_OS
, "failed to get temp path");
1320 len
= wcslen(buffer
);
1322 if (buffer
[len
- 1] != '\\' && len
< buffer_len_cch
)
1323 buffer
[len
++] = '\\';
1325 if (put_uuid_string(&buffer
[len
], (size_t)buffer_len_cch
- len
) < 0)
1331 static int winhttp_stream_write_buffered(
1332 git_smart_subtransport_stream
*stream
,
1336 winhttp_stream
*s
= (winhttp_stream
*)stream
;
1337 DWORD bytes_written
;
1339 if (!s
->request
&& winhttp_stream_connect(s
) < 0)
1342 /* Buffer the payload, using a temporary file so we delegate
1343 * memory management of the data to the operating system. */
1344 if (!s
->post_body
) {
1345 wchar_t temp_path
[MAX_PATH
+ 1];
1347 if (get_temp_file(temp_path
, MAX_PATH
+ 1) < 0)
1350 s
->post_body
= CreateFileW(temp_path
,
1351 GENERIC_READ
| GENERIC_WRITE
,
1352 FILE_SHARE_DELETE
, NULL
,
1354 FILE_ATTRIBUTE_TEMPORARY
| FILE_FLAG_DELETE_ON_CLOSE
| FILE_FLAG_SEQUENTIAL_SCAN
,
1357 if (INVALID_HANDLE_VALUE
== s
->post_body
) {
1358 s
->post_body
= NULL
;
1359 git_error_set(GIT_ERROR_OS
, "failed to create temporary file");
1364 if (!WriteFile(s
->post_body
, buffer
, (DWORD
)len
, &bytes_written
, NULL
)) {
1365 git_error_set(GIT_ERROR_OS
, "failed to write to temporary file");
1369 assert((DWORD
)len
== bytes_written
);
1371 s
->post_body_len
+= bytes_written
;
1376 static int winhttp_stream_write_chunked(
1377 git_smart_subtransport_stream
*stream
,
1381 winhttp_stream
*s
= (winhttp_stream
*)stream
;
1384 if (!s
->request
&& winhttp_stream_connect(s
) < 0)
1387 if (!s
->sent_request
) {
1388 /* Send Transfer-Encoding: chunked header */
1389 if (!WinHttpAddRequestHeaders(s
->request
,
1390 transfer_encoding
, (ULONG
) -1L,
1391 WINHTTP_ADDREQ_FLAG_ADD
)) {
1392 git_error_set(GIT_ERROR_OS
, "failed to add a header to the request");
1396 if ((error
= send_request(s
, 0, true)) < 0)
1399 s
->sent_request
= 1;
1402 if (len
> CACHED_POST_BODY_BUF_SIZE
) {
1403 /* Flush, if necessary */
1404 if (s
->chunk_buffer_len
> 0) {
1405 if (write_chunk(s
->request
, s
->chunk_buffer
, s
->chunk_buffer_len
) < 0)
1408 s
->chunk_buffer_len
= 0;
1411 /* Write chunk directly */
1412 if (write_chunk(s
->request
, buffer
, len
) < 0)
1416 /* Append as much to the buffer as we can */
1417 int count
= (int)min(CACHED_POST_BODY_BUF_SIZE
- s
->chunk_buffer_len
, len
);
1419 if (!s
->chunk_buffer
) {
1420 s
->chunk_buffer
= git__malloc(CACHED_POST_BODY_BUF_SIZE
);
1421 GIT_ERROR_CHECK_ALLOC(s
->chunk_buffer
);
1424 memcpy(s
->chunk_buffer
+ s
->chunk_buffer_len
, buffer
, count
);
1425 s
->chunk_buffer_len
+= count
;
1429 /* Is the buffer full? If so, then flush */
1430 if (CACHED_POST_BODY_BUF_SIZE
== s
->chunk_buffer_len
) {
1431 if (write_chunk(s
->request
, s
->chunk_buffer
, s
->chunk_buffer_len
) < 0)
1434 s
->chunk_buffer_len
= 0;
1436 /* Is there any remaining data from the source? */
1438 memcpy(s
->chunk_buffer
, buffer
, len
);
1439 s
->chunk_buffer_len
= (unsigned int)len
;
1447 static void winhttp_stream_free(git_smart_subtransport_stream
*stream
)
1449 winhttp_stream
*s
= (winhttp_stream
*)stream
;
1451 winhttp_stream_close(s
);
1455 static int winhttp_stream_alloc(winhttp_subtransport
*t
, winhttp_stream
**stream
)
1462 s
= git__calloc(1, sizeof(winhttp_stream
));
1463 GIT_ERROR_CHECK_ALLOC(s
);
1465 s
->parent
.subtransport
= &t
->parent
;
1466 s
->parent
.read
= winhttp_stream_read
;
1467 s
->parent
.write
= winhttp_stream_write_single
;
1468 s
->parent
.free
= winhttp_stream_free
;
1475 static int winhttp_uploadpack_ls(
1476 winhttp_subtransport
*t
,
1481 s
->service
= upload_pack_service
;
1482 s
->service_url
= upload_pack_ls_service_url
;
1488 static int winhttp_uploadpack(
1489 winhttp_subtransport
*t
,
1494 s
->service
= upload_pack_service
;
1495 s
->service_url
= upload_pack_service_url
;
1496 s
->verb
= post_verb
;
1501 static int winhttp_receivepack_ls(
1502 winhttp_subtransport
*t
,
1507 s
->service
= receive_pack_service
;
1508 s
->service_url
= receive_pack_ls_service_url
;
1514 static int winhttp_receivepack(
1515 winhttp_subtransport
*t
,
1520 /* WinHTTP only supports Transfer-Encoding: chunked
1521 * on Windows Vista (NT 6.0) and higher. */
1522 s
->chunked
= git_has_win32_version(6, 0, 0);
1525 s
->parent
.write
= winhttp_stream_write_chunked
;
1527 s
->parent
.write
= winhttp_stream_write_buffered
;
1529 s
->service
= receive_pack_service
;
1530 s
->service_url
= receive_pack_service_url
;
1531 s
->verb
= post_verb
;
1536 static int winhttp_action(
1537 git_smart_subtransport_stream
**stream
,
1538 git_smart_subtransport
*subtransport
,
1540 git_smart_service_t action
)
1542 winhttp_subtransport
*t
= (winhttp_subtransport
*)subtransport
;
1547 if ((ret
= git_net_url_parse(&t
->server
.url
, url
)) < 0 ||
1548 (ret
= winhttp_connect(t
)) < 0)
1551 if (winhttp_stream_alloc(t
, &s
) < 0)
1559 case GIT_SERVICE_UPLOADPACK_LS
:
1560 ret
= winhttp_uploadpack_ls(t
, s
);
1563 case GIT_SERVICE_UPLOADPACK
:
1564 ret
= winhttp_uploadpack(t
, s
);
1567 case GIT_SERVICE_RECEIVEPACK_LS
:
1568 ret
= winhttp_receivepack_ls(t
, s
);
1571 case GIT_SERVICE_RECEIVEPACK
:
1572 ret
= winhttp_receivepack(t
, s
);
1580 *stream
= &s
->parent
;
1585 static int winhttp_close(git_smart_subtransport
*subtransport
)
1587 winhttp_subtransport
*t
= (winhttp_subtransport
*)subtransport
;
1589 git_net_url_dispose(&t
->server
.url
);
1590 git_net_url_dispose(&t
->proxy
.url
);
1592 if (t
->server
.cred
) {
1593 t
->server
.cred
->free(t
->server
.cred
);
1594 t
->server
.cred
= NULL
;
1597 if (t
->proxy
.cred
) {
1598 t
->proxy
.cred
->free(t
->proxy
.cred
);
1599 t
->proxy
.cred
= NULL
;
1602 return winhttp_close_connection(t
);
1605 static void winhttp_free(git_smart_subtransport
*subtransport
)
1607 winhttp_subtransport
*t
= (winhttp_subtransport
*)subtransport
;
1609 winhttp_close(subtransport
);
1614 int git_smart_subtransport_http(git_smart_subtransport
**out
, git_transport
*owner
, void *param
)
1616 winhttp_subtransport
*t
;
1623 t
= git__calloc(1, sizeof(winhttp_subtransport
));
1624 GIT_ERROR_CHECK_ALLOC(t
);
1626 t
->owner
= (transport_smart
*)owner
;
1627 t
->parent
.action
= winhttp_action
;
1628 t
->parent
.close
= winhttp_close
;
1629 t
->parent
.free
= winhttp_free
;
1631 *out
= (git_smart_subtransport
*) t
;
1635 #endif /* GIT_WINHTTP */