├── .appveyor.yml ├── requests ├── utils.h ├── http.h ├── requests.vcxproj.filters ├── http.cpp ├── requests.vcxproj └── utils.cpp ├── README.md ├── requests.example ├── requests.example.vcxproj.filters ├── sample.cc └── requests.example.vcxproj ├── LICENSE ├── requests.sln ├── .gitattributes └── .gitignore /.appveyor.yml: -------------------------------------------------------------------------------- 1 | image: Visual Studio 2017 2 | platform: 3 | - x86 4 | - x64 5 | configuration: 6 | - Debug 7 | - Release 8 | 9 | 10 | artifacts: 11 | - path: requests\http.h 12 | name: requests::http header 13 | - path: requests\utils.h 14 | name: requests::utils header 15 | - path: Release\requests.lib 16 | name: requests_x86_release.lib 17 | - path: Debug\requests.lib 18 | name: requests_x86_debug.lib 19 | - path: x64\Release\requests.lib 20 | name: requests_x64_debug.lib 21 | - path: x64\Debug\requests.lib 22 | name: requests_x64_debug.lib 23 | -------------------------------------------------------------------------------- /requests/utils.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include "http.h" 6 | 7 | namespace req_utils 8 | { 9 | //converters 10 | LPCWSTR to_lpcwstr(const char* text); 11 | 12 | //generators 13 | std::string populate_uri(const std::string& content); 14 | std::string generate_post(requests::post_data pdata_map); 15 | std::map parse_headers(std::map h_map, DWORD verb); 16 | std::map parse_res_headers(std::string raw_headers); 17 | 18 | 19 | //text parsing-related 20 | std::vector split(const std::string& str, char separator); 21 | std::string return_between(const std::string& s, const std::string& start_delim, const std::string& stop_delim); 22 | bool starts_with(const std::string& str, const std::string& who); 23 | int string_index(const std::string& str, const std::string& who); 24 | 25 | //simple-crypt 26 | std::string encrypt_str(std::string vulstr); 27 | std::string decrypt_str(std::string vulstr); 28 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Requests 2 | 3 | C++ Windows-Based HTTP Library for facility and easiness 4 | 5 | ### Code Building 6 | 7 | 8 | #### Join us: 9 | 10 | Join CodeBuilding 11 | 12 | https://codebuilding.org 13 | 14 | ## Examples 15 | 16 | 17 | GET Request Example 18 | ```cpp 19 | #include // Let's include requests default header 20 | 21 | requests::Request* request = resquests::get("httpbin.org/get"); 22 | 23 | printf( "%s", request->status_code.c_str() ); 24 | 25 | // Output: 26 | 27 | 200 OK 28 | ``` 29 | 30 | #### For the following php script: 31 | ```php 32 | 36 | ``` 37 | POST Request example 38 | ```cpp 39 | #include 40 | 41 | requests::post_data data = { 42 | {"parameter1", "value1"}, 43 | {"parameter2", "value2"} 44 | }; 45 | 46 | requests::Request* request = requests::post( "localhost/example.php" , data ); 47 | 48 | printf( "%s", request->text.c_str() ); 49 | 50 | // Output: 51 | 52 | value1value2 53 | ``` 54 | 55 | test 56 | ## License 57 | 58 | -------------------------------------------------------------------------------- /requests.example/requests.example.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Arquivos de Origem 20 | 21 | 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Code Building 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /requests/http.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #pragma comment(lib, "winhttp.lib") 8 | 9 | namespace requests 10 | { 11 | typedef std::map post_data; 12 | typedef std::map req_headers; 13 | typedef std::map res_headers; 14 | 15 | const req_headers DEFAULT_HEADERS = {{"User-Agent", "Requests 2.0"}}; 16 | const req_headers DEFAULT_POST_HEADERS = { 17 | {"Content-Type", "application/x-www-form-urlencoded"}, {"User-Agent", "Requests 2.0"} 18 | }; 19 | 20 | struct request 21 | { 22 | std::string status_code; 23 | int content_length{}; 24 | std::string text; 25 | res_headers headers; 26 | }; 27 | 28 | 29 | request* execute( 30 | DWORD verb, 31 | const std::string& user_agent, 32 | const std::string& hostname, 33 | const std::string& raw_uri, 34 | const std::map& opt_headers, 35 | const std::string& pdata 36 | ); 37 | 38 | request* get(std::string url, req_headers h_data = DEFAULT_HEADERS, u_short port = 80); 39 | request* post(std::string url, const post_data& pdata, req_headers h_data = DEFAULT_POST_HEADERS, 40 | u_short port = 80); 41 | } -------------------------------------------------------------------------------- /requests.example/sample.cc: -------------------------------------------------------------------------------- 1 | #include "../requests/http.h" 2 | #include 3 | #include "../requests/utils.h" 4 | 5 | int main() 6 | { 7 | const requests::req_headers headers = { 8 | {"Custom-Header", "Custom Value :D"} 9 | }; 10 | 11 | const requests::post_data pdata = { 12 | {"foo", "bar"}, 13 | {"fizz", "liz"} 14 | }; 15 | 16 | const auto it = requests::get("https://www.google.com/",headers); 17 | const auto it2 = requests::post("httpbin.org/post", pdata, headers); 18 | const auto encrypted = req_utils::encrypt_str("httpbin.org/get"); 19 | const auto g_res = requests::get("httpbin.org/get"); 20 | const auto p_res = requests::post("httpbin.org/post", pdata); 21 | const auto nf_res = requests::get("httpbin.org/404"); 22 | const auto g_explicithttp_res = requests::get("http://httpbin.org/get"); 23 | const auto decrypturl_res = requests::get(req_utils::decrypt_str(encrypted)); 24 | 25 | assert(g_res->status_code == "200 OK"); 26 | assert(p_res->status_code == "200 OK"); 27 | assert(nf_res->status_code == "404 NOT FOUND"); 28 | assert(g_explicithttp_res->status_code == "200 OK"); 29 | assert(decrypturl_res->status_code == "200 OK"); 30 | assert(it->status_code == "200 OK"); 31 | assert(it2->status_code == "200 OK"); 32 | printf("That's all :D"); 33 | auto dispose = getchar(); 34 | } 35 | -------------------------------------------------------------------------------- /requests/requests.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | {22fc71d3-7feb-4e40-ba6f-5505e6e57509} 18 | 19 | 20 | 21 | 22 | requests 23 | 24 | 25 | requests 26 | 27 | 28 | 29 | 30 | requests 31 | 32 | 33 | requests 34 | 35 | 36 | -------------------------------------------------------------------------------- /requests.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.28010.2026 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "requests", "requests\requests.vcxproj", "{F8F68401-2068-4F78-9586-11A22299D4C1}" 7 | EndProject 8 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "requests.example", "requests.example\requests.example.vcxproj", "{6B741029-E951-4DCC-BD51-553EB6F8D14A}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|x64 = Debug|x64 13 | Debug|x86 = Debug|x86 14 | Release|x64 = Release|x64 15 | Release|x86 = Release|x86 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {F8F68401-2068-4F78-9586-11A22299D4C1}.Debug|x64.ActiveCfg = Debug|x64 19 | {F8F68401-2068-4F78-9586-11A22299D4C1}.Debug|x64.Build.0 = Debug|x64 20 | {F8F68401-2068-4F78-9586-11A22299D4C1}.Debug|x86.ActiveCfg = Debug|Win32 21 | {F8F68401-2068-4F78-9586-11A22299D4C1}.Debug|x86.Build.0 = Debug|Win32 22 | {F8F68401-2068-4F78-9586-11A22299D4C1}.Release|x64.ActiveCfg = Release|x64 23 | {F8F68401-2068-4F78-9586-11A22299D4C1}.Release|x64.Build.0 = Release|x64 24 | {F8F68401-2068-4F78-9586-11A22299D4C1}.Release|x86.ActiveCfg = Release|Win32 25 | {F8F68401-2068-4F78-9586-11A22299D4C1}.Release|x86.Build.0 = Release|Win32 26 | {6B741029-E951-4DCC-BD51-553EB6F8D14A}.Debug|x64.ActiveCfg = Debug|x64 27 | {6B741029-E951-4DCC-BD51-553EB6F8D14A}.Debug|x64.Build.0 = Debug|x64 28 | {6B741029-E951-4DCC-BD51-553EB6F8D14A}.Debug|x86.ActiveCfg = Debug|Win32 29 | {6B741029-E951-4DCC-BD51-553EB6F8D14A}.Debug|x86.Build.0 = Debug|Win32 30 | {6B741029-E951-4DCC-BD51-553EB6F8D14A}.Release|x64.ActiveCfg = Release|x64 31 | {6B741029-E951-4DCC-BD51-553EB6F8D14A}.Release|x64.Build.0 = Release|x64 32 | {6B741029-E951-4DCC-BD51-553EB6F8D14A}.Release|x86.ActiveCfg = Release|Win32 33 | {6B741029-E951-4DCC-BD51-553EB6F8D14A}.Release|x86.Build.0 = Release|Win32 34 | EndGlobalSection 35 | GlobalSection(SolutionProperties) = preSolution 36 | HideSolutionNode = FALSE 37 | EndGlobalSection 38 | GlobalSection(ExtensibilityGlobals) = postSolution 39 | SolutionGuid = {A09978EF-28BE-41FC-A8DD-65C1D4EDC9B5} 40 | EndGlobalSection 41 | EndGlobal 42 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | project.fragment.lock.json 46 | artifacts/ 47 | 48 | *_i.c 49 | *_p.c 50 | *_i.h 51 | *.ilk 52 | *.meta 53 | *.obj 54 | *.pch 55 | *.pdb 56 | *.pgc 57 | *.pgd 58 | *.rsp 59 | *.sbr 60 | *.tlb 61 | *.tli 62 | *.tlh 63 | *.tmp 64 | *.tmp_proj 65 | *.log 66 | *.vspscc 67 | *.vssscc 68 | .builds 69 | *.pidb 70 | *.svclog 71 | *.scc 72 | 73 | # Chutzpah Test files 74 | _Chutzpah* 75 | 76 | # Visual C++ cache files 77 | ipch/ 78 | *.aps 79 | *.ncb 80 | *.opendb 81 | *.opensdf 82 | *.sdf 83 | *.cachefile 84 | *.VC.db 85 | *.VC.VC.opendb 86 | 87 | # Visual Studio profiler 88 | *.psess 89 | *.vsp 90 | *.vspx 91 | *.sap 92 | 93 | # TFS 2012 Local Workspace 94 | $tf/ 95 | 96 | # Guidance Automation Toolkit 97 | *.gpState 98 | 99 | # ReSharper is a .NET coding add-in 100 | _ReSharper*/ 101 | *.[Rr]e[Ss]harper 102 | *.DotSettings.user 103 | 104 | # JustCode is a .NET coding add-in 105 | .JustCode 106 | 107 | # TeamCity is a build add-in 108 | _TeamCity* 109 | 110 | # DotCover is a Code Coverage Tool 111 | *.dotCover 112 | 113 | # NCrunch 114 | _NCrunch_* 115 | .*crunch*.local.xml 116 | nCrunchTemp_* 117 | 118 | # MightyMoose 119 | *.mm.* 120 | AutoTest.Net/ 121 | 122 | # Web workbench (sass) 123 | .sass-cache/ 124 | 125 | # Installshield output folder 126 | [Ee]xpress/ 127 | 128 | # DocProject is a documentation generator add-in 129 | DocProject/buildhelp/ 130 | DocProject/Help/*.HxT 131 | DocProject/Help/*.HxC 132 | DocProject/Help/*.hhc 133 | DocProject/Help/*.hhk 134 | DocProject/Help/*.hhp 135 | DocProject/Help/Html2 136 | DocProject/Help/html 137 | 138 | # Click-Once directory 139 | publish/ 140 | 141 | # Publish Web Output 142 | *.[Pp]ublish.xml 143 | *.azurePubxml 144 | # TODO: Comment the next line if you want to checkin your web deploy settings 145 | # but database connection strings (with potential passwords) will be unencrypted 146 | #*.pubxml 147 | *.publishproj 148 | 149 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 150 | # checkin your Azure Web App publish settings, but sensitive information contained 151 | # in these scripts will be unencrypted 152 | PublishScripts/ 153 | 154 | # NuGet Packages 155 | *.nupkg 156 | # The packages folder can be ignored because of Package Restore 157 | **/packages/* 158 | # except build/, which is used as an MSBuild target. 159 | !**/packages/build/ 160 | # Uncomment if necessary however generally it will be regenerated when needed 161 | #!**/packages/repositories.config 162 | # NuGet v3's project.json files produces more ignoreable files 163 | *.nuget.props 164 | *.nuget.targets 165 | 166 | # Microsoft Azure Build Output 167 | csx/ 168 | *.build.csdef 169 | 170 | # Microsoft Azure Emulator 171 | ecf/ 172 | rcf/ 173 | 174 | # Windows Store app package directories and files 175 | AppPackages/ 176 | BundleArtifacts/ 177 | Package.StoreAssociation.xml 178 | _pkginfo.txt 179 | 180 | # Visual Studio cache files 181 | # files ending in .cache can be ignored 182 | *.[Cc]ache 183 | # but keep track of directories ending in .cache 184 | !*.[Cc]ache/ 185 | 186 | # Others 187 | ClientBin/ 188 | ~$* 189 | *~ 190 | *.dbmdl 191 | *.dbproj.schemaview 192 | *.jfm 193 | *.pfx 194 | *.publishsettings 195 | node_modules/ 196 | orleans.codegen.cs 197 | 198 | # Since there are multiple workflows, uncomment next line to ignore bower_components 199 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 200 | #bower_components/ 201 | 202 | # RIA/Silverlight projects 203 | Generated_Code/ 204 | 205 | # Backup & report files from converting an old project file 206 | # to a newer Visual Studio version. Backup files are not needed, 207 | # because we have git ;-) 208 | _UpgradeReport_Files/ 209 | Backup*/ 210 | UpgradeLog*.XML 211 | UpgradeLog*.htm 212 | 213 | # SQL Server files 214 | *.mdf 215 | *.ldf 216 | 217 | # Business Intelligence projects 218 | *.rdl.data 219 | *.bim.layout 220 | *.bim_*.settings 221 | 222 | # Microsoft Fakes 223 | FakesAssemblies/ 224 | 225 | # GhostDoc plugin setting file 226 | *.GhostDoc.xml 227 | 228 | # Node.js Tools for Visual Studio 229 | .ntvs_analysis.dat 230 | 231 | # Visual Studio 6 build log 232 | *.plg 233 | 234 | # Visual Studio 6 workspace options file 235 | *.opt 236 | 237 | # Visual Studio LightSwitch build output 238 | **/*.HTMLClient/GeneratedArtifacts 239 | **/*.DesktopClient/GeneratedArtifacts 240 | **/*.DesktopClient/ModelManifest.xml 241 | **/*.Server/GeneratedArtifacts 242 | **/*.Server/ModelManifest.xml 243 | _Pvt_Extensions 244 | 245 | # Paket dependency manager 246 | .paket/paket.exe 247 | paket-files/ 248 | 249 | # FAKE - F# Make 250 | .fake/ 251 | 252 | # JetBrains Rider 253 | .idea/ 254 | *.sln.iml 255 | 256 | # CodeRush 257 | .cr/ 258 | 259 | # Python Tools for Visual Studio (PTVS) 260 | __pycache__/ 261 | *.pyc -------------------------------------------------------------------------------- /requests/http.cpp: -------------------------------------------------------------------------------- 1 | // This file is part of the "requests" project, https://github.com/Code-Building/requests 2 | // (c) 2019 Code Building https://codebuilding.org 3 | // 4 | // Licensed under the MIT License (the "License"); you may not use this 5 | // file except in compliance with the License. You may obtain a copy of 6 | // the License at: http://opensource.org/licenses/MIT 7 | 8 | 9 | #include "http.h" 10 | #include "utils.h" 11 | #include 12 | #include 13 | 14 | namespace requests 15 | { 16 | request* execute(const DWORD verb, 17 | const std::string& user_agent, 18 | const std::string& hostname, 19 | const std::string& raw_uri, 20 | const std::map& opt_headers, 21 | const std::string& pdata) 22 | { 23 | auto this_req = new request; 24 | std::wstring response_header; 25 | std::vector response_body; 26 | std::string plain_text; 27 | DWORD header_size = 0; 28 | DWORD dw_downloaded; 29 | 30 | 31 | const auto pd = const_cast(pdata.c_str()); 32 | const DWORD pdata_len = strlen(pd); 33 | 34 | 35 | DWORD dw_size = 0; 36 | LPVOID lp_out_buffer = nullptr; 37 | BOOL b_results = false; 38 | HINTERNET h_session = nullptr, h_connect = nullptr, h_request = nullptr; 39 | 40 | h_session = WinHttpOpen(req_utils::to_lpcwstr(user_agent.c_str()), WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, nullptr, 41 | nullptr, 0); 42 | 43 | if (h_session) 44 | h_connect = WinHttpConnect(h_session, req_utils::to_lpcwstr(hostname.c_str()), INTERNET_DEFAULT_HTTP_PORT, 45 | 0); 46 | 47 | if (h_connect) 48 | h_request = WinHttpOpenRequest(h_connect, verb == 1 ? L"GET" : L"POST", 49 | req_utils::to_lpcwstr(raw_uri.c_str()), nullptr, nullptr, nullptr, 0); 50 | 51 | for (auto& it : opt_headers) 52 | { 53 | auto header = it.first + ": " + it.second; 54 | WinHttpAddRequestHeaders(h_request, 55 | req_utils::to_lpcwstr(header.c_str()), 56 | -1L, 57 | WINHTTP_ADDREQ_FLAG_ADD); 58 | } 59 | 60 | 61 | if (h_request) 62 | b_results = WinHttpSendRequest(h_request, nullptr, 0, verb == 2 ? pd : nullptr, verb == 2 ? pdata_len : 0, 63 | verb == 2 ? pdata_len : 0, 0); 64 | 65 | if (b_results) 66 | b_results = WinHttpReceiveResponse(h_request, nullptr); 67 | 68 | if (b_results) 69 | { 70 | b_results = WinHttpQueryHeaders(h_request, WINHTTP_QUERY_RAW_HEADERS_CRLF, nullptr, nullptr, &header_size, 71 | nullptr); 72 | if ((!b_results) && (GetLastError() == ERROR_INSUFFICIENT_BUFFER)) 73 | { 74 | response_header.resize(header_size / sizeof(wchar_t)); 75 | if (response_header.empty()) 76 | b_results = TRUE; 77 | else 78 | { 79 | b_results = WinHttpQueryHeaders(h_request, WINHTTP_QUERY_RAW_HEADERS_CRLF, nullptr, 80 | &response_header[0], &header_size, nullptr); 81 | if (!b_results) header_size = 0; 82 | response_header.resize(header_size / sizeof(wchar_t)); 83 | } 84 | } 85 | } 86 | if (b_results) 87 | { 88 | do 89 | { 90 | dw_size = 0; 91 | b_results = WinHttpQueryDataAvailable(h_request, &dw_size); 92 | if (!b_results) 93 | throw std::exception("Uncaught error on WinHttpQueryDataAvailable()"); 94 | 95 | if (dw_size == 0) 96 | break; 97 | 98 | do 99 | { 100 | const DWORD dw_offset = response_body.size(); 101 | response_body.resize(dw_offset + dw_size); 102 | 103 | b_results = WinHttpReadData(h_request, &response_body[dw_offset], dw_size, &dw_downloaded); 104 | if (!b_results) 105 | throw std::exception("Uncaught exception on WinHttpReadData()"); 106 | 107 | response_body.resize(dw_offset + dw_downloaded); 108 | 109 | if (dw_downloaded == 0) 110 | break; 111 | 112 | dw_size -= dw_downloaded; 113 | } 114 | while (dw_size > 0); 115 | } 116 | while (true); 117 | } 118 | 119 | for (auto& x : response_body) 120 | plain_text += x; 121 | 122 | this_req->text = plain_text; 123 | this_req->headers = req_utils::parse_res_headers(std::string(response_header.begin(), response_header.end())); 124 | const auto len_header_value = this_req->headers["Content-Length"].c_str(); 125 | this_req->status_code = this_req->headers["Status"]; 126 | this_req->content_length = atoi(len_header_value); 127 | return this_req; 128 | } 129 | 130 | request* get(std::string url, req_headers h_data, const u_short port) 131 | { 132 | if (req_utils::starts_with(url, "http://")) 133 | url = url.substr(7); 134 | else if (req_utils::starts_with(url, "https://")) 135 | url = url.substr(8); 136 | 137 | if (url[url.length()] != '/' && req_utils::string_index(url, "/") == -1) 138 | url += "/"; 139 | 140 | const auto opt_headers = req_utils::parse_headers(h_data, 1); 141 | const auto ua = h_data["User-Agent"]; 142 | 143 | const auto requested_uri_raw = req_utils::populate_uri(url); 144 | const auto request_host = req_utils::split(url, '/')[0]; 145 | 146 | 147 | return execute(1, ua, request_host, requested_uri_raw, opt_headers, ""); 148 | } 149 | 150 | request* post(std::string url, const post_data& pdata, req_headers h_data, const u_short port) 151 | { 152 | if (req_utils::starts_with(url, "http://")) 153 | url = url.substr(7); 154 | else if (req_utils::starts_with(url, "https://")) 155 | url = url.substr(8); 156 | 157 | if (url[url.length()] != '/' && req_utils::string_index(url, "/") == -1) 158 | url += "/"; 159 | 160 | const auto opt_headers = req_utils::parse_headers(h_data, 2); 161 | const auto ua = h_data["User-Agent"]; 162 | 163 | const auto requested_uri_raw = req_utils::populate_uri(url); 164 | const auto request_host = req_utils::split(url, '/')[0]; 165 | const auto post_data_str = req_utils::generate_post(pdata); 166 | 167 | 168 | return execute(2, ua, request_host, requested_uri_raw, opt_headers, post_data_str); 169 | } 170 | } 171 | -------------------------------------------------------------------------------- /requests/requests.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 15.0 23 | {F8F68401-2068-4F78-9586-11A22299D4C1} 24 | requests 25 | 10.0 26 | 27 | 28 | 29 | StaticLibrary 30 | true 31 | v142 32 | MultiByte 33 | 34 | 35 | StaticLibrary 36 | false 37 | v142 38 | true 39 | MultiByte 40 | 41 | 42 | StaticLibrary 43 | true 44 | v142 45 | MultiByte 46 | 47 | 48 | StaticLibrary 49 | false 50 | v142 51 | true 52 | MultiByte 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | Level3 76 | Disabled 77 | true 78 | true 79 | 80 | 81 | 82 | 83 | Level3 84 | Disabled 85 | true 86 | true 87 | 88 | 89 | 90 | 91 | Level3 92 | MaxSpeed 93 | true 94 | true 95 | true 96 | true 97 | 98 | 99 | true 100 | true 101 | 102 | 103 | 104 | 105 | Level3 106 | MaxSpeed 107 | true 108 | true 109 | true 110 | true 111 | 112 | 113 | true 114 | true 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | -------------------------------------------------------------------------------- /requests.example/requests.example.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 15.0 23 | {6B741029-E951-4DCC-BD51-553EB6F8D14A} 24 | requestsexample 25 | 10.0 26 | 27 | 28 | 29 | Application 30 | true 31 | v142 32 | MultiByte 33 | 34 | 35 | Application 36 | false 37 | v142 38 | true 39 | MultiByte 40 | 41 | 42 | Application 43 | true 44 | v142 45 | MultiByte 46 | 47 | 48 | Application 49 | false 50 | v142 51 | true 52 | MultiByte 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | Level3 76 | Disabled 77 | true 78 | true 79 | 80 | 81 | 82 | 83 | Level3 84 | Disabled 85 | true 86 | true 87 | 88 | 89 | 90 | 91 | Level3 92 | MaxSpeed 93 | true 94 | true 95 | true 96 | true 97 | 98 | 99 | true 100 | true 101 | 102 | 103 | 104 | 105 | Level3 106 | MaxSpeed 107 | true 108 | true 109 | true 110 | true 111 | 112 | 113 | true 114 | true 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | {f8f68401-2068-4f78-9586-11a22299d4c1} 123 | 124 | 125 | 126 | 127 | 128 | -------------------------------------------------------------------------------- /requests/utils.cpp: -------------------------------------------------------------------------------- 1 | // This file is part of the "requests" project, https://github.com/Code-Building/requests 2 | // (c) 2019 Code Building https://codebuilding.org 3 | // 4 | // Licensed under the MIT License (the "License"); you may not use this 5 | // file except in compliance with the License. You may obtain a copy of 6 | // the License at: http://opensource.org/licenses/MIT 7 | 8 | 9 | #include "utils.h" 10 | 11 | 12 | /*This namespace is responsible of utilities on processing HTTP responses and their data*/ 13 | namespace req_utils 14 | { 15 | // Split function will receive a string and a char. The character will be responsible by 16 | // the separator between strings of the given string. 17 | // the first step is to loop a function while a given character exists in the given string 18 | // then, in that function, if the char was found, it assigns a index value to it. 19 | // with that value, we can use substring to get the value between the last found index and 20 | // actual found index - last found index. 21 | // (last found index will be actual found index+1 at the iteration end) 22 | // after that loop, we also need to return the value between last found index and actual 23 | // found index - last found index to ensure that strings like 24 | // a:b with the separator ':' return a & b. 25 | // we can visualize that with: 26 | // std::string x = "Hi:How:Are:You"; const char separator = ':'; 27 | // first loop iteration, found separator at index 2, the substr will be substr(0, (2-0)); 28 | // resulting in "Hi" pushed onto output vector 29 | // second loop iteration, found separator at index 6, the substr will be substr(3, (6-3)); 30 | // resulting in "How" pushed onto output vector 31 | // the loop will remains until all separators got consumed. 32 | // in this case, at "Are" since "You" don't have ':' at the end. 33 | // as we need to cover "You" also, we do a last substring bewteen last ':' index and string end. 34 | // and push it onto vector. 35 | std::vector split(const std::string& str, const char separator) 36 | { 37 | std::vector output; 38 | std::string::size_type prev_pos = 0, pos = 0; 39 | 40 | while ((pos = str.find(separator, pos)) != std::string::npos) 41 | { 42 | auto substring(str.substr(prev_pos, pos - prev_pos)); 43 | output.push_back(substring); 44 | prev_pos = ++pos; 45 | } 46 | 47 | output.push_back(str.substr(prev_pos, pos - prev_pos)); 48 | return output; 49 | } 50 | 51 | // Parse Headers function will recieve a map of two std::string's and then check 52 | // if crucial headers is included in that map, if not, it will include automatically 53 | // this is solved by looping through all given map keys and values and checking each 54 | // of them. 55 | std::map parse_headers(std::map h_map, const DWORD verb) 56 | { 57 | if (!h_map.count("Content-Type") && verb == 2) 58 | { 59 | h_map["Content-Type"] = "application/x-www-form-urlencoded"; 60 | } 61 | if (!h_map.count("User-Agent")) 62 | { 63 | h_map["User-Agent"] = "Requests 2.0"; 64 | } 65 | return h_map; 66 | } 67 | 68 | // Parse Response Headers function will recieve a raw std::string of response headers 69 | // a valid http response should start with 70 | // HTTP/N.N STATUS CODE 71 | // so let's deal with it. 72 | // we simply do a 8 characters -to- the first CLRF index substring to retrieve 73 | // the response code. 74 | // then, we can just apply the split function logic to get all headers between CLRF 75 | std::map parse_res_headers(std::string raw_headers) 76 | { 77 | std::map formatted; 78 | const auto x = string_index(raw_headers, "\r\n"); 79 | auto http_res = raw_headers.substr(8, x - 8); 80 | http_res.erase(0, 1); 81 | formatted["Status"] = http_res; 82 | raw_headers = raw_headers.substr(x); 83 | 84 | while (string_index(raw_headers, "\r\n") != -1) 85 | { 86 | raw_headers = raw_headers.substr(string_index(raw_headers, "\r\n") + 2); 87 | const auto delimeter = string_index(raw_headers, ":"); 88 | auto value = return_between(raw_headers, ":", "\r\n"); 89 | value.erase(0, 1); 90 | const auto header = raw_headers.substr(0, delimeter); 91 | formatted[header] = value; 92 | } 93 | 94 | return formatted; 95 | } 96 | 97 | // To LPCWSTR function will take a const char* and 98 | // then translate it into const wchar_t* aka LPCWSTR 99 | LPCWSTR to_lpcwstr(const char* text) 100 | { 101 | const auto size = strlen(text) + 1; 102 | auto* wtext = new wchar_t[size]; 103 | size_t out_size; 104 | mbstowcs_s(&out_size, wtext, size, text, size - 1); 105 | return wtext; 106 | } 107 | 108 | // Populate URI function, will take a string, split it 109 | // with '/' delimeter, skip the first index(hostname) 110 | // and then re-create the URL requested path 111 | std::string populate_uri(const std::string& content) 112 | { 113 | std::string uri; 114 | auto array = split(content, '/'); 115 | for (auto i = 1; array.size() > i; ++i) 116 | uri += "/" + array[i]; 117 | return uri; 118 | } 119 | 120 | // Generate Post function will take a std::map 121 | // then create a post payload string with each of it values. 122 | std::string generate_post(requests::post_data pdata_map) 123 | { 124 | std::string generated; 125 | for (auto& it : pdata_map) 126 | generated += it.first + "=" + it.second + "&"; 127 | generated.pop_back(); 128 | return generated; 129 | } 130 | 131 | // Return Between function will take a string and two delimeters to 132 | // search for which string is between those two delimeters. 133 | // first, it will search for the index of first delimeter then 134 | // store the length of that first delimeter, make a new variable 135 | // with substring on the end of first delimeter length and just 136 | // find the pos of second delimeter to use substring with 137 | // the substring of frist delimeter -to- last delimeter position. 138 | std::string return_between(const std::string& s, const std::string& start_delim, const std::string& stop_delim) 139 | { 140 | const auto first_delim_pos = s.find(start_delim); 141 | const auto end_pos_of_first_delim = first_delim_pos + start_delim.length(); 142 | auto fixed_next_search = s.substr(end_pos_of_first_delim); 143 | const auto last_delim_pos = fixed_next_search.find(stop_delim); 144 | 145 | return fixed_next_search.substr(0, 146 | last_delim_pos); 147 | } 148 | 149 | // Starts with function checks if a N string is at position 0 150 | // of string X. 151 | bool starts_with(const std::string& str, const std::string& who) 152 | { 153 | return str.rfind(who, 0) == 0; 154 | } 155 | 156 | // String Index function will return a N string position at 157 | // a string X. 158 | int string_index(const std::string& str, const std::string& who) 159 | { 160 | return str.find(who); 161 | } 162 | 163 | std::string encrypt_str(std::string vulstr) 164 | { 165 | auto v_str = vulstr; 166 | uint8_t doinc = 0; 167 | for (size_t i = 0; i < vulstr.length(); ++i) 168 | { 169 | if (doinc == 5) doinc = 0; 170 | *reinterpret_cast(&v_str[i]) = *reinterpret_cast(&vulstr[i]) + doinc; 171 | ++doinc; 172 | } 173 | return v_str; 174 | } 175 | 176 | std::string decrypt_str(std::string vulstr) 177 | { 178 | auto v_str = vulstr; 179 | uint8_t doinc = 0; 180 | for (size_t i = 0; i < vulstr.length(); ++i) 181 | { 182 | if (doinc == 5) doinc = 0; 183 | *reinterpret_cast(&v_str[i]) = *reinterpret_cast(&vulstr[i]) - doinc; 184 | ++doinc; 185 | } 186 | return v_str; 187 | } 188 | } 189 | --------------------------------------------------------------------------------