]> git.proxmox.com Git - libgit2.git/blob - src/transports/winhttp.c
Merge pull request #3812 from stinb/fetch-tag-update-callback
[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 gitno_connection_data proxy_connection_data;
95 git_cred *cred;
96 git_cred *url_cred;
97 git_cred *proxy_cred;
98 int auth_mechanism;
99 HINTERNET session;
100 HINTERNET connection;
101 } winhttp_subtransport;
102
103 static int apply_basic_credential_proxy(HINTERNET request, git_cred *cred)
104 {
105 git_cred_userpass_plaintext *c = (git_cred_userpass_plaintext *)cred;
106 wchar_t *user, *pass;
107 int error;
108
109 if ((error = git__utf8_to_16_alloc(&user, c->username)) < 0)
110 return error;
111
112 if ((error = git__utf8_to_16_alloc(&pass, c->password)) < 0)
113 return error;
114
115 if (!WinHttpSetCredentials(request, WINHTTP_AUTH_TARGET_PROXY, WINHTTP_AUTH_SCHEME_BASIC,
116 user, pass, NULL)) {
117 giterr_set(GITERR_OS, "failed to set proxy auth");
118 error = -1;
119 }
120
121 git__free(user);
122 git__free(pass);
123
124 return error;
125 }
126
127 static int apply_basic_credential(HINTERNET request, git_cred *cred)
128 {
129 git_cred_userpass_plaintext *c = (git_cred_userpass_plaintext *)cred;
130 git_buf buf = GIT_BUF_INIT, raw = GIT_BUF_INIT;
131 wchar_t *wide = NULL;
132 int error = -1, wide_len;
133
134 git_buf_printf(&raw, "%s:%s", c->username, c->password);
135
136 if (git_buf_oom(&raw) ||
137 git_buf_puts(&buf, "Authorization: Basic ") < 0 ||
138 git_buf_encode_base64(&buf, git_buf_cstr(&raw), raw.size) < 0)
139 goto on_error;
140
141 if ((wide_len = git__utf8_to_16_alloc(&wide, git_buf_cstr(&buf))) < 0) {
142 giterr_set(GITERR_OS, "Failed to convert string to wide form");
143 goto on_error;
144 }
145
146 if (!WinHttpAddRequestHeaders(request, wide, (ULONG) -1L, WINHTTP_ADDREQ_FLAG_ADD)) {
147 giterr_set(GITERR_OS, "Failed to add a header to the request");
148 goto on_error;
149 }
150
151 error = 0;
152
153 on_error:
154 /* We were dealing with plaintext passwords, so clean up after ourselves a bit. */
155 if (wide)
156 memset(wide, 0x0, wide_len * sizeof(wchar_t));
157
158 if (buf.size)
159 memset(buf.ptr, 0x0, buf.size);
160
161 if (raw.size)
162 memset(raw.ptr, 0x0, raw.size);
163
164 git__free(wide);
165 git_buf_free(&buf);
166 git_buf_free(&raw);
167 return error;
168 }
169
170 static int apply_default_credentials(HINTERNET request)
171 {
172 /* Either the caller explicitly requested that default credentials be passed,
173 * or our fallback credential callback was invoked and checked that the target
174 * URI was in the appropriate Internet Explorer security zone. By setting this
175 * flag, we guarantee that the credentials are delivered by WinHTTP. The default
176 * is "medium" which applies to the intranet and sounds like it would correspond
177 * to Internet Explorer security zones, but in fact does not. */
178 DWORD data = WINHTTP_AUTOLOGON_SECURITY_LEVEL_LOW;
179
180 if (!WinHttpSetOption(request, WINHTTP_OPTION_AUTOLOGON_POLICY, &data, sizeof(DWORD)))
181 return -1;
182
183 return 0;
184 }
185
186 static int fallback_cred_acquire_cb(
187 git_cred **cred,
188 const char *url,
189 const char *username_from_url,
190 unsigned int allowed_types,
191 void *payload)
192 {
193 int error = 1;
194
195 GIT_UNUSED(username_from_url);
196 GIT_UNUSED(payload);
197
198 /* If the target URI supports integrated Windows authentication
199 * as an authentication mechanism */
200 if (GIT_CREDTYPE_DEFAULT & allowed_types) {
201 wchar_t *wide_url;
202
203 /* Convert URL to wide characters */
204 if (git__utf8_to_16_alloc(&wide_url, url) < 0) {
205 giterr_set(GITERR_OS, "Failed to convert string to wide form");
206 return -1;
207 }
208
209 if (SUCCEEDED(CoInitializeEx(NULL, COINIT_MULTITHREADED))) {
210 IInternetSecurityManager* pISM;
211
212 /* And if the target URI is in the My Computer, Intranet, or Trusted zones */
213 if (SUCCEEDED(CoCreateInstance(&CLSID_InternetSecurityManager, NULL,
214 CLSCTX_ALL, &IID_IInternetSecurityManager, (void **)&pISM))) {
215 DWORD dwZone;
216
217 if (SUCCEEDED(pISM->lpVtbl->MapUrlToZone(pISM, wide_url, &dwZone, 0)) &&
218 (URLZONE_LOCAL_MACHINE == dwZone ||
219 URLZONE_INTRANET == dwZone ||
220 URLZONE_TRUSTED == dwZone)) {
221 git_cred *existing = *cred;
222
223 if (existing)
224 existing->free(existing);
225
226 /* Then use default Windows credentials to authenticate this request */
227 error = git_cred_default_new(cred);
228 }
229
230 pISM->lpVtbl->Release(pISM);
231 }
232
233 CoUninitialize();
234 }
235
236 git__free(wide_url);
237 }
238
239 return error;
240 }
241
242 static int certificate_check(winhttp_stream *s, int valid)
243 {
244 int error;
245 winhttp_subtransport *t = OWNING_SUBTRANSPORT(s);
246 PCERT_CONTEXT cert_ctx;
247 DWORD cert_ctx_size = sizeof(cert_ctx);
248 git_cert_x509 cert;
249
250 /* If there is no override, we should fail if WinHTTP doesn't think it's fine */
251 if (t->owner->certificate_check_cb == NULL && !valid)
252 return GIT_ECERTIFICATE;
253
254 if (t->owner->certificate_check_cb == NULL || !t->connection_data.use_ssl)
255 return 0;
256
257 if (!WinHttpQueryOption(s->request, WINHTTP_OPTION_SERVER_CERT_CONTEXT, &cert_ctx, &cert_ctx_size)) {
258 giterr_set(GITERR_OS, "failed to get server certificate");
259 return -1;
260 }
261
262 giterr_clear();
263 cert.parent.cert_type = GIT_CERT_X509;
264 cert.data = cert_ctx->pbCertEncoded;
265 cert.len = cert_ctx->cbCertEncoded;
266 error = t->owner->certificate_check_cb((git_cert *) &cert, valid, t->connection_data.host, t->owner->cred_acquire_payload);
267 CertFreeCertificateContext(cert_ctx);
268
269 if (error < 0 && !giterr_last())
270 giterr_set(GITERR_NET, "user cancelled certificate check");
271
272 return error;
273 }
274
275 static void winhttp_stream_close(winhttp_stream *s)
276 {
277 if (s->chunk_buffer) {
278 git__free(s->chunk_buffer);
279 s->chunk_buffer = NULL;
280 }
281
282 if (s->post_body) {
283 CloseHandle(s->post_body);
284 s->post_body = NULL;
285 }
286
287 if (s->request_uri) {
288 git__free(s->request_uri);
289 s->request_uri = NULL;
290 }
291
292 if (s->request) {
293 WinHttpCloseHandle(s->request);
294 s->request = NULL;
295 }
296
297 s->sent_request = 0;
298 }
299
300 /**
301 * Extract the url and password from a URL. The outputs are pointers
302 * into the input.
303 */
304 static int userpass_from_url(wchar_t **user, int *user_len,
305 wchar_t **pass, int *pass_len,
306 const wchar_t *url, int url_len)
307 {
308 URL_COMPONENTS components = { 0 };
309
310 components.dwStructSize = sizeof(components);
311 /* These tell WinHttpCrackUrl that we're interested in the fields */
312 components.dwUserNameLength = 1;
313 components.dwPasswordLength = 1;
314
315 if (!WinHttpCrackUrl(url, url_len, 0, &components)) {
316 giterr_set(GITERR_OS, "failed to extract user/pass from url");
317 return -1;
318 }
319
320 *user = components.lpszUserName;
321 *user_len = components.dwUserNameLength;
322 *pass = components.lpszPassword;
323 *pass_len = components.dwPasswordLength;
324
325 return 0;
326 }
327
328 #define SCHEME_HTTP "http://"
329 #define SCHEME_HTTPS "https://"
330
331 static int winhttp_stream_connect(winhttp_stream *s)
332 {
333 winhttp_subtransport *t = OWNING_SUBTRANSPORT(s);
334 git_buf buf = GIT_BUF_INIT;
335 char *proxy_url = NULL;
336 wchar_t ct[MAX_CONTENT_TYPE_LEN];
337 LPCWSTR types[] = { L"*/*", NULL };
338 BOOL peerdist = FALSE;
339 int error = -1;
340 unsigned long disable_redirects = WINHTTP_DISABLE_REDIRECTS;
341 int default_timeout = TIMEOUT_INFINITE;
342 int default_connect_timeout = DEFAULT_CONNECT_TIMEOUT;
343 size_t i;
344 const git_proxy_options *proxy_opts;
345
346 /* Prepare URL */
347 git_buf_printf(&buf, "%s%s", t->connection_data.path, s->service_url);
348
349 if (git_buf_oom(&buf))
350 return -1;
351
352 /* Convert URL to wide characters */
353 if (git__utf8_to_16_alloc(&s->request_uri, git_buf_cstr(&buf)) < 0) {
354 giterr_set(GITERR_OS, "Failed to convert string to wide form");
355 goto on_error;
356 }
357
358 /* Establish request */
359 s->request = WinHttpOpenRequest(
360 t->connection,
361 s->verb,
362 s->request_uri,
363 NULL,
364 WINHTTP_NO_REFERER,
365 types,
366 t->connection_data.use_ssl ? WINHTTP_FLAG_SECURE : 0);
367
368 if (!s->request) {
369 giterr_set(GITERR_OS, "Failed to open request");
370 goto on_error;
371 }
372
373 if (!WinHttpSetTimeouts(s->request, default_timeout, default_connect_timeout, default_timeout, default_timeout)) {
374 giterr_set(GITERR_OS, "Failed to set timeouts for WinHTTP");
375 goto on_error;
376 }
377
378 proxy_opts = &t->owner->proxy;
379 if (proxy_opts->type == GIT_PROXY_AUTO) {
380 /* Set proxy if necessary */
381 if (git_remote__get_http_proxy(t->owner->owner, !!t->connection_data.use_ssl, &proxy_url) < 0)
382 goto on_error;
383 }
384 else if (proxy_opts->type == GIT_PROXY_SPECIFIED) {
385 proxy_url = git__strdup(proxy_opts->url);
386 GITERR_CHECK_ALLOC(proxy_url);
387 }
388
389 if (proxy_url) {
390 git_buf processed_url = GIT_BUF_INIT;
391 WINHTTP_PROXY_INFO proxy_info;
392 wchar_t *proxy_wide;
393
394 if (!git__prefixcmp(proxy_url, SCHEME_HTTP)) {
395 t->proxy_connection_data.use_ssl = false;
396 } else if (!git__prefixcmp(proxy_url, SCHEME_HTTPS)) {
397 t->proxy_connection_data.use_ssl = true;
398 } else {
399 giterr_set(GITERR_NET, "invalid URL: '%s'", proxy_url);
400 return -1;
401 }
402
403 gitno_connection_data_free_ptrs(&t->proxy_connection_data);
404
405 if ((error = gitno_extract_url_parts(&t->proxy_connection_data.host, &t->proxy_connection_data.port, NULL,
406 &t->proxy_connection_data.user, &t->proxy_connection_data.pass, proxy_url, NULL)) < 0)
407 goto on_error;
408
409 if (t->proxy_connection_data.user && t->proxy_connection_data.pass) {
410 if (t->proxy_cred) {
411 t->proxy_cred->free(t->proxy_cred);
412 }
413
414 if ((error = git_cred_userpass_plaintext_new(&t->proxy_cred, t->proxy_connection_data.user, t->proxy_connection_data.pass)) < 0)
415 goto on_error;
416 }
417
418 if (t->proxy_connection_data.use_ssl)
419 git_buf_PUTS(&processed_url, SCHEME_HTTPS);
420 else
421 git_buf_PUTS(&processed_url, SCHEME_HTTP);
422
423 git_buf_puts(&processed_url, t->proxy_connection_data.host);
424 if (t->proxy_connection_data.port)
425 git_buf_printf(&processed_url, ":%s", t->proxy_connection_data.port);
426
427 if (git_buf_oom(&processed_url)) {
428 giterr_set_oom();
429 error = -1;
430 goto on_error;
431 }
432
433 /* Convert URL to wide characters */
434 error = git__utf8_to_16_alloc(&proxy_wide, processed_url.ptr);
435 git_buf_free(&processed_url);
436 if (error < 0)
437 goto on_error;
438
439 proxy_info.dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
440 proxy_info.lpszProxy = proxy_wide;
441 proxy_info.lpszProxyBypass = NULL;
442
443 if (!WinHttpSetOption(s->request,
444 WINHTTP_OPTION_PROXY,
445 &proxy_info,
446 sizeof(WINHTTP_PROXY_INFO))) {
447 giterr_set(GITERR_OS, "Failed to set proxy");
448 git__free(proxy_wide);
449 goto on_error;
450 }
451
452 git__free(proxy_wide);
453
454 if (t->proxy_cred) {
455 if (t->proxy_cred->credtype == GIT_CREDTYPE_USERPASS_PLAINTEXT) {
456 if ((error = apply_basic_credential_proxy(s->request, t->proxy_cred)) < 0)
457 goto on_error;
458 }
459 }
460
461 }
462
463 /* Disable WinHTTP redirects so we can handle them manually. Why, you ask?
464 * http://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/b2ff8879-ab9f-4218-8f09-16d25dff87ae
465 */
466 if (!WinHttpSetOption(s->request,
467 WINHTTP_OPTION_DISABLE_FEATURE,
468 &disable_redirects,
469 sizeof(disable_redirects))) {
470 giterr_set(GITERR_OS, "Failed to disable redirects");
471 goto on_error;
472 }
473
474 /* Strip unwanted headers (X-P2P-PeerDist, X-P2P-PeerDistEx) that WinHTTP
475 * adds itself. This option may not be supported by the underlying
476 * platform, so we do not error-check it */
477 WinHttpSetOption(s->request,
478 WINHTTP_OPTION_PEERDIST_EXTENSION_STATE,
479 &peerdist,
480 sizeof(peerdist));
481
482 /* Send Pragma: no-cache header */
483 if (!WinHttpAddRequestHeaders(s->request, pragma_nocache, (ULONG) -1L, WINHTTP_ADDREQ_FLAG_ADD)) {
484 giterr_set(GITERR_OS, "Failed to add a header to the request");
485 goto on_error;
486 }
487
488 if (post_verb == s->verb) {
489 /* Send Content-Type and Accept headers -- only necessary on a POST */
490 git_buf_clear(&buf);
491 if (git_buf_printf(&buf,
492 "Content-Type: application/x-git-%s-request",
493 s->service) < 0)
494 goto on_error;
495
496 if (git__utf8_to_16(ct, MAX_CONTENT_TYPE_LEN, git_buf_cstr(&buf)) < 0) {
497 giterr_set(GITERR_OS, "Failed to convert content-type to wide characters");
498 goto on_error;
499 }
500
501 if (!WinHttpAddRequestHeaders(s->request, ct, (ULONG)-1L,
502 WINHTTP_ADDREQ_FLAG_ADD | WINHTTP_ADDREQ_FLAG_REPLACE)) {
503 giterr_set(GITERR_OS, "Failed to add a header to the request");
504 goto on_error;
505 }
506
507 git_buf_clear(&buf);
508 if (git_buf_printf(&buf,
509 "Accept: application/x-git-%s-result",
510 s->service) < 0)
511 goto on_error;
512
513 if (git__utf8_to_16(ct, MAX_CONTENT_TYPE_LEN, git_buf_cstr(&buf)) < 0) {
514 giterr_set(GITERR_OS, "Failed to convert accept header to wide characters");
515 goto on_error;
516 }
517
518 if (!WinHttpAddRequestHeaders(s->request, ct, (ULONG)-1L,
519 WINHTTP_ADDREQ_FLAG_ADD | WINHTTP_ADDREQ_FLAG_REPLACE)) {
520 giterr_set(GITERR_OS, "Failed to add a header to the request");
521 goto on_error;
522 }
523 }
524
525 for (i = 0; i < t->owner->custom_headers.count; i++) {
526 if (t->owner->custom_headers.strings[i]) {
527 git_buf_clear(&buf);
528 git_buf_puts(&buf, t->owner->custom_headers.strings[i]);
529 if (git__utf8_to_16(ct, MAX_CONTENT_TYPE_LEN, git_buf_cstr(&buf)) < 0) {
530 giterr_set(GITERR_OS, "Failed to convert custom header to wide characters");
531 goto on_error;
532 }
533
534 if (!WinHttpAddRequestHeaders(s->request, ct, (ULONG)-1L,
535 WINHTTP_ADDREQ_FLAG_ADD | WINHTTP_ADDREQ_FLAG_REPLACE)) {
536 giterr_set(GITERR_OS, "Failed to add a header to the request");
537 goto on_error;
538 }
539 }
540 }
541
542 /* If requested, disable certificate validation */
543 if (t->connection_data.use_ssl) {
544 int flags;
545
546 if (t->owner->parent.read_flags(&t->owner->parent, &flags) < 0)
547 goto on_error;
548 }
549
550 /* If we have a credential on the subtransport, apply it to the request */
551 if (t->cred &&
552 t->cred->credtype == GIT_CREDTYPE_USERPASS_PLAINTEXT &&
553 t->auth_mechanism == GIT_WINHTTP_AUTH_BASIC &&
554 apply_basic_credential(s->request, t->cred) < 0)
555 goto on_error;
556 else if (t->cred &&
557 t->cred->credtype == GIT_CREDTYPE_DEFAULT &&
558 t->auth_mechanism == GIT_WINHTTP_AUTH_NEGOTIATE &&
559 apply_default_credentials(s->request) < 0)
560 goto on_error;
561
562 /* If no other credentials have been applied and the URL has username and
563 * password, use those */
564 if (!t->cred && t->connection_data.user && t->connection_data.pass) {
565 if (!t->url_cred &&
566 git_cred_userpass_plaintext_new(&t->url_cred, t->connection_data.user, t->connection_data.pass) < 0)
567 goto on_error;
568 if (apply_basic_credential(s->request, t->url_cred) < 0)
569 goto on_error;
570 }
571
572 /* We've done everything up to calling WinHttpSendRequest. */
573
574 error = 0;
575
576 on_error:
577 if (error < 0)
578 winhttp_stream_close(s);
579
580 git__free(proxy_url);
581 git_buf_free(&buf);
582 return error;
583 }
584
585 static int parse_unauthorized_response(
586 HINTERNET request,
587 int *allowed_types,
588 int *auth_mechanism)
589 {
590 DWORD supported, first, target;
591
592 *allowed_types = 0;
593 *auth_mechanism = 0;
594
595 /* WinHttpQueryHeaders() must be called before WinHttpQueryAuthSchemes().
596 * We can assume this was already done, since we know we are unauthorized.
597 */
598 if (!WinHttpQueryAuthSchemes(request, &supported, &first, &target)) {
599 giterr_set(GITERR_OS, "Failed to parse supported auth schemes");
600 return -1;
601 }
602
603 if (WINHTTP_AUTH_SCHEME_BASIC & supported) {
604 *allowed_types |= GIT_CREDTYPE_USERPASS_PLAINTEXT;
605 *auth_mechanism = GIT_WINHTTP_AUTH_BASIC;
606 }
607
608 if ((WINHTTP_AUTH_SCHEME_NTLM & supported) ||
609 (WINHTTP_AUTH_SCHEME_NEGOTIATE & supported)) {
610 *allowed_types |= GIT_CREDTYPE_DEFAULT;
611 *auth_mechanism = GIT_WINHTTP_AUTH_NEGOTIATE;
612 }
613
614 return 0;
615 }
616
617 static int write_chunk(HINTERNET request, const char *buffer, size_t len)
618 {
619 DWORD bytes_written;
620 git_buf buf = GIT_BUF_INIT;
621
622 /* Chunk header */
623 git_buf_printf(&buf, "%X\r\n", len);
624
625 if (git_buf_oom(&buf))
626 return -1;
627
628 if (!WinHttpWriteData(request,
629 git_buf_cstr(&buf), (DWORD)git_buf_len(&buf),
630 &bytes_written)) {
631 git_buf_free(&buf);
632 giterr_set(GITERR_OS, "Failed to write chunk header");
633 return -1;
634 }
635
636 git_buf_free(&buf);
637
638 /* Chunk body */
639 if (!WinHttpWriteData(request,
640 buffer, (DWORD)len,
641 &bytes_written)) {
642 giterr_set(GITERR_OS, "Failed to write chunk");
643 return -1;
644 }
645
646 /* Chunk footer */
647 if (!WinHttpWriteData(request,
648 "\r\n", 2,
649 &bytes_written)) {
650 giterr_set(GITERR_OS, "Failed to write chunk footer");
651 return -1;
652 }
653
654 return 0;
655 }
656
657 static int winhttp_close_connection(winhttp_subtransport *t)
658 {
659 int ret = 0;
660
661 if (t->connection) {
662 if (!WinHttpCloseHandle(t->connection)) {
663 giterr_set(GITERR_OS, "Unable to close connection");
664 ret = -1;
665 }
666
667 t->connection = NULL;
668 }
669
670 if (t->session) {
671 if (!WinHttpCloseHandle(t->session)) {
672 giterr_set(GITERR_OS, "Unable to close session");
673 ret = -1;
674 }
675
676 t->session = NULL;
677 }
678
679 return ret;
680 }
681
682 static int user_agent(git_buf *ua)
683 {
684 const char *custom = git_libgit2__user_agent();
685
686 git_buf_clear(ua);
687 git_buf_PUTS(ua, "git/1.0 (");
688
689 if (custom)
690 git_buf_puts(ua, custom);
691 else
692 git_buf_PUTS(ua, "libgit2 " LIBGIT2_VERSION);
693
694 return git_buf_putc(ua, ')');
695 }
696
697 static int winhttp_connect(
698 winhttp_subtransport *t)
699 {
700 wchar_t *wide_host;
701 int32_t port;
702 wchar_t *wide_ua;
703 git_buf ua = GIT_BUF_INIT;
704 int error = -1;
705 int default_timeout = TIMEOUT_INFINITE;
706 int default_connect_timeout = DEFAULT_CONNECT_TIMEOUT;
707
708 t->session = NULL;
709 t->connection = NULL;
710
711 /* Prepare port */
712 if (git__strtol32(&port, t->connection_data.port, NULL, 10) < 0)
713 return -1;
714
715 /* Prepare host */
716 if (git__utf8_to_16_alloc(&wide_host, t->connection_data.host) < 0) {
717 giterr_set(GITERR_OS, "Unable to convert host to wide characters");
718 return -1;
719 }
720
721 if ((error = user_agent(&ua)) < 0) {
722 git__free(wide_host);
723 return error;
724 }
725
726 if (git__utf8_to_16_alloc(&wide_ua, git_buf_cstr(&ua)) < 0) {
727 giterr_set(GITERR_OS, "Unable to convert host to wide characters");
728 git__free(wide_host);
729 git_buf_free(&ua);
730 return -1;
731 }
732
733 git_buf_free(&ua);
734
735 /* Establish session */
736 t->session = WinHttpOpen(
737 wide_ua,
738 WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
739 WINHTTP_NO_PROXY_NAME,
740 WINHTTP_NO_PROXY_BYPASS,
741 0);
742
743 if (!t->session) {
744 giterr_set(GITERR_OS, "Failed to init WinHTTP");
745 goto on_error;
746 }
747
748 if (!WinHttpSetTimeouts(t->session, default_timeout, default_connect_timeout, default_timeout, default_timeout)) {
749 giterr_set(GITERR_OS, "Failed to set timeouts for WinHTTP");
750 goto on_error;
751 }
752
753
754 /* Establish connection */
755 t->connection = WinHttpConnect(
756 t->session,
757 wide_host,
758 (INTERNET_PORT) port,
759 0);
760
761 if (!t->connection) {
762 giterr_set(GITERR_OS, "Failed to connect to host");
763 goto on_error;
764 }
765
766 error = 0;
767
768 on_error:
769 if (error < 0)
770 winhttp_close_connection(t);
771
772 git__free(wide_host);
773 git__free(wide_ua);
774
775 return error;
776 }
777
778 static int do_send_request(winhttp_stream *s, size_t len, int ignore_length)
779 {
780 if (ignore_length) {
781 if (!WinHttpSendRequest(s->request,
782 WINHTTP_NO_ADDITIONAL_HEADERS, 0,
783 WINHTTP_NO_REQUEST_DATA, 0,
784 WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH, 0)) {
785 return -1;
786 }
787 } else {
788 if (!WinHttpSendRequest(s->request,
789 WINHTTP_NO_ADDITIONAL_HEADERS, 0,
790 WINHTTP_NO_REQUEST_DATA, 0,
791 len, 0)) {
792 return -1;
793 }
794 }
795
796 return 0;
797 }
798
799 static int send_request(winhttp_stream *s, size_t len, int ignore_length)
800 {
801 int request_failed = 0, cert_valid = 1, error = 0;
802 DWORD ignore_flags;
803
804 if ((error = do_send_request(s, len, ignore_length)) < 0)
805 request_failed = 1;
806
807 if (request_failed) {
808 if (GetLastError() != ERROR_WINHTTP_SECURE_FAILURE) {
809 giterr_set(GITERR_OS, "failed to send request");
810 return -1;
811 } else {
812 cert_valid = 0;
813 }
814 }
815
816 giterr_clear();
817 if ((error = certificate_check(s, cert_valid)) < 0) {
818 if (!giterr_last())
819 giterr_set(GITERR_OS, "user cancelled certificate check");
820
821 return error;
822 }
823
824 /* if neither the request nor the certificate check returned errors, we're done */
825 if (!request_failed)
826 return 0;
827
828 ignore_flags = no_check_cert_flags;
829
830 if (!WinHttpSetOption(s->request, WINHTTP_OPTION_SECURITY_FLAGS, &ignore_flags, sizeof(ignore_flags))) {
831 giterr_set(GITERR_OS, "failed to set security options");
832 return -1;
833 }
834
835 if ((error = do_send_request(s, len, ignore_length)) < 0)
836 giterr_set(GITERR_OS, "failed to send request");
837
838 return error;
839 }
840
841 static int winhttp_stream_read(
842 git_smart_subtransport_stream *stream,
843 char *buffer,
844 size_t buf_size,
845 size_t *bytes_read)
846 {
847 winhttp_stream *s = (winhttp_stream *)stream;
848 winhttp_subtransport *t = OWNING_SUBTRANSPORT(s);
849 DWORD dw_bytes_read;
850 char replay_count = 0;
851 int error;
852
853 replay:
854 /* Enforce a reasonable cap on the number of replays */
855 if (++replay_count >= 7) {
856 giterr_set(GITERR_NET, "Too many redirects or authentication replays");
857 return -1;
858 }
859
860 /* Connect if necessary */
861 if (!s->request && winhttp_stream_connect(s) < 0)
862 return -1;
863
864 if (!s->received_response) {
865 DWORD status_code, status_code_length, content_type_length, bytes_written;
866 char expected_content_type_8[MAX_CONTENT_TYPE_LEN];
867 wchar_t expected_content_type[MAX_CONTENT_TYPE_LEN], content_type[MAX_CONTENT_TYPE_LEN];
868
869 if (!s->sent_request) {
870
871 if ((error = send_request(s, s->post_body_len, 0)) < 0)
872 return error;
873
874 s->sent_request = 1;
875 }
876
877 if (s->chunked) {
878 assert(s->verb == post_verb);
879
880 /* Flush, if necessary */
881 if (s->chunk_buffer_len > 0 &&
882 write_chunk(s->request, s->chunk_buffer, s->chunk_buffer_len) < 0)
883 return -1;
884
885 s->chunk_buffer_len = 0;
886
887 /* Write the final chunk. */
888 if (!WinHttpWriteData(s->request,
889 "0\r\n\r\n", 5,
890 &bytes_written)) {
891 giterr_set(GITERR_OS, "Failed to write final chunk");
892 return -1;
893 }
894 }
895 else if (s->post_body) {
896 char *buffer;
897 DWORD len = s->post_body_len, bytes_read;
898
899 if (INVALID_SET_FILE_POINTER == SetFilePointer(s->post_body,
900 0, 0, FILE_BEGIN) &&
901 NO_ERROR != GetLastError()) {
902 giterr_set(GITERR_OS, "Failed to reset file pointer");
903 return -1;
904 }
905
906 buffer = git__malloc(CACHED_POST_BODY_BUF_SIZE);
907
908 while (len > 0) {
909 DWORD bytes_written;
910
911 if (!ReadFile(s->post_body, buffer,
912 min(CACHED_POST_BODY_BUF_SIZE, len),
913 &bytes_read, NULL) ||
914 !bytes_read) {
915 git__free(buffer);
916 giterr_set(GITERR_OS, "Failed to read from temp file");
917 return -1;
918 }
919
920 if (!WinHttpWriteData(s->request, buffer,
921 bytes_read, &bytes_written)) {
922 git__free(buffer);
923 giterr_set(GITERR_OS, "Failed to write data");
924 return -1;
925 }
926
927 len -= bytes_read;
928 assert(bytes_read == bytes_written);
929 }
930
931 git__free(buffer);
932
933 /* Eagerly close the temp file */
934 CloseHandle(s->post_body);
935 s->post_body = NULL;
936 }
937
938 if (!WinHttpReceiveResponse(s->request, 0)) {
939 giterr_set(GITERR_OS, "Failed to receive response");
940 return -1;
941 }
942
943 /* Verify that we got a 200 back */
944 status_code_length = sizeof(status_code);
945
946 if (!WinHttpQueryHeaders(s->request,
947 WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER,
948 WINHTTP_HEADER_NAME_BY_INDEX,
949 &status_code, &status_code_length,
950 WINHTTP_NO_HEADER_INDEX)) {
951 giterr_set(GITERR_OS, "Failed to retrieve status code");
952 return -1;
953 }
954
955 /* The implementation of WinHTTP prior to Windows 7 will not
956 * redirect to an identical URI. Some Git hosters use self-redirects
957 * as part of their DoS mitigation strategy. Check first to see if we
958 * have a redirect status code, and that we haven't already streamed
959 * a post body. (We can't replay a streamed POST.) */
960 if (!s->chunked &&
961 (HTTP_STATUS_MOVED == status_code ||
962 HTTP_STATUS_REDIRECT == status_code ||
963 (HTTP_STATUS_REDIRECT_METHOD == status_code &&
964 get_verb == s->verb) ||
965 HTTP_STATUS_REDIRECT_KEEP_VERB == status_code)) {
966
967 /* Check for Windows 7. This workaround is only necessary on
968 * Windows Vista and earlier. Windows 7 is version 6.1. */
969 wchar_t *location;
970 DWORD location_length;
971 char *location8;
972
973 /* OK, fetch the Location header from the redirect. */
974 if (WinHttpQueryHeaders(s->request,
975 WINHTTP_QUERY_LOCATION,
976 WINHTTP_HEADER_NAME_BY_INDEX,
977 WINHTTP_NO_OUTPUT_BUFFER,
978 &location_length,
979 WINHTTP_NO_HEADER_INDEX) ||
980 GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
981 giterr_set(GITERR_OS, "Failed to read Location header");
982 return -1;
983 }
984
985 location = git__malloc(location_length);
986 GITERR_CHECK_ALLOC(location);
987
988 if (!WinHttpQueryHeaders(s->request,
989 WINHTTP_QUERY_LOCATION,
990 WINHTTP_HEADER_NAME_BY_INDEX,
991 location,
992 &location_length,
993 WINHTTP_NO_HEADER_INDEX)) {
994 giterr_set(GITERR_OS, "Failed to read Location header");
995 git__free(location);
996 return -1;
997 }
998
999 /* Convert the Location header to UTF-8 */
1000 if (git__utf16_to_8_alloc(&location8, location) < 0) {
1001 giterr_set(GITERR_OS, "Failed to convert Location header to UTF-8");
1002 git__free(location);
1003 return -1;
1004 }
1005
1006 git__free(location);
1007
1008 /* Replay the request */
1009 winhttp_stream_close(s);
1010
1011 if (!git__prefixcmp_icase(location8, prefix_https)) {
1012 /* Upgrade to secure connection; disconnect and start over */
1013 if (gitno_connection_data_from_url(&t->connection_data, location8, s->service_url) < 0) {
1014 git__free(location8);
1015 return -1;
1016 }
1017
1018 winhttp_close_connection(t);
1019
1020 if (winhttp_connect(t) < 0)
1021 return -1;
1022 }
1023
1024 git__free(location8);
1025 goto replay;
1026 }
1027
1028 /* Handle proxy authentication failures */
1029 if (status_code == HTTP_STATUS_PROXY_AUTH_REQ) {
1030 int allowed_types;
1031
1032 if (parse_unauthorized_response(s->request, &allowed_types, &t->auth_mechanism) < 0)
1033 return -1;
1034
1035 /* TODO: extract the username from the url, no payload? */
1036 if (t->owner->proxy.credentials) {
1037 int cred_error = 1;
1038 cred_error = t->owner->proxy.credentials(&t->proxy_cred, t->owner->proxy.url, NULL, allowed_types, NULL);
1039
1040 if (cred_error < 0)
1041 return cred_error;
1042 }
1043
1044 winhttp_stream_close(s);
1045 goto replay;
1046 }
1047
1048 /* Handle authentication failures */
1049 if (HTTP_STATUS_DENIED == status_code && get_verb == s->verb) {
1050 int allowed_types;
1051
1052 if (parse_unauthorized_response(s->request, &allowed_types, &t->auth_mechanism) < 0)
1053 return -1;
1054
1055 if (allowed_types) {
1056 int cred_error = 1;
1057
1058 git_cred_free(t->cred);
1059 t->cred = NULL;
1060 /* Start with the user-supplied credential callback, if present */
1061 if (t->owner->cred_acquire_cb) {
1062 cred_error = t->owner->cred_acquire_cb(&t->cred, t->owner->url,
1063 t->connection_data.user, allowed_types, t->owner->cred_acquire_payload);
1064
1065 /* Treat GIT_PASSTHROUGH as though git_cred_acquire_cb isn't set */
1066 if (cred_error == GIT_PASSTHROUGH)
1067 cred_error = 1;
1068 else if (cred_error < 0)
1069 return cred_error;
1070 }
1071
1072 /* Invoke the fallback credentials acquisition callback if necessary */
1073 if (cred_error > 0) {
1074 cred_error = fallback_cred_acquire_cb(&t->cred, t->owner->url,
1075 t->connection_data.user, allowed_types, NULL);
1076
1077 if (cred_error < 0)
1078 return cred_error;
1079 }
1080
1081 if (!cred_error) {
1082 assert(t->cred);
1083
1084 winhttp_stream_close(s);
1085
1086 /* Successfully acquired a credential */
1087 goto replay;
1088 }
1089 }
1090 }
1091
1092 if (HTTP_STATUS_OK != status_code) {
1093 giterr_set(GITERR_NET, "Request failed with status code: %d", status_code);
1094 return -1;
1095 }
1096
1097 /* Verify that we got the correct content-type back */
1098 if (post_verb == s->verb)
1099 p_snprintf(expected_content_type_8, MAX_CONTENT_TYPE_LEN, "application/x-git-%s-result", s->service);
1100 else
1101 p_snprintf(expected_content_type_8, MAX_CONTENT_TYPE_LEN, "application/x-git-%s-advertisement", s->service);
1102
1103 if (git__utf8_to_16(expected_content_type, MAX_CONTENT_TYPE_LEN, expected_content_type_8) < 0) {
1104 giterr_set(GITERR_OS, "Failed to convert expected content-type to wide characters");
1105 return -1;
1106 }
1107
1108 content_type_length = sizeof(content_type);
1109
1110 if (!WinHttpQueryHeaders(s->request,
1111 WINHTTP_QUERY_CONTENT_TYPE,
1112 WINHTTP_HEADER_NAME_BY_INDEX,
1113 &content_type, &content_type_length,
1114 WINHTTP_NO_HEADER_INDEX)) {
1115 giterr_set(GITERR_OS, "Failed to retrieve response content-type");
1116 return -1;
1117 }
1118
1119 if (wcscmp(expected_content_type, content_type)) {
1120 giterr_set(GITERR_NET, "Received unexpected content-type");
1121 return -1;
1122 }
1123
1124 s->received_response = 1;
1125 }
1126
1127 if (!WinHttpReadData(s->request,
1128 (LPVOID)buffer,
1129 (DWORD)buf_size,
1130 &dw_bytes_read))
1131 {
1132 giterr_set(GITERR_OS, "Failed to read data");
1133 return -1;
1134 }
1135
1136 *bytes_read = dw_bytes_read;
1137
1138 return 0;
1139 }
1140
1141 static int winhttp_stream_write_single(
1142 git_smart_subtransport_stream *stream,
1143 const char *buffer,
1144 size_t len)
1145 {
1146 winhttp_stream *s = (winhttp_stream *)stream;
1147 DWORD bytes_written;
1148 int error;
1149
1150 if (!s->request && winhttp_stream_connect(s) < 0)
1151 return -1;
1152
1153 /* This implementation of write permits only a single call. */
1154 if (s->sent_request) {
1155 giterr_set(GITERR_NET, "Subtransport configured for only one write");
1156 return -1;
1157 }
1158
1159 if ((error = send_request(s, len, 0)) < 0)
1160 return error;
1161
1162 s->sent_request = 1;
1163
1164 if (!WinHttpWriteData(s->request,
1165 (LPCVOID)buffer,
1166 (DWORD)len,
1167 &bytes_written)) {
1168 giterr_set(GITERR_OS, "Failed to write data");
1169 return -1;
1170 }
1171
1172 assert((DWORD)len == bytes_written);
1173
1174 return 0;
1175 }
1176
1177 static int put_uuid_string(LPWSTR buffer, size_t buffer_len_cch)
1178 {
1179 UUID uuid;
1180 RPC_STATUS status = UuidCreate(&uuid);
1181 int result;
1182
1183 if (RPC_S_OK != status &&
1184 RPC_S_UUID_LOCAL_ONLY != status &&
1185 RPC_S_UUID_NO_ADDRESS != status) {
1186 giterr_set(GITERR_NET, "Unable to generate name for temp file");
1187 return -1;
1188 }
1189
1190 if (buffer_len_cch < UUID_LENGTH_CCH + 1) {
1191 giterr_set(GITERR_NET, "Buffer too small for name of temp file");
1192 return -1;
1193 }
1194
1195 #if !defined(__MINGW32__) || defined(MINGW_HAS_SECURE_API)
1196 result = swprintf_s(buffer, buffer_len_cch,
1197 #else
1198 result = wsprintfW(buffer,
1199 #endif
1200 L"%08x%04x%04x%02x%02x%02x%02x%02x%02x%02x%02x",
1201 uuid.Data1, uuid.Data2, uuid.Data3,
1202 uuid.Data4[0], uuid.Data4[1], uuid.Data4[2], uuid.Data4[3],
1203 uuid.Data4[4], uuid.Data4[5], uuid.Data4[6], uuid.Data4[7]);
1204
1205 if (result < UUID_LENGTH_CCH) {
1206 giterr_set(GITERR_OS, "Unable to generate name for temp file");
1207 return -1;
1208 }
1209
1210 return 0;
1211 }
1212
1213 static int get_temp_file(LPWSTR buffer, DWORD buffer_len_cch)
1214 {
1215 size_t len;
1216
1217 if (!GetTempPathW(buffer_len_cch, buffer)) {
1218 giterr_set(GITERR_OS, "Failed to get temp path");
1219 return -1;
1220 }
1221
1222 len = wcslen(buffer);
1223
1224 if (buffer[len - 1] != '\\' && len < buffer_len_cch)
1225 buffer[len++] = '\\';
1226
1227 if (put_uuid_string(&buffer[len], (size_t)buffer_len_cch - len) < 0)
1228 return -1;
1229
1230 return 0;
1231 }
1232
1233 static int winhttp_stream_write_buffered(
1234 git_smart_subtransport_stream *stream,
1235 const char *buffer,
1236 size_t len)
1237 {
1238 winhttp_stream *s = (winhttp_stream *)stream;
1239 DWORD bytes_written;
1240
1241 if (!s->request && winhttp_stream_connect(s) < 0)
1242 return -1;
1243
1244 /* Buffer the payload, using a temporary file so we delegate
1245 * memory management of the data to the operating system. */
1246 if (!s->post_body) {
1247 wchar_t temp_path[MAX_PATH + 1];
1248
1249 if (get_temp_file(temp_path, MAX_PATH + 1) < 0)
1250 return -1;
1251
1252 s->post_body = CreateFileW(temp_path,
1253 GENERIC_READ | GENERIC_WRITE,
1254 FILE_SHARE_DELETE, NULL,
1255 CREATE_NEW,
1256 FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE | FILE_FLAG_SEQUENTIAL_SCAN,
1257 NULL);
1258
1259 if (INVALID_HANDLE_VALUE == s->post_body) {
1260 s->post_body = NULL;
1261 giterr_set(GITERR_OS, "Failed to create temporary file");
1262 return -1;
1263 }
1264 }
1265
1266 if (!WriteFile(s->post_body, buffer, (DWORD)len, &bytes_written, NULL)) {
1267 giterr_set(GITERR_OS, "Failed to write to temporary file");
1268 return -1;
1269 }
1270
1271 assert((DWORD)len == bytes_written);
1272
1273 s->post_body_len += bytes_written;
1274
1275 return 0;
1276 }
1277
1278 static int winhttp_stream_write_chunked(
1279 git_smart_subtransport_stream *stream,
1280 const char *buffer,
1281 size_t len)
1282 {
1283 winhttp_stream *s = (winhttp_stream *)stream;
1284 int error;
1285
1286 if (!s->request && winhttp_stream_connect(s) < 0)
1287 return -1;
1288
1289 if (!s->sent_request) {
1290 /* Send Transfer-Encoding: chunked header */
1291 if (!WinHttpAddRequestHeaders(s->request,
1292 transfer_encoding, (ULONG) -1L,
1293 WINHTTP_ADDREQ_FLAG_ADD)) {
1294 giterr_set(GITERR_OS, "Failed to add a header to the request");
1295 return -1;
1296 }
1297
1298 if ((error = send_request(s, 0, 1)) < 0)
1299 return error;
1300
1301 s->sent_request = 1;
1302 }
1303
1304 if (len > CACHED_POST_BODY_BUF_SIZE) {
1305 /* Flush, if necessary */
1306 if (s->chunk_buffer_len > 0) {
1307 if (write_chunk(s->request, s->chunk_buffer, s->chunk_buffer_len) < 0)
1308 return -1;
1309
1310 s->chunk_buffer_len = 0;
1311 }
1312
1313 /* Write chunk directly */
1314 if (write_chunk(s->request, buffer, len) < 0)
1315 return -1;
1316 }
1317 else {
1318 /* Append as much to the buffer as we can */
1319 int count = (int)min(CACHED_POST_BODY_BUF_SIZE - s->chunk_buffer_len, len);
1320
1321 if (!s->chunk_buffer)
1322 s->chunk_buffer = git__malloc(CACHED_POST_BODY_BUF_SIZE);
1323
1324 memcpy(s->chunk_buffer + s->chunk_buffer_len, buffer, count);
1325 s->chunk_buffer_len += count;
1326 buffer += count;
1327 len -= count;
1328
1329 /* Is the buffer full? If so, then flush */
1330 if (CACHED_POST_BODY_BUF_SIZE == s->chunk_buffer_len) {
1331 if (write_chunk(s->request, s->chunk_buffer, s->chunk_buffer_len) < 0)
1332 return -1;
1333
1334 s->chunk_buffer_len = 0;
1335
1336 /* Is there any remaining data from the source? */
1337 if (len > 0) {
1338 memcpy(s->chunk_buffer, buffer, len);
1339 s->chunk_buffer_len = (unsigned int)len;
1340 }
1341 }
1342 }
1343
1344 return 0;
1345 }
1346
1347 static void winhttp_stream_free(git_smart_subtransport_stream *stream)
1348 {
1349 winhttp_stream *s = (winhttp_stream *)stream;
1350
1351 winhttp_stream_close(s);
1352 git__free(s);
1353 }
1354
1355 static int winhttp_stream_alloc(winhttp_subtransport *t, winhttp_stream **stream)
1356 {
1357 winhttp_stream *s;
1358
1359 if (!stream)
1360 return -1;
1361
1362 s = git__calloc(1, sizeof(winhttp_stream));
1363 GITERR_CHECK_ALLOC(s);
1364
1365 s->parent.subtransport = &t->parent;
1366 s->parent.read = winhttp_stream_read;
1367 s->parent.write = winhttp_stream_write_single;
1368 s->parent.free = winhttp_stream_free;
1369
1370 *stream = s;
1371
1372 return 0;
1373 }
1374
1375 static int winhttp_uploadpack_ls(
1376 winhttp_subtransport *t,
1377 winhttp_stream *s)
1378 {
1379 GIT_UNUSED(t);
1380
1381 s->service = upload_pack_service;
1382 s->service_url = upload_pack_ls_service_url;
1383 s->verb = get_verb;
1384
1385 return 0;
1386 }
1387
1388 static int winhttp_uploadpack(
1389 winhttp_subtransport *t,
1390 winhttp_stream *s)
1391 {
1392 GIT_UNUSED(t);
1393
1394 s->service = upload_pack_service;
1395 s->service_url = upload_pack_service_url;
1396 s->verb = post_verb;
1397
1398 return 0;
1399 }
1400
1401 static int winhttp_receivepack_ls(
1402 winhttp_subtransport *t,
1403 winhttp_stream *s)
1404 {
1405 GIT_UNUSED(t);
1406
1407 s->service = receive_pack_service;
1408 s->service_url = receive_pack_ls_service_url;
1409 s->verb = get_verb;
1410
1411 return 0;
1412 }
1413
1414 static int winhttp_receivepack(
1415 winhttp_subtransport *t,
1416 winhttp_stream *s)
1417 {
1418 GIT_UNUSED(t);
1419
1420 /* WinHTTP only supports Transfer-Encoding: chunked
1421 * on Windows Vista (NT 6.0) and higher. */
1422 s->chunked = git_has_win32_version(6, 0, 0);
1423
1424 if (s->chunked)
1425 s->parent.write = winhttp_stream_write_chunked;
1426 else
1427 s->parent.write = winhttp_stream_write_buffered;
1428
1429 s->service = receive_pack_service;
1430 s->service_url = receive_pack_service_url;
1431 s->verb = post_verb;
1432
1433 return 0;
1434 }
1435
1436 static int winhttp_action(
1437 git_smart_subtransport_stream **stream,
1438 git_smart_subtransport *subtransport,
1439 const char *url,
1440 git_smart_service_t action)
1441 {
1442 winhttp_subtransport *t = (winhttp_subtransport *)subtransport;
1443 winhttp_stream *s;
1444 int ret = -1;
1445
1446 if (!t->connection)
1447 if ((ret = gitno_connection_data_from_url(&t->connection_data, url, NULL)) < 0 ||
1448 (ret = winhttp_connect(t)) < 0)
1449 return ret;
1450
1451 if (winhttp_stream_alloc(t, &s) < 0)
1452 return -1;
1453
1454 if (!stream)
1455 return -1;
1456
1457 switch (action)
1458 {
1459 case GIT_SERVICE_UPLOADPACK_LS:
1460 ret = winhttp_uploadpack_ls(t, s);
1461 break;
1462
1463 case GIT_SERVICE_UPLOADPACK:
1464 ret = winhttp_uploadpack(t, s);
1465 break;
1466
1467 case GIT_SERVICE_RECEIVEPACK_LS:
1468 ret = winhttp_receivepack_ls(t, s);
1469 break;
1470
1471 case GIT_SERVICE_RECEIVEPACK:
1472 ret = winhttp_receivepack(t, s);
1473 break;
1474
1475 default:
1476 assert(0);
1477 }
1478
1479 if (!ret)
1480 *stream = &s->parent;
1481
1482 return ret;
1483 }
1484
1485 static int winhttp_close(git_smart_subtransport *subtransport)
1486 {
1487 winhttp_subtransport *t = (winhttp_subtransport *)subtransport;
1488
1489 gitno_connection_data_free_ptrs(&t->connection_data);
1490 memset(&t->connection_data, 0x0, sizeof(gitno_connection_data));
1491 gitno_connection_data_free_ptrs(&t->proxy_connection_data);
1492 memset(&t->proxy_connection_data, 0x0, sizeof(gitno_connection_data));
1493
1494 if (t->cred) {
1495 t->cred->free(t->cred);
1496 t->cred = NULL;
1497 }
1498
1499 if (t->proxy_cred) {
1500 t->proxy_cred->free(t->proxy_cred);
1501 t->proxy_cred = NULL;
1502 }
1503
1504 if (t->url_cred) {
1505 t->url_cred->free(t->url_cred);
1506 t->url_cred = NULL;
1507 }
1508
1509 return winhttp_close_connection(t);
1510 }
1511
1512 static void winhttp_free(git_smart_subtransport *subtransport)
1513 {
1514 winhttp_subtransport *t = (winhttp_subtransport *)subtransport;
1515
1516 winhttp_close(subtransport);
1517
1518 git__free(t);
1519 }
1520
1521 int git_smart_subtransport_http(git_smart_subtransport **out, git_transport *owner, void *param)
1522 {
1523 winhttp_subtransport *t;
1524
1525 GIT_UNUSED(param);
1526
1527 if (!out)
1528 return -1;
1529
1530 t = git__calloc(1, sizeof(winhttp_subtransport));
1531 GITERR_CHECK_ALLOC(t);
1532
1533 t->owner = (transport_smart *)owner;
1534 t->parent.action = winhttp_action;
1535 t->parent.close = winhttp_close;
1536 t->parent.free = winhttp_free;
1537
1538 *out = (git_smart_subtransport *) t;
1539 return 0;
1540 }
1541
1542 #endif /* GIT_WINHTTP */