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
->connect_opts
.callbacks
.certificate_check
== 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
->connect_opts
.callbacks
.certificate_check
== 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
->connect_opts
.callbacks
.certificate_check((git_cert
*) &cert
, valid
, t
->server
.url
.host
, t
->owner
->connect_opts
.callbacks
.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_str buf
= GIT_STR_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_str_printf(&buf
, "%s%s", t
->server
.url
.path
, service_url
);
396 if (git_str_oom(&buf
))
399 /* Convert URL to wide characters */
400 if (git__utf8_to_16_alloc(&s
->request_uri
, git_str_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
->connect_opts
.proxy_opts
;
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_str processed_url
= GIT_STR_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_str_puts(&processed_url
, t
->proxy
.url
.scheme
);
457 git_str_PUTS(&processed_url
, "://");
459 if (git_net_url_is_ipv6(&t
->proxy
.url
))
460 git_str_putc(&processed_url
, '[');
462 git_str_puts(&processed_url
, t
->proxy
.url
.host
);
464 if (git_net_url_is_ipv6(&t
->proxy
.url
))
465 git_str_putc(&processed_url
, ']');
467 if (!git_net_url_is_default_port(&t
->proxy
.url
))
468 git_str_printf(&processed_url
, ":%s", t
->proxy
.url
.port
);
470 if (git_str_oom(&processed_url
)) {
475 /* Convert URL to wide characters */
476 error
= git__utf8_to_16_alloc(&proxy_wide
, processed_url
.ptr
);
477 git_str_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_str_printf(&buf
,
530 "Content-Type: application/x-git-%s-request",
534 if (git__utf8_to_16(ct
, MAX_CONTENT_TYPE_LEN
, git_str_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_str_printf(&buf
,
547 "Accept: application/x-git-%s-result",
551 if (git__utf8_to_16(ct
, MAX_CONTENT_TYPE_LEN
, git_str_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
->connect_opts
.custom_headers
.count
; i
++) {
564 if (t
->owner
->connect_opts
.custom_headers
.strings
[i
]) {
566 git_str_puts(&buf
, t
->owner
->connect_opts
.custom_headers
.strings
[i
]);
567 if (git__utf8_to_16(ct
, MAX_CONTENT_TYPE_LEN
, git_str_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 ((error
= apply_credentials(s
->request
, &t
->server
.url
, WINHTTP_AUTH_TARGET_SERVER
, t
->server
.cred
, t
->server
.auth_mechanisms
)) < 0)
583 /* We've done everything up to calling WinHttpSendRequest. */
589 winhttp_stream_close(s
);
591 git__free(proxy_url
);
592 git_str_dispose(&buf
);
596 static int parse_unauthorized_response(
598 int *allowed_mechanisms
,
601 DWORD supported
, first
, target
;
604 *allowed_mechanisms
= 0;
606 /* WinHttpQueryHeaders() must be called before WinHttpQueryAuthSchemes().
607 * We can assume this was already done, since we know we are unauthorized.
609 if (!WinHttpQueryAuthSchemes(request
, &supported
, &first
, &target
)) {
610 git_error_set(GIT_ERROR_OS
, "failed to parse supported auth schemes");
614 if (WINHTTP_AUTH_SCHEME_NTLM
& supported
) {
615 *allowed_types
|= GIT_CREDENTIAL_USERPASS_PLAINTEXT
;
616 *allowed_types
|= GIT_CREDENTIAL_DEFAULT
;
617 *allowed_mechanisms
|= GIT_WINHTTP_AUTH_NTLM
;
620 if (WINHTTP_AUTH_SCHEME_NEGOTIATE
& supported
) {
621 *allowed_types
|= GIT_CREDENTIAL_DEFAULT
;
622 *allowed_mechanisms
|= GIT_WINHTTP_AUTH_NEGOTIATE
;
625 if (WINHTTP_AUTH_SCHEME_BASIC
& supported
) {
626 *allowed_types
|= GIT_CREDENTIAL_USERPASS_PLAINTEXT
;
627 *allowed_mechanisms
|= GIT_WINHTTP_AUTH_BASIC
;
630 if (WINHTTP_AUTH_SCHEME_DIGEST
& supported
) {
631 *allowed_types
|= GIT_CREDENTIAL_USERPASS_PLAINTEXT
;
632 *allowed_mechanisms
|= GIT_WINHTTP_AUTH_DIGEST
;
638 static int write_chunk(HINTERNET request
, const char *buffer
, size_t len
)
641 git_str buf
= GIT_STR_INIT
;
644 git_str_printf(&buf
, "%"PRIXZ
"\r\n", len
);
646 if (git_str_oom(&buf
))
649 if (!WinHttpWriteData(request
,
650 git_str_cstr(&buf
), (DWORD
)git_str_len(&buf
),
652 git_str_dispose(&buf
);
653 git_error_set(GIT_ERROR_OS
, "failed to write chunk header");
657 git_str_dispose(&buf
);
660 if (!WinHttpWriteData(request
,
663 git_error_set(GIT_ERROR_OS
, "failed to write chunk");
668 if (!WinHttpWriteData(request
,
671 git_error_set(GIT_ERROR_OS
, "failed to write chunk footer");
678 static int winhttp_close_connection(winhttp_subtransport
*t
)
683 if (!WinHttpCloseHandle(t
->connection
)) {
684 git_error_set(GIT_ERROR_OS
, "unable to close connection");
688 t
->connection
= NULL
;
692 if (!WinHttpCloseHandle(t
->session
)) {
693 git_error_set(GIT_ERROR_OS
, "unable to close session");
703 static void CALLBACK
winhttp_status(
704 HINTERNET connection
,
712 GIT_UNUSED(connection
);
713 GIT_UNUSED(info_len
);
716 case 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
);
738 case WINHTTP_CALLBACK_STATUS_SENDING_REQUEST
:
739 ((winhttp_stream
*) ctx
)->status_sending_request_reached
= 1;
745 static int winhttp_connect(
746 winhttp_subtransport
*t
)
748 wchar_t *wide_host
= NULL
;
750 wchar_t *wide_ua
= NULL
;
751 git_str ipv6
= GIT_STR_INIT
, ua
= GIT_STR_INIT
;
754 int default_timeout
= TIMEOUT_INFINITE
;
755 int default_connect_timeout
= DEFAULT_CONNECT_TIMEOUT
;
757 WINHTTP_FLAG_SECURE_PROTOCOL_TLS1
|
758 WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1
|
759 WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2
|
760 WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_3
;
763 t
->connection
= NULL
;
766 if (git__strntol32(&port
, t
->server
.url
.port
,
767 strlen(t
->server
.url
.port
), NULL
, 10) < 0)
770 /* IPv6? Add braces around the host. */
771 if (git_net_url_is_ipv6(&t
->server
.url
)) {
772 if (git_str_printf(&ipv6
, "[%s]", t
->server
.url
.host
) < 0)
777 host
= t
->server
.url
.host
;
781 if (git__utf8_to_16_alloc(&wide_host
, host
) < 0) {
782 git_error_set(GIT_ERROR_OS
, "unable to convert host to wide characters");
787 if (git_http__user_agent(&ua
) < 0)
790 if (git__utf8_to_16_alloc(&wide_ua
, git_str_cstr(&ua
)) < 0) {
791 git_error_set(GIT_ERROR_OS
, "unable to convert host to wide characters");
795 /* Establish session */
796 t
->session
= WinHttpOpen(
798 WINHTTP_ACCESS_TYPE_DEFAULT_PROXY
,
799 WINHTTP_NO_PROXY_NAME
,
800 WINHTTP_NO_PROXY_BYPASS
,
804 git_error_set(GIT_ERROR_OS
, "failed to init WinHTTP");
809 * Do a best-effort attempt to enable TLS 1.3 and 1.2 but allow this to
810 * fail; if TLS 1.2 or 1.3 support is not available for some reason,
811 * ignore the failure (it will keep the default protocols).
813 if (WinHttpSetOption(t
->session
,
814 WINHTTP_OPTION_SECURE_PROTOCOLS
,
816 sizeof(protocols
)) == FALSE
) {
817 protocols
&= ~WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_3
;
818 WinHttpSetOption(t
->session
,
819 WINHTTP_OPTION_SECURE_PROTOCOLS
,
824 if (!WinHttpSetTimeouts(t
->session
, default_timeout
, default_connect_timeout
, default_timeout
, default_timeout
)) {
825 git_error_set(GIT_ERROR_OS
, "failed to set timeouts for WinHTTP");
830 /* Establish connection */
831 t
->connection
= WinHttpConnect(
834 (INTERNET_PORT
) port
,
837 if (!t
->connection
) {
838 git_error_set(GIT_ERROR_OS
, "failed to connect to host");
842 if (WinHttpSetStatusCallback(
845 WINHTTP_CALLBACK_FLAG_SECURE_FAILURE
| WINHTTP_CALLBACK_FLAG_SEND_REQUEST
,
847 ) == WINHTTP_INVALID_STATUS_CALLBACK
) {
848 git_error_set(GIT_ERROR_OS
, "failed to set status callback");
856 winhttp_close_connection(t
);
858 git_str_dispose(&ua
);
859 git_str_dispose(&ipv6
);
860 git__free(wide_host
);
866 static int do_send_request(winhttp_stream
*s
, size_t len
, bool chunked
)
871 if (len
> DWORD_MAX
) {
872 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
876 for (attempts
= 0; attempts
< 5; attempts
++) {
878 success
= WinHttpSendRequest(s
->request
,
879 WINHTTP_NO_ADDITIONAL_HEADERS
, 0,
880 WINHTTP_NO_REQUEST_DATA
, 0,
881 WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH
, (DWORD_PTR
)s
);
883 success
= WinHttpSendRequest(s
->request
,
884 WINHTTP_NO_ADDITIONAL_HEADERS
, 0,
885 WINHTTP_NO_REQUEST_DATA
, 0,
886 (DWORD
)len
, (DWORD_PTR
)s
);
889 if (success
|| GetLastError() != (DWORD
)SEC_E_BUFFER_TOO_SMALL
)
893 return success
? 0 : -1;
896 static int send_request(winhttp_stream
*s
, size_t len
, bool chunked
)
898 int request_failed
= 1, error
, attempts
= 0;
899 DWORD ignore_flags
, send_request_error
;
903 while (request_failed
&& attempts
++ < 3) {
905 int client_cert_requested
= 0;
907 if ((error
= do_send_request(s
, len
, chunked
)) < 0) {
908 send_request_error
= GetLastError();
910 switch (send_request_error
) {
911 case ERROR_WINHTTP_SECURE_FAILURE
:
914 case ERROR_WINHTTP_CLIENT_AUTH_CERT_NEEDED
:
915 client_cert_requested
= 1;
918 git_error_set(GIT_ERROR_OS
, "failed to send request");
924 * Only check the certificate if we were able to reach the sending request phase, or
925 * received a secure failure error. Otherwise, the server certificate won't be available
926 * since the request wasn't able to complete (e.g. proxy auth required)
929 (!request_failed
&& s
->status_sending_request_reached
)) {
931 if ((error
= certificate_check(s
, cert_valid
)) < 0) {
932 if (!git_error_last())
933 git_error_set(GIT_ERROR_OS
, "user cancelled certificate check");
939 /* if neither the request nor the certificate check returned errors, we're done */
944 ignore_flags
= no_check_cert_flags
;
945 if (!WinHttpSetOption(s
->request
, WINHTTP_OPTION_SECURITY_FLAGS
, &ignore_flags
, sizeof(ignore_flags
))) {
946 git_error_set(GIT_ERROR_OS
, "failed to set security options");
951 if (client_cert_requested
) {
953 * Client certificates are not supported, explicitly tell the server that
954 * (it's possible a client certificate was requested but is not required)
956 if (!WinHttpSetOption(s
->request
, WINHTTP_OPTION_CLIENT_CERT_CONTEXT
, WINHTTP_NO_CLIENT_CERT_CONTEXT
, 0)) {
957 git_error_set(GIT_ERROR_OS
, "failed to set client cert context");
966 static int acquire_credentials(
968 winhttp_server
*server
,
970 git_credential_acquire_cb cred_cb
,
971 void *cred_cb_payload
)
976 if (parse_unauthorized_response(&allowed_types
, &server
->auth_mechanisms
, request
) < 0)
980 git_credential_free(server
->cred
);
983 /* Start with URL-specified credentials, if there were any. */
984 if (!server
->url_cred_presented
&& server
->url
.username
&& server
->url
.password
) {
985 error
= acquire_url_cred(&server
->cred
, allowed_types
, server
->url
.username
, server
->url
.password
);
986 server
->url_cred_presented
= 1;
992 /* Next use the user-defined callback, if there is one. */
993 if (error
> 0 && cred_cb
) {
994 error
= cred_cb(&server
->cred
, url_str
, server
->url
.username
, allowed_types
, cred_cb_payload
);
996 /* Treat GIT_PASSTHROUGH as though git_credential_acquire_cb isn't set */
997 if (error
== GIT_PASSTHROUGH
)
1003 /* Finally, invoke the fallback default credential lookup. */
1005 error
= acquire_fallback_cred(&server
->cred
, url_str
, allowed_types
);
1013 * No error occurred but we could not find appropriate credentials.
1014 * This behaves like a pass-through.
1019 static int winhttp_stream_read(
1020 git_smart_subtransport_stream
*stream
,
1025 winhttp_stream
*s
= (winhttp_stream
*)stream
;
1026 winhttp_subtransport
*t
= OWNING_SUBTRANSPORT(s
);
1027 DWORD dw_bytes_read
;
1028 char replay_count
= 0;
1032 /* Enforce a reasonable cap on the number of replays */
1033 if (replay_count
++ >= GIT_HTTP_REPLAY_MAX
) {
1034 git_error_set(GIT_ERROR_HTTP
, "too many redirects or authentication replays");
1035 return GIT_ERROR
; /* not GIT_EAUTH because the exact cause is not clear */
1038 /* Connect if necessary */
1039 if (!s
->request
&& winhttp_stream_connect(s
) < 0)
1042 if (!s
->received_response
) {
1043 DWORD status_code
, status_code_length
, content_type_length
, bytes_written
;
1044 char expected_content_type_8
[MAX_CONTENT_TYPE_LEN
];
1045 wchar_t expected_content_type
[MAX_CONTENT_TYPE_LEN
], content_type
[MAX_CONTENT_TYPE_LEN
];
1047 if (!s
->sent_request
) {
1049 if ((error
= send_request(s
, s
->post_body_len
, false)) < 0)
1052 s
->sent_request
= 1;
1056 GIT_ASSERT(s
->verb
== post_verb
);
1058 /* Flush, if necessary */
1059 if (s
->chunk_buffer_len
> 0 &&
1060 write_chunk(s
->request
, s
->chunk_buffer
, s
->chunk_buffer_len
) < 0)
1063 s
->chunk_buffer_len
= 0;
1065 /* Write the final chunk. */
1066 if (!WinHttpWriteData(s
->request
,
1069 git_error_set(GIT_ERROR_OS
, "failed to write final chunk");
1073 else if (s
->post_body
) {
1075 DWORD len
= s
->post_body_len
, bytes_read
;
1077 if (INVALID_SET_FILE_POINTER
== SetFilePointer(s
->post_body
,
1078 0, 0, FILE_BEGIN
) &&
1079 NO_ERROR
!= GetLastError()) {
1080 git_error_set(GIT_ERROR_OS
, "failed to reset file pointer");
1084 buffer
= git__malloc(CACHED_POST_BODY_BUF_SIZE
);
1085 GIT_ERROR_CHECK_ALLOC(buffer
);
1088 DWORD bytes_written
;
1090 if (!ReadFile(s
->post_body
, buffer
,
1091 min(CACHED_POST_BODY_BUF_SIZE
, len
),
1092 &bytes_read
, NULL
) ||
1095 git_error_set(GIT_ERROR_OS
, "failed to read from temp file");
1099 if (!WinHttpWriteData(s
->request
, buffer
,
1100 bytes_read
, &bytes_written
)) {
1102 git_error_set(GIT_ERROR_OS
, "failed to write data");
1107 GIT_ASSERT(bytes_read
== bytes_written
);
1112 /* Eagerly close the temp file */
1113 CloseHandle(s
->post_body
);
1114 s
->post_body
= NULL
;
1117 if (!WinHttpReceiveResponse(s
->request
, 0)) {
1118 git_error_set(GIT_ERROR_OS
, "failed to receive response");
1122 /* Verify that we got a 200 back */
1123 status_code_length
= sizeof(status_code
);
1125 if (!WinHttpQueryHeaders(s
->request
,
1126 WINHTTP_QUERY_STATUS_CODE
| WINHTTP_QUERY_FLAG_NUMBER
,
1127 WINHTTP_HEADER_NAME_BY_INDEX
,
1128 &status_code
, &status_code_length
,
1129 WINHTTP_NO_HEADER_INDEX
)) {
1130 git_error_set(GIT_ERROR_OS
, "failed to retrieve status code");
1134 /* The implementation of WinHTTP prior to Windows 7 will not
1135 * redirect to an identical URI. Some Git hosters use self-redirects
1136 * as part of their DoS mitigation strategy. Check first to see if we
1137 * have a redirect status code, and that we haven't already streamed
1138 * a post body. (We can't replay a streamed POST.) */
1140 (HTTP_STATUS_MOVED
== status_code
||
1141 HTTP_STATUS_REDIRECT
== status_code
||
1142 (HTTP_STATUS_REDIRECT_METHOD
== status_code
&&
1143 get_verb
== s
->verb
) ||
1144 HTTP_STATUS_REDIRECT_KEEP_VERB
== status_code
||
1145 HTTP_STATUS_PERMANENT_REDIRECT
== status_code
)) {
1147 /* Check for Windows 7. This workaround is only necessary on
1148 * Windows Vista and earlier. Windows 7 is version 6.1. */
1150 DWORD location_length
;
1153 /* OK, fetch the Location header from the redirect. */
1154 if (WinHttpQueryHeaders(s
->request
,
1155 WINHTTP_QUERY_LOCATION
,
1156 WINHTTP_HEADER_NAME_BY_INDEX
,
1157 WINHTTP_NO_OUTPUT_BUFFER
,
1159 WINHTTP_NO_HEADER_INDEX
) ||
1160 GetLastError() != ERROR_INSUFFICIENT_BUFFER
) {
1161 git_error_set(GIT_ERROR_OS
, "failed to read Location header");
1165 location
= git__malloc(location_length
);
1166 GIT_ERROR_CHECK_ALLOC(location
);
1168 if (!WinHttpQueryHeaders(s
->request
,
1169 WINHTTP_QUERY_LOCATION
,
1170 WINHTTP_HEADER_NAME_BY_INDEX
,
1173 WINHTTP_NO_HEADER_INDEX
)) {
1174 git_error_set(GIT_ERROR_OS
, "failed to read Location header");
1175 git__free(location
);
1179 /* Convert the Location header to UTF-8 */
1180 if (git__utf16_to_8_alloc(&location8
, location
) < 0) {
1181 git_error_set(GIT_ERROR_OS
, "failed to convert Location header to UTF-8");
1182 git__free(location
);
1186 git__free(location
);
1188 /* Replay the request */
1189 winhttp_stream_close(s
);
1191 if (!git__prefixcmp_icase(location8
, prefix_https
)) {
1192 bool follow
= (t
->owner
->connect_opts
.follow_redirects
!= GIT_REMOTE_REDIRECT_NONE
);
1194 /* Upgrade to secure connection; disconnect and start over */
1195 if (git_net_url_apply_redirect(&t
->server
.url
, location8
, follow
, s
->service_url
) < 0) {
1196 git__free(location8
);
1200 winhttp_close_connection(t
);
1202 if (winhttp_connect(t
) < 0)
1206 git__free(location8
);
1210 /* Handle authentication failures */
1211 if (status_code
== HTTP_STATUS_DENIED
) {
1212 int error
= acquire_credentials(s
->request
,
1215 t
->owner
->connect_opts
.callbacks
.credentials
,
1216 t
->owner
->connect_opts
.callbacks
.payload
);
1220 } else if (!error
) {
1221 GIT_ASSERT(t
->server
.cred
);
1222 winhttp_stream_close(s
);
1225 } else if (status_code
== HTTP_STATUS_PROXY_AUTH_REQ
) {
1226 int error
= acquire_credentials(s
->request
,
1228 t
->owner
->connect_opts
.proxy_opts
.url
,
1229 t
->owner
->connect_opts
.proxy_opts
.credentials
,
1230 t
->owner
->connect_opts
.proxy_opts
.payload
);
1234 } else if (!error
) {
1235 GIT_ASSERT(t
->proxy
.cred
);
1236 winhttp_stream_close(s
);
1241 if (HTTP_STATUS_OK
!= status_code
) {
1242 git_error_set(GIT_ERROR_HTTP
, "request failed with status code: %lu", status_code
);
1246 /* Verify that we got the correct content-type back */
1247 if (post_verb
== s
->verb
)
1248 p_snprintf(expected_content_type_8
, MAX_CONTENT_TYPE_LEN
, "application/x-git-%s-result", s
->service
);
1250 p_snprintf(expected_content_type_8
, MAX_CONTENT_TYPE_LEN
, "application/x-git-%s-advertisement", s
->service
);
1252 if (git__utf8_to_16(expected_content_type
, MAX_CONTENT_TYPE_LEN
, expected_content_type_8
) < 0) {
1253 git_error_set(GIT_ERROR_OS
, "failed to convert expected content-type to wide characters");
1257 content_type_length
= sizeof(content_type
);
1259 if (!WinHttpQueryHeaders(s
->request
,
1260 WINHTTP_QUERY_CONTENT_TYPE
,
1261 WINHTTP_HEADER_NAME_BY_INDEX
,
1262 &content_type
, &content_type_length
,
1263 WINHTTP_NO_HEADER_INDEX
)) {
1264 git_error_set(GIT_ERROR_OS
, "failed to retrieve response content-type");
1268 if (wcscmp(expected_content_type
, content_type
)) {
1269 git_error_set(GIT_ERROR_HTTP
, "received unexpected content-type");
1273 s
->received_response
= 1;
1276 if (!WinHttpReadData(s
->request
,
1281 git_error_set(GIT_ERROR_OS
, "failed to read data");
1285 *bytes_read
= dw_bytes_read
;
1290 static int winhttp_stream_write_single(
1291 git_smart_subtransport_stream
*stream
,
1295 winhttp_stream
*s
= (winhttp_stream
*)stream
;
1296 DWORD bytes_written
;
1299 if (!s
->request
&& winhttp_stream_connect(s
) < 0)
1302 /* This implementation of write permits only a single call. */
1303 if (s
->sent_request
) {
1304 git_error_set(GIT_ERROR_HTTP
, "subtransport configured for only one write");
1308 if ((error
= send_request(s
, len
, false)) < 0)
1311 s
->sent_request
= 1;
1313 if (!WinHttpWriteData(s
->request
,
1317 git_error_set(GIT_ERROR_OS
, "failed to write data");
1321 GIT_ASSERT((DWORD
)len
== bytes_written
);
1326 static int put_uuid_string(LPWSTR buffer
, size_t buffer_len_cch
)
1329 RPC_STATUS status
= UuidCreate(&uuid
);
1332 if (RPC_S_OK
!= status
&&
1333 RPC_S_UUID_LOCAL_ONLY
!= status
&&
1334 RPC_S_UUID_NO_ADDRESS
!= status
) {
1335 git_error_set(GIT_ERROR_HTTP
, "unable to generate name for temp file");
1339 if (buffer_len_cch
< UUID_LENGTH_CCH
+ 1) {
1340 git_error_set(GIT_ERROR_HTTP
, "buffer too small for name of temp file");
1344 #if !defined(__MINGW32__) || defined(MINGW_HAS_SECURE_API)
1345 result
= swprintf_s(buffer
, buffer_len_cch
,
1347 result
= wsprintfW(buffer
,
1349 L
"%08x%04x%04x%02x%02x%02x%02x%02x%02x%02x%02x",
1350 uuid
.Data1
, uuid
.Data2
, uuid
.Data3
,
1351 uuid
.Data4
[0], uuid
.Data4
[1], uuid
.Data4
[2], uuid
.Data4
[3],
1352 uuid
.Data4
[4], uuid
.Data4
[5], uuid
.Data4
[6], uuid
.Data4
[7]);
1354 if (result
< UUID_LENGTH_CCH
) {
1355 git_error_set(GIT_ERROR_OS
, "unable to generate name for temp file");
1362 static int get_temp_file(LPWSTR buffer
, DWORD buffer_len_cch
)
1366 if (!GetTempPathW(buffer_len_cch
, buffer
)) {
1367 git_error_set(GIT_ERROR_OS
, "failed to get temp path");
1371 len
= wcslen(buffer
);
1373 if (buffer
[len
- 1] != '\\' && len
< buffer_len_cch
)
1374 buffer
[len
++] = '\\';
1376 if (put_uuid_string(&buffer
[len
], (size_t)buffer_len_cch
- len
) < 0)
1382 static int winhttp_stream_write_buffered(
1383 git_smart_subtransport_stream
*stream
,
1387 winhttp_stream
*s
= (winhttp_stream
*)stream
;
1388 DWORD bytes_written
;
1390 if (!s
->request
&& winhttp_stream_connect(s
) < 0)
1393 /* Buffer the payload, using a temporary file so we delegate
1394 * memory management of the data to the operating system. */
1395 if (!s
->post_body
) {
1396 wchar_t temp_path
[MAX_PATH
+ 1];
1398 if (get_temp_file(temp_path
, MAX_PATH
+ 1) < 0)
1401 s
->post_body
= CreateFileW(temp_path
,
1402 GENERIC_READ
| GENERIC_WRITE
,
1403 FILE_SHARE_DELETE
, NULL
,
1405 FILE_ATTRIBUTE_TEMPORARY
| FILE_FLAG_DELETE_ON_CLOSE
| FILE_FLAG_SEQUENTIAL_SCAN
,
1408 if (INVALID_HANDLE_VALUE
== s
->post_body
) {
1409 s
->post_body
= NULL
;
1410 git_error_set(GIT_ERROR_OS
, "failed to create temporary file");
1415 if (!WriteFile(s
->post_body
, buffer
, (DWORD
)len
, &bytes_written
, NULL
)) {
1416 git_error_set(GIT_ERROR_OS
, "failed to write to temporary file");
1420 GIT_ASSERT((DWORD
)len
== bytes_written
);
1422 s
->post_body_len
+= bytes_written
;
1427 static int winhttp_stream_write_chunked(
1428 git_smart_subtransport_stream
*stream
,
1432 winhttp_stream
*s
= (winhttp_stream
*)stream
;
1435 if (!s
->request
&& winhttp_stream_connect(s
) < 0)
1438 if (!s
->sent_request
) {
1439 /* Send Transfer-Encoding: chunked header */
1440 if (!WinHttpAddRequestHeaders(s
->request
,
1441 transfer_encoding
, (ULONG
) -1L,
1442 WINHTTP_ADDREQ_FLAG_ADD
)) {
1443 git_error_set(GIT_ERROR_OS
, "failed to add a header to the request");
1447 if ((error
= send_request(s
, 0, true)) < 0)
1450 s
->sent_request
= 1;
1453 if (len
> CACHED_POST_BODY_BUF_SIZE
) {
1454 /* Flush, if necessary */
1455 if (s
->chunk_buffer_len
> 0) {
1456 if (write_chunk(s
->request
, s
->chunk_buffer
, s
->chunk_buffer_len
) < 0)
1459 s
->chunk_buffer_len
= 0;
1462 /* Write chunk directly */
1463 if (write_chunk(s
->request
, buffer
, len
) < 0)
1467 /* Append as much to the buffer as we can */
1468 int count
= (int)min(CACHED_POST_BODY_BUF_SIZE
- s
->chunk_buffer_len
, len
);
1470 if (!s
->chunk_buffer
) {
1471 s
->chunk_buffer
= git__malloc(CACHED_POST_BODY_BUF_SIZE
);
1472 GIT_ERROR_CHECK_ALLOC(s
->chunk_buffer
);
1475 memcpy(s
->chunk_buffer
+ s
->chunk_buffer_len
, buffer
, count
);
1476 s
->chunk_buffer_len
+= count
;
1480 /* Is the buffer full? If so, then flush */
1481 if (CACHED_POST_BODY_BUF_SIZE
== s
->chunk_buffer_len
) {
1482 if (write_chunk(s
->request
, s
->chunk_buffer
, s
->chunk_buffer_len
) < 0)
1485 s
->chunk_buffer_len
= 0;
1487 /* Is there any remaining data from the source? */
1489 memcpy(s
->chunk_buffer
, buffer
, len
);
1490 s
->chunk_buffer_len
= (unsigned int)len
;
1498 static void winhttp_stream_free(git_smart_subtransport_stream
*stream
)
1500 winhttp_stream
*s
= (winhttp_stream
*)stream
;
1502 winhttp_stream_close(s
);
1506 static int winhttp_stream_alloc(winhttp_subtransport
*t
, winhttp_stream
**stream
)
1513 s
= git__calloc(1, sizeof(winhttp_stream
));
1514 GIT_ERROR_CHECK_ALLOC(s
);
1516 s
->parent
.subtransport
= &t
->parent
;
1517 s
->parent
.read
= winhttp_stream_read
;
1518 s
->parent
.write
= winhttp_stream_write_single
;
1519 s
->parent
.free
= winhttp_stream_free
;
1526 static int winhttp_uploadpack_ls(
1527 winhttp_subtransport
*t
,
1532 s
->service
= upload_pack_service
;
1533 s
->service_url
= upload_pack_ls_service_url
;
1539 static int winhttp_uploadpack(
1540 winhttp_subtransport
*t
,
1545 s
->service
= upload_pack_service
;
1546 s
->service_url
= upload_pack_service_url
;
1547 s
->verb
= post_verb
;
1552 static int winhttp_receivepack_ls(
1553 winhttp_subtransport
*t
,
1558 s
->service
= receive_pack_service
;
1559 s
->service_url
= receive_pack_ls_service_url
;
1565 static int winhttp_receivepack(
1566 winhttp_subtransport
*t
,
1571 /* WinHTTP only supports Transfer-Encoding: chunked
1572 * on Windows Vista (NT 6.0) and higher. */
1573 s
->chunked
= git_has_win32_version(6, 0, 0);
1576 s
->parent
.write
= winhttp_stream_write_chunked
;
1578 s
->parent
.write
= winhttp_stream_write_buffered
;
1580 s
->service
= receive_pack_service
;
1581 s
->service_url
= receive_pack_service_url
;
1582 s
->verb
= post_verb
;
1587 static int winhttp_action(
1588 git_smart_subtransport_stream
**stream
,
1589 git_smart_subtransport
*subtransport
,
1591 git_smart_service_t action
)
1593 winhttp_subtransport
*t
= (winhttp_subtransport
*)subtransport
;
1598 if ((ret
= git_net_url_parse(&t
->server
.url
, url
)) < 0 ||
1599 (ret
= winhttp_connect(t
)) < 0)
1602 if (winhttp_stream_alloc(t
, &s
) < 0)
1610 case GIT_SERVICE_UPLOADPACK_LS
:
1611 ret
= winhttp_uploadpack_ls(t
, s
);
1614 case GIT_SERVICE_UPLOADPACK
:
1615 ret
= winhttp_uploadpack(t
, s
);
1618 case GIT_SERVICE_RECEIVEPACK_LS
:
1619 ret
= winhttp_receivepack_ls(t
, s
);
1622 case GIT_SERVICE_RECEIVEPACK
:
1623 ret
= winhttp_receivepack(t
, s
);
1631 *stream
= &s
->parent
;
1636 static int winhttp_close(git_smart_subtransport
*subtransport
)
1638 winhttp_subtransport
*t
= (winhttp_subtransport
*)subtransport
;
1640 git_net_url_dispose(&t
->server
.url
);
1641 git_net_url_dispose(&t
->proxy
.url
);
1643 if (t
->server
.cred
) {
1644 t
->server
.cred
->free(t
->server
.cred
);
1645 t
->server
.cred
= NULL
;
1648 if (t
->proxy
.cred
) {
1649 t
->proxy
.cred
->free(t
->proxy
.cred
);
1650 t
->proxy
.cred
= NULL
;
1653 return winhttp_close_connection(t
);
1656 static void winhttp_free(git_smart_subtransport
*subtransport
)
1658 winhttp_subtransport
*t
= (winhttp_subtransport
*)subtransport
;
1660 winhttp_close(subtransport
);
1665 int git_smart_subtransport_http(git_smart_subtransport
**out
, git_transport
*owner
, void *param
)
1667 winhttp_subtransport
*t
;
1674 t
= git__calloc(1, sizeof(winhttp_subtransport
));
1675 GIT_ERROR_CHECK_ALLOC(t
);
1677 t
->owner
= (transport_smart
*)owner
;
1678 t
->parent
.action
= winhttp_action
;
1679 t
->parent
.close
= winhttp_close
;
1680 t
->parent
.free
= winhttp_free
;
1682 *out
= (git_smart_subtransport
*) t
;
1686 #endif /* GIT_WINHTTP */