]> git.proxmox.com Git - libgit2.git/blob - src/transports/winhttp.c
Merge pull request #3579 from ethomson/mingw_winhttp
[libgit2.git] / src / transports / winhttp.c
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
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.
6 */
7
8 #ifdef GIT_WINHTTP
9
10 #include "git2.h"
11 #include "git2/transport.h"
12 #include "buffer.h"
13 #include "posix.h"
14 #include "netops.h"
15 #include "smart.h"
16 #include "remote.h"
17 #include "repository.h"
18 #include "global.h"
19
20 #include <wincrypt.h>
21 #include <winhttp.h>
22
23 /* For IInternetSecurityManager zone check */
24 #include <objbase.h>
25 #include <urlmon.h>
26
27 #define WIDEN2(s) L ## s
28 #define WIDEN(s) WIDEN2(s)
29
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
38 #endif
39
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;
54
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 } };
62
63 # define CLSID_InternetSecurityManager CLSID_InternetSecurityManager_mingw
64 # define IID_IInternetSecurityManager IID_IInternetSecurityManager_mingw
65 #endif
66
67 #define OWNING_SUBTRANSPORT(s) ((winhttp_subtransport *)(s)->parent.subtransport)
68
69 typedef enum {
70 GIT_WINHTTP_AUTH_BASIC = 1,
71 GIT_WINHTTP_AUTH_NEGOTIATE = 2,
72 } winhttp_authmechanism_t;
73
74 typedef struct {
75 git_smart_subtransport_stream parent;
76 const char *service;
77 const char *service_url;
78 const wchar_t *verb;
79 HINTERNET request;
80 wchar_t *request_uri;
81 char *chunk_buffer;
82 unsigned chunk_buffer_len;
83 HANDLE post_body;
84 DWORD post_body_len;
85 unsigned sent_request : 1,
86 received_response : 1,
87 chunked : 1;
88 } winhttp_stream;
89
90 typedef struct {
91 git_smart_subtransport parent;
92 transport_smart *owner;
93 gitno_connection_data connection_data;
94 git_cred *cred;
95 git_cred *url_cred;
96 int auth_mechanism;
97 HINTERNET session;
98 HINTERNET connection;
99 } winhttp_subtransport;
100
101 static int apply_basic_credential(HINTERNET request, git_cred *cred)
102 {
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;
107
108 git_buf_printf(&raw, "%s:%s", c->username, c->password);
109
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)
113 goto on_error;
114
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");
117 goto on_error;
118 }
119
120 if (!WinHttpAddRequestHeaders(request, wide, (ULONG) -1L, WINHTTP_ADDREQ_FLAG_ADD)) {
121 giterr_set(GITERR_OS, "Failed to add a header to the request");
122 goto on_error;
123 }
124
125 error = 0;
126
127 on_error:
128 /* We were dealing with plaintext passwords, so clean up after ourselves a bit. */
129 if (wide)
130 memset(wide, 0x0, wide_len * sizeof(wchar_t));
131
132 if (buf.size)
133 memset(buf.ptr, 0x0, buf.size);
134
135 if (raw.size)
136 memset(raw.ptr, 0x0, raw.size);
137
138 git__free(wide);
139 git_buf_free(&buf);
140 git_buf_free(&raw);
141 return error;
142 }
143
144 static int apply_default_credentials(HINTERNET request)
145 {
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;
153
154 if (!WinHttpSetOption(request, WINHTTP_OPTION_AUTOLOGON_POLICY, &data, sizeof(DWORD)))
155 return -1;
156
157 return 0;
158 }
159
160 static int fallback_cred_acquire_cb(
161 git_cred **cred,
162 const char *url,
163 const char *username_from_url,
164 unsigned int allowed_types,
165 void *payload)
166 {
167 int error = 1;
168
169 GIT_UNUSED(username_from_url);
170 GIT_UNUSED(payload);
171
172 /* If the target URI supports integrated Windows authentication
173 * as an authentication mechanism */
174 if (GIT_CREDTYPE_DEFAULT & allowed_types) {
175 wchar_t *wide_url;
176
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");
180 return -1;
181 }
182
183 if (SUCCEEDED(CoInitializeEx(NULL, COINIT_MULTITHREADED))) {
184 IInternetSecurityManager* pISM;
185
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))) {
189 DWORD dwZone;
190
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;
196
197 if (existing)
198 existing->free(existing);
199
200 /* Then use default Windows credentials to authenticate this request */
201 error = git_cred_default_new(cred);
202 }
203
204 pISM->lpVtbl->Release(pISM);
205 }
206
207 CoUninitialize();
208 }
209
210 git__free(wide_url);
211 }
212
213 return error;
214 }
215
216 static int certificate_check(winhttp_stream *s, int valid)
217 {
218 int error;
219 winhttp_subtransport *t = OWNING_SUBTRANSPORT(s);
220 PCERT_CONTEXT cert_ctx;
221 DWORD cert_ctx_size = sizeof(cert_ctx);
222 git_cert_x509 cert;
223
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;
227
228 if (t->owner->certificate_check_cb == NULL || !t->connection_data.use_ssl)
229 return 0;
230
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");
233 return -1;
234 }
235
236 giterr_clear();
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);
242
243 if (error < 0 && !giterr_last())
244 giterr_set(GITERR_NET, "user cancelled certificate check");
245
246 return error;
247 }
248
249 static void winhttp_stream_close(winhttp_stream *s)
250 {
251 if (s->chunk_buffer) {
252 git__free(s->chunk_buffer);
253 s->chunk_buffer = NULL;
254 }
255
256 if (s->post_body) {
257 CloseHandle(s->post_body);
258 s->post_body = NULL;
259 }
260
261 if (s->request_uri) {
262 git__free(s->request_uri);
263 s->request_uri = NULL;
264 }
265
266 if (s->request) {
267 WinHttpCloseHandle(s->request);
268 s->request = NULL;
269 }
270
271 s->sent_request = 0;
272 }
273
274 static int winhttp_stream_connect(winhttp_stream *s)
275 {
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;
282 int error = -1;
283 unsigned long disable_redirects = WINHTTP_DISABLE_REDIRECTS;
284 int default_timeout = TIMEOUT_INFINITE;
285 int default_connect_timeout = DEFAULT_CONNECT_TIMEOUT;
286 int i;
287
288 /* Prepare URL */
289 git_buf_printf(&buf, "%s%s", t->connection_data.path, s->service_url);
290
291 if (git_buf_oom(&buf))
292 return -1;
293
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");
297 goto on_error;
298 }
299
300 /* Establish request */
301 s->request = WinHttpOpenRequest(
302 t->connection,
303 s->verb,
304 s->request_uri,
305 NULL,
306 WINHTTP_NO_REFERER,
307 types,
308 t->connection_data.use_ssl ? WINHTTP_FLAG_SECURE : 0);
309
310 if (!s->request) {
311 giterr_set(GITERR_OS, "Failed to open request");
312 goto on_error;
313 }
314
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");
317 goto on_error;
318 }
319
320 /* Set proxy if necessary */
321 if (git_remote__get_http_proxy(t->owner->owner, !!t->connection_data.use_ssl, &proxy_url) < 0)
322 goto on_error;
323
324 if (proxy_url) {
325 WINHTTP_PROXY_INFO proxy_info;
326 wchar_t *proxy_wide;
327
328 /* Convert URL to wide characters */
329 int proxy_wide_len = git__utf8_to_16_alloc(&proxy_wide, proxy_url);
330
331 if (proxy_wide_len < 0) {
332 giterr_set(GITERR_OS, "Failed to convert string to wide form");
333 goto on_error;
334 }
335
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';
340
341 proxy_info.dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
342 proxy_info.lpszProxy = proxy_wide;
343 proxy_info.lpszProxyBypass = NULL;
344
345 if (!WinHttpSetOption(s->request,
346 WINHTTP_OPTION_PROXY,
347 &proxy_info,
348 sizeof(WINHTTP_PROXY_INFO))) {
349 giterr_set(GITERR_OS, "Failed to set proxy");
350 git__free(proxy_wide);
351 goto on_error;
352 }
353
354 git__free(proxy_wide);
355 }
356
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
359 */
360 if (!WinHttpSetOption(s->request,
361 WINHTTP_OPTION_DISABLE_FEATURE,
362 &disable_redirects,
363 sizeof(disable_redirects))) {
364 giterr_set(GITERR_OS, "Failed to disable redirects");
365 goto on_error;
366 }
367
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,
373 &peerdist,
374 sizeof(peerdist));
375
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");
379 goto on_error;
380 }
381
382 if (post_verb == s->verb) {
383 /* Send Content-Type and Accept headers -- only necessary on a POST */
384 git_buf_clear(&buf);
385 if (git_buf_printf(&buf,
386 "Content-Type: application/x-git-%s-request",
387 s->service) < 0)
388 goto on_error;
389
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");
392 goto on_error;
393 }
394
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");
398 goto on_error;
399 }
400
401 git_buf_clear(&buf);
402 if (git_buf_printf(&buf,
403 "Accept: application/x-git-%s-result",
404 s->service) < 0)
405 goto on_error;
406
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");
409 goto on_error;
410 }
411
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");
415 goto on_error;
416 }
417 }
418
419 for (i = 0; i < t->owner->custom_headers.count; i++) {
420 if (t->owner->custom_headers.strings[i]) {
421 git_buf_clear(&buf);
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");
425 goto on_error;
426 }
427
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");
431 goto on_error;
432 }
433 }
434 }
435
436 /* If requested, disable certificate validation */
437 if (t->connection_data.use_ssl) {
438 int flags;
439
440 if (t->owner->parent.read_flags(&t->owner->parent, &flags) < 0)
441 goto on_error;
442 }
443
444 /* If we have a credential on the subtransport, apply it to the request */
445 if (t->cred &&
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)
449 goto on_error;
450 else if (t->cred &&
451 t->cred->credtype == GIT_CREDTYPE_DEFAULT &&
452 t->auth_mechanism == GIT_WINHTTP_AUTH_NEGOTIATE &&
453 apply_default_credentials(s->request) < 0)
454 goto on_error;
455
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) {
459 if (!t->url_cred &&
460 git_cred_userpass_plaintext_new(&t->url_cred, t->connection_data.user, t->connection_data.pass) < 0)
461 goto on_error;
462 if (apply_basic_credential(s->request, t->url_cred) < 0)
463 goto on_error;
464 }
465
466 /* We've done everything up to calling WinHttpSendRequest. */
467
468 error = 0;
469
470 on_error:
471 if (error < 0)
472 winhttp_stream_close(s);
473
474 git__free(proxy_url);
475 git_buf_free(&buf);
476 return error;
477 }
478
479 static int parse_unauthorized_response(
480 HINTERNET request,
481 int *allowed_types,
482 int *auth_mechanism)
483 {
484 DWORD supported, first, target;
485
486 *allowed_types = 0;
487 *auth_mechanism = 0;
488
489 /* WinHttpQueryHeaders() must be called before WinHttpQueryAuthSchemes().
490 * We can assume this was already done, since we know we are unauthorized.
491 */
492 if (!WinHttpQueryAuthSchemes(request, &supported, &first, &target)) {
493 giterr_set(GITERR_OS, "Failed to parse supported auth schemes");
494 return -1;
495 }
496
497 if (WINHTTP_AUTH_SCHEME_BASIC & supported) {
498 *allowed_types |= GIT_CREDTYPE_USERPASS_PLAINTEXT;
499 *auth_mechanism = GIT_WINHTTP_AUTH_BASIC;
500 }
501
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;
506 }
507
508 return 0;
509 }
510
511 static int write_chunk(HINTERNET request, const char *buffer, size_t len)
512 {
513 DWORD bytes_written;
514 git_buf buf = GIT_BUF_INIT;
515
516 /* Chunk header */
517 git_buf_printf(&buf, "%X\r\n", len);
518
519 if (git_buf_oom(&buf))
520 return -1;
521
522 if (!WinHttpWriteData(request,
523 git_buf_cstr(&buf), (DWORD)git_buf_len(&buf),
524 &bytes_written)) {
525 git_buf_free(&buf);
526 giterr_set(GITERR_OS, "Failed to write chunk header");
527 return -1;
528 }
529
530 git_buf_free(&buf);
531
532 /* Chunk body */
533 if (!WinHttpWriteData(request,
534 buffer, (DWORD)len,
535 &bytes_written)) {
536 giterr_set(GITERR_OS, "Failed to write chunk");
537 return -1;
538 }
539
540 /* Chunk footer */
541 if (!WinHttpWriteData(request,
542 "\r\n", 2,
543 &bytes_written)) {
544 giterr_set(GITERR_OS, "Failed to write chunk footer");
545 return -1;
546 }
547
548 return 0;
549 }
550
551 static int winhttp_close_connection(winhttp_subtransport *t)
552 {
553 int ret = 0;
554
555 if (t->connection) {
556 if (!WinHttpCloseHandle(t->connection)) {
557 giterr_set(GITERR_OS, "Unable to close connection");
558 ret = -1;
559 }
560
561 t->connection = NULL;
562 }
563
564 if (t->session) {
565 if (!WinHttpCloseHandle(t->session)) {
566 giterr_set(GITERR_OS, "Unable to close session");
567 ret = -1;
568 }
569
570 t->session = NULL;
571 }
572
573 return ret;
574 }
575
576 static int user_agent(git_buf *ua)
577 {
578 const char *custom = git_libgit2__user_agent();
579
580 git_buf_clear(ua);
581 git_buf_PUTS(ua, "git/1.0 (");
582
583 if (custom)
584 git_buf_puts(ua, custom);
585 else
586 git_buf_PUTS(ua, "libgit2 " LIBGIT2_VERSION);
587
588 return git_buf_putc(ua, ')');
589 }
590
591 static int winhttp_connect(
592 winhttp_subtransport *t)
593 {
594 wchar_t *wide_host;
595 int32_t port;
596 wchar_t *wide_ua;
597 git_buf ua = GIT_BUF_INIT;
598 int error = -1;
599 int default_timeout = TIMEOUT_INFINITE;
600 int default_connect_timeout = DEFAULT_CONNECT_TIMEOUT;
601
602 t->session = NULL;
603 t->connection = NULL;
604
605 /* Prepare port */
606 if (git__strtol32(&port, t->connection_data.port, NULL, 10) < 0)
607 return -1;
608
609 /* Prepare host */
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");
612 return -1;
613 }
614
615 if ((error = user_agent(&ua)) < 0) {
616 git__free(wide_host);
617 return error;
618 }
619
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);
623 git_buf_free(&ua);
624 return -1;
625 }
626
627 git_buf_free(&ua);
628
629 /* Establish session */
630 t->session = WinHttpOpen(
631 wide_ua,
632 WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
633 WINHTTP_NO_PROXY_NAME,
634 WINHTTP_NO_PROXY_BYPASS,
635 0);
636
637 if (!t->session) {
638 giterr_set(GITERR_OS, "Failed to init WinHTTP");
639 goto on_error;
640 }
641
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");
644 goto on_error;
645 }
646
647
648 /* Establish connection */
649 t->connection = WinHttpConnect(
650 t->session,
651 wide_host,
652 (INTERNET_PORT) port,
653 0);
654
655 if (!t->connection) {
656 giterr_set(GITERR_OS, "Failed to connect to host");
657 goto on_error;
658 }
659
660 error = 0;
661
662 on_error:
663 if (error < 0)
664 winhttp_close_connection(t);
665
666 git__free(wide_host);
667 git__free(wide_ua);
668
669 return error;
670 }
671
672 static int do_send_request(winhttp_stream *s, size_t len, int ignore_length)
673 {
674 if (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)) {
679 return -1;
680 }
681 } else {
682 if (!WinHttpSendRequest(s->request,
683 WINHTTP_NO_ADDITIONAL_HEADERS, 0,
684 WINHTTP_NO_REQUEST_DATA, 0,
685 len, 0)) {
686 return -1;
687 }
688 }
689
690 return 0;
691 }
692
693 static int send_request(winhttp_stream *s, size_t len, int ignore_length)
694 {
695 int request_failed = 0, cert_valid = 1, error = 0;
696 DWORD ignore_flags;
697
698 if ((error = do_send_request(s, len, ignore_length)) < 0)
699 request_failed = 1;
700
701 if (request_failed) {
702 if (GetLastError() != ERROR_WINHTTP_SECURE_FAILURE) {
703 giterr_set(GITERR_OS, "failed to send request");
704 return -1;
705 } else {
706 cert_valid = 0;
707 }
708 }
709
710 giterr_clear();
711 if ((error = certificate_check(s, cert_valid)) < 0) {
712 if (!giterr_last())
713 giterr_set(GITERR_OS, "user cancelled certificate check");
714
715 return error;
716 }
717
718 /* if neither the request nor the certificate check returned errors, we're done */
719 if (!request_failed)
720 return 0;
721
722 ignore_flags = no_check_cert_flags;
723
724 if (!WinHttpSetOption(s->request, WINHTTP_OPTION_SECURITY_FLAGS, &ignore_flags, sizeof(ignore_flags))) {
725 giterr_set(GITERR_OS, "failed to set security options");
726 return -1;
727 }
728
729 if ((error = do_send_request(s, len, ignore_length)) < 0)
730 giterr_set(GITERR_OS, "failed to send request");
731
732 return error;
733 }
734
735 static int winhttp_stream_read(
736 git_smart_subtransport_stream *stream,
737 char *buffer,
738 size_t buf_size,
739 size_t *bytes_read)
740 {
741 winhttp_stream *s = (winhttp_stream *)stream;
742 winhttp_subtransport *t = OWNING_SUBTRANSPORT(s);
743 DWORD dw_bytes_read;
744 char replay_count = 0;
745 int error;
746
747 replay:
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");
751 return -1;
752 }
753
754 /* Connect if necessary */
755 if (!s->request && winhttp_stream_connect(s) < 0)
756 return -1;
757
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];
762
763 if (!s->sent_request) {
764
765 if ((error = send_request(s, s->post_body_len, 0)) < 0)
766 return error;
767
768 s->sent_request = 1;
769 }
770
771 if (s->chunked) {
772 assert(s->verb == post_verb);
773
774 /* Flush, if necessary */
775 if (s->chunk_buffer_len > 0 &&
776 write_chunk(s->request, s->chunk_buffer, s->chunk_buffer_len) < 0)
777 return -1;
778
779 s->chunk_buffer_len = 0;
780
781 /* Write the final chunk. */
782 if (!WinHttpWriteData(s->request,
783 "0\r\n\r\n", 5,
784 &bytes_written)) {
785 giterr_set(GITERR_OS, "Failed to write final chunk");
786 return -1;
787 }
788 }
789 else if (s->post_body) {
790 char *buffer;
791 DWORD len = s->post_body_len, bytes_read;
792
793 if (INVALID_SET_FILE_POINTER == SetFilePointer(s->post_body,
794 0, 0, FILE_BEGIN) &&
795 NO_ERROR != GetLastError()) {
796 giterr_set(GITERR_OS, "Failed to reset file pointer");
797 return -1;
798 }
799
800 buffer = git__malloc(CACHED_POST_BODY_BUF_SIZE);
801
802 while (len > 0) {
803 DWORD bytes_written;
804
805 if (!ReadFile(s->post_body, buffer,
806 min(CACHED_POST_BODY_BUF_SIZE, len),
807 &bytes_read, NULL) ||
808 !bytes_read) {
809 git__free(buffer);
810 giterr_set(GITERR_OS, "Failed to read from temp file");
811 return -1;
812 }
813
814 if (!WinHttpWriteData(s->request, buffer,
815 bytes_read, &bytes_written)) {
816 git__free(buffer);
817 giterr_set(GITERR_OS, "Failed to write data");
818 return -1;
819 }
820
821 len -= bytes_read;
822 assert(bytes_read == bytes_written);
823 }
824
825 git__free(buffer);
826
827 /* Eagerly close the temp file */
828 CloseHandle(s->post_body);
829 s->post_body = NULL;
830 }
831
832 if (!WinHttpReceiveResponse(s->request, 0)) {
833 giterr_set(GITERR_OS, "Failed to receive response");
834 return -1;
835 }
836
837 /* Verify that we got a 200 back */
838 status_code_length = sizeof(status_code);
839
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");
846 return -1;
847 }
848
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.) */
854 if (!s->chunked &&
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)) {
860
861 /* Check for Windows 7. This workaround is only necessary on
862 * Windows Vista and earlier. Windows 7 is version 6.1. */
863 wchar_t *location;
864 DWORD location_length;
865 char *location8;
866
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,
872 &location_length,
873 WINHTTP_NO_HEADER_INDEX) ||
874 GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
875 giterr_set(GITERR_OS, "Failed to read Location header");
876 return -1;
877 }
878
879 location = git__malloc(location_length);
880 GITERR_CHECK_ALLOC(location);
881
882 if (!WinHttpQueryHeaders(s->request,
883 WINHTTP_QUERY_LOCATION,
884 WINHTTP_HEADER_NAME_BY_INDEX,
885 location,
886 &location_length,
887 WINHTTP_NO_HEADER_INDEX)) {
888 giterr_set(GITERR_OS, "Failed to read Location header");
889 git__free(location);
890 return -1;
891 }
892
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");
896 git__free(location);
897 return -1;
898 }
899
900 git__free(location);
901
902 /* Replay the request */
903 winhttp_stream_close(s);
904
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);
909 return -1;
910 }
911
912 winhttp_close_connection(t);
913
914 if (winhttp_connect(t) < 0)
915 return -1;
916 }
917
918 git__free(location8);
919 goto replay;
920 }
921
922 /* Handle authentication failures */
923 if (HTTP_STATUS_DENIED == status_code && get_verb == s->verb) {
924 int allowed_types;
925
926 if (parse_unauthorized_response(s->request, &allowed_types, &t->auth_mechanism) < 0)
927 return -1;
928
929 if (allowed_types &&
930 (!t->cred || 0 == (t->cred->credtype & allowed_types))) {
931 int cred_error = 1;
932
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);
937
938 /* Treat GIT_PASSTHROUGH as though git_cred_acquire_cb isn't set */
939 if (cred_error == GIT_PASSTHROUGH)
940 cred_error = 1;
941 else if (cred_error < 0)
942 return cred_error;
943 }
944
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);
949
950 if (cred_error < 0)
951 return cred_error;
952 }
953
954 if (!cred_error) {
955 assert(t->cred);
956
957 winhttp_stream_close(s);
958
959 /* Successfully acquired a credential */
960 goto replay;
961 }
962 }
963 }
964
965 if (HTTP_STATUS_OK != status_code) {
966 giterr_set(GITERR_NET, "Request failed with status code: %d", status_code);
967 return -1;
968 }
969
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);
973 else
974 p_snprintf(expected_content_type_8, MAX_CONTENT_TYPE_LEN, "application/x-git-%s-advertisement", s->service);
975
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");
978 return -1;
979 }
980
981 content_type_length = sizeof(content_type);
982
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");
989 return -1;
990 }
991
992 if (wcscmp(expected_content_type, content_type)) {
993 giterr_set(GITERR_NET, "Received unexpected content-type");
994 return -1;
995 }
996
997 s->received_response = 1;
998 }
999
1000 if (!WinHttpReadData(s->request,
1001 (LPVOID)buffer,
1002 (DWORD)buf_size,
1003 &dw_bytes_read))
1004 {
1005 giterr_set(GITERR_OS, "Failed to read data");
1006 return -1;
1007 }
1008
1009 *bytes_read = dw_bytes_read;
1010
1011 return 0;
1012 }
1013
1014 static int winhttp_stream_write_single(
1015 git_smart_subtransport_stream *stream,
1016 const char *buffer,
1017 size_t len)
1018 {
1019 winhttp_stream *s = (winhttp_stream *)stream;
1020 DWORD bytes_written;
1021 int error;
1022
1023 if (!s->request && winhttp_stream_connect(s) < 0)
1024 return -1;
1025
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");
1029 return -1;
1030 }
1031
1032 if ((error = send_request(s, len, 0)) < 0)
1033 return error;
1034
1035 s->sent_request = 1;
1036
1037 if (!WinHttpWriteData(s->request,
1038 (LPCVOID)buffer,
1039 (DWORD)len,
1040 &bytes_written)) {
1041 giterr_set(GITERR_OS, "Failed to write data");
1042 return -1;
1043 }
1044
1045 assert((DWORD)len == bytes_written);
1046
1047 return 0;
1048 }
1049
1050 static int put_uuid_string(LPWSTR buffer, size_t buffer_len_cch)
1051 {
1052 UUID uuid;
1053 RPC_STATUS status = UuidCreate(&uuid);
1054 int result;
1055
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");
1060 return -1;
1061 }
1062
1063 if (buffer_len_cch < UUID_LENGTH_CCH + 1) {
1064 giterr_set(GITERR_NET, "Buffer too small for name of temp file");
1065 return -1;
1066 }
1067
1068 #if !defined(__MINGW32__) || defined(MINGW_HAS_SECURE_API)
1069 result = swprintf_s(buffer, buffer_len_cch,
1070 #else
1071 result = wsprintfW(buffer,
1072 #endif
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]);
1077
1078 if (result < UUID_LENGTH_CCH) {
1079 giterr_set(GITERR_OS, "Unable to generate name for temp file");
1080 return -1;
1081 }
1082
1083 return 0;
1084 }
1085
1086 static int get_temp_file(LPWSTR buffer, DWORD buffer_len_cch)
1087 {
1088 size_t len;
1089
1090 if (!GetTempPathW(buffer_len_cch, buffer)) {
1091 giterr_set(GITERR_OS, "Failed to get temp path");
1092 return -1;
1093 }
1094
1095 len = wcslen(buffer);
1096
1097 if (buffer[len - 1] != '\\' && len < buffer_len_cch)
1098 buffer[len++] = '\\';
1099
1100 if (put_uuid_string(&buffer[len], (size_t)buffer_len_cch - len) < 0)
1101 return -1;
1102
1103 return 0;
1104 }
1105
1106 static int winhttp_stream_write_buffered(
1107 git_smart_subtransport_stream *stream,
1108 const char *buffer,
1109 size_t len)
1110 {
1111 winhttp_stream *s = (winhttp_stream *)stream;
1112 DWORD bytes_written;
1113
1114 if (!s->request && winhttp_stream_connect(s) < 0)
1115 return -1;
1116
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];
1121
1122 if (get_temp_file(temp_path, MAX_PATH + 1) < 0)
1123 return -1;
1124
1125 s->post_body = CreateFileW(temp_path,
1126 GENERIC_READ | GENERIC_WRITE,
1127 FILE_SHARE_DELETE, NULL,
1128 CREATE_NEW,
1129 FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE | FILE_FLAG_SEQUENTIAL_SCAN,
1130 NULL);
1131
1132 if (INVALID_HANDLE_VALUE == s->post_body) {
1133 s->post_body = NULL;
1134 giterr_set(GITERR_OS, "Failed to create temporary file");
1135 return -1;
1136 }
1137 }
1138
1139 if (!WriteFile(s->post_body, buffer, (DWORD)len, &bytes_written, NULL)) {
1140 giterr_set(GITERR_OS, "Failed to write to temporary file");
1141 return -1;
1142 }
1143
1144 assert((DWORD)len == bytes_written);
1145
1146 s->post_body_len += bytes_written;
1147
1148 return 0;
1149 }
1150
1151 static int winhttp_stream_write_chunked(
1152 git_smart_subtransport_stream *stream,
1153 const char *buffer,
1154 size_t len)
1155 {
1156 winhttp_stream *s = (winhttp_stream *)stream;
1157 int error;
1158
1159 if (!s->request && winhttp_stream_connect(s) < 0)
1160 return -1;
1161
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");
1168 return -1;
1169 }
1170
1171 if ((error = send_request(s, 0, 1)) < 0)
1172 return error;
1173
1174 s->sent_request = 1;
1175 }
1176
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)
1181 return -1;
1182
1183 s->chunk_buffer_len = 0;
1184 }
1185
1186 /* Write chunk directly */
1187 if (write_chunk(s->request, buffer, len) < 0)
1188 return -1;
1189 }
1190 else {
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);
1193
1194 if (!s->chunk_buffer)
1195 s->chunk_buffer = git__malloc(CACHED_POST_BODY_BUF_SIZE);
1196
1197 memcpy(s->chunk_buffer + s->chunk_buffer_len, buffer, count);
1198 s->chunk_buffer_len += count;
1199 buffer += count;
1200 len -= count;
1201
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)
1205 return -1;
1206
1207 s->chunk_buffer_len = 0;
1208
1209 /* Is there any remaining data from the source? */
1210 if (len > 0) {
1211 memcpy(s->chunk_buffer, buffer, len);
1212 s->chunk_buffer_len = (unsigned int)len;
1213 }
1214 }
1215 }
1216
1217 return 0;
1218 }
1219
1220 static void winhttp_stream_free(git_smart_subtransport_stream *stream)
1221 {
1222 winhttp_stream *s = (winhttp_stream *)stream;
1223
1224 winhttp_stream_close(s);
1225 git__free(s);
1226 }
1227
1228 static int winhttp_stream_alloc(winhttp_subtransport *t, winhttp_stream **stream)
1229 {
1230 winhttp_stream *s;
1231
1232 if (!stream)
1233 return -1;
1234
1235 s = git__calloc(1, sizeof(winhttp_stream));
1236 GITERR_CHECK_ALLOC(s);
1237
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;
1242
1243 *stream = s;
1244
1245 return 0;
1246 }
1247
1248 static int winhttp_uploadpack_ls(
1249 winhttp_subtransport *t,
1250 winhttp_stream *s)
1251 {
1252 GIT_UNUSED(t);
1253
1254 s->service = upload_pack_service;
1255 s->service_url = upload_pack_ls_service_url;
1256 s->verb = get_verb;
1257
1258 return 0;
1259 }
1260
1261 static int winhttp_uploadpack(
1262 winhttp_subtransport *t,
1263 winhttp_stream *s)
1264 {
1265 GIT_UNUSED(t);
1266
1267 s->service = upload_pack_service;
1268 s->service_url = upload_pack_service_url;
1269 s->verb = post_verb;
1270
1271 return 0;
1272 }
1273
1274 static int winhttp_receivepack_ls(
1275 winhttp_subtransport *t,
1276 winhttp_stream *s)
1277 {
1278 GIT_UNUSED(t);
1279
1280 s->service = receive_pack_service;
1281 s->service_url = receive_pack_ls_service_url;
1282 s->verb = get_verb;
1283
1284 return 0;
1285 }
1286
1287 static int winhttp_receivepack(
1288 winhttp_subtransport *t,
1289 winhttp_stream *s)
1290 {
1291 GIT_UNUSED(t);
1292
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);
1296
1297 if (s->chunked)
1298 s->parent.write = winhttp_stream_write_chunked;
1299 else
1300 s->parent.write = winhttp_stream_write_buffered;
1301
1302 s->service = receive_pack_service;
1303 s->service_url = receive_pack_service_url;
1304 s->verb = post_verb;
1305
1306 return 0;
1307 }
1308
1309 static int winhttp_action(
1310 git_smart_subtransport_stream **stream,
1311 git_smart_subtransport *subtransport,
1312 const char *url,
1313 git_smart_service_t action)
1314 {
1315 winhttp_subtransport *t = (winhttp_subtransport *)subtransport;
1316 winhttp_stream *s;
1317 int ret = -1;
1318
1319 if (!t->connection)
1320 if ((ret = gitno_connection_data_from_url(&t->connection_data, url, NULL)) < 0 ||
1321 (ret = winhttp_connect(t)) < 0)
1322 return ret;
1323
1324 if (winhttp_stream_alloc(t, &s) < 0)
1325 return -1;
1326
1327 if (!stream)
1328 return -1;
1329
1330 switch (action)
1331 {
1332 case GIT_SERVICE_UPLOADPACK_LS:
1333 ret = winhttp_uploadpack_ls(t, s);
1334 break;
1335
1336 case GIT_SERVICE_UPLOADPACK:
1337 ret = winhttp_uploadpack(t, s);
1338 break;
1339
1340 case GIT_SERVICE_RECEIVEPACK_LS:
1341 ret = winhttp_receivepack_ls(t, s);
1342 break;
1343
1344 case GIT_SERVICE_RECEIVEPACK:
1345 ret = winhttp_receivepack(t, s);
1346 break;
1347
1348 default:
1349 assert(0);
1350 }
1351
1352 if (!ret)
1353 *stream = &s->parent;
1354
1355 return ret;
1356 }
1357
1358 static int winhttp_close(git_smart_subtransport *subtransport)
1359 {
1360 winhttp_subtransport *t = (winhttp_subtransport *)subtransport;
1361
1362 gitno_connection_data_free_ptrs(&t->connection_data);
1363 memset(&t->connection_data, 0x0, sizeof(gitno_connection_data));
1364
1365 if (t->cred) {
1366 t->cred->free(t->cred);
1367 t->cred = NULL;
1368 }
1369
1370 if (t->url_cred) {
1371 t->url_cred->free(t->url_cred);
1372 t->url_cred = NULL;
1373 }
1374
1375 return winhttp_close_connection(t);
1376 }
1377
1378 static void winhttp_free(git_smart_subtransport *subtransport)
1379 {
1380 winhttp_subtransport *t = (winhttp_subtransport *)subtransport;
1381
1382 winhttp_close(subtransport);
1383
1384 git__free(t);
1385 }
1386
1387 int git_smart_subtransport_http(git_smart_subtransport **out, git_transport *owner, void *param)
1388 {
1389 winhttp_subtransport *t;
1390
1391 GIT_UNUSED(param);
1392
1393 if (!out)
1394 return -1;
1395
1396 t = git__calloc(1, sizeof(winhttp_subtransport));
1397 GITERR_CHECK_ALLOC(t);
1398
1399 t->owner = (transport_smart *)owner;
1400 t->parent.action = winhttp_action;
1401 t->parent.close = winhttp_close;
1402 t->parent.free = winhttp_free;
1403
1404 *out = (git_smart_subtransport *) t;
1405 return 0;
1406 }
1407
1408 #endif /* GIT_WINHTTP */