]> git.proxmox.com Git - libgit2.git/blob - src/transports/winhttp.c
Merge pull request #3575 from pmq20/master-13jan16
[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 size_t 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 int cred_error = 1;
931
932 git_cred_free(t->cred);
933 t->cred = NULL;
934 /* Start with the user-supplied credential callback, if present */
935 if (t->owner->cred_acquire_cb) {
936 cred_error = t->owner->cred_acquire_cb(&t->cred, t->owner->url,
937 t->connection_data.user, allowed_types, t->owner->cred_acquire_payload);
938
939 /* Treat GIT_PASSTHROUGH as though git_cred_acquire_cb isn't set */
940 if (cred_error == GIT_PASSTHROUGH)
941 cred_error = 1;
942 else if (cred_error < 0)
943 return cred_error;
944 }
945
946 /* Invoke the fallback credentials acquisition callback if necessary */
947 if (cred_error > 0) {
948 cred_error = fallback_cred_acquire_cb(&t->cred, t->owner->url,
949 t->connection_data.user, allowed_types, NULL);
950
951 if (cred_error < 0)
952 return cred_error;
953 }
954
955 if (!cred_error) {
956 assert(t->cred);
957
958 winhttp_stream_close(s);
959
960 /* Successfully acquired a credential */
961 goto replay;
962 }
963 }
964 }
965
966 if (HTTP_STATUS_OK != status_code) {
967 giterr_set(GITERR_NET, "Request failed with status code: %d", status_code);
968 return -1;
969 }
970
971 /* Verify that we got the correct content-type back */
972 if (post_verb == s->verb)
973 p_snprintf(expected_content_type_8, MAX_CONTENT_TYPE_LEN, "application/x-git-%s-result", s->service);
974 else
975 p_snprintf(expected_content_type_8, MAX_CONTENT_TYPE_LEN, "application/x-git-%s-advertisement", s->service);
976
977 if (git__utf8_to_16(expected_content_type, MAX_CONTENT_TYPE_LEN, expected_content_type_8) < 0) {
978 giterr_set(GITERR_OS, "Failed to convert expected content-type to wide characters");
979 return -1;
980 }
981
982 content_type_length = sizeof(content_type);
983
984 if (!WinHttpQueryHeaders(s->request,
985 WINHTTP_QUERY_CONTENT_TYPE,
986 WINHTTP_HEADER_NAME_BY_INDEX,
987 &content_type, &content_type_length,
988 WINHTTP_NO_HEADER_INDEX)) {
989 giterr_set(GITERR_OS, "Failed to retrieve response content-type");
990 return -1;
991 }
992
993 if (wcscmp(expected_content_type, content_type)) {
994 giterr_set(GITERR_NET, "Received unexpected content-type");
995 return -1;
996 }
997
998 s->received_response = 1;
999 }
1000
1001 if (!WinHttpReadData(s->request,
1002 (LPVOID)buffer,
1003 (DWORD)buf_size,
1004 &dw_bytes_read))
1005 {
1006 giterr_set(GITERR_OS, "Failed to read data");
1007 return -1;
1008 }
1009
1010 *bytes_read = dw_bytes_read;
1011
1012 return 0;
1013 }
1014
1015 static int winhttp_stream_write_single(
1016 git_smart_subtransport_stream *stream,
1017 const char *buffer,
1018 size_t len)
1019 {
1020 winhttp_stream *s = (winhttp_stream *)stream;
1021 DWORD bytes_written;
1022 int error;
1023
1024 if (!s->request && winhttp_stream_connect(s) < 0)
1025 return -1;
1026
1027 /* This implementation of write permits only a single call. */
1028 if (s->sent_request) {
1029 giterr_set(GITERR_NET, "Subtransport configured for only one write");
1030 return -1;
1031 }
1032
1033 if ((error = send_request(s, len, 0)) < 0)
1034 return error;
1035
1036 s->sent_request = 1;
1037
1038 if (!WinHttpWriteData(s->request,
1039 (LPCVOID)buffer,
1040 (DWORD)len,
1041 &bytes_written)) {
1042 giterr_set(GITERR_OS, "Failed to write data");
1043 return -1;
1044 }
1045
1046 assert((DWORD)len == bytes_written);
1047
1048 return 0;
1049 }
1050
1051 static int put_uuid_string(LPWSTR buffer, size_t buffer_len_cch)
1052 {
1053 UUID uuid;
1054 RPC_STATUS status = UuidCreate(&uuid);
1055 int result;
1056
1057 if (RPC_S_OK != status &&
1058 RPC_S_UUID_LOCAL_ONLY != status &&
1059 RPC_S_UUID_NO_ADDRESS != status) {
1060 giterr_set(GITERR_NET, "Unable to generate name for temp file");
1061 return -1;
1062 }
1063
1064 if (buffer_len_cch < UUID_LENGTH_CCH + 1) {
1065 giterr_set(GITERR_NET, "Buffer too small for name of temp file");
1066 return -1;
1067 }
1068
1069 #if !defined(__MINGW32__) || defined(MINGW_HAS_SECURE_API)
1070 result = swprintf_s(buffer, buffer_len_cch,
1071 #else
1072 result = wsprintfW(buffer,
1073 #endif
1074 L"%08x%04x%04x%02x%02x%02x%02x%02x%02x%02x%02x",
1075 uuid.Data1, uuid.Data2, uuid.Data3,
1076 uuid.Data4[0], uuid.Data4[1], uuid.Data4[2], uuid.Data4[3],
1077 uuid.Data4[4], uuid.Data4[5], uuid.Data4[6], uuid.Data4[7]);
1078
1079 if (result < UUID_LENGTH_CCH) {
1080 giterr_set(GITERR_OS, "Unable to generate name for temp file");
1081 return -1;
1082 }
1083
1084 return 0;
1085 }
1086
1087 static int get_temp_file(LPWSTR buffer, DWORD buffer_len_cch)
1088 {
1089 size_t len;
1090
1091 if (!GetTempPathW(buffer_len_cch, buffer)) {
1092 giterr_set(GITERR_OS, "Failed to get temp path");
1093 return -1;
1094 }
1095
1096 len = wcslen(buffer);
1097
1098 if (buffer[len - 1] != '\\' && len < buffer_len_cch)
1099 buffer[len++] = '\\';
1100
1101 if (put_uuid_string(&buffer[len], (size_t)buffer_len_cch - len) < 0)
1102 return -1;
1103
1104 return 0;
1105 }
1106
1107 static int winhttp_stream_write_buffered(
1108 git_smart_subtransport_stream *stream,
1109 const char *buffer,
1110 size_t len)
1111 {
1112 winhttp_stream *s = (winhttp_stream *)stream;
1113 DWORD bytes_written;
1114
1115 if (!s->request && winhttp_stream_connect(s) < 0)
1116 return -1;
1117
1118 /* Buffer the payload, using a temporary file so we delegate
1119 * memory management of the data to the operating system. */
1120 if (!s->post_body) {
1121 wchar_t temp_path[MAX_PATH + 1];
1122
1123 if (get_temp_file(temp_path, MAX_PATH + 1) < 0)
1124 return -1;
1125
1126 s->post_body = CreateFileW(temp_path,
1127 GENERIC_READ | GENERIC_WRITE,
1128 FILE_SHARE_DELETE, NULL,
1129 CREATE_NEW,
1130 FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE | FILE_FLAG_SEQUENTIAL_SCAN,
1131 NULL);
1132
1133 if (INVALID_HANDLE_VALUE == s->post_body) {
1134 s->post_body = NULL;
1135 giterr_set(GITERR_OS, "Failed to create temporary file");
1136 return -1;
1137 }
1138 }
1139
1140 if (!WriteFile(s->post_body, buffer, (DWORD)len, &bytes_written, NULL)) {
1141 giterr_set(GITERR_OS, "Failed to write to temporary file");
1142 return -1;
1143 }
1144
1145 assert((DWORD)len == bytes_written);
1146
1147 s->post_body_len += bytes_written;
1148
1149 return 0;
1150 }
1151
1152 static int winhttp_stream_write_chunked(
1153 git_smart_subtransport_stream *stream,
1154 const char *buffer,
1155 size_t len)
1156 {
1157 winhttp_stream *s = (winhttp_stream *)stream;
1158 int error;
1159
1160 if (!s->request && winhttp_stream_connect(s) < 0)
1161 return -1;
1162
1163 if (!s->sent_request) {
1164 /* Send Transfer-Encoding: chunked header */
1165 if (!WinHttpAddRequestHeaders(s->request,
1166 transfer_encoding, (ULONG) -1L,
1167 WINHTTP_ADDREQ_FLAG_ADD)) {
1168 giterr_set(GITERR_OS, "Failed to add a header to the request");
1169 return -1;
1170 }
1171
1172 if ((error = send_request(s, 0, 1)) < 0)
1173 return error;
1174
1175 s->sent_request = 1;
1176 }
1177
1178 if (len > CACHED_POST_BODY_BUF_SIZE) {
1179 /* Flush, if necessary */
1180 if (s->chunk_buffer_len > 0) {
1181 if (write_chunk(s->request, s->chunk_buffer, s->chunk_buffer_len) < 0)
1182 return -1;
1183
1184 s->chunk_buffer_len = 0;
1185 }
1186
1187 /* Write chunk directly */
1188 if (write_chunk(s->request, buffer, len) < 0)
1189 return -1;
1190 }
1191 else {
1192 /* Append as much to the buffer as we can */
1193 int count = (int)min(CACHED_POST_BODY_BUF_SIZE - s->chunk_buffer_len, len);
1194
1195 if (!s->chunk_buffer)
1196 s->chunk_buffer = git__malloc(CACHED_POST_BODY_BUF_SIZE);
1197
1198 memcpy(s->chunk_buffer + s->chunk_buffer_len, buffer, count);
1199 s->chunk_buffer_len += count;
1200 buffer += count;
1201 len -= count;
1202
1203 /* Is the buffer full? If so, then flush */
1204 if (CACHED_POST_BODY_BUF_SIZE == s->chunk_buffer_len) {
1205 if (write_chunk(s->request, s->chunk_buffer, s->chunk_buffer_len) < 0)
1206 return -1;
1207
1208 s->chunk_buffer_len = 0;
1209
1210 /* Is there any remaining data from the source? */
1211 if (len > 0) {
1212 memcpy(s->chunk_buffer, buffer, len);
1213 s->chunk_buffer_len = (unsigned int)len;
1214 }
1215 }
1216 }
1217
1218 return 0;
1219 }
1220
1221 static void winhttp_stream_free(git_smart_subtransport_stream *stream)
1222 {
1223 winhttp_stream *s = (winhttp_stream *)stream;
1224
1225 winhttp_stream_close(s);
1226 git__free(s);
1227 }
1228
1229 static int winhttp_stream_alloc(winhttp_subtransport *t, winhttp_stream **stream)
1230 {
1231 winhttp_stream *s;
1232
1233 if (!stream)
1234 return -1;
1235
1236 s = git__calloc(1, sizeof(winhttp_stream));
1237 GITERR_CHECK_ALLOC(s);
1238
1239 s->parent.subtransport = &t->parent;
1240 s->parent.read = winhttp_stream_read;
1241 s->parent.write = winhttp_stream_write_single;
1242 s->parent.free = winhttp_stream_free;
1243
1244 *stream = s;
1245
1246 return 0;
1247 }
1248
1249 static int winhttp_uploadpack_ls(
1250 winhttp_subtransport *t,
1251 winhttp_stream *s)
1252 {
1253 GIT_UNUSED(t);
1254
1255 s->service = upload_pack_service;
1256 s->service_url = upload_pack_ls_service_url;
1257 s->verb = get_verb;
1258
1259 return 0;
1260 }
1261
1262 static int winhttp_uploadpack(
1263 winhttp_subtransport *t,
1264 winhttp_stream *s)
1265 {
1266 GIT_UNUSED(t);
1267
1268 s->service = upload_pack_service;
1269 s->service_url = upload_pack_service_url;
1270 s->verb = post_verb;
1271
1272 return 0;
1273 }
1274
1275 static int winhttp_receivepack_ls(
1276 winhttp_subtransport *t,
1277 winhttp_stream *s)
1278 {
1279 GIT_UNUSED(t);
1280
1281 s->service = receive_pack_service;
1282 s->service_url = receive_pack_ls_service_url;
1283 s->verb = get_verb;
1284
1285 return 0;
1286 }
1287
1288 static int winhttp_receivepack(
1289 winhttp_subtransport *t,
1290 winhttp_stream *s)
1291 {
1292 GIT_UNUSED(t);
1293
1294 /* WinHTTP only supports Transfer-Encoding: chunked
1295 * on Windows Vista (NT 6.0) and higher. */
1296 s->chunked = git_has_win32_version(6, 0, 0);
1297
1298 if (s->chunked)
1299 s->parent.write = winhttp_stream_write_chunked;
1300 else
1301 s->parent.write = winhttp_stream_write_buffered;
1302
1303 s->service = receive_pack_service;
1304 s->service_url = receive_pack_service_url;
1305 s->verb = post_verb;
1306
1307 return 0;
1308 }
1309
1310 static int winhttp_action(
1311 git_smart_subtransport_stream **stream,
1312 git_smart_subtransport *subtransport,
1313 const char *url,
1314 git_smart_service_t action)
1315 {
1316 winhttp_subtransport *t = (winhttp_subtransport *)subtransport;
1317 winhttp_stream *s;
1318 int ret = -1;
1319
1320 if (!t->connection)
1321 if ((ret = gitno_connection_data_from_url(&t->connection_data, url, NULL)) < 0 ||
1322 (ret = winhttp_connect(t)) < 0)
1323 return ret;
1324
1325 if (winhttp_stream_alloc(t, &s) < 0)
1326 return -1;
1327
1328 if (!stream)
1329 return -1;
1330
1331 switch (action)
1332 {
1333 case GIT_SERVICE_UPLOADPACK_LS:
1334 ret = winhttp_uploadpack_ls(t, s);
1335 break;
1336
1337 case GIT_SERVICE_UPLOADPACK:
1338 ret = winhttp_uploadpack(t, s);
1339 break;
1340
1341 case GIT_SERVICE_RECEIVEPACK_LS:
1342 ret = winhttp_receivepack_ls(t, s);
1343 break;
1344
1345 case GIT_SERVICE_RECEIVEPACK:
1346 ret = winhttp_receivepack(t, s);
1347 break;
1348
1349 default:
1350 assert(0);
1351 }
1352
1353 if (!ret)
1354 *stream = &s->parent;
1355
1356 return ret;
1357 }
1358
1359 static int winhttp_close(git_smart_subtransport *subtransport)
1360 {
1361 winhttp_subtransport *t = (winhttp_subtransport *)subtransport;
1362
1363 gitno_connection_data_free_ptrs(&t->connection_data);
1364 memset(&t->connection_data, 0x0, sizeof(gitno_connection_data));
1365
1366 if (t->cred) {
1367 t->cred->free(t->cred);
1368 t->cred = NULL;
1369 }
1370
1371 if (t->url_cred) {
1372 t->url_cred->free(t->url_cred);
1373 t->url_cred = NULL;
1374 }
1375
1376 return winhttp_close_connection(t);
1377 }
1378
1379 static void winhttp_free(git_smart_subtransport *subtransport)
1380 {
1381 winhttp_subtransport *t = (winhttp_subtransport *)subtransport;
1382
1383 winhttp_close(subtransport);
1384
1385 git__free(t);
1386 }
1387
1388 int git_smart_subtransport_http(git_smart_subtransport **out, git_transport *owner, void *param)
1389 {
1390 winhttp_subtransport *t;
1391
1392 GIT_UNUSED(param);
1393
1394 if (!out)
1395 return -1;
1396
1397 t = git__calloc(1, sizeof(winhttp_subtransport));
1398 GITERR_CHECK_ALLOC(t);
1399
1400 t->owner = (transport_smart *)owner;
1401 t->parent.action = winhttp_action;
1402 t->parent.close = winhttp_close;
1403 t->parent.free = winhttp_free;
1404
1405 *out = (git_smart_subtransport *) t;
1406 return 0;
1407 }
1408
1409 #endif /* GIT_WINHTTP */