Skip to content

Commit b6d5d2f

Browse files
committed
Retrieve web data without saving to file
1 parent 0ab1fc7 commit b6d5d2f

2 files changed

Lines changed: 48 additions & 30 deletions

File tree

include/utility/utility.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,13 @@ struct skif_get_web_uri_t {
278278
wchar_t wszLocalPath[MAX_PATH + 2] = { };
279279
LPCWSTR method = L"GET";
280280
bool https = false;
281-
std::string body;
282-
std::wstring header;
281+
std::string body = { };
282+
std::wstring header = { };
283+
std::wstring user_agent = L"Special K - Asset Crawler";
283284
};
284285

285-
DWORD WINAPI SKIF_Util_GetWebUri (skif_get_web_uri_t* get);
286-
DWORD SKIF_Util_GetWebResource (std::wstring url, std::wstring_view destination, std::wstring method = L"GET", std::wstring header = L"", std::string body = "");
286+
DWORD WINAPI SKIF_Util_GetWebUri (skif_get_web_uri_t* get, std::string* response_body = nullptr);
287+
DWORD SKIF_Util_GetWebResource (std::wstring url, std::wstring_view file_path, std::wstring method = L"GET", std::wstring header = L"", std::string body = "", std::wstring user_agent = L"", std::string* response_body = nullptr);
287288
skif_get_web_uri_t SKIF_Util_CrackWebUrl (const std::wstring url);
288289

289290

src/utility/utility.cpp

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3678,7 +3678,7 @@ SKIF_Util_GetHotKeyStateSVCTemp (void)
36783678

36793679
DWORD
36803680
WINAPI
3681-
SKIF_Util_GetWebUri (skif_get_web_uri_t* get)
3681+
SKIF_Util_GetWebUri (skif_get_web_uri_t* get, std::string* response_body)
36823682
{
36833683
static SKIF_RegistrySettings& _registry = SKIF_RegistrySettings::GetInstance ( );
36843684

@@ -3715,15 +3715,16 @@ SKIF_Util_GetWebUri (skif_get_web_uri_t* get)
37153715
return 0;
37163716
};
37173717

3718-
PLOG_VERBOSE << "Method : " << std::wstring(get->method);
3719-
PLOG_VERBOSE << "Target : " << ((get->https) ? "https://" : "http://") << get->wszHostName << get->wszHostPath;
3720-
PLOG_VERBOSE_IF(get->wszExtraInfo[0] != L'\0') << "Fragment: " << get->wszExtraInfo;
3721-
PLOG_VERBOSE_IF(! get->header.empty()) << "Header : " << get->header;
3722-
PLOG_VERBOSE_IF(! get->body.empty()) << " Body : " << get->body;
3718+
PLOG_VERBOSE << "Method: " << std::wstring(get->method);
3719+
PLOG_VERBOSE << "Target: " << ((get->https) ? "https://" : "http://") << get->wszHostName << get->wszHostPath;
3720+
PLOG_VERBOSE_IF( get->wszExtraInfo[0] != L'\0') << " Query: " << get->wszExtraInfo;
3721+
PLOG_VERBOSE << " U-A: " << get->user_agent;
3722+
PLOG_VERBOSE_IF(! get->header.empty()) << "Header: " << get->header;
3723+
PLOG_VERBOSE_IF(! get->body.empty()) << " Body: " << get->body;
37233724

37243725
hInetRoot =
37253726
InternetOpen (
3726-
L"Special K - Asset Crawler",
3727+
get->user_agent.c_str(),
37273728
INTERNET_OPEN_TYPE_DIRECT,
37283729
nullptr, nullptr,
37293730
0x00 );
@@ -3842,19 +3843,35 @@ SKIF_Util_GetWebUri (skif_get_web_uri_t* get)
38423843
break;
38433844
}
38443845

3845-
FILE *fOut = nullptr;
3846-
3847-
_wfopen_s (&fOut, get->wszLocalPath, L"wb+" );
3848-
3849-
if (fOut != nullptr)
3846+
if (response_body != nullptr)
38503847
{
3851-
fwrite (concat_buffer.data (), concat_buffer.size (), 1, fOut);
3852-
fflush (fOut);
3853-
fclose (fOut);
3848+
response_body->clear();
3849+
response_body->append (concat_buffer.data(), concat_buffer.size());
3850+
}
38543851

3852+
if (get->wszLocalPath[0] == '\0')
3853+
{
38553854
CLEANUP (true);
38563855
return 1;
38573856
}
3857+
3858+
// Write to file...
3859+
else
3860+
{
3861+
FILE *fOut = nullptr;
3862+
3863+
_wfopen_s (&fOut, get->wszLocalPath, L"wb+" );
3864+
3865+
if (fOut != nullptr)
3866+
{
3867+
fwrite (concat_buffer.data (), concat_buffer.size (), 1, fOut);
3868+
fflush (fOut);
3869+
fclose (fOut);
3870+
3871+
CLEANUP (true);
3872+
return 1;
3873+
}
3874+
}
38583875
}
38593876

38603877
else { // dwStatusCode != 200
@@ -3866,20 +3883,20 @@ SKIF_Util_GetWebUri (skif_get_web_uri_t* get)
38663883
}
38673884

38683885
DWORD
3869-
SKIF_Util_GetWebResource (std::wstring url, std::wstring_view destination, std::wstring method, std::wstring header, std::string body)
3886+
SKIF_Util_GetWebResource (std::wstring url, std::wstring_view file_path, std::wstring method, std::wstring header, std::string body, std::wstring user_agent, std::string* response_body)
38703887
{
38713888
auto* get =
38723889
new skif_get_web_uri_t { };
38733890

38743891
URL_COMPONENTSW urlcomps = { };
38753892

3876-
urlcomps.dwStructSize = sizeof (URL_COMPONENTSW);
3893+
urlcomps.dwStructSize = sizeof (URL_COMPONENTSW);
38773894

3878-
urlcomps.lpszHostName = get->wszHostName;
3879-
urlcomps.dwHostNameLength = INTERNET_MAX_HOST_NAME_LENGTH;
3895+
urlcomps.lpszHostName = get->wszHostName;
3896+
urlcomps.dwHostNameLength = INTERNET_MAX_HOST_NAME_LENGTH;
38803897

3881-
urlcomps.lpszUrlPath = get->wszHostPath;
3882-
urlcomps.dwUrlPathLength = INTERNET_MAX_PATH_LENGTH;
3898+
urlcomps.lpszUrlPath = get->wszHostPath;
3899+
urlcomps.dwUrlPathLength = INTERNET_MAX_PATH_LENGTH;
38833900

38843901
urlcomps.lpszExtraInfo = get->wszExtraInfo;
38853902
urlcomps.dwExtraInfoLength = INTERNET_MAX_PATH_LENGTH;
@@ -3893,15 +3910,15 @@ SKIF_Util_GetWebResource (std::wstring url, std::wstring_view destination, std::
38933910
if (! body.empty())
38943911
get->body = body;
38953912

3913+
if (! user_agent.empty())
3914+
get->user_agent = user_agent;
3915+
38963916
if (InternetCrackUrl (url.c_str(), static_cast <DWORD> (url.length ()), 0x00, &urlcomps))
38973917
{
3898-
wcsncpy ( get->wszLocalPath,
3899-
destination.data (),
3900-
MAX_PATH );
3901-
3918+
wcsncpy (get->wszLocalPath, file_path.data (), MAX_PATH);
39023919
get->https = (urlcomps.nScheme == INTERNET_SCHEME_HTTPS);
39033920

3904-
return SKIF_Util_GetWebUri (get);
3921+
return SKIF_Util_GetWebUri (get, response_body);
39053922
}
39063923

39073924
else {

0 commit comments

Comments
 (0)