MCPcopy Create free account
hub / github.com/AutoHotkey/AutoHotkey / Download

Method Download

source/script_autoit.cpp:924–1015  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

922
923
924ResultType Line::Download(LPTSTR aURL, LPTSTR aFilespec)
925{
926 // v1.0.44.07: Set default to INTERNET_FLAG_RELOAD vs. 0 because the vast majority of usages would want
927 // the file to be retrieved directly rather than from the cache.
928 // v1.0.46.04: Added more no-cache flags because otherwise, it definitely falls back to the cache if
929 // the remote server doesn't repond (and perhaps other errors), which defeats the ability to use the
930 // Download command for uptime/server monitoring. Also, in spite of what MSDN says, it seems nearly
931 // certain based on other sources that more than one flag is supported. Someone also mentioned that
932 // INTERNET_FLAG_CACHE_IF_NET_FAIL is related to this, but there's no way to specify it in these
933 // particular calls, and it's the opposite of the desired behavior anyway; so it seems impossible to
934 // turn it off explicitly.
935 DWORD flags_for_open_url = INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE;
936 aURL = omit_leading_whitespace(aURL);
937 if (*aURL == '*') // v1.0.44.07: Provide an option to override flags_for_open_url.
938 {
939 flags_for_open_url = ATOU(++aURL);
940 LPTSTR cp;
941 if (cp = StrChrAny(aURL, _T(" \t"))) // Find first space or tab.
942 aURL = omit_leading_whitespace(cp);
943 }
944
945 // Open the internet session. v1.0.45.03: Provide a non-NULL user-agent because some servers reject
946 // requests that lack a user-agent. Furthermore, it's more professional to have one, in which case it
947 // should probably be kept as simple and unchanging as possible. Using something like the script's name
948 // as the user agent (even if documented) seems like a bad idea because it might contain personal/sensitive info.
949 HINTERNET hInet = InternetOpen(_T("AutoHotkey"), INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY, NULL, NULL, 0);
950 if (!hInet)
951 return Throw();
952
953 // Open the required URL
954 HINTERNET hFile = InternetOpenUrl(hInet, aURL, NULL, 0, flags_for_open_url, 0);
955 if (!hFile)
956 {
957 InternetCloseHandle(hInet);
958 return Throw();
959 }
960
961 // Open our output file
962 FILE *fptr = _tfopen(aFilespec, _T("wb")); // Open in binary write/destroy mode
963 if (!fptr)
964 {
965 InternetCloseHandle(hFile);
966 InternetCloseHandle(hInet);
967 return Throw();
968 }
969
970 BYTE bufData[1024 * 1]; // v1.0.44.11: Reduced from 8 KB to alleviate GUI window lag during Download. Testing shows this reduction doesn't affect performance on high-speed downloads (in fact, downloads are slightly faster; I tested two sites, one at 184 KB/s and the other at 380 KB/s). It might affect slow downloads, but that seems less likely so wasn't tested.
971 INTERNET_BUFFERSA buffers = {0};
972 buffers.dwStructSize = sizeof(INTERNET_BUFFERSA);
973 buffers.lpvBuffer = bufData;
974 buffers.dwBufferLength = sizeof(bufData);
975
976 LONG_OPERATION_INIT
977
978 // Read the file. I don't think synchronous transfers typically generate the pseudo-error
979 // ERROR_IO_PENDING, so that is not checked here. That's probably just for async transfers.
980 // IRF_NO_WAIT is used to avoid requiring the call to block until the buffer is full. By
981 // having it return the moment there is any data in the buffer, the program is made more

Callers

nothing calls this directly

Calls 2

omit_leading_whitespaceFunction · 0.85
StrChrAnyFunction · 0.85

Tested by

no test coverage detected