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"
21 #include "git2/sys/credential.h"
26 /* For IInternetSecurityManager zone check */
30 #define WIDEN2(s) L ## s
31 #define WIDEN(s) WIDEN2(s)
33 #define MAX_CONTENT_TYPE_LEN 100
34 #define WINHTTP_OPTION_PEERDIST_EXTENSION_STATE 109
35 #define CACHED_POST_BODY_BUF_SIZE 4096
36 #define UUID_LENGTH_CCH 32
37 #define TIMEOUT_INFINITE -1
38 #define DEFAULT_CONNECT_TIMEOUT 60000
39 #ifndef WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH
40 #define WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH 0
43 #ifndef WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1
44 # define WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1 0x00000200
47 #ifndef WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2
48 # define WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2 0x00000800
51 #ifndef WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_3
52 # define WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_3 0x00002000
55 #ifndef WINHTTP_NO_CLIENT_CERT_CONTEXT
56 # define WINHTTP_NO_CLIENT_CERT_CONTEXT NULL
59 #ifndef HTTP_STATUS_PERMANENT_REDIRECT
60 # define HTTP_STATUS_PERMANENT_REDIRECT 308
64 # define DWORD_MAX 0xffffffff
67 bool git_http__expect_continue
= false;
69 static const char *prefix_https
= "https://";
70 static const char *upload_pack_service
= "upload-pack";
71 static const char *upload_pack_ls_service_url
= "/info/refs?service=git-upload-pack";
72 static const char *upload_pack_service_url
= "/git-upload-pack";
73 static const char *receive_pack_service
= "receive-pack";
74 static const char *receive_pack_ls_service_url
= "/info/refs?service=git-receive-pack";
75 static const char *receive_pack_service_url
= "/git-receive-pack";
76 static const wchar_t *get_verb
= L
"GET";
77 static const wchar_t *post_verb
= L
"POST";
78 static const wchar_t *pragma_nocache
= L
"Pragma: no-cache";
79 static const wchar_t *transfer_encoding
= L
"Transfer-Encoding: chunked";
80 static const int no_check_cert_flags
= SECURITY_FLAG_IGNORE_CERT_CN_INVALID
|
81 SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
|
82 SECURITY_FLAG_IGNORE_UNKNOWN_CA
;
84 #if defined(__MINGW32__)
85 static const CLSID CLSID_InternetSecurityManager_mingw
=
86 { 0x7B8A2D94, 0x0AC9, 0x11D1,
87 { 0x89, 0x6C, 0x00, 0xC0, 0x4F, 0xB6, 0xBF, 0xC4 } };
88 static const IID IID_IInternetSecurityManager_mingw
=
89 { 0x79EAC9EE, 0xBAF9, 0x11CE,
90 { 0x8C, 0x82, 0x00, 0xAA, 0x00, 0x4B, 0xA9, 0x0B } };
92 # define CLSID_InternetSecurityManager CLSID_InternetSecurityManager_mingw
93 # define IID_IInternetSecurityManager IID_IInternetSecurityManager_mingw
96 #define OWNING_SUBTRANSPORT(s) ((winhttp_subtransport *)(s)->parent.subtransport)
99 GIT_WINHTTP_AUTH_BASIC
= 1,
100 GIT_WINHTTP_AUTH_NTLM
= 2,
101 GIT_WINHTTP_AUTH_NEGOTIATE
= 4,
102 GIT_WINHTTP_AUTH_DIGEST
= 8,
103 } winhttp_authmechanism_t
;
106 git_smart_subtransport_stream parent
;
108 const char *service_url
;
111 wchar_t *request_uri
;
113 unsigned chunk_buffer_len
;
116 unsigned sent_request
: 1,
117 received_response
: 1,
119 status_sending_request_reached
: 1;
124 git_credential
*cred
;
126 bool url_cred_presented
;
130 git_smart_subtransport parent
;
131 transport_smart
*owner
;
133 winhttp_server server
;
134 winhttp_server proxy
;
137 HINTERNET connection
;
138 } winhttp_subtransport
;
140 static int apply_userpass_credentials(HINTERNET request
, DWORD target
, int mechanisms
, git_credential
*cred
)
142 git_credential_userpass_plaintext
*c
= (git_credential_userpass_plaintext
*)cred
;
143 wchar_t *user
= NULL
, *pass
= NULL
;
144 int user_len
= 0, pass_len
= 0, error
= 0;
147 if (mechanisms
& GIT_WINHTTP_AUTH_NEGOTIATE
) {
148 native_scheme
= WINHTTP_AUTH_SCHEME_NEGOTIATE
;
149 } else if (mechanisms
& GIT_WINHTTP_AUTH_NTLM
) {
150 native_scheme
= WINHTTP_AUTH_SCHEME_NTLM
;
151 } else if (mechanisms
& GIT_WINHTTP_AUTH_DIGEST
) {
152 native_scheme
= WINHTTP_AUTH_SCHEME_DIGEST
;
153 } else if (mechanisms
& GIT_WINHTTP_AUTH_BASIC
) {
154 native_scheme
= WINHTTP_AUTH_SCHEME_BASIC
;
156 git_error_set(GIT_ERROR_HTTP
, "invalid authentication scheme");
161 if ((error
= user_len
= git__utf8_to_16_alloc(&user
, c
->username
)) < 0)
164 if ((error
= pass_len
= git__utf8_to_16_alloc(&pass
, c
->password
)) < 0)
167 if (!WinHttpSetCredentials(request
, target
, native_scheme
, user
, pass
, NULL
)) {
168 git_error_set(GIT_ERROR_OS
, "failed to set credentials");
174 git__memzero(user
, user_len
* sizeof(wchar_t));
177 git__memzero(pass
, pass_len
* sizeof(wchar_t));
185 static int apply_default_credentials(HINTERNET request
, DWORD target
, int mechanisms
)
187 DWORD autologon_level
= WINHTTP_AUTOLOGON_SECURITY_LEVEL_LOW
;
188 DWORD native_scheme
= 0;
190 if ((mechanisms
& GIT_WINHTTP_AUTH_NEGOTIATE
) != 0) {
191 native_scheme
= WINHTTP_AUTH_SCHEME_NEGOTIATE
;
192 } else if ((mechanisms
& GIT_WINHTTP_AUTH_NTLM
) != 0) {
193 native_scheme
= WINHTTP_AUTH_SCHEME_NTLM
;
195 git_error_set(GIT_ERROR_HTTP
, "invalid authentication scheme");
200 * Autologon policy must be "low" to use default creds.
201 * This is safe as the user has explicitly requested it.
203 if (!WinHttpSetOption(request
, WINHTTP_OPTION_AUTOLOGON_POLICY
, &autologon_level
, sizeof(DWORD
))) {
204 git_error_set(GIT_ERROR_OS
, "could not configure logon policy");
208 if (!WinHttpSetCredentials(request
, target
, native_scheme
, NULL
, NULL
, NULL
)) {
209 git_error_set(GIT_ERROR_OS
, "could not configure credentials");
216 static int acquire_url_cred(
217 git_credential
**cred
,
218 unsigned int allowed_types
,
219 const char *username
,
220 const char *password
)
222 if (allowed_types
& GIT_CREDENTIAL_USERPASS_PLAINTEXT
)
223 return git_credential_userpass_plaintext_new(cred
, username
, password
);
225 if ((allowed_types
& GIT_CREDENTIAL_DEFAULT
) && *username
== '\0' && *password
== '\0')
226 return git_credential_default_new(cred
);
231 static int acquire_fallback_cred(
232 git_credential
**cred
,
234 unsigned int allowed_types
)
238 /* If the target URI supports integrated Windows authentication
239 * as an authentication mechanism */
240 if (GIT_CREDENTIAL_DEFAULT
& allowed_types
) {
242 HRESULT hCoInitResult
;
244 /* Convert URL to wide characters */
245 if (git__utf8_to_16_alloc(&wide_url
, url
) < 0) {
246 git_error_set(GIT_ERROR_OS
, "failed to convert string to wide form");
250 hCoInitResult
= CoInitializeEx(NULL
, COINIT_MULTITHREADED
);
252 if (SUCCEEDED(hCoInitResult
) || hCoInitResult
== RPC_E_CHANGED_MODE
) {
253 IInternetSecurityManager
*pISM
;
255 /* And if the target URI is in the My Computer, Intranet, or Trusted zones */
256 if (SUCCEEDED(CoCreateInstance(&CLSID_InternetSecurityManager
, NULL
,
257 CLSCTX_ALL
, &IID_IInternetSecurityManager
, (void **)&pISM
))) {
260 if (SUCCEEDED(pISM
->lpVtbl
->MapUrlToZone(pISM
, wide_url
, &dwZone
, 0)) &&
261 (URLZONE_LOCAL_MACHINE
== dwZone
||
262 URLZONE_INTRANET
== dwZone
||
263 URLZONE_TRUSTED
== dwZone
)) {
264 git_credential
*existing
= *cred
;
267 existing
->free(existing
);
269 /* Then use default Windows credentials to authenticate this request */
270 error
= git_credential_default_new(cred
);
273 pISM
->lpVtbl
->Release(pISM
);
276 /* Only uninitialize if the call to CoInitializeEx was successful. */
277 if (SUCCEEDED(hCoInitResult
))
287 static int certificate_check(winhttp_stream
*s
, int valid
)
290 winhttp_subtransport
*t
= OWNING_SUBTRANSPORT(s
);
291 PCERT_CONTEXT cert_ctx
;
292 DWORD cert_ctx_size
= sizeof(cert_ctx
);
295 /* If there is no override, we should fail if WinHTTP doesn't think it's fine */
296 if (t
->owner
->certificate_check_cb
== NULL
&& !valid
) {
297 if (!git_error_last())
298 git_error_set(GIT_ERROR_HTTP
, "unknown certificate check failure");
300 return GIT_ECERTIFICATE
;
303 if (t
->owner
->certificate_check_cb
== NULL
|| git__strcmp(t
->server
.url
.scheme
, "https") != 0)
306 if (!WinHttpQueryOption(s
->request
, WINHTTP_OPTION_SERVER_CERT_CONTEXT
, &cert_ctx
, &cert_ctx_size
)) {
307 git_error_set(GIT_ERROR_OS
, "failed to get server certificate");
312 cert
.parent
.cert_type
= GIT_CERT_X509
;
313 cert
.data
= cert_ctx
->pbCertEncoded
;
314 cert
.len
= cert_ctx
->cbCertEncoded
;
315 error
= t
->owner
->certificate_check_cb((git_cert
*) &cert
, valid
, t
->server
.url
.host
, t
->owner
->message_cb_payload
);
316 CertFreeCertificateContext(cert_ctx
);
318 if (error
== GIT_PASSTHROUGH
)
319 error
= valid
? 0 : GIT_ECERTIFICATE
;
321 if (error
< 0 && !git_error_last())
322 git_error_set(GIT_ERROR_HTTP
, "user cancelled certificate check");
327 static void winhttp_stream_close(winhttp_stream
*s
)
329 if (s
->chunk_buffer
) {
330 git__free(s
->chunk_buffer
);
331 s
->chunk_buffer
= NULL
;
335 CloseHandle(s
->post_body
);
339 if (s
->request_uri
) {
340 git__free(s
->request_uri
);
341 s
->request_uri
= NULL
;
345 WinHttpCloseHandle(s
->request
);
352 static int apply_credentials(
356 git_credential
*creds
,
363 /* If we have creds, just apply them */
364 if (creds
&& creds
->credtype
== GIT_CREDENTIAL_USERPASS_PLAINTEXT
)
365 error
= apply_userpass_credentials(request
, target
, mechanisms
, creds
);
366 else if (creds
&& creds
->credtype
== GIT_CREDENTIAL_DEFAULT
)
367 error
= apply_default_credentials(request
, target
, mechanisms
);
372 static int winhttp_stream_connect(winhttp_stream
*s
)
374 winhttp_subtransport
*t
= OWNING_SUBTRANSPORT(s
);
375 git_buf buf
= GIT_BUF_INIT
;
376 char *proxy_url
= NULL
;
377 wchar_t ct
[MAX_CONTENT_TYPE_LEN
];
378 LPCWSTR types
[] = { L
"*/*", NULL
};
379 BOOL peerdist
= FALSE
;
381 unsigned long disable_redirects
= WINHTTP_DISABLE_REDIRECTS
;
382 int default_timeout
= TIMEOUT_INFINITE
;
383 int default_connect_timeout
= DEFAULT_CONNECT_TIMEOUT
;
384 DWORD autologon_policy
= WINHTTP_AUTOLOGON_SECURITY_LEVEL_HIGH
;
386 const char *service_url
= s
->service_url
;
388 const git_proxy_options
*proxy_opts
;
390 /* If path already ends in /, remove the leading slash from service_url */
391 if ((git__suffixcmp(t
->server
.url
.path
, "/") == 0) && (git__prefixcmp(service_url
, "/") == 0))
394 git_buf_printf(&buf
, "%s%s", t
->server
.url
.path
, service_url
);
396 if (git_buf_oom(&buf
))
399 /* Convert URL to wide characters */
400 if (git__utf8_to_16_alloc(&s
->request_uri
, git_buf_cstr(&buf
)) < 0) {
401 git_error_set(GIT_ERROR_OS
, "failed to convert string to wide form");
405 /* Establish request */
406 s
->request
= WinHttpOpenRequest(
413 git__strcmp(t
->server
.url
.scheme
, "https") == 0 ? WINHTTP_FLAG_SECURE
: 0);
416 git_error_set(GIT_ERROR_OS
, "failed to open request");
420 /* Never attempt default credentials; we'll provide them explicitly. */
421 if (!WinHttpSetOption(s
->request
, WINHTTP_OPTION_AUTOLOGON_POLICY
, &autologon_policy
, sizeof(DWORD
)))
424 if (!WinHttpSetTimeouts(s
->request
, default_timeout
, default_connect_timeout
, default_timeout
, default_timeout
)) {
425 git_error_set(GIT_ERROR_OS
, "failed to set timeouts for WinHTTP");
429 proxy_opts
= &t
->owner
->proxy
;
430 if (proxy_opts
->type
== GIT_PROXY_AUTO
) {
431 /* Set proxy if necessary */
432 if (git_remote__http_proxy(&proxy_url
, t
->owner
->owner
, &t
->server
.url
) < 0)
435 else if (proxy_opts
->type
== GIT_PROXY_SPECIFIED
) {
436 proxy_url
= git__strdup(proxy_opts
->url
);
437 GIT_ERROR_CHECK_ALLOC(proxy_url
);
441 git_buf processed_url
= GIT_BUF_INIT
;
442 WINHTTP_PROXY_INFO proxy_info
;
445 git_net_url_dispose(&t
->proxy
.url
);
447 if ((error
= git_net_url_parse(&t
->proxy
.url
, proxy_url
)) < 0)
450 if (strcmp(t
->proxy
.url
.scheme
, "http") != 0 && strcmp(t
->proxy
.url
.scheme
, "https") != 0) {
451 git_error_set(GIT_ERROR_HTTP
, "invalid URL: '%s'", proxy_url
);
456 git_buf_puts(&processed_url
, t
->proxy
.url
.scheme
);
457 git_buf_PUTS(&processed_url
, "://");
459 if (git_net_url_is_ipv6(&t
->proxy
.url
))
460 git_buf_putc(&processed_url
, '[');
462 git_buf_puts(&processed_url
, t
->proxy
.url
.host
);
464 if (git_net_url_is_ipv6(&t
->proxy
.url
))
465 git_buf_putc(&processed_url
, ']');
467 if (!git_net_url_is_default_port(&t
->proxy
.url
))
468 git_buf_printf(&processed_url
, ":%s", t
->proxy
.url
.port
);
470 if (git_buf_oom(&processed_url
)) {
475 /* Convert URL to wide characters */
476 error
= git__utf8_to_16_alloc(&proxy_wide
, processed_url
.ptr
);
477 git_buf_dispose(&processed_url
);
481 proxy_info
.dwAccessType
= WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
482 proxy_info
.lpszProxy
= proxy_wide
;
483 proxy_info
.lpszProxyBypass
= NULL
;
485 if (!WinHttpSetOption(s
->request
,
486 WINHTTP_OPTION_PROXY
,
488 sizeof(WINHTTP_PROXY_INFO
))) {
489 git_error_set(GIT_ERROR_OS
, "failed to set proxy");
490 git__free(proxy_wide
);
494 git__free(proxy_wide
);
496 if ((error
= apply_credentials(s
->request
, &t
->proxy
.url
, WINHTTP_AUTH_TARGET_PROXY
, t
->proxy
.cred
, t
->proxy
.auth_mechanisms
)) < 0)
500 /* Disable WinHTTP redirects so we can handle them manually. Why, you ask?
501 * http://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/b2ff8879-ab9f-4218-8f09-16d25dff87ae
503 if (!WinHttpSetOption(s
->request
,
504 WINHTTP_OPTION_DISABLE_FEATURE
,
506 sizeof(disable_redirects
))) {
507 git_error_set(GIT_ERROR_OS
, "failed to disable redirects");
512 /* Strip unwanted headers (X-P2P-PeerDist, X-P2P-PeerDistEx) that WinHTTP
513 * adds itself. This option may not be supported by the underlying
514 * platform, so we do not error-check it */
515 WinHttpSetOption(s
->request
,
516 WINHTTP_OPTION_PEERDIST_EXTENSION_STATE
,
520 /* Send Pragma: no-cache header */
521 if (!WinHttpAddRequestHeaders(s
->request
, pragma_nocache
, (ULONG
) -1L, WINHTTP_ADDREQ_FLAG_ADD
)) {
522 git_error_set(GIT_ERROR_OS
, "failed to add a header to the request");
526 if (post_verb
== s
->verb
) {
527 /* Send Content-Type and Accept headers -- only necessary on a POST */
529 if (git_buf_printf(&buf
,
530 "Content-Type: application/x-git-%s-request",
534 if (git__utf8_to_16(ct
, MAX_CONTENT_TYPE_LEN
, git_buf_cstr(&buf
)) < 0) {
535 git_error_set(GIT_ERROR_OS
, "failed to convert content-type to wide characters");
539 if (!WinHttpAddRequestHeaders(s
->request
, ct
, (ULONG
)-1L,
540 WINHTTP_ADDREQ_FLAG_ADD
| WINHTTP_ADDREQ_FLAG_REPLACE
)) {
541 git_error_set(GIT_ERROR_OS
, "failed to add a header to the request");
546 if (git_buf_printf(&buf
,
547 "Accept: application/x-git-%s-result",
551 if (git__utf8_to_16(ct
, MAX_CONTENT_TYPE_LEN
, git_buf_cstr(&buf
)) < 0) {
552 git_error_set(GIT_ERROR_OS
, "failed to convert accept header to wide characters");
556 if (!WinHttpAddRequestHeaders(s
->request
, ct
, (ULONG
)-1L,
557 WINHTTP_ADDREQ_FLAG_ADD
| WINHTTP_ADDREQ_FLAG_REPLACE
)) {
558 git_error_set(GIT_ERROR_OS
, "failed to add a header to the request");
563 for (i
= 0; i
< t
->owner
->custom_headers
.count
; i
++) {
564 if (t
->owner
->custom_headers
.strings
[i
]) {
566 git_buf_puts(&buf
, t
->owner
->custom_headers
.strings
[i
]);
567 if (git__utf8_to_16(ct
, MAX_CONTENT_TYPE_LEN
, git_buf_cstr(&buf
)) < 0) {
568 git_error_set(GIT_ERROR_OS
, "failed to convert custom header to wide characters");
572 if (!WinHttpAddRequestHeaders(s
->request
, ct
, (ULONG
)-1L,
573 WINHTTP_ADDREQ_FLAG_ADD
| WINHTTP_ADDREQ_FLAG_REPLACE
)) {
574 git_error_set(GIT_ERROR_OS
, "failed to add a header to the request");
580 /* If requested, disable certificate validation */
581 if (strcmp(t
->server
.url
.scheme
, "https") == 0) {
584 if (t
->owner
->parent
.read_flags(&t
->owner
->parent
, &flags
) < 0)
588 if ((error
= apply_credentials(s
->request
, &t
->server
.url
, WINHTTP_AUTH_TARGET_SERVER
, t
->server
.cred
, t
->server
.auth_mechanisms
)) < 0)
591 /* We've done everything up to calling WinHttpSendRequest. */
597 winhttp_stream_close(s
);
599 git__free(proxy_url
);
600 git_buf_dispose(&buf
);
604 static int parse_unauthorized_response(
606 int *allowed_mechanisms
,
609 DWORD supported
, first
, target
;
612 *allowed_mechanisms
= 0;
614 /* WinHttpQueryHeaders() must be called before WinHttpQueryAuthSchemes().
615 * We can assume this was already done, since we know we are unauthorized.
617 if (!WinHttpQueryAuthSchemes(request
, &supported
, &first
, &target
)) {
618 git_error_set(GIT_ERROR_OS
, "failed to parse supported auth schemes");
622 if (WINHTTP_AUTH_SCHEME_NTLM
& supported
) {
623 *allowed_types
|= GIT_CREDENTIAL_USERPASS_PLAINTEXT
;
624 *allowed_types
|= GIT_CREDENTIAL_DEFAULT
;
625 *allowed_mechanisms
|= GIT_WINHTTP_AUTH_NTLM
;
628 if (WINHTTP_AUTH_SCHEME_NEGOTIATE
& supported
) {
629 *allowed_types
|= GIT_CREDENTIAL_DEFAULT
;
630 *allowed_mechanisms
|= GIT_WINHTTP_AUTH_NEGOTIATE
;
633 if (WINHTTP_AUTH_SCHEME_BASIC
& supported
) {
634 *allowed_types
|= GIT_CREDENTIAL_USERPASS_PLAINTEXT
;
635 *allowed_mechanisms
|= GIT_WINHTTP_AUTH_BASIC
;
638 if (WINHTTP_AUTH_SCHEME_DIGEST
& supported
) {
639 *allowed_types
|= GIT_CREDENTIAL_USERPASS_PLAINTEXT
;
640 *allowed_mechanisms
|= GIT_WINHTTP_AUTH_DIGEST
;
646 static int write_chunk(HINTERNET request
, const char *buffer
, size_t len
)
649 git_buf buf
= GIT_BUF_INIT
;
652 git_buf_printf(&buf
, "%"PRIXZ
"\r\n", len
);
654 if (git_buf_oom(&buf
))
657 if (!WinHttpWriteData(request
,
658 git_buf_cstr(&buf
), (DWORD
)git_buf_len(&buf
),
660 git_buf_dispose(&buf
);
661 git_error_set(GIT_ERROR_OS
, "failed to write chunk header");
665 git_buf_dispose(&buf
);
668 if (!WinHttpWriteData(request
,
671 git_error_set(GIT_ERROR_OS
, "failed to write chunk");
676 if (!WinHttpWriteData(request
,
679 git_error_set(GIT_ERROR_OS
, "failed to write chunk footer");
686 static int winhttp_close_connection(winhttp_subtransport
*t
)
691 if (!WinHttpCloseHandle(t
->connection
)) {
692 git_error_set(GIT_ERROR_OS
, "unable to close connection");
696 t
->connection
= NULL
;
700 if (!WinHttpCloseHandle(t
->session
)) {
701 git_error_set(GIT_ERROR_OS
, "unable to close session");
711 static void CALLBACK
winhttp_status(
712 HINTERNET connection
,
720 GIT_UNUSED(connection
);
721 GIT_UNUSED(info_len
);
724 case WINHTTP_CALLBACK_STATUS_SECURE_FAILURE
:
725 status
= *((DWORD
*)info
);
727 if ((status
& WINHTTP_CALLBACK_STATUS_FLAG_CERT_CN_INVALID
))
728 git_error_set(GIT_ERROR_HTTP
, "SSL certificate issued for different common name");
729 else if ((status
& WINHTTP_CALLBACK_STATUS_FLAG_CERT_DATE_INVALID
))
730 git_error_set(GIT_ERROR_HTTP
, "SSL certificate has expired");
731 else if ((status
& WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CA
))
732 git_error_set(GIT_ERROR_HTTP
, "SSL certificate signed by unknown CA");
733 else if ((status
& WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CERT
))
734 git_error_set(GIT_ERROR_HTTP
, "SSL certificate is invalid");
735 else if ((status
& WINHTTP_CALLBACK_STATUS_FLAG_CERT_REV_FAILED
))
736 git_error_set(GIT_ERROR_HTTP
, "certificate revocation check failed");
737 else if ((status
& WINHTTP_CALLBACK_STATUS_FLAG_CERT_REVOKED
))
738 git_error_set(GIT_ERROR_HTTP
, "SSL certificate was revoked");
739 else if ((status
& WINHTTP_CALLBACK_STATUS_FLAG_SECURITY_CHANNEL_ERROR
))
740 git_error_set(GIT_ERROR_HTTP
, "security libraries could not be loaded");
742 git_error_set(GIT_ERROR_HTTP
, "unknown security error %lu", status
);
746 case WINHTTP_CALLBACK_STATUS_SENDING_REQUEST
:
747 ((winhttp_stream
*) ctx
)->status_sending_request_reached
= 1;
753 static int winhttp_connect(
754 winhttp_subtransport
*t
)
756 wchar_t *wide_host
= NULL
;
758 wchar_t *wide_ua
= NULL
;
759 git_buf ipv6
= GIT_BUF_INIT
, ua
= GIT_BUF_INIT
;
762 int default_timeout
= TIMEOUT_INFINITE
;
763 int default_connect_timeout
= DEFAULT_CONNECT_TIMEOUT
;
765 WINHTTP_FLAG_SECURE_PROTOCOL_TLS1
|
766 WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1
|
767 WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2
|
768 WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_3
;
771 t
->connection
= NULL
;
774 if (git__strntol32(&port
, t
->server
.url
.port
,
775 strlen(t
->server
.url
.port
), NULL
, 10) < 0)
778 /* IPv6? Add braces around the host. */
779 if (git_net_url_is_ipv6(&t
->server
.url
)) {
780 if (git_buf_printf(&ipv6
, "[%s]", t
->server
.url
.host
) < 0)
785 host
= t
->server
.url
.host
;
789 if (git__utf8_to_16_alloc(&wide_host
, host
) < 0) {
790 git_error_set(GIT_ERROR_OS
, "unable to convert host to wide characters");
795 if (git_http__user_agent(&ua
) < 0)
798 if (git__utf8_to_16_alloc(&wide_ua
, git_buf_cstr(&ua
)) < 0) {
799 git_error_set(GIT_ERROR_OS
, "unable to convert host to wide characters");
803 /* Establish session */
804 t
->session
= WinHttpOpen(
806 WINHTTP_ACCESS_TYPE_DEFAULT_PROXY
,
807 WINHTTP_NO_PROXY_NAME
,
808 WINHTTP_NO_PROXY_BYPASS
,
812 git_error_set(GIT_ERROR_OS
, "failed to init WinHTTP");
817 * Do a best-effort attempt to enable TLS 1.3 and 1.2 but allow this to
818 * fail; if TLS 1.2 or 1.3 support is not available for some reason,
819 * ignore the failure (it will keep the default protocols).
821 if (WinHttpSetOption(t
->session
,
822 WINHTTP_OPTION_SECURE_PROTOCOLS
,
824 sizeof(protocols
)) == FALSE
) {
825 protocols
&= ~WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_3
;
826 WinHttpSetOption(t
->session
,
827 WINHTTP_OPTION_SECURE_PROTOCOLS
,
832 if (!WinHttpSetTimeouts(t
->session
, default_timeout
, default_connect_timeout
, default_timeout
, default_timeout
)) {
833 git_error_set(GIT_ERROR_OS
, "failed to set timeouts for WinHTTP");
838 /* Establish connection */
839 t
->connection
= WinHttpConnect(
842 (INTERNET_PORT
) port
,
845 if (!t
->connection
) {
846 git_error_set(GIT_ERROR_OS
, "failed to connect to host");
850 if (WinHttpSetStatusCallback(
853 WINHTTP_CALLBACK_FLAG_SECURE_FAILURE
| WINHTTP_CALLBACK_FLAG_SEND_REQUEST
,
855 ) == WINHTTP_INVALID_STATUS_CALLBACK
) {
856 git_error_set(GIT_ERROR_OS
, "failed to set status callback");
864 winhttp_close_connection(t
);
866 git_buf_dispose(&ua
);
867 git_buf_dispose(&ipv6
);
868 git__free(wide_host
);
874 static int do_send_request(winhttp_stream
*s
, size_t len
, bool chunked
)
879 if (len
> DWORD_MAX
) {
880 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
884 for (attempts
= 0; attempts
< 5; attempts
++) {
886 success
= WinHttpSendRequest(s
->request
,
887 WINHTTP_NO_ADDITIONAL_HEADERS
, 0,
888 WINHTTP_NO_REQUEST_DATA
, 0,
889 WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH
, (DWORD_PTR
)s
);
891 success
= WinHttpSendRequest(s
->request
,
892 WINHTTP_NO_ADDITIONAL_HEADERS
, 0,
893 WINHTTP_NO_REQUEST_DATA
, 0,
894 (DWORD
)len
, (DWORD_PTR
)s
);
897 if (success
|| GetLastError() != (DWORD
)SEC_E_BUFFER_TOO_SMALL
)
901 return success
? 0 : -1;
904 static int send_request(winhttp_stream
*s
, size_t len
, bool chunked
)
906 int request_failed
= 1, error
, attempts
= 0;
907 DWORD ignore_flags
, send_request_error
;
911 while (request_failed
&& attempts
++ < 3) {
913 int client_cert_requested
= 0;
915 if ((error
= do_send_request(s
, len
, chunked
)) < 0) {
916 send_request_error
= GetLastError();
918 switch (send_request_error
) {
919 case ERROR_WINHTTP_SECURE_FAILURE
:
922 case ERROR_WINHTTP_CLIENT_AUTH_CERT_NEEDED
:
923 client_cert_requested
= 1;
926 git_error_set(GIT_ERROR_OS
, "failed to send request");
932 * Only check the certificate if we were able to reach the sending request phase, or
933 * received a secure failure error. Otherwise, the server certificate won't be available
934 * since the request wasn't able to complete (e.g. proxy auth required)
937 (!request_failed
&& s
->status_sending_request_reached
)) {
939 if ((error
= certificate_check(s
, cert_valid
)) < 0) {
940 if (!git_error_last())
941 git_error_set(GIT_ERROR_OS
, "user cancelled certificate check");
947 /* if neither the request nor the certificate check returned errors, we're done */
952 ignore_flags
= no_check_cert_flags
;
953 if (!WinHttpSetOption(s
->request
, WINHTTP_OPTION_SECURITY_FLAGS
, &ignore_flags
, sizeof(ignore_flags
))) {
954 git_error_set(GIT_ERROR_OS
, "failed to set security options");
959 if (client_cert_requested
) {
961 * Client certificates are not supported, explicitly tell the server that
962 * (it's possible a client certificate was requested but is not required)
964 if (!WinHttpSetOption(s
->request
, WINHTTP_OPTION_CLIENT_CERT_CONTEXT
, WINHTTP_NO_CLIENT_CERT_CONTEXT
, 0)) {
965 git_error_set(GIT_ERROR_OS
, "failed to set client cert context");
974 static int acquire_credentials(
976 winhttp_server
*server
,
978 git_credential_acquire_cb cred_cb
,
979 void *cred_cb_payload
)
984 if (parse_unauthorized_response(&allowed_types
, &server
->auth_mechanisms
, request
) < 0)
988 git_credential_free(server
->cred
);
991 /* Start with URL-specified credentials, if there were any. */
992 if (!server
->url_cred_presented
&& server
->url
.username
&& server
->url
.password
) {
993 error
= acquire_url_cred(&server
->cred
, allowed_types
, server
->url
.username
, server
->url
.password
);
994 server
->url_cred_presented
= 1;
1000 /* Next use the user-defined callback, if there is one. */
1001 if (error
> 0 && cred_cb
) {
1002 error
= cred_cb(&server
->cred
, url_str
, server
->url
.username
, allowed_types
, cred_cb_payload
);
1004 /* Treat GIT_PASSTHROUGH as though git_credential_acquire_cb isn't set */
1005 if (error
== GIT_PASSTHROUGH
)
1011 /* Finally, invoke the fallback default credential lookup. */
1013 error
= acquire_fallback_cred(&server
->cred
, url_str
, allowed_types
);
1021 * No error occurred but we could not find appropriate credentials.
1022 * This behaves like a pass-through.
1027 static int winhttp_stream_read(
1028 git_smart_subtransport_stream
*stream
,
1033 winhttp_stream
*s
= (winhttp_stream
*)stream
;
1034 winhttp_subtransport
*t
= OWNING_SUBTRANSPORT(s
);
1035 DWORD dw_bytes_read
;
1036 char replay_count
= 0;
1040 /* Enforce a reasonable cap on the number of replays */
1041 if (replay_count
++ >= GIT_HTTP_REPLAY_MAX
) {
1042 git_error_set(GIT_ERROR_HTTP
, "too many redirects or authentication replays");
1043 return GIT_ERROR
; /* not GIT_EAUTH because the exact cause is not clear */
1046 /* Connect if necessary */
1047 if (!s
->request
&& winhttp_stream_connect(s
) < 0)
1050 if (!s
->received_response
) {
1051 DWORD status_code
, status_code_length
, content_type_length
, bytes_written
;
1052 char expected_content_type_8
[MAX_CONTENT_TYPE_LEN
];
1053 wchar_t expected_content_type
[MAX_CONTENT_TYPE_LEN
], content_type
[MAX_CONTENT_TYPE_LEN
];
1055 if (!s
->sent_request
) {
1057 if ((error
= send_request(s
, s
->post_body_len
, false)) < 0)
1060 s
->sent_request
= 1;
1064 GIT_ASSERT(s
->verb
== post_verb
);
1066 /* Flush, if necessary */
1067 if (s
->chunk_buffer_len
> 0 &&
1068 write_chunk(s
->request
, s
->chunk_buffer
, s
->chunk_buffer_len
) < 0)
1071 s
->chunk_buffer_len
= 0;
1073 /* Write the final chunk. */
1074 if (!WinHttpWriteData(s
->request
,
1077 git_error_set(GIT_ERROR_OS
, "failed to write final chunk");
1081 else if (s
->post_body
) {
1083 DWORD len
= s
->post_body_len
, bytes_read
;
1085 if (INVALID_SET_FILE_POINTER
== SetFilePointer(s
->post_body
,
1086 0, 0, FILE_BEGIN
) &&
1087 NO_ERROR
!= GetLastError()) {
1088 git_error_set(GIT_ERROR_OS
, "failed to reset file pointer");
1092 buffer
= git__malloc(CACHED_POST_BODY_BUF_SIZE
);
1093 GIT_ERROR_CHECK_ALLOC(buffer
);
1096 DWORD bytes_written
;
1098 if (!ReadFile(s
->post_body
, buffer
,
1099 min(CACHED_POST_BODY_BUF_SIZE
, len
),
1100 &bytes_read
, NULL
) ||
1103 git_error_set(GIT_ERROR_OS
, "failed to read from temp file");
1107 if (!WinHttpWriteData(s
->request
, buffer
,
1108 bytes_read
, &bytes_written
)) {
1110 git_error_set(GIT_ERROR_OS
, "failed to write data");
1115 GIT_ASSERT(bytes_read
== bytes_written
);
1120 /* Eagerly close the temp file */
1121 CloseHandle(s
->post_body
);
1122 s
->post_body
= NULL
;
1125 if (!WinHttpReceiveResponse(s
->request
, 0)) {
1126 git_error_set(GIT_ERROR_OS
, "failed to receive response");
1130 /* Verify that we got a 200 back */
1131 status_code_length
= sizeof(status_code
);
1133 if (!WinHttpQueryHeaders(s
->request
,
1134 WINHTTP_QUERY_STATUS_CODE
| WINHTTP_QUERY_FLAG_NUMBER
,
1135 WINHTTP_HEADER_NAME_BY_INDEX
,
1136 &status_code
, &status_code_length
,
1137 WINHTTP_NO_HEADER_INDEX
)) {
1138 git_error_set(GIT_ERROR_OS
, "failed to retrieve status code");
1142 /* The implementation of WinHTTP prior to Windows 7 will not
1143 * redirect to an identical URI. Some Git hosters use self-redirects
1144 * as part of their DoS mitigation strategy. Check first to see if we
1145 * have a redirect status code, and that we haven't already streamed
1146 * a post body. (We can't replay a streamed POST.) */
1148 (HTTP_STATUS_MOVED
== status_code
||
1149 HTTP_STATUS_REDIRECT
== status_code
||
1150 (HTTP_STATUS_REDIRECT_METHOD
== status_code
&&
1151 get_verb
== s
->verb
) ||
1152 HTTP_STATUS_REDIRECT_KEEP_VERB
== status_code
||
1153 HTTP_STATUS_PERMANENT_REDIRECT
== status_code
)) {
1155 /* Check for Windows 7. This workaround is only necessary on
1156 * Windows Vista and earlier. Windows 7 is version 6.1. */
1158 DWORD location_length
;
1161 /* OK, fetch the Location header from the redirect. */
1162 if (WinHttpQueryHeaders(s
->request
,
1163 WINHTTP_QUERY_LOCATION
,
1164 WINHTTP_HEADER_NAME_BY_INDEX
,
1165 WINHTTP_NO_OUTPUT_BUFFER
,
1167 WINHTTP_NO_HEADER_INDEX
) ||
1168 GetLastError() != ERROR_INSUFFICIENT_BUFFER
) {
1169 git_error_set(GIT_ERROR_OS
, "failed to read Location header");
1173 location
= git__malloc(location_length
);
1174 GIT_ERROR_CHECK_ALLOC(location
);
1176 if (!WinHttpQueryHeaders(s
->request
,
1177 WINHTTP_QUERY_LOCATION
,
1178 WINHTTP_HEADER_NAME_BY_INDEX
,
1181 WINHTTP_NO_HEADER_INDEX
)) {
1182 git_error_set(GIT_ERROR_OS
, "failed to read Location header");
1183 git__free(location
);
1187 /* Convert the Location header to UTF-8 */
1188 if (git__utf16_to_8_alloc(&location8
, location
) < 0) {
1189 git_error_set(GIT_ERROR_OS
, "failed to convert Location header to UTF-8");
1190 git__free(location
);
1194 git__free(location
);
1196 /* Replay the request */
1197 winhttp_stream_close(s
);
1199 if (!git__prefixcmp_icase(location8
, prefix_https
)) {
1200 /* Upgrade to secure connection; disconnect and start over */
1201 if (git_net_url_apply_redirect(&t
->server
.url
, location8
, s
->service_url
) < 0) {
1202 git__free(location8
);
1206 winhttp_close_connection(t
);
1208 if (winhttp_connect(t
) < 0)
1212 git__free(location8
);
1216 /* Handle authentication failures */
1217 if (status_code
== HTTP_STATUS_DENIED
) {
1218 int error
= acquire_credentials(s
->request
,
1221 t
->owner
->cred_acquire_cb
,
1222 t
->owner
->cred_acquire_payload
);
1226 } else if (!error
) {
1227 GIT_ASSERT(t
->server
.cred
);
1228 winhttp_stream_close(s
);
1231 } else if (status_code
== HTTP_STATUS_PROXY_AUTH_REQ
) {
1232 int error
= acquire_credentials(s
->request
,
1234 t
->owner
->proxy
.url
,
1235 t
->owner
->proxy
.credentials
,
1236 t
->owner
->proxy
.payload
);
1240 } else if (!error
) {
1241 GIT_ASSERT(t
->proxy
.cred
);
1242 winhttp_stream_close(s
);
1247 if (HTTP_STATUS_OK
!= status_code
) {
1248 git_error_set(GIT_ERROR_HTTP
, "request failed with status code: %lu", status_code
);
1252 /* Verify that we got the correct content-type back */
1253 if (post_verb
== s
->verb
)
1254 p_snprintf(expected_content_type_8
, MAX_CONTENT_TYPE_LEN
, "application/x-git-%s-result", s
->service
);
1256 p_snprintf(expected_content_type_8
, MAX_CONTENT_TYPE_LEN
, "application/x-git-%s-advertisement", s
->service
);
1258 if (git__utf8_to_16(expected_content_type
, MAX_CONTENT_TYPE_LEN
, expected_content_type_8
) < 0) {
1259 git_error_set(GIT_ERROR_OS
, "failed to convert expected content-type to wide characters");
1263 content_type_length
= sizeof(content_type
);
1265 if (!WinHttpQueryHeaders(s
->request
,
1266 WINHTTP_QUERY_CONTENT_TYPE
,
1267 WINHTTP_HEADER_NAME_BY_INDEX
,
1268 &content_type
, &content_type_length
,
1269 WINHTTP_NO_HEADER_INDEX
)) {
1270 git_error_set(GIT_ERROR_OS
, "failed to retrieve response content-type");
1274 if (wcscmp(expected_content_type
, content_type
)) {
1275 git_error_set(GIT_ERROR_HTTP
, "received unexpected content-type");
1279 s
->received_response
= 1;
1282 if (!WinHttpReadData(s
->request
,
1287 git_error_set(GIT_ERROR_OS
, "failed to read data");
1291 *bytes_read
= dw_bytes_read
;
1296 static int winhttp_stream_write_single(
1297 git_smart_subtransport_stream
*stream
,
1301 winhttp_stream
*s
= (winhttp_stream
*)stream
;
1302 DWORD bytes_written
;
1305 if (!s
->request
&& winhttp_stream_connect(s
) < 0)
1308 /* This implementation of write permits only a single call. */
1309 if (s
->sent_request
) {
1310 git_error_set(GIT_ERROR_HTTP
, "subtransport configured for only one write");
1314 if ((error
= send_request(s
, len
, false)) < 0)
1317 s
->sent_request
= 1;
1319 if (!WinHttpWriteData(s
->request
,
1323 git_error_set(GIT_ERROR_OS
, "failed to write data");
1327 GIT_ASSERT((DWORD
)len
== bytes_written
);
1332 static int put_uuid_string(LPWSTR buffer
, size_t buffer_len_cch
)
1335 RPC_STATUS status
= UuidCreate(&uuid
);
1338 if (RPC_S_OK
!= status
&&
1339 RPC_S_UUID_LOCAL_ONLY
!= status
&&
1340 RPC_S_UUID_NO_ADDRESS
!= status
) {
1341 git_error_set(GIT_ERROR_HTTP
, "unable to generate name for temp file");
1345 if (buffer_len_cch
< UUID_LENGTH_CCH
+ 1) {
1346 git_error_set(GIT_ERROR_HTTP
, "buffer too small for name of temp file");
1350 #if !defined(__MINGW32__) || defined(MINGW_HAS_SECURE_API)
1351 result
= swprintf_s(buffer
, buffer_len_cch
,
1353 result
= wsprintfW(buffer
,
1355 L
"%08x%04x%04x%02x%02x%02x%02x%02x%02x%02x%02x",
1356 uuid
.Data1
, uuid
.Data2
, uuid
.Data3
,
1357 uuid
.Data4
[0], uuid
.Data4
[1], uuid
.Data4
[2], uuid
.Data4
[3],
1358 uuid
.Data4
[4], uuid
.Data4
[5], uuid
.Data4
[6], uuid
.Data4
[7]);
1360 if (result
< UUID_LENGTH_CCH
) {
1361 git_error_set(GIT_ERROR_OS
, "unable to generate name for temp file");
1368 static int get_temp_file(LPWSTR buffer
, DWORD buffer_len_cch
)
1372 if (!GetTempPathW(buffer_len_cch
, buffer
)) {
1373 git_error_set(GIT_ERROR_OS
, "failed to get temp path");
1377 len
= wcslen(buffer
);
1379 if (buffer
[len
- 1] != '\\' && len
< buffer_len_cch
)
1380 buffer
[len
++] = '\\';
1382 if (put_uuid_string(&buffer
[len
], (size_t)buffer_len_cch
- len
) < 0)
1388 static int winhttp_stream_write_buffered(
1389 git_smart_subtransport_stream
*stream
,
1393 winhttp_stream
*s
= (winhttp_stream
*)stream
;
1394 DWORD bytes_written
;
1396 if (!s
->request
&& winhttp_stream_connect(s
) < 0)
1399 /* Buffer the payload, using a temporary file so we delegate
1400 * memory management of the data to the operating system. */
1401 if (!s
->post_body
) {
1402 wchar_t temp_path
[MAX_PATH
+ 1];
1404 if (get_temp_file(temp_path
, MAX_PATH
+ 1) < 0)
1407 s
->post_body
= CreateFileW(temp_path
,
1408 GENERIC_READ
| GENERIC_WRITE
,
1409 FILE_SHARE_DELETE
, NULL
,
1411 FILE_ATTRIBUTE_TEMPORARY
| FILE_FLAG_DELETE_ON_CLOSE
| FILE_FLAG_SEQUENTIAL_SCAN
,
1414 if (INVALID_HANDLE_VALUE
== s
->post_body
) {
1415 s
->post_body
= NULL
;
1416 git_error_set(GIT_ERROR_OS
, "failed to create temporary file");
1421 if (!WriteFile(s
->post_body
, buffer
, (DWORD
)len
, &bytes_written
, NULL
)) {
1422 git_error_set(GIT_ERROR_OS
, "failed to write to temporary file");
1426 GIT_ASSERT((DWORD
)len
== bytes_written
);
1428 s
->post_body_len
+= bytes_written
;
1433 static int winhttp_stream_write_chunked(
1434 git_smart_subtransport_stream
*stream
,
1438 winhttp_stream
*s
= (winhttp_stream
*)stream
;
1441 if (!s
->request
&& winhttp_stream_connect(s
) < 0)
1444 if (!s
->sent_request
) {
1445 /* Send Transfer-Encoding: chunked header */
1446 if (!WinHttpAddRequestHeaders(s
->request
,
1447 transfer_encoding
, (ULONG
) -1L,
1448 WINHTTP_ADDREQ_FLAG_ADD
)) {
1449 git_error_set(GIT_ERROR_OS
, "failed to add a header to the request");
1453 if ((error
= send_request(s
, 0, true)) < 0)
1456 s
->sent_request
= 1;
1459 if (len
> CACHED_POST_BODY_BUF_SIZE
) {
1460 /* Flush, if necessary */
1461 if (s
->chunk_buffer_len
> 0) {
1462 if (write_chunk(s
->request
, s
->chunk_buffer
, s
->chunk_buffer_len
) < 0)
1465 s
->chunk_buffer_len
= 0;
1468 /* Write chunk directly */
1469 if (write_chunk(s
->request
, buffer
, len
) < 0)
1473 /* Append as much to the buffer as we can */
1474 int count
= (int)min(CACHED_POST_BODY_BUF_SIZE
- s
->chunk_buffer_len
, len
);
1476 if (!s
->chunk_buffer
) {
1477 s
->chunk_buffer
= git__malloc(CACHED_POST_BODY_BUF_SIZE
);
1478 GIT_ERROR_CHECK_ALLOC(s
->chunk_buffer
);
1481 memcpy(s
->chunk_buffer
+ s
->chunk_buffer_len
, buffer
, count
);
1482 s
->chunk_buffer_len
+= count
;
1486 /* Is the buffer full? If so, then flush */
1487 if (CACHED_POST_BODY_BUF_SIZE
== s
->chunk_buffer_len
) {
1488 if (write_chunk(s
->request
, s
->chunk_buffer
, s
->chunk_buffer_len
) < 0)
1491 s
->chunk_buffer_len
= 0;
1493 /* Is there any remaining data from the source? */
1495 memcpy(s
->chunk_buffer
, buffer
, len
);
1496 s
->chunk_buffer_len
= (unsigned int)len
;
1504 static void winhttp_stream_free(git_smart_subtransport_stream
*stream
)
1506 winhttp_stream
*s
= (winhttp_stream
*)stream
;
1508 winhttp_stream_close(s
);
1512 static int winhttp_stream_alloc(winhttp_subtransport
*t
, winhttp_stream
**stream
)
1519 s
= git__calloc(1, sizeof(winhttp_stream
));
1520 GIT_ERROR_CHECK_ALLOC(s
);
1522 s
->parent
.subtransport
= &t
->parent
;
1523 s
->parent
.read
= winhttp_stream_read
;
1524 s
->parent
.write
= winhttp_stream_write_single
;
1525 s
->parent
.free
= winhttp_stream_free
;
1532 static int winhttp_uploadpack_ls(
1533 winhttp_subtransport
*t
,
1538 s
->service
= upload_pack_service
;
1539 s
->service_url
= upload_pack_ls_service_url
;
1545 static int winhttp_uploadpack(
1546 winhttp_subtransport
*t
,
1551 s
->service
= upload_pack_service
;
1552 s
->service_url
= upload_pack_service_url
;
1553 s
->verb
= post_verb
;
1558 static int winhttp_receivepack_ls(
1559 winhttp_subtransport
*t
,
1564 s
->service
= receive_pack_service
;
1565 s
->service_url
= receive_pack_ls_service_url
;
1571 static int winhttp_receivepack(
1572 winhttp_subtransport
*t
,
1577 /* WinHTTP only supports Transfer-Encoding: chunked
1578 * on Windows Vista (NT 6.0) and higher. */
1579 s
->chunked
= git_has_win32_version(6, 0, 0);
1582 s
->parent
.write
= winhttp_stream_write_chunked
;
1584 s
->parent
.write
= winhttp_stream_write_buffered
;
1586 s
->service
= receive_pack_service
;
1587 s
->service_url
= receive_pack_service_url
;
1588 s
->verb
= post_verb
;
1593 static int winhttp_action(
1594 git_smart_subtransport_stream
**stream
,
1595 git_smart_subtransport
*subtransport
,
1597 git_smart_service_t action
)
1599 winhttp_subtransport
*t
= (winhttp_subtransport
*)subtransport
;
1604 if ((ret
= git_net_url_parse(&t
->server
.url
, url
)) < 0 ||
1605 (ret
= winhttp_connect(t
)) < 0)
1608 if (winhttp_stream_alloc(t
, &s
) < 0)
1616 case GIT_SERVICE_UPLOADPACK_LS
:
1617 ret
= winhttp_uploadpack_ls(t
, s
);
1620 case GIT_SERVICE_UPLOADPACK
:
1621 ret
= winhttp_uploadpack(t
, s
);
1624 case GIT_SERVICE_RECEIVEPACK_LS
:
1625 ret
= winhttp_receivepack_ls(t
, s
);
1628 case GIT_SERVICE_RECEIVEPACK
:
1629 ret
= winhttp_receivepack(t
, s
);
1637 *stream
= &s
->parent
;
1642 static int winhttp_close(git_smart_subtransport
*subtransport
)
1644 winhttp_subtransport
*t
= (winhttp_subtransport
*)subtransport
;
1646 git_net_url_dispose(&t
->server
.url
);
1647 git_net_url_dispose(&t
->proxy
.url
);
1649 if (t
->server
.cred
) {
1650 t
->server
.cred
->free(t
->server
.cred
);
1651 t
->server
.cred
= NULL
;
1654 if (t
->proxy
.cred
) {
1655 t
->proxy
.cred
->free(t
->proxy
.cred
);
1656 t
->proxy
.cred
= NULL
;
1659 return winhttp_close_connection(t
);
1662 static void winhttp_free(git_smart_subtransport
*subtransport
)
1664 winhttp_subtransport
*t
= (winhttp_subtransport
*)subtransport
;
1666 winhttp_close(subtransport
);
1671 int git_smart_subtransport_http(git_smart_subtransport
**out
, git_transport
*owner
, void *param
)
1673 winhttp_subtransport
*t
;
1680 t
= git__calloc(1, sizeof(winhttp_subtransport
));
1681 GIT_ERROR_CHECK_ALLOC(t
);
1683 t
->owner
= (transport_smart
*)owner
;
1684 t
->parent
.action
= winhttp_action
;
1685 t
->parent
.close
= winhttp_close
;
1686 t
->parent
.free
= winhttp_free
;
1688 *out
= (git_smart_subtransport
*) t
;
1692 #endif /* GIT_WINHTTP */