Skip to content

Commit 4d9940d

Browse files
author
hack-tramp
committed
added file download
1 parent 1472f7f commit 4d9940d

26 files changed

+110
-27
lines changed

.vs/slnx.sqlite

220 KB
Binary file not shown.

.vs/wnetwrap/v16/.suo

5 KB
Binary file not shown.
99.4 MB
Binary file not shown.
99.4 MB
Binary file not shown.

Debug/wnetwrap.exe

-156 KB
Binary file not shown.

example.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,39 @@ int main()
1010

1111
req my_request; //GET method used by default
1212

13-
my_request.set_header( "Referer" , "my.referer.com" );
14-
my_request.set_header("RefErEr", "my.bla.com");
13+
1514
my_request.set_header( "Connection" , "keep-alive" );
15+
my_request.set_header("Referer", "bla.com");
16+
17+
resp my_response = HttpsRequest("https://github.com/whoshuu/cpr/archive/refs/tags/1.6.0.zip", my_request, "dl");
1618

17-
resp my_response = HttpsRequest("https://www.example.com", my_request);
18-
cout << my_response.text << endl;
1919
cout << "security protocol: " + my_response.protocol << endl;
20-
20+
cout << endl << "sent headers map:" << endl;
21+
for (auto elem : my_request.headers)
22+
{
23+
cout << elem.first + " : " + elem.second + "\r\n";
24+
}
25+
cout << endl << "sent headers map:" << endl;
26+
for (auto elem : my_response.sent_headers)
27+
{
28+
cout << elem.first + " : " + elem.second + "\r\n";
29+
}
30+
cout << endl << "recd headers map:" << endl;
31+
for (auto elem : my_response.received_headers)
32+
{
33+
cout << elem.first + " : " + elem.second + "\r\n";
34+
}
35+
cout << my_response.text << endl;
2136
//cout << "recd header: " + my_response.get_header("Referer") << std::endl;
2237
//cout << "sent header: " + my_response.get_header("ReferEr", "sent") << std::endl;
23-
38+
/*
2439
my_request.method = "POST";
2540
my_request.set_header("Content-Type:", "application/json");
2641
my_request.postdata = "{\"b\":\"a\"}";
2742
my_response = HttpsRequest("https://postman-echo.com/post", my_request);
2843
2944
cout << my_response.raw << endl;
30-
/*
45+
3146
cout << "sent headers map:" << endl;
3247
for (auto elem : my_response.sent_headers)
3348
{

wnetwrap.cpp

Lines changed: 69 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#pragma comment(lib, "Wininet.lib")
44

5-
wrap::resp wrap::HttpsRequest(std::string site, wrap::req request) {
5+
wrap::resp wrap::HttpsRequest(std::string site, wrap::req request, std::string dload = "") {
66
wrap::resp output;
77
HINTERNET hInternet = InternetOpenA(request.ua.c_str(), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
88

@@ -15,7 +15,7 @@ wrap::resp wrap::HttpsRequest(std::string site, wrap::req request) {
1515
{
1616
//do some very basic URI parsing to separate host (for InternetConnect) from path (used in HttpOpenRequest)
1717
//also to see what protocol is specified
18-
std::string host, path, protocol = "";
18+
std::string host, path, protocol, urlfile = "";
1919

2020
//protocol and host
2121
host = site;
@@ -24,16 +24,19 @@ wrap::resp wrap::HttpsRequest(std::string site, wrap::req request) {
2424
host = host.substr(host.find("://") + 3);
2525
//cout << "clipped host: " + host << endl;
2626
}
27-
else { //otherwise assume it's http (not https)
28-
protocol = "http";
27+
else { //otherwise assume it's https
28+
protocol = "https";
2929
}
30-
//default is http
30+
//default is https
3131
DWORD service = INTERNET_SERVICE_HTTP;
32-
INTERNET_PORT port = INTERNET_DEFAULT_HTTP_PORT;
32+
INTERNET_PORT port = INTERNET_DEFAULT_HTTPS_PORT;
3333

3434
if (protocol == "https") {
3535
port = INTERNET_DEFAULT_HTTPS_PORT;
3636
}
37+
else if (protocol == "http") {
38+
port = INTERNET_DEFAULT_HTTP_PORT;
39+
}
3740
else if (protocol == "ftp") {
3841
port = INTERNET_DEFAULT_FTP_PORT;
3942
service = INTERNET_SERVICE_FTP;
@@ -52,21 +55,44 @@ wrap::resp wrap::HttpsRequest(std::string site, wrap::req request) {
5255
path = host.substr(host.find("?"));
5356
host = host.substr(0, host.find("?"));
5457
}
58+
//if theres a file extension, store filename
59+
if (path.find_last_of("/")!= std::string::npos) {
60+
if (path.substr(path.find_last_of("/") + 1).find(".") != std::string::npos) {
61+
urlfile = path.substr(path.find_last_of("/") + 1);
62+
}
63+
}
64+
else {
65+
if (path.find_last_of("?") != std::string::npos) {
66+
if (path.substr(path.find_last_of("?") + 1).find(".") != std::string::npos) {
67+
urlfile = path.substr(path.find_last_of("?") + 1);
68+
}
69+
}
70+
}
71+
}
72+
else { //if its the last char, trim because wininet doesnt like a trailing / or ?
73+
host = host.substr(0, host.size() - 1);
74+
//std::cout << "trimmed last char of host" << std::endl;
5575
}
5676
}
57-
58-
//cout << "host: "+host << endl << "path: "+path << endl << "protocol: "+protocol << endl;
77+
//entering dl means the file is saved as its original filename
78+
if (dload == "dl") {
79+
dload = urlfile;
80+
}
81+
std::cout << port << std::endl;
82+
std::cout << service << std::endl;
83+
std::cout << "host: "+host << std::endl << "path: "+path << std::endl << "protocol: "+protocol << std::endl << "file: " + urlfile << std::endl << "input url: " + site << std::endl;
5984
HINTERNET hConnect = InternetConnectA(hInternet, host.c_str(), port, NULL, NULL, service, 0, NULL);
6085

86+
6187
if (hConnect == NULL)
6288
{
6389
output.err = "InternetConnect failed: " + GetLastError();
6490
return output;
6591
}
6692
else
6793
{
68-
std::string param = ""; //eg "Dir1/Dir2/Login.php?page=1" - might need leading /
69-
HINTERNET hRequest = HttpOpenRequestA(hConnect, request.method.c_str(), path.c_str(), NULL, NULL, request.AcceptedTypes, INTERNET_FLAG_SECURE, 0);
94+
//eg path "Dir1/Dir2/Login.php?page=1" - might need leading /
95+
HINTERNET hRequest = HttpOpenRequestA(hConnect, request.method.c_str(), path.c_str(), "HTTP/1.1", NULL, request.AcceptedTypes, INTERNET_FLAG_SECURE, 0);
7096

7197
if (hRequest == NULL)
7298
{
@@ -79,12 +105,16 @@ wrap::resp wrap::HttpsRequest(std::string site, wrap::req request) {
79105
//see remarks here: https://docs.microsoft.com/en-us/windows/win32/api/wininet/nf-wininet-httpsendrequestw
80106

81107
//assemble headers from map
82-
std::string final_headers;
108+
std::string final_headers = "";
83109
for (auto elem : request.headers)
84110
{
85-
final_headers += elem.first + " : " + elem.second + "\r\n";
111+
final_headers += elem.first + ":" + elem.second + "\r\n";
112+
}
113+
if (final_headers != "") {
114+
final_headers += "\r\n\r\n"; //null terminated string
115+
86116
}
87-
final_headers += "\r\n\r\n"; //null terminated std::string
117+
88118

89119
std::string pdata = "";
90120
DWORD pdlength = 0;
@@ -94,8 +124,14 @@ wrap::resp wrap::HttpsRequest(std::string site, wrap::req request) {
94124
if (pdata.size() < 4294967295) { //to get rid of size_t to dword c4267
95125
pdlength = (DWORD)pdata.size();
96126
}
127+
97128
}
98-
if (!HttpSendRequestA(hRequest, final_headers.c_str(), -1L, (LPVOID)pdata.c_str(), pdlength))
129+
//std::cout << "final headers c string:" << std::endl;
130+
//std::cout << final_headers.c_str() << std::endl;
131+
//std::cout << final_headers.size() << std::endl;
132+
//BOOL addhdr = HttpAddRequestHeadersA(hRequest, final_headers.c_str(), -1L, HTTP_ADDREQ_FLAG_ADD);
133+
BOOL sendr = HttpSendRequestA(hRequest, final_headers.c_str(), -1L, (LPVOID) pdata.c_str(), pdlength);
134+
if (!sendr)
99135
{
100136
output.err = "HttpSendRequest failed with error code " + GetLastError();
101137
return output;
@@ -105,14 +141,29 @@ wrap::resp wrap::HttpsRequest(std::string site, wrap::req request) {
105141
std::string strResponse;
106142
const int nBuffSize = 1024;
107143
char buff[nBuffSize];
108-
144+
FILE* pfile = nullptr;
145+
if (dload != "") {
146+
pfile = fopen(dload.c_str(), "wb");
147+
}
148+
109149
BOOL bKeepReading = true;
110150
DWORD dwBytesRead = -1;
111151

112152
while (bKeepReading && dwBytesRead != 0)
113153
{
114154
bKeepReading = InternetReadFile(hRequest, buff, nBuffSize, &dwBytesRead);
115-
strResponse.append(buff, dwBytesRead);
155+
156+
if (pfile != nullptr) {
157+
fwrite(buff, sizeof(char), dwBytesRead, pfile);
158+
}
159+
else {
160+
strResponse.append(buff, dwBytesRead);
161+
}
162+
163+
}
164+
if (pfile!=nullptr) {
165+
fflush(pfile);
166+
fclose(pfile);
116167
}
117168

118169

@@ -173,6 +224,7 @@ wrap::resp wrap::HttpsRequest(std::string site, wrap::req request) {
173224
else {
174225

175226
if (sent_headers != NULL) {
227+
std::cout << std::endl << sent_headers << std::endl;
176228
std::string s(sent_headers, d);
177229
//break headers std::string into map
178230
std::string delimiter = "\n";
@@ -188,7 +240,7 @@ wrap::resp wrap::HttpsRequest(std::string site, wrap::req request) {
188240
//cout << "adding: " + first +" " + second << endl;
189241
//NOTE: SENT HEADER KEYS ARE RETURNED WITH A TRAILING SPACE BY WININET - RECD HEADER KEYS ARENT
190242
//FOR THIS REASON FOR SENT HEADERS WE DO SUBSTR 0, token.find(":") - 1
191-
output.sent_headers.insert(std::pair<std::string, std::string>(token.substr(0, token.find(":") - 1), token.substr(token.find(":") + 1)));
243+
output.sent_headers.insert(std::pair<std::string, std::string>(token.substr(0, token.find(":") ), token.substr(token.find(":") + 1)));
192244
}
193245
s.erase(0, pos + delimiter.length());
194246
}

wnetwrap.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
#include <windows.h>
55
#include <WinInet.h>
66
#include <Winineti.h>
7-
7+
#include <stdio.h>
8+
#include <iostream>
89
#include <map>
910
#include <regex>
1011

@@ -35,7 +36,7 @@ namespace wrap {
3536
}
3637
};
3738
std::string method = "GET";
38-
LPCSTR AcceptedTypes[100] = { "*/*","*/*",NULL }; //must be null terminated std::string array
39+
LPCSTR AcceptedTypes[100] = { "*/*" , NULL }; //must be null terminated std::string array
3940
std::string postdata;
4041
std::string ua = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:86.0) Gecko/20100101 Firefox/86.0";
4142
};
@@ -69,6 +70,7 @@ namespace wrap {
6970
output = received_headers[key];
7071
}
7172
}
73+
7274
return output;
7375
}
7476
catch (...) {
@@ -88,7 +90,7 @@ namespace wrap {
8890
std::string err;
8991
};
9092

91-
resp HttpsRequest(std::string site, req request);
93+
resp HttpsRequest(std::string site, req request,std::string dload);
9294
std::string text_from_html(std::string html);
9395

9496

wnetwrap.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<ConfigurationType>Application</ConfigurationType>
3939
<UseDebugLibraries>true</UseDebugLibraries>
4040
<PlatformToolset>v142</PlatformToolset>
41+
<CharacterSet>Unicode</CharacterSet>
4142
</PropertyGroup>
4243
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
4344
<ConfigurationType>Application</ConfigurationType>

x64/Debug/example.obj

889 KB
Binary file not shown.

0 commit comments

Comments
 (0)