Commit 8f426d7dd26856150950d080fb821044bb2ea811

Philip Kelley 2014-06-09T11:43:25

Win32: Enable WinHTTP for MinGW

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
diff --git a/.travis.yml b/.travis.yml
index 68b29b1..28ec0e0 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -27,7 +27,7 @@ matrix:
      compiler: gcc
  include:
    - compiler: i586-mingw32msvc-gcc
-     env: OPTIONS="-DBUILD_CLAR=OFF -DWIN32=ON -DMINGW=ON -DUSE_SSH=OFF"
+     env: OPTIONS="-DCMAKE_TOOLCHAIN_FILE=../script/toolchain-mingw32.cmake" SKIP_TESTS=1
      os: linux
    - compiler: gcc
      env: COVERITY=1
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1948fc8..6e52ef9 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -13,6 +13,7 @@
 
 PROJECT(libgit2 C)
 CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+CMAKE_POLICY(SET CMP0015 NEW)
 
 # Add find modules to the path
 SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules/")
@@ -58,10 +59,6 @@ IF(MSVC)
 	# are linking statically
 	OPTION( STATIC_CRT		"Link the static CRT libraries"	ON  )
 
-	# By default, libgit2 is built with WinHTTP.  To use the built-in
-	# HTTP transport, invoke CMake with the "-DWINHTTP=OFF" argument.
-	OPTION( WINHTTP			"Use Win32 WinHTTP routines"	ON  )
-
 	# If you want to embed a copy of libssh2 into libgit2, pass a
 	# path to libssh2
 	OPTION( EMBED_SSH_PATH		"Path to libssh2 to embed (Windows)" OFF )
@@ -71,6 +68,12 @@ IF(MSVC)
 	ADD_DEFINITIONS(-D_CRT_NONSTDC_NO_DEPRECATE)
 ENDIF()
 
+IF(WIN32)
+	# By default, libgit2 is built with WinHTTP.  To use the built-in
+	# HTTP transport, invoke CMake with the "-DWINHTTP=OFF" argument.
+	OPTION(	WINHTTP			"Use Win32 WinHTTP routines"	ON	)
+ENDIF()
+
 # This variable will contain the libraries we need to put into
 # libgit2.pc's Requires.private. That is, what we're linking to or
 # what someone who's statically linking us needs to link to.
@@ -147,10 +150,38 @@ IF (WIN32 AND EMBED_SSH_PATH)
 	ADD_DEFINITIONS(-DGIT_SSH)
 ENDIF()
 
-IF (WIN32 AND WINHTTP AND NOT MINGW)
+IF (WIN32 AND WINHTTP)
 	ADD_DEFINITIONS(-DGIT_WINHTTP)
 	INCLUDE_DIRECTORIES(deps/http-parser)
 	FILE(GLOB SRC_HTTP deps/http-parser/*.c deps/http-parser/*.h)
+
+	# Since MinGW does not come with headers or an import library for winhttp,
+	# we have to include a private header and generate our own import library
+	IF (MINGW)
+		FIND_PROGRAM(DLLTOOL dlltool CMAKE_FIND_ROOT_PATH_BOTH)
+		IF (NOT DLLTOOL)
+			MESSAGE(FATAL_ERROR "Could not find dlltool command")
+		ENDIF ()
+
+		SET(LIBWINHTTP_PATH "${CMAKE_CURRENT_SOURCE_DIR}/deps/winhttp")
+
+		ADD_CUSTOM_COMMAND(
+			OUTPUT ${LIBWINHTTP_PATH}/libwinhttp.a
+			COMMAND ${DLLTOOL} -d winhttp.def -k -D winhttp.dll -l libwinhttp.a
+			DEPENDS ${LIBWINHTTP_PATH}/winhttp.def
+			WORKING_DIRECTORY ${LIBWINHTTP_PATH}
+		)
+
+		SET_SOURCE_FILES_PROPERTIES(
+			${CMAKE_CURRENT_SOURCE_DIR}/src/transports/winhttp.c
+			PROPERTIES OBJECT_DEPENDS ${LIBWINHTTP_PATH}/libwinhttp.a
+		)
+
+		INCLUDE_DIRECTORIES(deps/winhttp)
+		LINK_DIRECTORIES(deps/winhttp)
+	ENDIF ()
+
+	LINK_LIBRARIES(winhttp rpcrt4)
 ELSE ()
 	IF (NOT AMIGA AND USE_OPENSSL)
 		FIND_PACKAGE(OpenSSL)
diff --git a/deps/winhttp/urlmon.h b/deps/winhttp/urlmon.h
new file mode 100644
index 0000000..900e996
--- /dev/null
+++ b/deps/winhttp/urlmon.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#ifndef __CUSTOM_URLMON_H
+#define __CUSTOM_URLMON_H
+
+typedef struct IInternetSecurityManager IInternetSecurityManager;
+
+typedef struct IInternetSecurityManagerVtbl
+{
+	HRESULT(STDMETHODCALLTYPE *QueryInterface)(IInternetSecurityManager *, REFIID, void **);
+	ULONG(STDMETHODCALLTYPE *AddRef)(IInternetSecurityManager *);
+	ULONG(STDMETHODCALLTYPE *Release)(IInternetSecurityManager *);
+	LPVOID SetSecuritySite;
+	LPVOID GetSecuritySite;
+	HRESULT(STDMETHODCALLTYPE *MapUrlToZone)(IInternetSecurityManager *, LPCWSTR, DWORD *, DWORD);
+	LPVOID GetSecurityId;
+	LPVOID ProcessUrlAction;
+	LPVOID QueryCustomPolicy;
+	LPVOID SetZoneMapping;
+	LPVOID GetZoneMappings;
+} IInternetSecurityManagerVtbl;
+
+struct IInternetSecurityManager
+{
+	CONST_VTBL struct IInternetSecurityManagerVtbl *lpVtbl;
+};
+
+#define URLZONE_LOCAL_MACHINE 0
+#define URLZONE_INTRANET      1
+#define URLZONE_TRUSTED       2
+
+#endif /* __CUSTOM_URLMON_H */
diff --git a/deps/winhttp/winhttp.def b/deps/winhttp/winhttp.def
new file mode 100644
index 0000000..1b2d3f0
--- /dev/null
+++ b/deps/winhttp/winhttp.def
@@ -0,0 +1,29 @@
+LIBRARY		WINHTTP
+EXPORTS
+WinHttpAddRequestHeaders@16
+WinHttpCheckPlatform@0
+WinHttpCloseHandle@4
+WinHttpConnect@16
+WinHttpCrackUrl@16
+WinHttpCreateUrl@16
+WinHttpDetectAutoProxyConfigUrl@8
+WinHttpGetDefaultProxyConfiguration@4
+WinHttpGetIEProxyConfigForCurrentUser@4
+WinHttpGetProxyForUrl@16
+WinHttpOpen@20
+WinHttpOpenRequest@28
+WinHttpQueryAuthSchemes@16
+WinHttpQueryDataAvailable@8
+WinHttpQueryHeaders@24
+WinHttpQueryOption@16
+WinHttpReadData@16
+WinHttpReceiveResponse@8
+WinHttpSendRequest@28
+WinHttpSetCredentials@24
+WinHttpSetDefaultProxyConfiguration@4
+WinHttpSetOption@16
+WinHttpSetStatusCallback@16
+WinHttpSetTimeouts@20
+WinHttpTimeFromSystemTime@8
+WinHttpTimeToSystemTime@8
+WinHttpWriteData@16
diff --git a/deps/winhttp/winhttp.h b/deps/winhttp/winhttp.h
new file mode 100644
index 0000000..51e8265
--- /dev/null
+++ b/deps/winhttp/winhttp.h
@@ -0,0 +1,584 @@
+/*
+ * Copyright (C) 2007 Francois Gouget
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+#ifndef __WINE_WINHTTP_H
+#define __WINE_WINHTTP_H
+
+#ifdef _WIN64
+#include <pshpack8.h>
+#else
+#include <pshpack4.h>
+#endif
+
+#define WINHTTPAPI
+#define BOOLAPI WINHTTPAPI BOOL WINAPI
+
+
+typedef LPVOID HINTERNET;
+typedef HINTERNET *LPHINTERNET;
+
+#define INTERNET_DEFAULT_PORT           0
+#define INTERNET_DEFAULT_HTTP_PORT      80
+#define INTERNET_DEFAULT_HTTPS_PORT     443
+typedef WORD INTERNET_PORT;
+typedef INTERNET_PORT *LPINTERNET_PORT;
+
+#define INTERNET_SCHEME_HTTP            1
+#define INTERNET_SCHEME_HTTPS           2
+typedef int INTERNET_SCHEME, *LPINTERNET_SCHEME;
+
+#define ICU_ESCAPE  0x80000000
+
+/* flags for WinHttpOpen */
+#define WINHTTP_FLAG_ASYNC                  0x10000000
+
+/* flags for WinHttpOpenRequest */
+#define WINHTTP_FLAG_ESCAPE_PERCENT         0x00000004
+#define WINHTTP_FLAG_NULL_CODEPAGE          0x00000008
+#define WINHTTP_FLAG_ESCAPE_DISABLE         0x00000040
+#define WINHTTP_FLAG_ESCAPE_DISABLE_QUERY   0x00000080
+#define WINHTTP_FLAG_BYPASS_PROXY_CACHE     0x00000100
+#define WINHTTP_FLAG_REFRESH                WINHTTP_FLAG_BYPASS_PROXY_CACHE
+#define WINHTTP_FLAG_SECURE                 0x00800000
+
+#define WINHTTP_ACCESS_TYPE_DEFAULT_PROXY   0
+#define WINHTTP_ACCESS_TYPE_NO_PROXY        1
+#define WINHTTP_ACCESS_TYPE_NAMED_PROXY     3
+
+#define WINHTTP_NO_PROXY_NAME               NULL
+#define WINHTTP_NO_PROXY_BYPASS             NULL
+
+#define WINHTTP_NO_REFERER                  NULL
+#define WINHTTP_DEFAULT_ACCEPT_TYPES        NULL
+
+#define WINHTTP_NO_ADDITIONAL_HEADERS       NULL
+#define WINHTTP_NO_REQUEST_DATA             NULL
+
+#define WINHTTP_HEADER_NAME_BY_INDEX        NULL
+#define WINHTTP_NO_OUTPUT_BUFFER            NULL
+#define WINHTTP_NO_HEADER_INDEX             NULL
+
+#define WINHTTP_ADDREQ_INDEX_MASK                    0x0000FFFF
+#define WINHTTP_ADDREQ_FLAGS_MASK                    0xFFFF0000
+#define WINHTTP_ADDREQ_FLAG_ADD_IF_NEW               0x10000000
+#define WINHTTP_ADDREQ_FLAG_ADD                      0x20000000
+#define WINHTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA      0x40000000
+#define WINHTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON  0x01000000
+#define WINHTTP_ADDREQ_FLAG_COALESCE                 WINHTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA
+#define WINHTTP_ADDREQ_FLAG_REPLACE                  0x80000000
+
+#define WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH 0
+
+/* flags for WinHttp{Set/Query}Options */
+#define WINHTTP_FIRST_OPTION                         WINHTTP_OPTION_CALLBACK
+#define WINHTTP_OPTION_CALLBACK                       1
+#define WINHTTP_OPTION_RESOLVE_TIMEOUT                2
+#define WINHTTP_OPTION_CONNECT_TIMEOUT                3
+#define WINHTTP_OPTION_CONNECT_RETRIES                4
+#define WINHTTP_OPTION_SEND_TIMEOUT                   5
+#define WINHTTP_OPTION_RECEIVE_TIMEOUT                6
+#define WINHTTP_OPTION_RECEIVE_RESPONSE_TIMEOUT       7
+#define WINHTTP_OPTION_HANDLE_TYPE                    9
+#define WINHTTP_OPTION_READ_BUFFER_SIZE              12
+#define WINHTTP_OPTION_WRITE_BUFFER_SIZE             13
+#define WINHTTP_OPTION_PARENT_HANDLE                 21
+#define WINHTTP_OPTION_EXTENDED_ERROR                24
+#define WINHTTP_OPTION_SECURITY_FLAGS                31
+#define WINHTTP_OPTION_SECURITY_CERTIFICATE_STRUCT   32
+#define WINHTTP_OPTION_URL                           34
+#define WINHTTP_OPTION_SECURITY_KEY_BITNESS          36
+#define WINHTTP_OPTION_PROXY                         38
+#define WINHTTP_OPTION_USER_AGENT                    41
+#define WINHTTP_OPTION_CONTEXT_VALUE                 45
+#define WINHTTP_OPTION_CLIENT_CERT_CONTEXT           47
+#define WINHTTP_OPTION_REQUEST_PRIORITY              58
+#define WINHTTP_OPTION_HTTP_VERSION                  59
+#define WINHTTP_OPTION_DISABLE_FEATURE               63
+#define WINHTTP_OPTION_CODEPAGE                      68
+#define WINHTTP_OPTION_MAX_CONNS_PER_SERVER          73
+#define WINHTTP_OPTION_MAX_CONNS_PER_1_0_SERVER      74
+#define WINHTTP_OPTION_AUTOLOGON_POLICY              77
+#define WINHTTP_OPTION_SERVER_CERT_CONTEXT           78
+#define WINHTTP_OPTION_ENABLE_FEATURE                79
+#define WINHTTP_OPTION_WORKER_THREAD_COUNT           80
+#define WINHTTP_OPTION_PASSPORT_COBRANDING_TEXT      81
+#define WINHTTP_OPTION_PASSPORT_COBRANDING_URL       82
+#define WINHTTP_OPTION_CONFIGURE_PASSPORT_AUTH       83
+#define WINHTTP_OPTION_SECURE_PROTOCOLS              84
+#define WINHTTP_OPTION_ENABLETRACING                 85
+#define WINHTTP_OPTION_PASSPORT_SIGN_OUT             86
+#define WINHTTP_OPTION_PASSPORT_RETURN_URL           87
+#define WINHTTP_OPTION_REDIRECT_POLICY               88
+#define WINHTTP_OPTION_MAX_HTTP_AUTOMATIC_REDIRECTS  89
+#define WINHTTP_OPTION_MAX_HTTP_STATUS_CONTINUE      90
+#define WINHTTP_OPTION_MAX_RESPONSE_HEADER_SIZE      91
+#define WINHTTP_OPTION_MAX_RESPONSE_DRAIN_SIZE       92
+#define WINHTTP_OPTION_CONNECTION_INFO               93
+#define WINHTTP_OPTION_CLIENT_CERT_ISSUER_LIST       94
+#define WINHTTP_OPTION_SPN                           96
+#define WINHTTP_OPTION_GLOBAL_PROXY_CREDS            97
+#define WINHTTP_OPTION_GLOBAL_SERVER_CREDS           98
+#define WINHTTP_OPTION_UNLOAD_NOTIFY_EVENT           99
+#define WINHTTP_OPTION_REJECT_USERPWD_IN_URL         100
+#define WINHTTP_OPTION_USE_GLOBAL_SERVER_CREDENTIALS 101
+#define WINHTTP_LAST_OPTION                          WINHTTP_OPTION_USE_GLOBAL_SERVER_CREDENTIALS
+#define WINHTTP_OPTION_USERNAME                      0x1000
+#define WINHTTP_OPTION_PASSWORD                      0x1001
+#define WINHTTP_OPTION_PROXY_USERNAME                0x1002
+#define WINHTTP_OPTION_PROXY_PASSWORD                0x1003
+
+#define WINHTTP_CONNS_PER_SERVER_UNLIMITED 0xFFFFFFFF
+
+#define WINHTTP_AUTOLOGON_SECURITY_LEVEL_MEDIUM   0
+#define WINHTTP_AUTOLOGON_SECURITY_LEVEL_LOW      1
+#define WINHTTP_AUTOLOGON_SECURITY_LEVEL_HIGH     2
+#define WINHTTP_AUTOLOGON_SECURITY_LEVEL_DEFAULT  WINHTTP_AUTOLOGON_SECURITY_LEVEL_MEDIUM
+
+#define WINHTTP_OPTION_REDIRECT_POLICY_NEVER                        0
+#define WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP       1
+#define WINHTTP_OPTION_REDIRECT_POLICY_ALWAYS                       2
+#define WINHTTP_OPTION_REDIRECT_POLICY_LAST            WINHTTP_OPTION_REDIRECT_POLICY_ALWAYS
+#define WINHTTP_OPTION_REDIRECT_POLICY_DEFAULT         WINHTTP_OPTION_REDIRECT_POLICY_DISALLOW_HTTPS_TO_HTTP
+
+#define WINHTTP_DISABLE_PASSPORT_AUTH    0x00000000
+#define WINHTTP_ENABLE_PASSPORT_AUTH     0x10000000
+#define WINHTTP_DISABLE_PASSPORT_KEYRING 0x20000000
+#define WINHTTP_ENABLE_PASSPORT_KEYRING  0x40000000
+
+#define WINHTTP_DISABLE_COOKIES                   0x00000001
+#define WINHTTP_DISABLE_REDIRECTS                 0x00000002
+#define WINHTTP_DISABLE_AUTHENTICATION            0x00000004
+#define WINHTTP_DISABLE_KEEP_ALIVE                0x00000008
+#define WINHTTP_ENABLE_SSL_REVOCATION             0x00000001
+#define WINHTTP_ENABLE_SSL_REVERT_IMPERSONATION   0x00000002
+#define WINHTTP_DISABLE_SPN_SERVER_PORT           0x00000000
+#define WINHTTP_ENABLE_SPN_SERVER_PORT            0x00000001
+#define WINHTTP_OPTION_SPN_MASK                   WINHTTP_ENABLE_SPN_SERVER_PORT
+
+/* Options for WinHttpOpenRequest */
+#define WINHTTP_NO_REFERER             NULL
+#define WINHTTP_DEFAULT_ACCEPT_TYPES   NULL
+
+/* Options for WinHttpSendRequest */
+#define WINHTTP_NO_ADDITIONAL_HEADERS   NULL
+#define WINHTTP_NO_REQUEST_DATA         NULL
+
+/* WinHTTP error codes */
+#define WINHTTP_ERROR_BASE                                  12000
+#define ERROR_WINHTTP_OUT_OF_HANDLES                        (WINHTTP_ERROR_BASE + 1)
+#define ERROR_WINHTTP_TIMEOUT                               (WINHTTP_ERROR_BASE + 2)
+#define ERROR_WINHTTP_INTERNAL_ERROR                        (WINHTTP_ERROR_BASE + 4)
+#define ERROR_WINHTTP_INVALID_URL                           (WINHTTP_ERROR_BASE + 5)
+#define ERROR_WINHTTP_UNRECOGNIZED_SCHEME                   (WINHTTP_ERROR_BASE + 6)
+#define ERROR_WINHTTP_NAME_NOT_RESOLVED                     (WINHTTP_ERROR_BASE + 7)
+#define ERROR_WINHTTP_INVALID_OPTION                        (WINHTTP_ERROR_BASE + 9)
+#define ERROR_WINHTTP_OPTION_NOT_SETTABLE                   (WINHTTP_ERROR_BASE + 11)
+#define ERROR_WINHTTP_SHUTDOWN                              (WINHTTP_ERROR_BASE + 12)
+#define ERROR_WINHTTP_LOGIN_FAILURE                         (WINHTTP_ERROR_BASE + 15)
+#define ERROR_WINHTTP_OPERATION_CANCELLED                   (WINHTTP_ERROR_BASE + 17)
+#define ERROR_WINHTTP_INCORRECT_HANDLE_TYPE                 (WINHTTP_ERROR_BASE + 18)
+#define ERROR_WINHTTP_INCORRECT_HANDLE_STATE                (WINHTTP_ERROR_BASE + 19)
+#define ERROR_WINHTTP_CANNOT_CONNECT                        (WINHTTP_ERROR_BASE + 29)
+#define ERROR_WINHTTP_CONNECTION_ERROR                      (WINHTTP_ERROR_BASE + 30)
+#define ERROR_WINHTTP_RESEND_REQUEST                        (WINHTTP_ERROR_BASE + 32)
+#define ERROR_WINHTTP_SECURE_CERT_DATE_INVALID              (WINHTTP_ERROR_BASE + 37)
+#define ERROR_WINHTTP_SECURE_CERT_CN_INVALID                (WINHTTP_ERROR_BASE + 38)
+#define ERROR_WINHTTP_CLIENT_AUTH_CERT_NEEDED               (WINHTTP_ERROR_BASE + 44)
+#define ERROR_WINHTTP_SECURE_INVALID_CA                     (WINHTTP_ERROR_BASE + 45)
+#define ERROR_WINHTTP_SECURE_CERT_REV_FAILED                (WINHTTP_ERROR_BASE + 57)
+#define ERROR_WINHTTP_CANNOT_CALL_BEFORE_OPEN               (WINHTTP_ERROR_BASE + 100)
+#define ERROR_WINHTTP_CANNOT_CALL_BEFORE_SEND               (WINHTTP_ERROR_BASE + 101)
+#define ERROR_WINHTTP_CANNOT_CALL_AFTER_SEND                (WINHTTP_ERROR_BASE + 102)
+#define ERROR_WINHTTP_CANNOT_CALL_AFTER_OPEN                (WINHTTP_ERROR_BASE + 103)
+#define ERROR_WINHTTP_HEADER_NOT_FOUND                      (WINHTTP_ERROR_BASE + 150)
+#define ERROR_WINHTTP_INVALID_SERVER_RESPONSE               (WINHTTP_ERROR_BASE + 152)
+#define ERROR_WINHTTP_INVALID_HEADER                        (WINHTTP_ERROR_BASE + 153)
+#define ERROR_WINHTTP_INVALID_QUERY_REQUEST                 (WINHTTP_ERROR_BASE + 154)
+#define ERROR_WINHTTP_HEADER_ALREADY_EXISTS                 (WINHTTP_ERROR_BASE + 155)
+#define ERROR_WINHTTP_REDIRECT_FAILED                       (WINHTTP_ERROR_BASE + 156)
+#define ERROR_WINHTTP_SECURE_CHANNEL_ERROR                  (WINHTTP_ERROR_BASE + 157)
+#define ERROR_WINHTTP_BAD_AUTO_PROXY_SCRIPT                 (WINHTTP_ERROR_BASE + 166)
+#define ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT             (WINHTTP_ERROR_BASE + 167)
+#define ERROR_WINHTTP_SECURE_INVALID_CERT                   (WINHTTP_ERROR_BASE + 169)
+#define ERROR_WINHTTP_SECURE_CERT_REVOKED                   (WINHTTP_ERROR_BASE + 170)
+#define ERROR_WINHTTP_NOT_INITIALIZED                       (WINHTTP_ERROR_BASE + 172)
+#define ERROR_WINHTTP_SECURE_FAILURE                        (WINHTTP_ERROR_BASE + 175)
+#define ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR              (WINHTTP_ERROR_BASE + 178)
+#define ERROR_WINHTTP_SECURE_CERT_WRONG_USAGE               (WINHTTP_ERROR_BASE + 179)
+#define ERROR_WINHTTP_AUTODETECTION_FAILED                  (WINHTTP_ERROR_BASE + 180)
+#define ERROR_WINHTTP_HEADER_COUNT_EXCEEDED                 (WINHTTP_ERROR_BASE + 181)
+#define ERROR_WINHTTP_HEADER_SIZE_OVERFLOW                  (WINHTTP_ERROR_BASE + 182)
+#define ERROR_WINHTTP_CHUNKED_ENCODING_HEADER_SIZE_OVERFLOW (WINHTTP_ERROR_BASE + 183)
+#define ERROR_WINHTTP_RESPONSE_DRAIN_OVERFLOW               (WINHTTP_ERROR_BASE + 184)
+#define ERROR_WINHTTP_CLIENT_CERT_NO_PRIVATE_KEY            (WINHTTP_ERROR_BASE + 185)
+#define ERROR_WINHTTP_CLIENT_CERT_NO_ACCESS_PRIVATE_KEY     (WINHTTP_ERROR_BASE + 186)
+#define WINHTTP_ERROR_LAST                                  (WINHTTP_ERROR_BASE + 186)
+
+/* WinHttp status codes */
+#define HTTP_STATUS_CONTINUE            100
+#define HTTP_STATUS_SWITCH_PROTOCOLS    101
+#define HTTP_STATUS_OK                  200
+#define HTTP_STATUS_CREATED             201
+#define HTTP_STATUS_ACCEPTED            202
+#define HTTP_STATUS_PARTIAL             203
+#define HTTP_STATUS_NO_CONTENT          204
+#define HTTP_STATUS_RESET_CONTENT       205
+#define HTTP_STATUS_PARTIAL_CONTENT     206
+#define HTTP_STATUS_WEBDAV_MULTI_STATUS 207
+#define HTTP_STATUS_AMBIGUOUS           300
+#define HTTP_STATUS_MOVED               301
+#define HTTP_STATUS_REDIRECT            302
+#define HTTP_STATUS_REDIRECT_METHOD     303
+#define HTTP_STATUS_NOT_MODIFIED        304
+#define HTTP_STATUS_USE_PROXY           305
+#define HTTP_STATUS_REDIRECT_KEEP_VERB  307
+#define HTTP_STATUS_BAD_REQUEST         400
+#define HTTP_STATUS_DENIED              401
+#define HTTP_STATUS_PAYMENT_REQ         402
+#define HTTP_STATUS_FORBIDDEN           403
+#define HTTP_STATUS_NOT_FOUND           404
+#define HTTP_STATUS_BAD_METHOD          405
+#define HTTP_STATUS_NONE_ACCEPTABLE     406
+#define HTTP_STATUS_PROXY_AUTH_REQ      407
+#define HTTP_STATUS_REQUEST_TIMEOUT     408
+#define HTTP_STATUS_CONFLICT            409
+#define HTTP_STATUS_GONE                410
+#define HTTP_STATUS_LENGTH_REQUIRED     411
+#define HTTP_STATUS_PRECOND_FAILED      412
+#define HTTP_STATUS_REQUEST_TOO_LARGE   413
+#define HTTP_STATUS_URI_TOO_LONG        414
+#define HTTP_STATUS_UNSUPPORTED_MEDIA   415
+#define HTTP_STATUS_RETRY_WITH          449
+#define HTTP_STATUS_SERVER_ERROR        500
+#define HTTP_STATUS_NOT_SUPPORTED       501
+#define HTTP_STATUS_BAD_GATEWAY         502
+#define HTTP_STATUS_SERVICE_UNAVAIL     503
+#define HTTP_STATUS_GATEWAY_TIMEOUT     504
+#define HTTP_STATUS_VERSION_NOT_SUP     505
+#define HTTP_STATUS_FIRST               HTTP_STATUS_CONTINUE
+#define HTTP_STATUS_LAST                HTTP_STATUS_VERSION_NOT_SUP
+
+#define SECURITY_FLAG_IGNORE_UNKNOWN_CA         0x00000100
+#define SECURITY_FLAG_IGNORE_CERT_DATE_INVALID  0x00002000
+#define SECURITY_FLAG_IGNORE_CERT_CN_INVALID    0x00001000
+#define SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE   0x00000200
+#define SECURITY_FLAG_SECURE                    0x00000001
+#define SECURITY_FLAG_STRENGTH_WEAK             0x10000000
+#define SECURITY_FLAG_STRENGTH_MEDIUM           0x40000000
+#define SECURITY_FLAG_STRENGTH_STRONG           0x20000000
+
+#define ICU_NO_ENCODE          0x20000000
+#define ICU_DECODE             0x10000000
+#define ICU_NO_META            0x08000000
+#define ICU_ENCODE_SPACES_ONLY 0x04000000
+#define ICU_BROWSER_MODE       0x02000000
+#define ICU_ENCODE_PERCENT     0x00001000
+
+/* Query flags */
+#define WINHTTP_QUERY_MIME_VERSION                 0
+#define WINHTTP_QUERY_CONTENT_TYPE                 1
+#define WINHTTP_QUERY_CONTENT_TRANSFER_ENCODING    2
+#define WINHTTP_QUERY_CONTENT_ID                   3
+#define WINHTTP_QUERY_CONTENT_DESCRIPTION          4
+#define WINHTTP_QUERY_CONTENT_LENGTH               5
+#define WINHTTP_QUERY_CONTENT_LANGUAGE             6
+#define WINHTTP_QUERY_ALLOW                        7
+#define WINHTTP_QUERY_PUBLIC                       8
+#define WINHTTP_QUERY_DATE                         9
+#define WINHTTP_QUERY_EXPIRES                      10
+#define WINHTTP_QUERY_LAST_MODIFIED                11
+#define WINHTTP_QUERY_MESSAGE_ID                   12
+#define WINHTTP_QUERY_URI                          13
+#define WINHTTP_QUERY_DERIVED_FROM                 14
+#define WINHTTP_QUERY_COST                         15
+#define WINHTTP_QUERY_LINK                         16
+#define WINHTTP_QUERY_PRAGMA                       17
+#define WINHTTP_QUERY_VERSION                      18
+#define WINHTTP_QUERY_STATUS_CODE                  19
+#define WINHTTP_QUERY_STATUS_TEXT                  20
+#define WINHTTP_QUERY_RAW_HEADERS                  21
+#define WINHTTP_QUERY_RAW_HEADERS_CRLF             22
+#define WINHTTP_QUERY_CONNECTION                   23
+#define WINHTTP_QUERY_ACCEPT                       24
+#define WINHTTP_QUERY_ACCEPT_CHARSET               25
+#define WINHTTP_QUERY_ACCEPT_ENCODING              26
+#define WINHTTP_QUERY_ACCEPT_LANGUAGE              27
+#define WINHTTP_QUERY_AUTHORIZATION                28
+#define WINHTTP_QUERY_CONTENT_ENCODING             29
+#define WINHTTP_QUERY_FORWARDED                    30
+#define WINHTTP_QUERY_FROM                         31
+#define WINHTTP_QUERY_IF_MODIFIED_SINCE            32
+#define WINHTTP_QUERY_LOCATION                     33
+#define WINHTTP_QUERY_ORIG_URI                     34
+#define WINHTTP_QUERY_REFERER                      35
+#define WINHTTP_QUERY_RETRY_AFTER                  36
+#define WINHTTP_QUERY_SERVER                       37
+#define WINHTTP_QUERY_TITLE                        38
+#define WINHTTP_QUERY_USER_AGENT                   39
+#define WINHTTP_QUERY_WWW_AUTHENTICATE             40
+#define WINHTTP_QUERY_PROXY_AUTHENTICATE           41
+#define WINHTTP_QUERY_ACCEPT_RANGES                42
+#define WINHTTP_QUERY_SET_COOKIE                   43
+#define WINHTTP_QUERY_COOKIE                       44
+#define WINHTTP_QUERY_REQUEST_METHOD               45
+#define WINHTTP_QUERY_REFRESH                      46
+#define WINHTTP_QUERY_CONTENT_DISPOSITION          47
+#define WINHTTP_QUERY_AGE                          48
+#define WINHTTP_QUERY_CACHE_CONTROL                49
+#define WINHTTP_QUERY_CONTENT_BASE                 50
+#define WINHTTP_QUERY_CONTENT_LOCATION             51
+#define WINHTTP_QUERY_CONTENT_MD5                  52
+#define WINHTTP_QUERY_CONTENT_RANGE                53
+#define WINHTTP_QUERY_ETAG                         54
+#define WINHTTP_QUERY_HOST                         55
+#define WINHTTP_QUERY_IF_MATCH                     56
+#define WINHTTP_QUERY_IF_NONE_MATCH                57
+#define WINHTTP_QUERY_IF_RANGE                     58
+#define WINHTTP_QUERY_IF_UNMODIFIED_SINCE          59
+#define WINHTTP_QUERY_MAX_FORWARDS                 60
+#define WINHTTP_QUERY_PROXY_AUTHORIZATION          61
+#define WINHTTP_QUERY_RANGE                        62
+#define WINHTTP_QUERY_TRANSFER_ENCODING            63
+#define WINHTTP_QUERY_UPGRADE                      64
+#define WINHTTP_QUERY_VARY                         65
+#define WINHTTP_QUERY_VIA                          66
+#define WINHTTP_QUERY_WARNING                      67
+#define WINHTTP_QUERY_EXPECT                       68
+#define WINHTTP_QUERY_PROXY_CONNECTION             69
+#define WINHTTP_QUERY_UNLESS_MODIFIED_SINCE        70
+#define WINHTTP_QUERY_PROXY_SUPPORT                75
+#define WINHTTP_QUERY_AUTHENTICATION_INFO          76
+#define WINHTTP_QUERY_PASSPORT_URLS                77
+#define WINHTTP_QUERY_PASSPORT_CONFIG              78
+#define WINHTTP_QUERY_MAX                          78
+#define WINHTTP_QUERY_CUSTOM                       65535
+#define WINHTTP_QUERY_FLAG_REQUEST_HEADERS         0x80000000
+#define WINHTTP_QUERY_FLAG_SYSTEMTIME              0x40000000
+#define WINHTTP_QUERY_FLAG_NUMBER                  0x20000000
+
+/* Callback options */
+#define WINHTTP_CALLBACK_STATUS_RESOLVING_NAME          0x00000001
+#define WINHTTP_CALLBACK_STATUS_NAME_RESOLVED           0x00000002
+#define WINHTTP_CALLBACK_STATUS_CONNECTING_TO_SERVER    0x00000004
+#define WINHTTP_CALLBACK_STATUS_CONNECTED_TO_SERVER     0x00000008
+#define WINHTTP_CALLBACK_STATUS_SENDING_REQUEST         0x00000010
+#define WINHTTP_CALLBACK_STATUS_REQUEST_SENT            0x00000020
+#define WINHTTP_CALLBACK_STATUS_RECEIVING_RESPONSE      0x00000040
+#define WINHTTP_CALLBACK_STATUS_RESPONSE_RECEIVED       0x00000080
+#define WINHTTP_CALLBACK_STATUS_CLOSING_CONNECTION      0x00000100
+#define WINHTTP_CALLBACK_STATUS_CONNECTION_CLOSED       0x00000200
+#define WINHTTP_CALLBACK_STATUS_HANDLE_CREATED          0x00000400
+#define WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING          0x00000800
+#define WINHTTP_CALLBACK_STATUS_DETECTING_PROXY         0x00001000
+#define WINHTTP_CALLBACK_STATUS_REDIRECT                0x00004000
+#define WINHTTP_CALLBACK_STATUS_INTERMEDIATE_RESPONSE   0x00008000
+#define WINHTTP_CALLBACK_STATUS_SECURE_FAILURE          0x00010000
+#define WINHTTP_CALLBACK_STATUS_HEADERS_AVAILABLE       0x00020000
+#define WINHTTP_CALLBACK_STATUS_DATA_AVAILABLE          0x00040000
+#define WINHTTP_CALLBACK_STATUS_READ_COMPLETE           0x00080000
+#define WINHTTP_CALLBACK_STATUS_WRITE_COMPLETE          0x00100000
+#define WINHTTP_CALLBACK_STATUS_REQUEST_ERROR           0x00200000
+#define WINHTTP_CALLBACK_STATUS_SENDREQUEST_COMPLETE    0x00400000
+#define WINHTTP_CALLBACK_FLAG_RESOLVE_NAME              (WINHTTP_CALLBACK_STATUS_RESOLVING_NAME | WINHTTP_CALLBACK_STATUS_NAME_RESOLVED)
+#define WINHTTP_CALLBACK_FLAG_CONNECT_TO_SERVER         (WINHTTP_CALLBACK_STATUS_CONNECTING_TO_SERVER | WINHTTP_CALLBACK_STATUS_CONNECTED_TO_SERVER)
+#define WINHTTP_CALLBACK_FLAG_SEND_REQUEST              (WINHTTP_CALLBACK_STATUS_SENDING_REQUEST | WINHTTP_CALLBACK_STATUS_REQUEST_SENT)
+#define WINHTTP_CALLBACK_FLAG_RECEIVE_RESPONSE          (WINHTTP_CALLBACK_STATUS_RECEIVING_RESPONSE | WINHTTP_CALLBACK_STATUS_RESPONSE_RECEIVED)
+#define WINHTTP_CALLBACK_FLAG_CLOSE_CONNECTION          (WINHTTP_CALLBACK_STATUS_CLOSING_CONNECTION | WINHTTP_CALLBACK_STATUS_CONNECTION_CLOSED)
+#define WINHTTP_CALLBACK_FLAG_HANDLES                   (WINHTTP_CALLBACK_STATUS_HANDLE_CREATED | WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING)
+#define WINHTTP_CALLBACK_FLAG_DETECTING_PROXY           WINHTTP_CALLBACK_STATUS_DETECTING_PROXY
+#define WINHTTP_CALLBACK_FLAG_REDIRECT                  WINHTTP_CALLBACK_STATUS_REDIRECT
+#define WINHTTP_CALLBACK_FLAG_INTERMEDIATE_RESPONSE     WINHTTP_CALLBACK_STATUS_INTERMEDIATE_RESPONSE
+#define WINHTTP_CALLBACK_FLAG_SECURE_FAILURE            WINHTTP_CALLBACK_STATUS_SECURE_FAILURE
+#define WINHTTP_CALLBACK_FLAG_SENDREQUEST_COMPLETE      WINHTTP_CALLBACK_STATUS_SENDREQUEST_COMPLETE
+#define WINHTTP_CALLBACK_FLAG_HEADERS_AVAILABLE         WINHTTP_CALLBACK_STATUS_HEADERS_AVAILABLE
+#define WINHTTP_CALLBACK_FLAG_DATA_AVAILABLE            WINHTTP_CALLBACK_STATUS_DATA_AVAILABLE
+#define WINHTTP_CALLBACK_FLAG_READ_COMPLETE             WINHTTP_CALLBACK_STATUS_READ_COMPLETE
+#define WINHTTP_CALLBACK_FLAG_WRITE_COMPLETE            WINHTTP_CALLBACK_STATUS_WRITE_COMPLETE
+#define WINHTTP_CALLBACK_FLAG_REQUEST_ERROR             WINHTTP_CALLBACK_STATUS_REQUEST_ERROR
+#define WINHTTP_CALLBACK_FLAG_ALL_COMPLETIONS           (WINHTTP_CALLBACK_STATUS_SENDREQUEST_COMPLETE | WINHTTP_CALLBACK_STATUS_HEADERS_AVAILABLE \
+                                                        | WINHTTP_CALLBACK_STATUS_DATA_AVAILABLE | WINHTTP_CALLBACK_STATUS_READ_COMPLETE          \
+                                                        | WINHTTP_CALLBACK_STATUS_WRITE_COMPLETE | WINHTTP_CALLBACK_STATUS_REQUEST_ERROR)
+#define WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS         0xffffffff
+#define WINHTTP_INVALID_STATUS_CALLBACK                 ((WINHTTP_STATUS_CALLBACK)(-1))
+
+#define API_RECEIVE_RESPONSE          (1)
+#define API_QUERY_DATA_AVAILABLE      (2)
+#define API_READ_DATA                 (3)
+#define API_WRITE_DATA                (4)
+#define API_SEND_REQUEST              (5)
+
+#define WINHTTP_HANDLE_TYPE_SESSION                  1
+#define WINHTTP_HANDLE_TYPE_CONNECT                  2
+#define WINHTTP_HANDLE_TYPE_REQUEST                  3
+
+#define WINHTTP_CALLBACK_STATUS_FLAG_CERT_REV_FAILED         0x00000001
+#define WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CERT            0x00000002
+#define WINHTTP_CALLBACK_STATUS_FLAG_CERT_REVOKED            0x00000004
+#define WINHTTP_CALLBACK_STATUS_FLAG_INVALID_CA              0x00000008
+#define WINHTTP_CALLBACK_STATUS_FLAG_CERT_CN_INVALID         0x00000010
+#define WINHTTP_CALLBACK_STATUS_FLAG_CERT_DATE_INVALID       0x00000020
+#define WINHTTP_CALLBACK_STATUS_FLAG_CERT_WRONG_USAGE        0x00000040
+#define WINHTTP_CALLBACK_STATUS_FLAG_SECURITY_CHANNEL_ERROR  0x80000000
+
+#define WINHTTP_FLAG_SECURE_PROTOCOL_SSL2  0x00000008
+#define WINHTTP_FLAG_SECURE_PROTOCOL_SSL3  0x00000020
+#define WINHTTP_FLAG_SECURE_PROTOCOL_TLS1  0x00000080
+#define WINHTTP_FLAG_SECURE_PROTOCOL_ALL   (WINHTTP_FLAG_SECURE_PROTOCOL_SSL2 | WINHTTP_FLAG_SECURE_PROTOCOL_SSL3 | WINHTTP_FLAG_SECURE_PROTOCOL_TLS1)
+
+#define WINHTTP_AUTH_SCHEME_BASIC      0x00000001
+#define WINHTTP_AUTH_SCHEME_NTLM       0x00000002
+#define WINHTTP_AUTH_SCHEME_PASSPORT   0x00000004
+#define WINHTTP_AUTH_SCHEME_DIGEST     0x00000008
+#define WINHTTP_AUTH_SCHEME_NEGOTIATE  0x00000010
+
+#define WINHTTP_AUTH_TARGET_SERVER     0x00000000
+#define WINHTTP_AUTH_TARGET_PROXY      0x00000001
+
+#define WINHTTP_TIME_FORMAT_BUFSIZE    62
+
+typedef struct
+{
+    DWORD   dwStructSize;
+    LPWSTR  lpszScheme;
+    DWORD   dwSchemeLength;
+    INTERNET_SCHEME nScheme;
+    LPWSTR  lpszHostName;
+    DWORD   dwHostNameLength;
+    INTERNET_PORT nPort;
+    LPWSTR  lpszUserName;
+    DWORD   dwUserNameLength;
+    LPWSTR  lpszPassword;
+    DWORD   dwPasswordLength;
+    LPWSTR  lpszUrlPath;
+    DWORD   dwUrlPathLength;
+    LPWSTR  lpszExtraInfo;
+    DWORD   dwExtraInfoLength;
+} URL_COMPONENTS, *LPURL_COMPONENTS;
+typedef URL_COMPONENTS URL_COMPONENTSW;
+typedef LPURL_COMPONENTS LPURL_COMPONENTSW;
+
+typedef struct
+{
+    DWORD_PTR dwResult;
+    DWORD dwError;
+} WINHTTP_ASYNC_RESULT, *LPWINHTTP_ASYNC_RESULT;
+
+typedef struct
+{
+    FILETIME ftExpiry;
+    FILETIME ftStart;
+    LPWSTR lpszSubjectInfo;
+    LPWSTR lpszIssuerInfo;
+    LPWSTR lpszProtocolName;
+    LPWSTR lpszSignatureAlgName;
+    LPWSTR lpszEncryptionAlgName;
+    DWORD dwKeySize;
+} WINHTTP_CERTIFICATE_INFO;
+
+typedef struct
+{
+    DWORD dwAccessType;
+    LPWSTR lpszProxy;
+    LPWSTR lpszProxyBypass;
+} WINHTTP_PROXY_INFO, *LPWINHTTP_PROXY_INFO;
+typedef WINHTTP_PROXY_INFO WINHTTP_PROXY_INFOW;
+typedef LPWINHTTP_PROXY_INFO LPWINHTTP_PROXY_INFOW;
+
+typedef struct
+{
+    BOOL   fAutoDetect;
+    LPWSTR lpszAutoConfigUrl;
+    LPWSTR lpszProxy;
+    LPWSTR lpszProxyBypass;
+} WINHTTP_CURRENT_USER_IE_PROXY_CONFIG;
+
+typedef VOID (CALLBACK *WINHTTP_STATUS_CALLBACK)(HINTERNET,DWORD_PTR,DWORD,LPVOID,DWORD);
+
+#define WINHTTP_AUTO_DETECT_TYPE_DHCP   0x00000001
+#define WINHTTP_AUTO_DETECT_TYPE_DNS_A  0x00000002
+
+#define WINHTTP_AUTOPROXY_AUTO_DETECT           0x00000001
+#define WINHTTP_AUTOPROXY_CONFIG_URL            0x00000002
+#define WINHTTP_AUTOPROXY_RUN_INPROCESS         0x00010000
+#define WINHTTP_AUTOPROXY_RUN_OUTPROCESS_ONLY   0x00020000
+
+typedef struct
+{
+    DWORD dwFlags;
+    DWORD dwAutoDetectFlags;
+    LPCWSTR lpszAutoConfigUrl;
+    LPVOID lpvReserved;
+    DWORD dwReserved;
+    BOOL fAutoLogonIfChallenged;
+} WINHTTP_AUTOPROXY_OPTIONS;
+
+typedef struct
+{
+    DWORD dwMajorVersion;
+    DWORD dwMinorVersion;
+} HTTP_VERSION_INFO, *LPHTTP_VERSION_INFO;
+
+#ifdef _WS2DEF_
+typedef struct
+{
+    DWORD cbSize;
+    SOCKADDR_STORAGE LocalAddress;
+    SOCKADDR_STORAGE RemoteAddress;
+} WINHTTP_CONNECTION_INFO;
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+BOOL        WINAPI WinHttpAddRequestHeaders(HINTERNET,LPCWSTR,DWORD,DWORD);
+BOOL        WINAPI WinHttpDetectAutoProxyConfigUrl(DWORD,LPWSTR*);
+BOOL        WINAPI WinHttpCheckPlatform(void);
+BOOL        WINAPI WinHttpCloseHandle(HINTERNET);
+HINTERNET   WINAPI WinHttpConnect(HINTERNET,LPCWSTR,INTERNET_PORT,DWORD);
+BOOL        WINAPI WinHttpCrackUrl(LPCWSTR,DWORD,DWORD,LPURL_COMPONENTS);
+BOOL        WINAPI WinHttpCreateUrl(LPURL_COMPONENTS,DWORD,LPWSTR,LPDWORD);
+BOOL        WINAPI WinHttpGetDefaultProxyConfiguration(WINHTTP_PROXY_INFO*);
+BOOL        WINAPI WinHttpGetIEProxyConfigForCurrentUser(WINHTTP_CURRENT_USER_IE_PROXY_CONFIG*);
+BOOL        WINAPI WinHttpGetProxyForUrl(HINTERNET,LPCWSTR,WINHTTP_AUTOPROXY_OPTIONS*,WINHTTP_PROXY_INFO*);
+HINTERNET   WINAPI WinHttpOpen(LPCWSTR,DWORD,LPCWSTR,LPCWSTR,DWORD);
+HINTERNET   WINAPI WinHttpOpenRequest(HINTERNET,LPCWSTR,LPCWSTR,LPCWSTR,LPCWSTR,LPCWSTR*,DWORD);
+BOOL        WINAPI WinHttpQueryAuthParams(HINTERNET,DWORD,LPVOID*);
+BOOL        WINAPI WinHttpQueryAuthSchemes(HINTERNET,LPDWORD,LPDWORD,LPDWORD);
+BOOL        WINAPI WinHttpQueryDataAvailable(HINTERNET,LPDWORD);
+BOOL        WINAPI WinHttpQueryHeaders(HINTERNET,DWORD,LPCWSTR,LPVOID,LPDWORD,LPDWORD);
+BOOL        WINAPI WinHttpQueryOption(HINTERNET,DWORD,LPVOID,LPDWORD);
+BOOL        WINAPI WinHttpReadData(HINTERNET,LPVOID,DWORD,LPDWORD);
+BOOL        WINAPI WinHttpReceiveResponse(HINTERNET,LPVOID);
+BOOL        WINAPI WinHttpSendRequest(HINTERNET,LPCWSTR,DWORD,LPVOID,DWORD,DWORD,DWORD_PTR);
+BOOL        WINAPI WinHttpSetDefaultProxyConfiguration(WINHTTP_PROXY_INFO*);
+BOOL        WINAPI WinHttpSetCredentials(HINTERNET,DWORD,DWORD,LPCWSTR,LPCWSTR,LPVOID);
+BOOL        WINAPI WinHttpSetOption(HINTERNET,DWORD,LPVOID,DWORD);
+WINHTTP_STATUS_CALLBACK WINAPI WinHttpSetStatusCallback(HINTERNET,WINHTTP_STATUS_CALLBACK,DWORD,DWORD_PTR);
+BOOL        WINAPI WinHttpSetTimeouts(HINTERNET,int,int,int,int);
+BOOL        WINAPI WinHttpTimeFromSystemTime(const SYSTEMTIME *,LPWSTR);
+BOOL        WINAPI WinHttpTimeToSystemTime(LPCWSTR,SYSTEMTIME*);
+BOOL        WINAPI WinHttpWriteData(HINTERNET,LPCVOID,DWORD,LPDWORD);
+
+#ifdef __cplusplus
+}
+#endif
+
+#include <poppack.h>
+
+#endif  /* __WINE_WINHTTP_H */
diff --git a/script/cibuild.sh b/script/cibuild.sh
index 8983dcc..bdc3529 100755
--- a/script/cibuild.sh
+++ b/script/cibuild.sh
@@ -6,17 +6,25 @@ then
 	exit $?;
 fi
 
+mkdir _build
+cd _build
+# shellcheck disable=SC2086
+cmake .. -DCMAKE_INSTALL_PREFIX=../_install $OPTIONS || exit $?
+make -j2 install || exit $?
+
+# If this platform doesn't support test execution, bail out now
+if [ -n "$SKIP_TESTS" ];
+then
+	exit $?;
+fi
+
 # Create a test repo which we can use for the online::push tests
 mkdir "$HOME"/_temp
 git init --bare "$HOME"/_temp/test.git
 git daemon --listen=localhost --export-all --enable=receive-pack --base-path="$HOME"/_temp "$HOME"/_temp 2>/dev/null &
 export GITTEST_REMOTE_URL="git://localhost/test.git"
 
-mkdir _build
-cd _build
-# shellcheck disable=SC2086
-cmake .. -DCMAKE_INSTALL_PREFIX=../_install $OPTIONS || exit $?
-make -j2 install || exit $?
+# Run the test suite
 ctest -V . || exit $?
 
 # Now that we've tested the raw git protocol, let's set up ssh to we
diff --git a/script/toolchain-mingw32.cmake b/script/toolchain-mingw32.cmake
new file mode 100644
index 0000000..3ccdec2
--- /dev/null
+++ b/script/toolchain-mingw32.cmake
@@ -0,0 +1,11 @@
+# CMake toolchain file for Win32 cross-compile
+SET(CMAKE_SYSTEM_NAME Windows)
+
+SET(CMAKE_C_COMPILER i586-mingw32msvc-gcc)
+SET(CMAKE_RC_COMPILER i586-mingw32msvc-windres)
+
+SET(CMAKE_FIND_ROOT_PATH /usr/i586-mingw32msvc)
+
+SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
+SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
+SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
diff --git a/src/transports/winhttp.c b/src/transports/winhttp.c
index 278ef22..1d05379 100644
--- a/src/transports/winhttp.c
+++ b/src/transports/winhttp.c
@@ -19,17 +19,9 @@
 #include <wincrypt.h>
 #pragma comment(lib, "crypt32")
 #include <winhttp.h>
-#pragma comment(lib, "winhttp")
-
-#include <strsafe.h>
-
-/* For IInternetSecurityManager zone check */
 #include <objbase.h>
 #include <urlmon.h>
 
-/* For UuidCreate */
-#pragma comment(lib, "rpcrt4")
-
 #define WIDEN2(s) L ## s
 #define WIDEN(s) WIDEN2(s)
 
@@ -43,7 +35,6 @@
 #define WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH 0
 #endif
 
-static const char *prefix_http = "http://";
 static const char *prefix_https = "https://";
 static const char *upload_pack_service = "upload-pack";
 static const char *upload_pack_ls_service_url = "/info/refs?service=git-upload-pack";
@@ -59,6 +50,13 @@ static const int no_check_cert_flags = SECURITY_FLAG_IGNORE_CERT_CN_INVALID |
 	SECURITY_FLAG_IGNORE_CERT_DATE_INVALID |
 	SECURITY_FLAG_IGNORE_UNKNOWN_CA;
 
+#if defined(__MINGW32__)
+static const CLSID CLSID_InternetSecurityManager = { 0x7B8A2D94, 0x0AC9, 0x11D1,
+	{ 0x89, 0x6C, 0x00, 0xC0, 0x4F, 0xB6, 0xBF, 0xC4 } };
+static const IID IID_IInternetSecurityManager = { 0x79EAC9EE, 0xBAF9, 0x11CE,
+	{ 0x8C, 0x82, 0x00, 0xAA, 0x00, 0x4B, 0xA9, 0x0B } };
+#endif
+
 #define OWNING_SUBTRANSPORT(s) ((winhttp_subtransport *)(s)->parent.subtransport)
 
 typedef enum {
@@ -272,7 +270,7 @@ static int winhttp_stream_connect(winhttp_stream *s)
 	git_buf buf = GIT_BUF_INIT;
 	char *proxy_url = NULL;
 	wchar_t ct[MAX_CONTENT_TYPE_LEN];
-	wchar_t *types[] = { L"*/*", NULL };
+	LPCWSTR types[] = { L"*/*", NULL };
 	BOOL peerdist = FALSE;
 	int error = -1;
 	unsigned long disable_redirects = WINHTTP_DISABLE_REDIRECTS;
@@ -551,8 +549,7 @@ static int winhttp_close_connection(winhttp_subtransport *t)
 }
 
 static int winhttp_connect(
-	winhttp_subtransport *t,
-	const char *url)
+	winhttp_subtransport *t)
 {
 	wchar_t *ua = L"git/1.0 (libgit2 " WIDEN(LIBGIT2_VERSION) L")";
 	wchar_t *wide_host;
@@ -561,8 +558,6 @@ static int winhttp_connect(
 	int default_timeout = TIMEOUT_INFINITE;
 	int default_connect_timeout = DEFAULT_CONNECT_TIMEOUT;
 
-	GIT_UNUSED(url);
-
 	t->session = NULL;
 	t->connection = NULL;
 
@@ -860,7 +855,7 @@ replay:
 
 				winhttp_close_connection(t);
 
-				if (winhttp_connect(t, location8) < 0)
+				if (winhttp_connect(t) < 0)
 					return -1;
 			}
 
@@ -963,7 +958,6 @@ static int winhttp_stream_write_single(
 	size_t len)
 {
 	winhttp_stream *s = (winhttp_stream *)stream;
-	winhttp_subtransport *t = OWNING_SUBTRANSPORT(s);
 	DWORD bytes_written;
 	int error;
 
@@ -998,7 +992,7 @@ static int put_uuid_string(LPWSTR buffer, size_t buffer_len_cch)
 {
 	UUID uuid;
 	RPC_STATUS status = UuidCreate(&uuid);
-	HRESULT result;
+	int result;
 
 	if (RPC_S_OK != status &&
 		RPC_S_UUID_LOCAL_ONLY != status &&
@@ -1012,14 +1006,14 @@ static int put_uuid_string(LPWSTR buffer, size_t buffer_len_cch)
 		return -1;
 	}
 
-	result = StringCbPrintfW(
-		buffer, buffer_len_cch,
+	result = wsprintfW(
+		buffer,
 		L"%08x%04x%04x%02x%02x%02x%02x%02x%02x%02x%02x",
 		uuid.Data1, uuid.Data2, uuid.Data3,
 		uuid.Data4[0], uuid.Data4[1], uuid.Data4[2], uuid.Data4[3],
 		uuid.Data4[4], uuid.Data4[5], uuid.Data4[6], uuid.Data4[7]);
 
-	if (FAILED(result)) {
+	if (result < UUID_LENGTH_CCH) {
 		giterr_set(GITERR_OS, "Unable to generate name for temp file");
 		return -1;
 	}
@@ -1053,7 +1047,6 @@ static int winhttp_stream_write_buffered(
 	size_t len)
 {
 	winhttp_stream *s = (winhttp_stream *)stream;
-	winhttp_subtransport *t = OWNING_SUBTRANSPORT(s);
 	DWORD bytes_written;
 
 	if (!s->request && winhttp_stream_connect(s) < 0)
@@ -1135,7 +1128,7 @@ static int winhttp_stream_write_chunked(
 	}
 	else {
 		/* Append as much to the buffer as we can */
-		int count = min(CACHED_POST_BODY_BUF_SIZE - s->chunk_buffer_len, (int)len);
+		int count = (int)min(CACHED_POST_BODY_BUF_SIZE - s->chunk_buffer_len, len);
 
 		if (!s->chunk_buffer)
 			s->chunk_buffer = git__malloc(CACHED_POST_BODY_BUF_SIZE);
@@ -1195,6 +1188,8 @@ static int winhttp_uploadpack_ls(
 	winhttp_subtransport *t,
 	winhttp_stream *s)
 {
+	GIT_UNUSED(t);
+
 	s->service = upload_pack_service;
 	s->service_url = upload_pack_ls_service_url;
 	s->verb = get_verb;
@@ -1206,6 +1201,8 @@ static int winhttp_uploadpack(
 	winhttp_subtransport *t,
 	winhttp_stream *s)
 {
+	GIT_UNUSED(t);
+
 	s->service = upload_pack_service;
 	s->service_url = upload_pack_service_url;
 	s->verb = post_verb;
@@ -1217,6 +1214,8 @@ static int winhttp_receivepack_ls(
 	winhttp_subtransport *t,
 	winhttp_stream *s)
 {
+	GIT_UNUSED(t);
+
 	s->service = receive_pack_service;
 	s->service_url = receive_pack_ls_service_url;
 	s->verb = get_verb;
@@ -1228,6 +1227,8 @@ static int winhttp_receivepack(
 	winhttp_subtransport *t,
 	winhttp_stream *s)
 {
+	GIT_UNUSED(t);
+
 	/* WinHTTP only supports Transfer-Encoding: chunked
 	 * on Windows Vista (NT 6.0) and higher. */
 	s->chunked = git_has_win32_version(6, 0, 0);
@@ -1256,7 +1257,7 @@ static int winhttp_action(
 
 	if (!t->connection)
 		if ((ret = gitno_connection_data_from_url(&t->connection_data, url, NULL)) < 0 ||
-			 (ret = winhttp_connect(t, url)) < 0)
+			 (ret = winhttp_connect(t)) < 0)
 			return ret;
 
 	if (winhttp_stream_alloc(t, &s) < 0)