-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWininet.ew
281 lines (224 loc) · 10.7 KB
/
Wininet.ew
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
include std/dll.e
include std/machine.e
include std/net/url.e
include std/types.e
include std/utils.e
include euphoria/info.e
public constant
INTERNET_INVALID_PORT_NUMBER = 0,
$
public constant
INTERNET_DEFAULT_FTP_PORT = 21,
INTERNET_DEFAULT_GOPHER_PORT = 70,
INTERNET_DEFAULT_HTTP_PORT = 80,
INTERNET_DEFAULT_HTTPS_PORT = 443,
INTERNET_DEFAULT_SOCKS_PORT = 1080,
$
public constant
INTERNET_KEEP_ALIVE_UNKNOWN = (-1),
INTERNET_KEEP_ALIVE_ENABLED = 1,
INTERNET_KEEP_ALIVE_DISABLED = 0,
$
public constant
INTERNET_REQFLAG_FROM_CACHE = 0x00000001,
INTERNET_REQFLAG_ASYNC = 0x00000002,
INTERNET_REQFLAG_VIA_PROXY = 0x00000004,
INTERNET_REQFLAG_NO_HEADERS = 0x00000008,
INTERNET_REQFLAG_PASSIVE = 0x00000010,
INTERNET_REQFLAG_CACHE_WRITE_DISABLED = 0x00000040,
INTERNET_REQFLAG_NET_TIMEOUT = 0x00000080,
$
public constant
INTERNET_FLAG_ASYNC = 0x10000000,
INTERNET_FLAG_CACHE_ASYNC = 0x00000080,
INTERNET_FLAG_CACHE_IF_NET_FAIL = 0x00010000,
INTERNET_FLAG_EXISTING_CONNECT = 0x20000000,
INTERNET_FLAG_FORMS_SUBMIT = 0x00000040,
INTERNET_FLAG_FROM_CACHE = 0x01000000,
INTERNET_FLAG_FWD_BACK = 0x00000020,
INTERNET_FLAG_HYPERLINK = 0x00000400,
INTERNET_FLAG_IGNORE_CERT_CN_INVALID = 0x00001000,
INTERNET_FLAG_IGNORE_CERT_DATE_INVALID = 0x00002000,
INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP = 0x00008000,
INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS = 0x00004000,
INTERNET_FLAG_KEEP_CONNECTION = 0x00400000,
INTERNET_FLAG_MAKE_PERSISTENT = 0x02000000,
INTERNET_FLAG_NEED_FILE = 0x00000010,
INTERNET_FLAG_NO_AUTH = 0x00040000,
INTERNET_FLAG_NO_AUTO_REDIRECT = 0x00200000,
INTERNET_FLAG_NO_CACHE_WRITE = 0x04000000,
INTERNET_FLAG_NO_COOKIES = 0x00080000,
INTERNET_FLAG_NO_UI = 0x00000200,
INTERNET_FLAG_PASSIVE = 0x08000000,
INTERNET_FLAG_PRAGMA_NOCACHE = 0x00000100,
INTERNET_FLAG_RAW_DATA = 0x40000000,
INTERNET_FLAG_READ_PREFETCH = 0x00100000,
INTERNET_FLAG_RELOAD = 0x80000000,
INTERNET_FLAG_RESTRICTED_ZONE = 0x00020000,
INTERNET_FLAG_RESYNCHRONIZE = 0x00000800,
INTERNET_FLAG_SECURE = 0x00800000,
INTERNET_FLAG_DONT_CACHE = INTERNET_FLAG_NO_CACHE_WRITE,
INTERNET_FLAG_MUST_CACHE_REQUEST = INTERNET_FLAG_NEED_FILE,
INTERNET_FLAG_OFFLINE = INTERNET_FLAG_FROM_CACHE,
$
public constant
INTERNET_OPEN_TYPE_PRECONFIG = 0,
INTERNET_OPEN_TYPE_DIRECT = 1,
INTERNET_OPEN_TYPE_PROXY = 3,
INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY = 4,
$
public constant
INTERNET_SERVICE_FTP = 1,
INTERNET_SERVICE_GOPHER = 2,
INTERNET_SERVICE_HTTP = 3,
$
public constant
INTERNET_STATUS_RESOLVING_NAME = 10,
INTERNET_STATUS_NAME_RESOLVED = 11,
INTERNET_STATUS_CONNECTING_TO_SERVER = 20,
INTERNET_STATUS_CONNECTED_TO_SERVER = 21,
INTERNET_STATUS_SENDING_REQUEST = 30,
INTERNET_STATUS_REQUEST_SENT = 31,
INTERNET_STATUS_RECEIVING_RESPONSE = 40,
INTERNET_STATUS_RESPONSE_RECEIVED = 41,
INTERNET_STATUS_CTL_RESPONSE_RECEIVED = 42,
INTERNET_STATUS_PREFETCH = 43,
INTERNET_STATUS_CLOSING_CONNECTION = 50,
INTERNET_STATUS_CONNECTION_CLOSED = 51,
INTERNET_STATUS_HANDLE_CREATED = 60,
INTERNET_STATUS_HANDLE_CLOSING = 70,
INTERNET_STATUS_DETECTING_PROXY = 80,
INTERNET_STATUS_REQUEST_COMPLETE = 100,
INTERNET_STATUS_REDIRECT = 110,
INTERNET_STATUS_INTERMEDIATE_RESPONSE = 120,
INTERNET_STATUS_USER_INPUT_REQUIRED = 140,
INTERNET_STATUS_STATE_CHANGE = 200,
INTERNET_STATUS_COOKIE_SENT = 320,
INTERNET_STATUS_COOKIE_RECEIVED = 321,
INTERNET_STATUS_PRIVACY_IMPACTED = 324,
INTERNET_STATUS_P3P_HEADER = 325,
INTERNET_STATUS_P3P_POLICYREF = 326,
INTERNET_STATUS_COOKIE_HISTORY = 327,
$
public constant
INTERNET_STATE_CONNECTED = 0x00000001,
INTERNET_STATE_DISCONNECTED = 0x00000002,
INTERNET_STATE_DISCONNECTED_BY_USER = 0x00000010,
INTERNET_STATE_IDLE = 0x00000100,
INTERNET_STATE_BUSY = 0x00000200,
$
public constant ERROR_INSUFFICIENT_BUFFER = 122
public constant AGENT_STRING = "Euphoria/" & version_string_short()
atom Wininet = open_dll( "Wininet.dll" )
constant
C_DWORD_PTR = C_POINTER,
C_HINTERNET = C_HANDLE,
C_INTERNET_PORT = C_INT,
C_LPCTSTR = C_POINTER,
C_LPDWORD = C_POINTER,
C_LPVOID = C_POINTER,
$
constant
xHttpOpenRequest = define_c_func( Wininet, "HttpOpenRequestW", {C_HINTERNET,C_LPCTSTR,C_LPCTSTR,C_LPCTSTR,C_LPCTSTR,C_LPCTSTR,C_DWORD,C_DWORD_PTR}, C_HINTERNET ),
xHttpSendRequest = define_c_func( Wininet, "HttpSendRequestW", {C_HINTERNET,C_LPCTSTR,C_DWORD,C_LPVOID,C_DWORD}, C_BOOL ),
xInternetCloseHandle = define_c_func( Wininet, "InternetCloseHandle", {C_HINTERNET}, C_BOOL ),
xInternetConnect = define_c_func( Wininet, "InternetConnectW", {C_HINTERNET,C_LPCTSTR,C_INTERNET_PORT,C_LPCTSTR,C_LPCTSTR,C_DWORD,C_DWORD,C_DWORD_PTR}, C_HINTERNET ),
xInternetOpen = define_c_func( Wininet, "InternetOpenW", {C_LPCTSTR,C_DWORD,C_LPCTSTR,C_LPCTSTR,C_DWORD}, C_HINTERNET ),
xInternetReadFile = define_c_func( Wininet, "InternetReadFile", {C_HINTERNET,C_LPVOID,C_DWORD,C_LPDWORD}, C_BOOL ),
$
function allocate_wstring_pointer_array( object string_list, boolean cleanup = FALSE )
if atom( string_list ) then return NULL end if
atom array_pointer = allocate_data( sizeof(C_POINTER) * length(string_list) )
atom offset = array_pointer
for i = 1 to length( string_list ) do
poke_pointer( offset, allocate_wstring(string_list[i]) )
offset += sizeof( C_POINTER )
end for
if cleanup then
return delete_routine( array_pointer, routine_id("free_pointer_array") )
end if
return array_pointer
end function
public function HttpOpenRequest( atom hConnect, object lpszVerb, object lpszObjectName, object lpszVersion = NULL, object lpszReferer = NULL, object lplpszAcceptTypes = NULL, atom dwFlags = 0, atom dwContext = NULL )
if sequence( lpszVerb ) then lpszVerb = allocate_wstring( lpszVerb, 1 ) end if
if sequence( lpszObjectName ) then lpszObjectName = allocate_wstring( lpszObjectName, 1 ) end if
if sequence( lpszReferer ) then lpszReferer = allocate_wstring( lpszReferer, 1 ) end if
if sequence( lplpszAcceptTypes ) then
lplpszAcceptTypes = allocate_wstring_pointer_array( lplpszAcceptTypes, 1 )
end if
return c_func( xHttpOpenRequest, {hConnect,lpszVerb,lpszObjectName,lpszVersion,lpszReferer,lplpszAcceptTypes,dwFlags,dwContext} )
end function
public function HttpSendRequest( atom hRequest, object lpszHeaders = NULL, atom dwHeadersLength = 0, atom lpOptional = NULL, atom dwOptionalLength = 0 )
if sequence( lpszHeaders ) then
dwHeadersLength = length( lpszHeaders )
lpszHeaders = allocate_wstring( lpszHeaders, 1 )
end if
if sequence( lpOptional ) then
dwOptionalLength = length( lpOptional )
lpOptional = allocate_wstring( lpOptional, 1 )
end if
return c_func( xHttpSendRequest, {hRequest,lpszHeaders,dwHeadersLength,lpOptional,dwOptionalLength} )
end function
public function InternetCloseHandle( atom hInternet )
return c_func( xInternetCloseHandle, {hInternet} )
end function
public function InternetConnect( atom hInternet, object lpszServerName, atom nServerPort, object lpszUsername = NULL, object lpszPassword = NULL, atom dwService = INTERNET_SERVICE_HTTP, atom dwFlags = 0, atom dwContext = NULL )
if sequence( lpszServerName ) then lpszServerName = allocate_wstring( lpszServerName, 1 ) end if
if sequence( lpszUsername ) then lpszUsername = allocate_wstring( lpszUsername, 1 ) end if
if sequence( lpszPassword ) then lpszPassword = allocate_wstring( lpszPassword, 1 ) end if
return c_func( xInternetConnect, {hInternet,lpszServerName,nServerPort,lpszUsername,lpszPassword,dwService,dwFlags,dwContext} )
end function
public function InternetOpen( object lpszAgent, atom dwAccessType, object lpszProxyName = NULL, object lpszProxyBypass = NULL, atom dwFlags = 0 )
if sequence( lpszAgent ) then lpszAgent = allocate_wstring( lpszAgent, 1 ) end if
if sequence( lpszProxyName ) then lpszProxyName = allocate_wstring( lpszProxyName, 1 ) end if
if sequence( lpszProxyBypass ) then lpszProxyBypass = allocate_wstring( lpszProxyBypass, 1 ) end if
return c_func( xInternetOpen, {lpszAgent,dwAccessType,lpszProxyName,lpszProxyBypass,dwFlags} )
end function
public function InternetReadFile( atom hFile, atom dwNumberOfBytesToRead = 4096 )
atom lpBuffer = allocate_data( dwNumberOfBytesToRead, 1 )
mem_set( lpBuffer, NULL, dwNumberOfBytesToRead )
atom lpdwNumberOfBytesRead = allocate_data( sizeof(C_DWORD), 1 )
atom hResult = c_func( xInternetReadFile, {hFile,lpBuffer,dwNumberOfBytesToRead,lpdwNumberOfBytesRead} )
if hResult = FALSE then
return {}
end if
atom dwNumberOfBytesRead = peek4s( lpdwNumberOfBytesRead )
return peek({ lpBuffer, dwNumberOfBytesRead })
end function
public function HttpGetRequest( sequence url, object accept_types = NULL, atom flags = INTERNET_FLAG_NO_UI )
sequence parsed = url:parse( url )
object protocol = parsed[URL_PROTOCOL]
object hostname = parsed[URL_HOSTNAME]
object port = parsed[URL_PORT]
object path = parsed[URL_PATH]
object user = parsed[URL_USER]
object password = parsed[URL_PASSWORD]
object query = parsed[URL_QUERY_STRING]
if equal( protocol, "https" ) and not and_bits( flags, INTERNET_FLAG_SECURE ) then
flags += INTERNET_FLAG_SECURE
end if
if equal( port, 0 ) then
port = iff( equal(protocol, "https"),
INTERNET_DEFAULT_HTTPS_PORT,
INTERNET_DEFAULT_HTTP_PORT )
end if
if not equal( query, 0 ) then
path = path & '?' & query
end if
atom ih = InternetOpen( AGENT_STRING, INTERNET_OPEN_TYPE_PRECONFIG )
atom ch = InternetConnect( ih, hostname, port, user, password )
atom req = HttpOpenRequest( ch, "GET", path, , , accept_types, flags )
sequence buff = {}
if HttpSendRequest( req ) then
sequence data
while length( data ) with entry do
buff &= data
entry
data = InternetReadFile( req )
end while
end if
InternetCloseHandle( req )
InternetCloseHandle( ch )
InternetCloseHandle( ih )
return buff
end function