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.
11 #include "git2/transport.h"
17 #include "repository.h"
23 /* For IInternetSecurityManager zone check */
27 #define WIDEN2(s) L ## s
28 #define WIDEN(s) WIDEN2(s)
30 #define MAX_CONTENT_TYPE_LEN 100
31 #define WINHTTP_OPTION_PEERDIST_EXTENSION_STATE 109
32 #define CACHED_POST_BODY_BUF_SIZE 4096
33 #define UUID_LENGTH_CCH 32
34 #define TIMEOUT_INFINITE -1
35 #define DEFAULT_CONNECT_TIMEOUT 60000
36 #ifndef WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH
37 #define WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH 0
40 static const char *prefix_https
= "https://";
41 static const char *upload_pack_service
= "upload-pack";
42 static const char *upload_pack_ls_service_url
= "/info/refs?service=git-upload-pack";
43 static const char *upload_pack_service_url
= "/git-upload-pack";
44 static const char *receive_pack_service
= "receive-pack";
45 static const char *receive_pack_ls_service_url
= "/info/refs?service=git-receive-pack";
46 static const char *receive_pack_service_url
= "/git-receive-pack";
47 static const wchar_t *get_verb
= L
"GET";
48 static const wchar_t *post_verb
= L
"POST";
49 static const wchar_t *pragma_nocache
= L
"Pragma: no-cache";
50 static const wchar_t *transfer_encoding
= L
"Transfer-Encoding: chunked";
51 static const int no_check_cert_flags
= SECURITY_FLAG_IGNORE_CERT_CN_INVALID
|
52 SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
|
53 SECURITY_FLAG_IGNORE_UNKNOWN_CA
;
55 #if defined(__MINGW32__)
56 static const CLSID CLSID_InternetSecurityManager_mingw
=
57 { 0x7B8A2D94, 0x0AC9, 0x11D1,
58 { 0x89, 0x6C, 0x00, 0xC0, 0x4F, 0xB6, 0xBF, 0xC4 } };
59 static const IID IID_IInternetSecurityManager_mingw
=
60 { 0x79EAC9EE, 0xBAF9, 0x11CE,
61 { 0x8C, 0x82, 0x00, 0xAA, 0x00, 0x4B, 0xA9, 0x0B } };
63 # define CLSID_InternetSecurityManager CLSID_InternetSecurityManager_mingw
64 # define IID_IInternetSecurityManager IID_IInternetSecurityManager_mingw
67 #define OWNING_SUBTRANSPORT(s) ((winhttp_subtransport *)(s)->parent.subtransport)
70 GIT_WINHTTP_AUTH_BASIC
= 1,
71 GIT_WINHTTP_AUTH_NEGOTIATE
= 2,
72 } winhttp_authmechanism_t
;
75 git_smart_subtransport_stream parent
;
77 const char *service_url
;
82 unsigned chunk_buffer_len
;
85 unsigned sent_request
: 1,
86 received_response
: 1,
91 git_smart_subtransport parent
;
92 transport_smart
*owner
;
93 gitno_connection_data connection_data
;
99 } winhttp_subtransport
;
101 static int apply_basic_credential(HINTERNET request
, git_cred
*cred
)
103 git_cred_userpass_plaintext
*c
= (git_cred_userpass_plaintext
*)cred
;
104 git_buf buf
= GIT_BUF_INIT
, raw
= GIT_BUF_INIT
;
105 wchar_t *wide
= NULL
;
106 int error
= -1, wide_len
;
108 git_buf_printf(&raw
, "%s:%s", c
->username
, c
->password
);
110 if (git_buf_oom(&raw
) ||
111 git_buf_puts(&buf
, "Authorization: Basic ") < 0 ||
112 git_buf_encode_base64(&buf
, git_buf_cstr(&raw
), raw
.size
) < 0)
115 if ((wide_len
= git__utf8_to_16_alloc(&wide
, git_buf_cstr(&buf
))) < 0) {
116 giterr_set(GITERR_OS
, "Failed to convert string to wide form");
120 if (!WinHttpAddRequestHeaders(request
, wide
, (ULONG
) -1L, WINHTTP_ADDREQ_FLAG_ADD
)) {
121 giterr_set(GITERR_OS
, "Failed to add a header to the request");
128 /* We were dealing with plaintext passwords, so clean up after ourselves a bit. */
130 memset(wide
, 0x0, wide_len
* sizeof(wchar_t));
133 memset(buf
.ptr
, 0x0, buf
.size
);
136 memset(raw
.ptr
, 0x0, raw
.size
);
144 static int apply_default_credentials(HINTERNET request
)
146 /* Either the caller explicitly requested that default credentials be passed,
147 * or our fallback credential callback was invoked and checked that the target
148 * URI was in the appropriate Internet Explorer security zone. By setting this
149 * flag, we guarantee that the credentials are delivered by WinHTTP. The default
150 * is "medium" which applies to the intranet and sounds like it would correspond
151 * to Internet Explorer security zones, but in fact does not. */
152 DWORD data
= WINHTTP_AUTOLOGON_SECURITY_LEVEL_LOW
;
154 if (!WinHttpSetOption(request
, WINHTTP_OPTION_AUTOLOGON_POLICY
, &data
, sizeof(DWORD
)))
160 static int fallback_cred_acquire_cb(
163 const char *username_from_url
,
164 unsigned int allowed_types
,
169 GIT_UNUSED(username_from_url
);
172 /* If the target URI supports integrated Windows authentication
173 * as an authentication mechanism */
174 if (GIT_CREDTYPE_DEFAULT
& allowed_types
) {
177 /* Convert URL to wide characters */
178 if (git__utf8_to_16_alloc(&wide_url
, url
) < 0) {
179 giterr_set(GITERR_OS
, "Failed to convert string to wide form");
183 if (SUCCEEDED(CoInitializeEx(NULL
, COINIT_MULTITHREADED
))) {
184 IInternetSecurityManager
* pISM
;
186 /* And if the target URI is in the My Computer, Intranet, or Trusted zones */
187 if (SUCCEEDED(CoCreateInstance(&CLSID_InternetSecurityManager
, NULL
,
188 CLSCTX_ALL
, &IID_IInternetSecurityManager
, (void **)&pISM
))) {
191 if (SUCCEEDED(pISM
->lpVtbl
->MapUrlToZone(pISM
, wide_url
, &dwZone
, 0)) &&
192 (URLZONE_LOCAL_MACHINE
== dwZone
||
193 URLZONE_INTRANET
== dwZone
||
194 URLZONE_TRUSTED
== dwZone
)) {
195 git_cred
*existing
= *cred
;
198 existing
->free(existing
);
200 /* Then use default Windows credentials to authenticate this request */
201 error
= git_cred_default_new(cred
);
204 pISM
->lpVtbl
->Release(pISM
);
216 static int certificate_check(winhttp_stream
*s
, int valid
)
219 winhttp_subtransport
*t
= OWNING_SUBTRANSPORT(s
);
220 PCERT_CONTEXT cert_ctx
;
221 DWORD cert_ctx_size
= sizeof(cert_ctx
);
224 /* If there is no override, we should fail if WinHTTP doesn't think it's fine */
225 if (t
->owner
->certificate_check_cb
== NULL
&& !valid
)
226 return GIT_ECERTIFICATE
;
228 if (t
->owner
->certificate_check_cb
== NULL
|| !t
->connection_data
.use_ssl
)
231 if (!WinHttpQueryOption(s
->request
, WINHTTP_OPTION_SERVER_CERT_CONTEXT
, &cert_ctx
, &cert_ctx_size
)) {
232 giterr_set(GITERR_OS
, "failed to get server certificate");
237 cert
.parent
.cert_type
= GIT_CERT_X509
;
238 cert
.data
= cert_ctx
->pbCertEncoded
;
239 cert
.len
= cert_ctx
->cbCertEncoded
;
240 error
= t
->owner
->certificate_check_cb((git_cert
*) &cert
, valid
, t
->connection_data
.host
, t
->owner
->cred_acquire_payload
);
241 CertFreeCertificateContext(cert_ctx
);
243 if (error
< 0 && !giterr_last())
244 giterr_set(GITERR_NET
, "user cancelled certificate check");
249 static void winhttp_stream_close(winhttp_stream
*s
)
251 if (s
->chunk_buffer
) {
252 git__free(s
->chunk_buffer
);
253 s
->chunk_buffer
= NULL
;
257 CloseHandle(s
->post_body
);
261 if (s
->request_uri
) {
262 git__free(s
->request_uri
);
263 s
->request_uri
= NULL
;
267 WinHttpCloseHandle(s
->request
);
274 static int winhttp_stream_connect(winhttp_stream
*s
)
276 winhttp_subtransport
*t
= OWNING_SUBTRANSPORT(s
);
277 git_buf buf
= GIT_BUF_INIT
;
278 char *proxy_url
= NULL
;
279 wchar_t ct
[MAX_CONTENT_TYPE_LEN
];
280 LPCWSTR types
[] = { L
"*/*", NULL
};
281 BOOL peerdist
= FALSE
;
283 unsigned long disable_redirects
= WINHTTP_DISABLE_REDIRECTS
;
284 int default_timeout
= TIMEOUT_INFINITE
;
285 int default_connect_timeout
= DEFAULT_CONNECT_TIMEOUT
;
289 git_buf_printf(&buf
, "%s%s", t
->connection_data
.path
, s
->service_url
);
291 if (git_buf_oom(&buf
))
294 /* Convert URL to wide characters */
295 if (git__utf8_to_16_alloc(&s
->request_uri
, git_buf_cstr(&buf
)) < 0) {
296 giterr_set(GITERR_OS
, "Failed to convert string to wide form");
300 /* Establish request */
301 s
->request
= WinHttpOpenRequest(
308 t
->connection_data
.use_ssl
? WINHTTP_FLAG_SECURE
: 0);
311 giterr_set(GITERR_OS
, "Failed to open request");
315 if (!WinHttpSetTimeouts(s
->request
, default_timeout
, default_connect_timeout
, default_timeout
, default_timeout
)) {
316 giterr_set(GITERR_OS
, "Failed to set timeouts for WinHTTP");
320 /* Set proxy if necessary */
321 if (git_remote__get_http_proxy(t
->owner
->owner
, !!t
->connection_data
.use_ssl
, &proxy_url
) < 0)
325 WINHTTP_PROXY_INFO proxy_info
;
328 /* Convert URL to wide characters */
329 int proxy_wide_len
= git__utf8_to_16_alloc(&proxy_wide
, proxy_url
);
331 if (proxy_wide_len
< 0) {
332 giterr_set(GITERR_OS
, "Failed to convert string to wide form");
336 /* Strip any trailing forward slash on the proxy URL;
337 * WinHTTP doesn't like it if one is present */
338 if (proxy_wide_len
> 1 && L
'/' == proxy_wide
[proxy_wide_len
- 2])
339 proxy_wide
[proxy_wide_len
- 2] = L
'\0';
341 proxy_info
.dwAccessType
= WINHTTP_ACCESS_TYPE_NAMED_PROXY
;
342 proxy_info
.lpszProxy
= proxy_wide
;
343 proxy_info
.lpszProxyBypass
= NULL
;
345 if (!WinHttpSetOption(s
->request
,
346 WINHTTP_OPTION_PROXY
,
348 sizeof(WINHTTP_PROXY_INFO
))) {
349 giterr_set(GITERR_OS
, "Failed to set proxy");
350 git__free(proxy_wide
);
354 git__free(proxy_wide
);
357 /* Disable WinHTTP redirects so we can handle them manually. Why, you ask?
358 * http://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/b2ff8879-ab9f-4218-8f09-16d25dff87ae
360 if (!WinHttpSetOption(s
->request
,
361 WINHTTP_OPTION_DISABLE_FEATURE
,
363 sizeof(disable_redirects
))) {
364 giterr_set(GITERR_OS
, "Failed to disable redirects");
368 /* Strip unwanted headers (X-P2P-PeerDist, X-P2P-PeerDistEx) that WinHTTP
369 * adds itself. This option may not be supported by the underlying
370 * platform, so we do not error-check it */
371 WinHttpSetOption(s
->request
,
372 WINHTTP_OPTION_PEERDIST_EXTENSION_STATE
,
376 /* Send Pragma: no-cache header */
377 if (!WinHttpAddRequestHeaders(s
->request
, pragma_nocache
, (ULONG
) -1L, WINHTTP_ADDREQ_FLAG_ADD
)) {
378 giterr_set(GITERR_OS
, "Failed to add a header to the request");
382 if (post_verb
== s
->verb
) {
383 /* Send Content-Type and Accept headers -- only necessary on a POST */
385 if (git_buf_printf(&buf
,
386 "Content-Type: application/x-git-%s-request",
390 if (git__utf8_to_16(ct
, MAX_CONTENT_TYPE_LEN
, git_buf_cstr(&buf
)) < 0) {
391 giterr_set(GITERR_OS
, "Failed to convert content-type to wide characters");
395 if (!WinHttpAddRequestHeaders(s
->request
, ct
, (ULONG
)-1L,
396 WINHTTP_ADDREQ_FLAG_ADD
| WINHTTP_ADDREQ_FLAG_REPLACE
)) {
397 giterr_set(GITERR_OS
, "Failed to add a header to the request");
402 if (git_buf_printf(&buf
,
403 "Accept: application/x-git-%s-result",
407 if (git__utf8_to_16(ct
, MAX_CONTENT_TYPE_LEN
, git_buf_cstr(&buf
)) < 0) {
408 giterr_set(GITERR_OS
, "Failed to convert accept header to wide characters");
412 if (!WinHttpAddRequestHeaders(s
->request
, ct
, (ULONG
)-1L,
413 WINHTTP_ADDREQ_FLAG_ADD
| WINHTTP_ADDREQ_FLAG_REPLACE
)) {
414 giterr_set(GITERR_OS
, "Failed to add a header to the request");
419 for (i
= 0; i
< t
->owner
->custom_headers
.count
; i
++) {
420 if (t
->owner
->custom_headers
.strings
[i
]) {
422 git_buf_puts(&buf
, t
->owner
->custom_headers
.strings
[i
]);
423 if (git__utf8_to_16(ct
, MAX_CONTENT_TYPE_LEN
, git_buf_cstr(&buf
)) < 0) {
424 giterr_set(GITERR_OS
, "Failed to convert custom header to wide characters");
428 if (!WinHttpAddRequestHeaders(s
->request
, ct
, (ULONG
)-1L,
429 WINHTTP_ADDREQ_FLAG_ADD
| WINHTTP_ADDREQ_FLAG_REPLACE
)) {
430 giterr_set(GITERR_OS
, "Failed to add a header to the request");
436 /* If requested, disable certificate validation */
437 if (t
->connection_data
.use_ssl
) {
440 if (t
->owner
->parent
.read_flags(&t
->owner
->parent
, &flags
) < 0)
444 /* If we have a credential on the subtransport, apply it to the request */
446 t
->cred
->credtype
== GIT_CREDTYPE_USERPASS_PLAINTEXT
&&
447 t
->auth_mechanism
== GIT_WINHTTP_AUTH_BASIC
&&
448 apply_basic_credential(s
->request
, t
->cred
) < 0)
451 t
->cred
->credtype
== GIT_CREDTYPE_DEFAULT
&&
452 t
->auth_mechanism
== GIT_WINHTTP_AUTH_NEGOTIATE
&&
453 apply_default_credentials(s
->request
) < 0)
456 /* If no other credentials have been applied and the URL has username and
457 * password, use those */
458 if (!t
->cred
&& t
->connection_data
.user
&& t
->connection_data
.pass
) {
460 git_cred_userpass_plaintext_new(&t
->url_cred
, t
->connection_data
.user
, t
->connection_data
.pass
) < 0)
462 if (apply_basic_credential(s
->request
, t
->url_cred
) < 0)
466 /* We've done everything up to calling WinHttpSendRequest. */
472 winhttp_stream_close(s
);
474 git__free(proxy_url
);
479 static int parse_unauthorized_response(
484 DWORD supported
, first
, target
;
489 /* WinHttpQueryHeaders() must be called before WinHttpQueryAuthSchemes().
490 * We can assume this was already done, since we know we are unauthorized.
492 if (!WinHttpQueryAuthSchemes(request
, &supported
, &first
, &target
)) {
493 giterr_set(GITERR_OS
, "Failed to parse supported auth schemes");
497 if (WINHTTP_AUTH_SCHEME_BASIC
& supported
) {
498 *allowed_types
|= GIT_CREDTYPE_USERPASS_PLAINTEXT
;
499 *auth_mechanism
= GIT_WINHTTP_AUTH_BASIC
;
502 if ((WINHTTP_AUTH_SCHEME_NTLM
& supported
) ||
503 (WINHTTP_AUTH_SCHEME_NEGOTIATE
& supported
)) {
504 *allowed_types
|= GIT_CREDTYPE_DEFAULT
;
505 *auth_mechanism
= GIT_WINHTTP_AUTH_NEGOTIATE
;
511 static int write_chunk(HINTERNET request
, const char *buffer
, size_t len
)
514 git_buf buf
= GIT_BUF_INIT
;
517 git_buf_printf(&buf
, "%X\r\n", len
);
519 if (git_buf_oom(&buf
))
522 if (!WinHttpWriteData(request
,
523 git_buf_cstr(&buf
), (DWORD
)git_buf_len(&buf
),
526 giterr_set(GITERR_OS
, "Failed to write chunk header");
533 if (!WinHttpWriteData(request
,
536 giterr_set(GITERR_OS
, "Failed to write chunk");
541 if (!WinHttpWriteData(request
,
544 giterr_set(GITERR_OS
, "Failed to write chunk footer");
551 static int winhttp_close_connection(winhttp_subtransport
*t
)
556 if (!WinHttpCloseHandle(t
->connection
)) {
557 giterr_set(GITERR_OS
, "Unable to close connection");
561 t
->connection
= NULL
;
565 if (!WinHttpCloseHandle(t
->session
)) {
566 giterr_set(GITERR_OS
, "Unable to close session");
576 static int user_agent(git_buf
*ua
)
578 const char *custom
= git_libgit2__user_agent();
581 git_buf_PUTS(ua
, "git/1.0 (");
584 git_buf_puts(ua
, custom
);
586 git_buf_PUTS(ua
, "libgit2 " LIBGIT2_VERSION
);
588 return git_buf_putc(ua
, ')');
591 static int winhttp_connect(
592 winhttp_subtransport
*t
)
597 git_buf ua
= GIT_BUF_INIT
;
599 int default_timeout
= TIMEOUT_INFINITE
;
600 int default_connect_timeout
= DEFAULT_CONNECT_TIMEOUT
;
603 t
->connection
= NULL
;
606 if (git__strtol32(&port
, t
->connection_data
.port
, NULL
, 10) < 0)
610 if (git__utf8_to_16_alloc(&wide_host
, t
->connection_data
.host
) < 0) {
611 giterr_set(GITERR_OS
, "Unable to convert host to wide characters");
615 if ((error
= user_agent(&ua
)) < 0) {
616 git__free(wide_host
);
620 if (git__utf8_to_16_alloc(&wide_ua
, git_buf_cstr(&ua
)) < 0) {
621 giterr_set(GITERR_OS
, "Unable to convert host to wide characters");
622 git__free(wide_host
);
629 /* Establish session */
630 t
->session
= WinHttpOpen(
632 WINHTTP_ACCESS_TYPE_DEFAULT_PROXY
,
633 WINHTTP_NO_PROXY_NAME
,
634 WINHTTP_NO_PROXY_BYPASS
,
638 giterr_set(GITERR_OS
, "Failed to init WinHTTP");
642 if (!WinHttpSetTimeouts(t
->session
, default_timeout
, default_connect_timeout
, default_timeout
, default_timeout
)) {
643 giterr_set(GITERR_OS
, "Failed to set timeouts for WinHTTP");
648 /* Establish connection */
649 t
->connection
= WinHttpConnect(
652 (INTERNET_PORT
) port
,
655 if (!t
->connection
) {
656 giterr_set(GITERR_OS
, "Failed to connect to host");
664 winhttp_close_connection(t
);
666 git__free(wide_host
);
672 static int do_send_request(winhttp_stream
*s
, size_t len
, int ignore_length
)
675 if (!WinHttpSendRequest(s
->request
,
676 WINHTTP_NO_ADDITIONAL_HEADERS
, 0,
677 WINHTTP_NO_REQUEST_DATA
, 0,
678 WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH
, 0)) {
682 if (!WinHttpSendRequest(s
->request
,
683 WINHTTP_NO_ADDITIONAL_HEADERS
, 0,
684 WINHTTP_NO_REQUEST_DATA
, 0,
693 static int send_request(winhttp_stream
*s
, size_t len
, int ignore_length
)
695 int request_failed
= 0, cert_valid
= 1, error
= 0;
698 if ((error
= do_send_request(s
, len
, ignore_length
)) < 0)
701 if (request_failed
) {
702 if (GetLastError() != ERROR_WINHTTP_SECURE_FAILURE
) {
703 giterr_set(GITERR_OS
, "failed to send request");
711 if ((error
= certificate_check(s
, cert_valid
)) < 0) {
713 giterr_set(GITERR_OS
, "user cancelled certificate check");
718 /* if neither the request nor the certificate check returned errors, we're done */
722 ignore_flags
= no_check_cert_flags
;
724 if (!WinHttpSetOption(s
->request
, WINHTTP_OPTION_SECURITY_FLAGS
, &ignore_flags
, sizeof(ignore_flags
))) {
725 giterr_set(GITERR_OS
, "failed to set security options");
729 if ((error
= do_send_request(s
, len
, ignore_length
)) < 0)
730 giterr_set(GITERR_OS
, "failed to send request");
735 static int winhttp_stream_read(
736 git_smart_subtransport_stream
*stream
,
741 winhttp_stream
*s
= (winhttp_stream
*)stream
;
742 winhttp_subtransport
*t
= OWNING_SUBTRANSPORT(s
);
744 char replay_count
= 0;
748 /* Enforce a reasonable cap on the number of replays */
749 if (++replay_count
>= 7) {
750 giterr_set(GITERR_NET
, "Too many redirects or authentication replays");
754 /* Connect if necessary */
755 if (!s
->request
&& winhttp_stream_connect(s
) < 0)
758 if (!s
->received_response
) {
759 DWORD status_code
, status_code_length
, content_type_length
, bytes_written
;
760 char expected_content_type_8
[MAX_CONTENT_TYPE_LEN
];
761 wchar_t expected_content_type
[MAX_CONTENT_TYPE_LEN
], content_type
[MAX_CONTENT_TYPE_LEN
];
763 if (!s
->sent_request
) {
765 if ((error
= send_request(s
, s
->post_body_len
, 0)) < 0)
772 assert(s
->verb
== post_verb
);
774 /* Flush, if necessary */
775 if (s
->chunk_buffer_len
> 0 &&
776 write_chunk(s
->request
, s
->chunk_buffer
, s
->chunk_buffer_len
) < 0)
779 s
->chunk_buffer_len
= 0;
781 /* Write the final chunk. */
782 if (!WinHttpWriteData(s
->request
,
785 giterr_set(GITERR_OS
, "Failed to write final chunk");
789 else if (s
->post_body
) {
791 DWORD len
= s
->post_body_len
, bytes_read
;
793 if (INVALID_SET_FILE_POINTER
== SetFilePointer(s
->post_body
,
795 NO_ERROR
!= GetLastError()) {
796 giterr_set(GITERR_OS
, "Failed to reset file pointer");
800 buffer
= git__malloc(CACHED_POST_BODY_BUF_SIZE
);
805 if (!ReadFile(s
->post_body
, buffer
,
806 min(CACHED_POST_BODY_BUF_SIZE
, len
),
807 &bytes_read
, NULL
) ||
810 giterr_set(GITERR_OS
, "Failed to read from temp file");
814 if (!WinHttpWriteData(s
->request
, buffer
,
815 bytes_read
, &bytes_written
)) {
817 giterr_set(GITERR_OS
, "Failed to write data");
822 assert(bytes_read
== bytes_written
);
827 /* Eagerly close the temp file */
828 CloseHandle(s
->post_body
);
832 if (!WinHttpReceiveResponse(s
->request
, 0)) {
833 giterr_set(GITERR_OS
, "Failed to receive response");
837 /* Verify that we got a 200 back */
838 status_code_length
= sizeof(status_code
);
840 if (!WinHttpQueryHeaders(s
->request
,
841 WINHTTP_QUERY_STATUS_CODE
| WINHTTP_QUERY_FLAG_NUMBER
,
842 WINHTTP_HEADER_NAME_BY_INDEX
,
843 &status_code
, &status_code_length
,
844 WINHTTP_NO_HEADER_INDEX
)) {
845 giterr_set(GITERR_OS
, "Failed to retrieve status code");
849 /* The implementation of WinHTTP prior to Windows 7 will not
850 * redirect to an identical URI. Some Git hosters use self-redirects
851 * as part of their DoS mitigation strategy. Check first to see if we
852 * have a redirect status code, and that we haven't already streamed
853 * a post body. (We can't replay a streamed POST.) */
855 (HTTP_STATUS_MOVED
== status_code
||
856 HTTP_STATUS_REDIRECT
== status_code
||
857 (HTTP_STATUS_REDIRECT_METHOD
== status_code
&&
858 get_verb
== s
->verb
) ||
859 HTTP_STATUS_REDIRECT_KEEP_VERB
== status_code
)) {
861 /* Check for Windows 7. This workaround is only necessary on
862 * Windows Vista and earlier. Windows 7 is version 6.1. */
864 DWORD location_length
;
867 /* OK, fetch the Location header from the redirect. */
868 if (WinHttpQueryHeaders(s
->request
,
869 WINHTTP_QUERY_LOCATION
,
870 WINHTTP_HEADER_NAME_BY_INDEX
,
871 WINHTTP_NO_OUTPUT_BUFFER
,
873 WINHTTP_NO_HEADER_INDEX
) ||
874 GetLastError() != ERROR_INSUFFICIENT_BUFFER
) {
875 giterr_set(GITERR_OS
, "Failed to read Location header");
879 location
= git__malloc(location_length
);
880 GITERR_CHECK_ALLOC(location
);
882 if (!WinHttpQueryHeaders(s
->request
,
883 WINHTTP_QUERY_LOCATION
,
884 WINHTTP_HEADER_NAME_BY_INDEX
,
887 WINHTTP_NO_HEADER_INDEX
)) {
888 giterr_set(GITERR_OS
, "Failed to read Location header");
893 /* Convert the Location header to UTF-8 */
894 if (git__utf16_to_8_alloc(&location8
, location
) < 0) {
895 giterr_set(GITERR_OS
, "Failed to convert Location header to UTF-8");
902 /* Replay the request */
903 winhttp_stream_close(s
);
905 if (!git__prefixcmp_icase(location8
, prefix_https
)) {
906 /* Upgrade to secure connection; disconnect and start over */
907 if (gitno_connection_data_from_url(&t
->connection_data
, location8
, s
->service_url
) < 0) {
908 git__free(location8
);
912 winhttp_close_connection(t
);
914 if (winhttp_connect(t
) < 0)
918 git__free(location8
);
922 /* Handle authentication failures */
923 if (HTTP_STATUS_DENIED
== status_code
&& get_verb
== s
->verb
) {
926 if (parse_unauthorized_response(s
->request
, &allowed_types
, &t
->auth_mechanism
) < 0)
930 (!t
->cred
|| 0 == (t
->cred
->credtype
& allowed_types
))) {
933 /* Start with the user-supplied credential callback, if present */
934 if (t
->owner
->cred_acquire_cb
) {
935 cred_error
= t
->owner
->cred_acquire_cb(&t
->cred
, t
->owner
->url
,
936 t
->connection_data
.user
, allowed_types
, t
->owner
->cred_acquire_payload
);
938 /* Treat GIT_PASSTHROUGH as though git_cred_acquire_cb isn't set */
939 if (cred_error
== GIT_PASSTHROUGH
)
941 else if (cred_error
< 0)
945 /* Invoke the fallback credentials acquisition callback if necessary */
946 if (cred_error
> 0) {
947 cred_error
= fallback_cred_acquire_cb(&t
->cred
, t
->owner
->url
,
948 t
->connection_data
.user
, allowed_types
, NULL
);
957 winhttp_stream_close(s
);
959 /* Successfully acquired a credential */
965 if (HTTP_STATUS_OK
!= status_code
) {
966 giterr_set(GITERR_NET
, "Request failed with status code: %d", status_code
);
970 /* Verify that we got the correct content-type back */
971 if (post_verb
== s
->verb
)
972 p_snprintf(expected_content_type_8
, MAX_CONTENT_TYPE_LEN
, "application/x-git-%s-result", s
->service
);
974 p_snprintf(expected_content_type_8
, MAX_CONTENT_TYPE_LEN
, "application/x-git-%s-advertisement", s
->service
);
976 if (git__utf8_to_16(expected_content_type
, MAX_CONTENT_TYPE_LEN
, expected_content_type_8
) < 0) {
977 giterr_set(GITERR_OS
, "Failed to convert expected content-type to wide characters");
981 content_type_length
= sizeof(content_type
);
983 if (!WinHttpQueryHeaders(s
->request
,
984 WINHTTP_QUERY_CONTENT_TYPE
,
985 WINHTTP_HEADER_NAME_BY_INDEX
,
986 &content_type
, &content_type_length
,
987 WINHTTP_NO_HEADER_INDEX
)) {
988 giterr_set(GITERR_OS
, "Failed to retrieve response content-type");
992 if (wcscmp(expected_content_type
, content_type
)) {
993 giterr_set(GITERR_NET
, "Received unexpected content-type");
997 s
->received_response
= 1;
1000 if (!WinHttpReadData(s
->request
,
1005 giterr_set(GITERR_OS
, "Failed to read data");
1009 *bytes_read
= dw_bytes_read
;
1014 static int winhttp_stream_write_single(
1015 git_smart_subtransport_stream
*stream
,
1019 winhttp_stream
*s
= (winhttp_stream
*)stream
;
1020 DWORD bytes_written
;
1023 if (!s
->request
&& winhttp_stream_connect(s
) < 0)
1026 /* This implementation of write permits only a single call. */
1027 if (s
->sent_request
) {
1028 giterr_set(GITERR_NET
, "Subtransport configured for only one write");
1032 if ((error
= send_request(s
, len
, 0)) < 0)
1035 s
->sent_request
= 1;
1037 if (!WinHttpWriteData(s
->request
,
1041 giterr_set(GITERR_OS
, "Failed to write data");
1045 assert((DWORD
)len
== bytes_written
);
1050 static int put_uuid_string(LPWSTR buffer
, size_t buffer_len_cch
)
1053 RPC_STATUS status
= UuidCreate(&uuid
);
1056 if (RPC_S_OK
!= status
&&
1057 RPC_S_UUID_LOCAL_ONLY
!= status
&&
1058 RPC_S_UUID_NO_ADDRESS
!= status
) {
1059 giterr_set(GITERR_NET
, "Unable to generate name for temp file");
1063 if (buffer_len_cch
< UUID_LENGTH_CCH
+ 1) {
1064 giterr_set(GITERR_NET
, "Buffer too small for name of temp file");
1068 #if !defined(__MINGW32__) || defined(MINGW_HAS_SECURE_API)
1069 result
= swprintf_s(buffer
, buffer_len_cch
,
1071 result
= wsprintfW(buffer
,
1073 L
"%08x%04x%04x%02x%02x%02x%02x%02x%02x%02x%02x",
1074 uuid
.Data1
, uuid
.Data2
, uuid
.Data3
,
1075 uuid
.Data4
[0], uuid
.Data4
[1], uuid
.Data4
[2], uuid
.Data4
[3],
1076 uuid
.Data4
[4], uuid
.Data4
[5], uuid
.Data4
[6], uuid
.Data4
[7]);
1078 if (result
< UUID_LENGTH_CCH
) {
1079 giterr_set(GITERR_OS
, "Unable to generate name for temp file");
1086 static int get_temp_file(LPWSTR buffer
, DWORD buffer_len_cch
)
1090 if (!GetTempPathW(buffer_len_cch
, buffer
)) {
1091 giterr_set(GITERR_OS
, "Failed to get temp path");
1095 len
= wcslen(buffer
);
1097 if (buffer
[len
- 1] != '\\' && len
< buffer_len_cch
)
1098 buffer
[len
++] = '\\';
1100 if (put_uuid_string(&buffer
[len
], (size_t)buffer_len_cch
- len
) < 0)
1106 static int winhttp_stream_write_buffered(
1107 git_smart_subtransport_stream
*stream
,
1111 winhttp_stream
*s
= (winhttp_stream
*)stream
;
1112 DWORD bytes_written
;
1114 if (!s
->request
&& winhttp_stream_connect(s
) < 0)
1117 /* Buffer the payload, using a temporary file so we delegate
1118 * memory management of the data to the operating system. */
1119 if (!s
->post_body
) {
1120 wchar_t temp_path
[MAX_PATH
+ 1];
1122 if (get_temp_file(temp_path
, MAX_PATH
+ 1) < 0)
1125 s
->post_body
= CreateFileW(temp_path
,
1126 GENERIC_READ
| GENERIC_WRITE
,
1127 FILE_SHARE_DELETE
, NULL
,
1129 FILE_ATTRIBUTE_TEMPORARY
| FILE_FLAG_DELETE_ON_CLOSE
| FILE_FLAG_SEQUENTIAL_SCAN
,
1132 if (INVALID_HANDLE_VALUE
== s
->post_body
) {
1133 s
->post_body
= NULL
;
1134 giterr_set(GITERR_OS
, "Failed to create temporary file");
1139 if (!WriteFile(s
->post_body
, buffer
, (DWORD
)len
, &bytes_written
, NULL
)) {
1140 giterr_set(GITERR_OS
, "Failed to write to temporary file");
1144 assert((DWORD
)len
== bytes_written
);
1146 s
->post_body_len
+= bytes_written
;
1151 static int winhttp_stream_write_chunked(
1152 git_smart_subtransport_stream
*stream
,
1156 winhttp_stream
*s
= (winhttp_stream
*)stream
;
1159 if (!s
->request
&& winhttp_stream_connect(s
) < 0)
1162 if (!s
->sent_request
) {
1163 /* Send Transfer-Encoding: chunked header */
1164 if (!WinHttpAddRequestHeaders(s
->request
,
1165 transfer_encoding
, (ULONG
) -1L,
1166 WINHTTP_ADDREQ_FLAG_ADD
)) {
1167 giterr_set(GITERR_OS
, "Failed to add a header to the request");
1171 if ((error
= send_request(s
, 0, 1)) < 0)
1174 s
->sent_request
= 1;
1177 if (len
> CACHED_POST_BODY_BUF_SIZE
) {
1178 /* Flush, if necessary */
1179 if (s
->chunk_buffer_len
> 0) {
1180 if (write_chunk(s
->request
, s
->chunk_buffer
, s
->chunk_buffer_len
) < 0)
1183 s
->chunk_buffer_len
= 0;
1186 /* Write chunk directly */
1187 if (write_chunk(s
->request
, buffer
, len
) < 0)
1191 /* Append as much to the buffer as we can */
1192 int count
= (int)min(CACHED_POST_BODY_BUF_SIZE
- s
->chunk_buffer_len
, len
);
1194 if (!s
->chunk_buffer
)
1195 s
->chunk_buffer
= git__malloc(CACHED_POST_BODY_BUF_SIZE
);
1197 memcpy(s
->chunk_buffer
+ s
->chunk_buffer_len
, buffer
, count
);
1198 s
->chunk_buffer_len
+= count
;
1202 /* Is the buffer full? If so, then flush */
1203 if (CACHED_POST_BODY_BUF_SIZE
== s
->chunk_buffer_len
) {
1204 if (write_chunk(s
->request
, s
->chunk_buffer
, s
->chunk_buffer_len
) < 0)
1207 s
->chunk_buffer_len
= 0;
1209 /* Is there any remaining data from the source? */
1211 memcpy(s
->chunk_buffer
, buffer
, len
);
1212 s
->chunk_buffer_len
= (unsigned int)len
;
1220 static void winhttp_stream_free(git_smart_subtransport_stream
*stream
)
1222 winhttp_stream
*s
= (winhttp_stream
*)stream
;
1224 winhttp_stream_close(s
);
1228 static int winhttp_stream_alloc(winhttp_subtransport
*t
, winhttp_stream
**stream
)
1235 s
= git__calloc(1, sizeof(winhttp_stream
));
1236 GITERR_CHECK_ALLOC(s
);
1238 s
->parent
.subtransport
= &t
->parent
;
1239 s
->parent
.read
= winhttp_stream_read
;
1240 s
->parent
.write
= winhttp_stream_write_single
;
1241 s
->parent
.free
= winhttp_stream_free
;
1248 static int winhttp_uploadpack_ls(
1249 winhttp_subtransport
*t
,
1254 s
->service
= upload_pack_service
;
1255 s
->service_url
= upload_pack_ls_service_url
;
1261 static int winhttp_uploadpack(
1262 winhttp_subtransport
*t
,
1267 s
->service
= upload_pack_service
;
1268 s
->service_url
= upload_pack_service_url
;
1269 s
->verb
= post_verb
;
1274 static int winhttp_receivepack_ls(
1275 winhttp_subtransport
*t
,
1280 s
->service
= receive_pack_service
;
1281 s
->service_url
= receive_pack_ls_service_url
;
1287 static int winhttp_receivepack(
1288 winhttp_subtransport
*t
,
1293 /* WinHTTP only supports Transfer-Encoding: chunked
1294 * on Windows Vista (NT 6.0) and higher. */
1295 s
->chunked
= git_has_win32_version(6, 0, 0);
1298 s
->parent
.write
= winhttp_stream_write_chunked
;
1300 s
->parent
.write
= winhttp_stream_write_buffered
;
1302 s
->service
= receive_pack_service
;
1303 s
->service_url
= receive_pack_service_url
;
1304 s
->verb
= post_verb
;
1309 static int winhttp_action(
1310 git_smart_subtransport_stream
**stream
,
1311 git_smart_subtransport
*subtransport
,
1313 git_smart_service_t action
)
1315 winhttp_subtransport
*t
= (winhttp_subtransport
*)subtransport
;
1320 if ((ret
= gitno_connection_data_from_url(&t
->connection_data
, url
, NULL
)) < 0 ||
1321 (ret
= winhttp_connect(t
)) < 0)
1324 if (winhttp_stream_alloc(t
, &s
) < 0)
1332 case GIT_SERVICE_UPLOADPACK_LS
:
1333 ret
= winhttp_uploadpack_ls(t
, s
);
1336 case GIT_SERVICE_UPLOADPACK
:
1337 ret
= winhttp_uploadpack(t
, s
);
1340 case GIT_SERVICE_RECEIVEPACK_LS
:
1341 ret
= winhttp_receivepack_ls(t
, s
);
1344 case GIT_SERVICE_RECEIVEPACK
:
1345 ret
= winhttp_receivepack(t
, s
);
1353 *stream
= &s
->parent
;
1358 static int winhttp_close(git_smart_subtransport
*subtransport
)
1360 winhttp_subtransport
*t
= (winhttp_subtransport
*)subtransport
;
1362 gitno_connection_data_free_ptrs(&t
->connection_data
);
1363 memset(&t
->connection_data
, 0x0, sizeof(gitno_connection_data
));
1366 t
->cred
->free(t
->cred
);
1371 t
->url_cred
->free(t
->url_cred
);
1375 return winhttp_close_connection(t
);
1378 static void winhttp_free(git_smart_subtransport
*subtransport
)
1380 winhttp_subtransport
*t
= (winhttp_subtransport
*)subtransport
;
1382 winhttp_close(subtransport
);
1387 int git_smart_subtransport_http(git_smart_subtransport
**out
, git_transport
*owner
, void *param
)
1389 winhttp_subtransport
*t
;
1396 t
= git__calloc(1, sizeof(winhttp_subtransport
));
1397 GITERR_CHECK_ALLOC(t
);
1399 t
->owner
= (transport_smart
*)owner
;
1400 t
->parent
.action
= winhttp_action
;
1401 t
->parent
.close
= winhttp_close
;
1402 t
->parent
.free
= winhttp_free
;
1404 *out
= (git_smart_subtransport
*) t
;
1408 #endif /* GIT_WINHTTP */