winhttp.go (4371B)
1 //go:build windows 2 3 package winhttp 4 5 import ( 6 "syscall" 7 "unsafe" 8 ) 9 10 const ( 11 DEFAULT_PORT = 0 // use the protocol-specific default 12 DEFAULT_HTTP_PORT = 80 // " " HTTP " 13 DEFAULT_HTTPS_PORT = 443 // " " HTTPS " 14 ) 15 16 type ( 17 HINTERNET = uintptr 18 DWORD = uint32 19 BOOL = uint32 20 LPVOID = unsafe.Pointer 21 LPCWSTR = *uint16 22 LPDWORD = *DWORD 23 DWORD_PTR = uint 24 ) 25 26 type AccessType DWORD 27 28 const ( 29 AcessTypeDefaultProxy AccessType = 0 30 AcessTypeNoProxy AccessType = 1 31 AcessTypeNamedProxy AccessType = 3 32 AcessTypeAutoProxy AccessType = 4 33 ) 34 35 type OpenFlag DWORD 36 37 const ( 38 OpenFlagNone OpenFlag = 0 39 OpenFlagAsync OpenFlag = 0x10000000 // this session is asynchronous (where supported) 40 ) 41 42 type OpenRequestFlag DWORD 43 44 const ( 45 OpenRequestFlagNone OpenRequestFlag = 0 46 OpenRequestFlagSecure OpenRequestFlag = 0x00800000 // use SSL if applicable (HTTPS) 47 OpenRequestFlagEscapePercent OpenRequestFlag = 0x00000004 // if escaping enabled, escape percent as well 48 OpenRequestFlagNullCodepage OpenRequestFlag = 0x00000008 // assume all symbols are ASCII, use fast convertion 49 OpenRequestFlagBypassProxyCache OpenRequestFlag = 0x00000100 // add "pragma: no-cache" request header 50 OpenRequestFlagRefresh OpenRequestFlag = 0x00000100 51 OpenRequestFlagEscapeDisable OpenRequestFlag = 0x00000040 // disable escaping 52 OpenRequestFlagEscapeDisableQuery OpenRequestFlag = 0x00000080 // if escaping enabled escape path part, but do not escape query 53 ) 54 55 var ( 56 _WinHttp = syscall.NewLazyDLL("WinHttp") 57 58 _Open = _WinHttp.NewProc("WinHttpOpen") 59 _Connect = _WinHttp.NewProc("WinHttpConnect") 60 _OpenRequest = _WinHttp.NewProc("WinHttpOpenRequest") 61 _SendRequest = _WinHttp.NewProc("WinHttpSendRequest") 62 _ReceiveResponse = _WinHttp.NewProc("WinHttpReceiveResponse") 63 _ReadData = _WinHttp.NewProc("WinHttpReadData") 64 _QueryDataAvailable = _WinHttp.NewProc("WinHttpQueryDataAvailable") 65 _CloseHandle = _WinHttp.NewProc("WinHttpCloseHandle") 66 ) 67 68 func Open( 69 agent LPCWSTR, 70 access AccessType, 71 proxy, 72 proxy_bypass LPCWSTR, 73 flags OpenFlag, 74 ) HINTERNET { 75 r, _, _ := _Open.Call( 76 uintptr(unsafe.Pointer(agent)), 77 uintptr(access), 78 uintptr(unsafe.Pointer(proxy)), 79 uintptr(unsafe.Pointer(proxy_bypass)), 80 uintptr(flags), 81 ) 82 return r 83 } 84 85 func Connect( 86 hSession HINTERNET, 87 pswzServerName LPCWSTR, 88 nServerPort, 89 dwReserved DWORD, 90 ) HINTERNET { 91 r, _, _ := _Connect.Call( 92 uintptr(hSession), 93 uintptr(unsafe.Pointer(pswzServerName)), 94 uintptr(nServerPort), 95 uintptr(dwReserved), 96 ) 97 return r 98 } 99 100 func OpenRequest( 101 hConnect HINTERNET, 102 pwszVerb LPCWSTR, 103 pwszObjectName LPCWSTR, 104 pwszVersion LPCWSTR, 105 pwszReferrer LPCWSTR, 106 ppwszAcceptTypes *LPCWSTR, 107 dwFlags OpenRequestFlag, 108 ) HINTERNET { 109 r, _, _ := _OpenRequest.Call( 110 uintptr(hConnect), 111 uintptr(unsafe.Pointer(pwszVerb)), 112 uintptr(unsafe.Pointer(pwszObjectName)), 113 uintptr(unsafe.Pointer(pwszVersion)), 114 uintptr(unsafe.Pointer(pwszReferrer)), 115 uintptr(unsafe.Pointer(ppwszAcceptTypes)), 116 uintptr(dwFlags), 117 ) 118 return r 119 } 120 121 func SendRequest( 122 hRequest HINTERNET, 123 lpszHeaders LPCWSTR, 124 dwHeadersLength DWORD, 125 lpOptional LPVOID, 126 dwOptionalLength DWORD, 127 dwTotalLength DWORD, 128 dwContext DWORD_PTR, 129 ) BOOL { 130 r, _, _ := _SendRequest.Call( 131 uintptr(hRequest), 132 uintptr(unsafe.Pointer(lpszHeaders)), 133 uintptr(dwHeadersLength), 134 uintptr(lpOptional), 135 uintptr(dwOptionalLength), 136 uintptr(dwTotalLength), 137 uintptr(dwContext), 138 ) 139 return BOOL(r) 140 } 141 142 func ReceiveResponse( 143 hRequest HINTERNET, 144 lpReserved LPVOID, 145 ) BOOL { 146 r, _, _ := _ReceiveResponse.Call( 147 uintptr(hRequest), 148 uintptr(lpReserved), 149 ) 150 return BOOL(r) 151 } 152 153 func ReadData( 154 hRequest HINTERNET, 155 lpBuffer LPVOID, 156 dwNumberOfBytesToRead DWORD, 157 lpdwNumberOfBytesRead LPDWORD, 158 ) BOOL { 159 r, _, _ := _ReadData.Call( 160 uintptr(hRequest), 161 uintptr(lpBuffer), 162 uintptr(dwNumberOfBytesToRead), 163 uintptr(unsafe.Pointer(lpdwNumberOfBytesRead)), 164 ) 165 return BOOL(r) 166 } 167 168 func QueryDataAvailable( 169 hRequest HINTERNET, 170 lpdwNumberOfBytesAvailable LPDWORD, 171 ) BOOL { 172 r, _, _ := _QueryDataAvailable.Call( 173 uintptr(hRequest), 174 uintptr(unsafe.Pointer(lpdwNumberOfBytesAvailable)), 175 ) 176 return BOOL(r) 177 } 178 179 func CloseHandle( 180 hInternet HINTERNET, 181 ) BOOL { 182 r, _, _ := _CloseHandle.Call(uintptr(hInternet)) 183 return BOOL(r) 184 }