Skip to content

Commit ddf83a5

Browse files
committed
* Moved HttpGetRequest() to Wininet.ew
* Updated HttpGetRequest() to support any size response. * Updated demo.ex to download files of different sizes.
1 parent a4f756f commit ddf83a5

File tree

2 files changed

+103
-43
lines changed

2 files changed

+103
-43
lines changed

Wininet.ew

+81-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11

22
include std/dll.e
33
include std/machine.e
4+
include std/net/url.e
45
include std/types.e
6+
include euphoria/info.e
57

68
public constant
79
INTERNET_INVALID_PORT_NUMBER = 0,
@@ -78,10 +80,48 @@ public constant
7880
INTERNET_SERVICE_HTTP = 3,
7981
$
8082

83+
public constant
84+
INTERNET_STATUS_RESOLVING_NAME = 10,
85+
INTERNET_STATUS_NAME_RESOLVED = 11,
86+
INTERNET_STATUS_CONNECTING_TO_SERVER = 20,
87+
INTERNET_STATUS_CONNECTED_TO_SERVER = 21,
88+
INTERNET_STATUS_SENDING_REQUEST = 30,
89+
INTERNET_STATUS_REQUEST_SENT = 31,
90+
INTERNET_STATUS_RECEIVING_RESPONSE = 40,
91+
INTERNET_STATUS_RESPONSE_RECEIVED = 41,
92+
INTERNET_STATUS_CTL_RESPONSE_RECEIVED = 42,
93+
INTERNET_STATUS_PREFETCH = 43,
94+
INTERNET_STATUS_CLOSING_CONNECTION = 50,
95+
INTERNET_STATUS_CONNECTION_CLOSED = 51,
96+
INTERNET_STATUS_HANDLE_CREATED = 60,
97+
INTERNET_STATUS_HANDLE_CLOSING = 70,
98+
INTERNET_STATUS_DETECTING_PROXY = 80,
99+
INTERNET_STATUS_REQUEST_COMPLETE = 100,
100+
INTERNET_STATUS_REDIRECT = 110,
101+
INTERNET_STATUS_INTERMEDIATE_RESPONSE = 120,
102+
INTERNET_STATUS_USER_INPUT_REQUIRED = 140,
103+
INTERNET_STATUS_STATE_CHANGE = 200,
104+
INTERNET_STATUS_COOKIE_SENT = 320,
105+
INTERNET_STATUS_COOKIE_RECEIVED = 321,
106+
INTERNET_STATUS_PRIVACY_IMPACTED = 324,
107+
INTERNET_STATUS_P3P_HEADER = 325,
108+
INTERNET_STATUS_P3P_POLICYREF = 326,
109+
INTERNET_STATUS_COOKIE_HISTORY = 327,
110+
$
111+
112+
public constant
113+
INTERNET_STATE_CONNECTED = 0x00000001,
114+
INTERNET_STATE_DISCONNECTED = 0x00000002,
115+
INTERNET_STATE_DISCONNECTED_BY_USER = 0x00000010,
116+
INTERNET_STATE_IDLE = 0x00000100,
117+
INTERNET_STATE_BUSY = 0x00000200,
118+
$
119+
81120
public constant ERROR_INSUFFICIENT_BUFFER = 122
82121

122+
public constant AGENT_STRING = "Euphoria/" & version_string_short()
123+
83124
atom Wininet = open_dll( "Wininet.dll" )
84-
? Wininet
85125

86126
constant
87127
C_DWORD_PTR = C_POINTER,
@@ -101,13 +141,6 @@ constant
101141
xInternetReadFile = define_c_func( Wininet, "InternetReadFile", {C_HINTERNET,C_LPVOID,C_DWORD,C_LPDWORD}, C_BOOL ),
102142
$
103143

104-
? xHttpOpenRequest
105-
& xHttpSendRequest
106-
& xInternetCloseHandle
107-
& xInternetConnect
108-
& xInternetOpen
109-
& xInternetReadFile
110-
111144
function allocate_wstring_pointer_array( object string_list, boolean cleanup = FALSE )
112145

113146
if atom( string_list ) then return NULL end if
@@ -194,4 +227,44 @@ public function InternetReadFile( atom hFile, atom dwNumberOfBytesToRead = 4096
194227
return peek({ lpBuffer, dwNumberOfBytesRead })
195228
end function
196229

230+
public function HttpGetRequest( sequence url, object accept_types = NULL )
231+
232+
atom flags = INTERNET_FLAG_NO_UI
233+
atom port = INTERNET_DEFAULT_HTTP_PORT
234+
235+
sequence parsed = url:parse( url )
236+
237+
if equal( parsed[1], "https" ) then
238+
flags += INTERNET_FLAG_SECURE
239+
port = INTERNET_DEFAULT_HTTPS_PORT
240+
end if
241+
242+
if not equal( parsed[3], 0 ) then
243+
port = parsed[3]
244+
end if
245+
246+
atom ih = InternetOpen( AGENT_STRING, INTERNET_OPEN_TYPE_PRECONFIG )
247+
atom ch = InternetConnect( ih, parsed[2], port )
248+
atom req = HttpOpenRequest( ch, "GET", parsed[4], , , accept_types, flags )
249+
250+
sequence buff = {}
251+
252+
if HttpSendRequest( req ) then
253+
254+
sequence data
255+
256+
while length( data ) with entry do
257+
buff &= data
258+
entry
259+
data = InternetReadFile( req )
260+
end while
261+
262+
end if
263+
264+
InternetCloseHandle( req )
265+
InternetCloseHandle( ch )
266+
InternetCloseHandle( ih )
267+
268+
return buff
269+
end function
197270

demo.ex

+22-35
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,39 @@
11

22
include Wininet.ew
3-
include std/net/url.e
43
include std/pretty.e
54

6-
procedure print_r( object value, sequence name = "", sequence options = {2} )
7-
8-
if length( name ) then
9-
printf( 1, "%s = ", {name} )
10-
end if
11-
12-
pretty_print( 1, value, options )
13-
puts( 1, "\n" )
14-
15-
end procedure
5+
constant UNITS = {"bytes","KiB","MiB"}
166

17-
public function HttpGetRequest( sequence url )
18-
19-
atom flags = INTERNET_FLAG_NO_UI
20-
atom port = INTERNET_DEFAULT_HTTP_PORT
7+
function format_size( atom size )
218

22-
sequence parsed = url:parse( url )
9+
integer index = 1
2310

24-
if equal( parsed[1], "https" ) then
25-
flags += INTERNET_FLAG_SECURE
26-
port = INTERNET_DEFAULT_HTTPS_PORT
27-
end if
11+
while size > 1024 do
12+
size /= 1024
13+
index += 1
14+
end while
2815

29-
atom ih = InternetOpen( "Mozilla/4.0 (compatible)", INTERNET_OPEN_TYPE_PRECONFIG )
30-
atom ch = InternetConnect( ih, parsed[2], port )
31-
atom req = HttpOpenRequest( ch, "GET", parsed[4], , , , flags )
32-
33-
object data = {}
16+
return sprintf( "%0.2f %s", {size,UNITS[index]} )
17+
end function
18+
19+
procedure download_file( sequence url )
3420

35-
if HttpSendRequest( req ) then
36-
data = InternetReadFile( req )
37-
end if
21+
sequence data = HttpGetRequest( url )
22+
sequence size = format_size( length(data) )
3823

39-
InternetCloseHandle( req )
40-
InternetCloseHandle( ch )
41-
InternetCloseHandle( ih )
24+
printf( 1, "%10s %s\n", {size,url} )
4225

43-
return data
44-
end function
26+
end procedure
4527

4628
procedure main()
4729

48-
object data = HttpGetRequest( "https://myhtf.net/server-ip.txt" )
49-
print_r( data, "data" )
30+
download_file( "https://openeuphoria.org/index.wc" )
31+
download_file( "https://openeuphoria.org/forum/" )
32+
download_file( "https://sourceforge.net/projects/rapideuphoria/files/Euphoria/4.0.5/euphoria-4.0.5.pdf" )
33+
download_file( "https://sourceforge.net/projects/rapideuphoria/files/Euphoria/4.0.5/euphoria-4.0.5-html.zip" )
34+
download_file( "https://sourceforge.net/projects/rapideuphoria/files/Euphoria/4.0.5/euphoria_4.0.5_i386.deb" )
35+
download_file( "https://sourceforge.net/projects/rapideuphoria/files/Euphoria/4.0.5/euphoria-4.0.5.exe" )
36+
download_file( "https://sourceforge.net/projects/rapideuphoria/files/Euphoria/4.1.0-beta2/euphoria-4.1.0-x86.exe" )
5037

5138
end procedure
5239

0 commit comments

Comments
 (0)