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