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 }
0 commit comments