Combined string isn't working for libCurl in C++ - c++

Basically, my program/code I'm using now is returning a message from a PHP script. The issue I'm having is when I give it a combined string for the URL, it doesn't return anything. But when I enter it manually, it returns the correct information. Here is the code I am using below:
std::string Login(std::string uname, std::string pass)
{
CURL* curl;
CURLcode res;
std::string readBuffer;
std::string path = "localhost/files/login.php?username=" + uname + "&password=" + pass;
std::cout << uname << " " << pass;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, "localhost/files/login.php?username=123&password=123");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
return readBuffer;
}
return "Failed";
}
This way works because I enter the URL manually, but when I do this it doesn't return anything:
std::string path = "localhost/files/login.php?username=" + uname + "&password=" + pass;
curl_easy_setopt(curl, CURLOPT_URL, path);
I'm uncertain whether I'm using the wrong variable or something else. I'm new to using PHP and libCurl and anything web related.

CURLOPT_URL expects a C-style null-terminated char* string pointer as input, not a std::string. You can use the std::string::c_str() method to get a compatible const char* pointer from the std::string:
curl_easy_setopt(curl, CURLOPT_URL, path.c_str());

Related

C++ libcurl multithreading

I'm trying to build a small multithreading program which takes a subdomains and test them if they are alive on http or https, I've problem that's my program doesn't produce the correct output each time I get different output and also freeze and doesn't continue execution. I followed http://www.cplusplus.com/reference/thread/thread/thread/ when implementing the multithreading.
int main(int argc, char const *argv[] )
{
if (argc < 2){
cout << "Usage httplive <path to subdomains>" << endl;
}
ifstream http(argv[1]);
string line;
vector <std::thread> thread_pool;
while (getline(http, line)){
thread_pool.push_back(thread(httpTest,line, true));
thread_pool.push_back(thread(httpTest, line, false));
}
for (auto& t : thread_pool){
t.join();
}
return 0;
}
void httpTest(string line, bool Flag){
CURL *curl = curl_easy_init();
CURLcode res;
if (curl) {
line = Flag ? "https://" + line : "http://"+ line;
curl_easy_setopt(curl, CURLOPT_URL, const_cast<char*>(line.c_str()));
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
// curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1L);
res = curl_easy_perform(curl);
// cout << res << endl;
if (res == CURLE_OK ) cout << line << endl;
}
curl_easy_cleanup(curl);
}

c++ : libcurl request in file format error

i'm a beginner in c++. I want to send request to a API, for this i use libcurl, and stock the response on a string and copy the string in a file. it is my test file :
#include <iostream>
#include <string>
#include <curl/curl.h>
#include <fstream>
int MyCurlObject::curlWriter(char *data, size_t size, size_t nmemb, std::string *buffer) {
int result = 0;
if (buffer != NULL) {
buffer->append(data, size * nmemb);
result = size * nmemb;
}
return result;
}
int main (){
std::string url = "https://www.google.com/";
std::string content;
curl = curl_easy_init();
if(!curl)
{
std::cerr << "impossible d'initialiser curl." << std::endl;
}
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &MyCurlObject::curlWriter);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
const CURLcode rc = curl_easy_perform(curl);
if( rc != CURLE_OK ) {
std::cout << "Error from cURL: " << curl_easy_strerror(rc) << std::endl;
}
std::ofstream file(fileName);
if(!file){
std::cerr << "can't open this file : " << fileName << std::endl;
}
file << content;
file.close();
return 0;
}
My files contain all of my string, but line in file not contain end of line symbol ( i display all of symbol with notepade++ and i just see CR in end of line ) and if i make this :
std::ifstream file(name);
if(file)
{
std::string crash;
int nbrOfLine = 0;
while(getline(file, crash))
{
std::cout << crash;
nbrOfLine++;
}
}
return 1 but my file contain 1500 lines.
thank you in advance

libcurl how to request HTTPS in cpp

CURL *curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (curl) {
string html;
curl_easy_setopt(curl, CURLOPT_URL, "https://www.google.com");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &html);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
curl_easy_setopt(curl, CURLOPT_CAINFO, "D:\\certification\\DigiCertHighAssuranceEVRootCA.crt");
res = curl_easy_perform(curl);
cout << "Error : " << curl_easy_strerror(res) << endl;
cout << html;
The Error message is Unsupported protocol
The http site is okay but The https site doesn't work
What should I add in curl_easy_setopt()

Way to get word frequency from string?

Hey guys I have the following issue. I've been using C++ to scrape website using to find 5 most frequent words in outputHTML which is string. Currently I have following code. Any hint would be awesome.
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &htmlOutput);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
std::cout << htmlOutput << std::endl;
}
Here's some hints for more awesomeness:
std::istringstream awsome_stream(web_text);
std::string word;
std::map<std::string, unsigned int> kewl_words;
while (awsome_stream >> word)
{
kewl_words[word]++;
}
std::cout << "Occurances of 'div': " << kewl_words["div"] << "\n";

C++: How to check if a directory exists on an FTP server using libcurl

I'm at my wit's end on this one. I need to check if a directory exists on a remote FTP server. Here's what I'm thinking:
//ls - lists the names of the files in the remote directory
string query = "ls /public_html/somefolder/";
//prepare to send
headers = curl_slist_append(headers, query.c_str());
curl_easy_setopt(curl, CURLOPT_QUOTE, headers);
//send query to ftp server
res = curl_easy_perform(curl);
//check result
if(res == CURLE_OK) {
cout << "FOLDER EXISTS";
} else {
cout << "FOLDER DOESN'T EXIST";
}
When I check what the res variable contains, it outputs:
CURLE_QUOTE_ERROR (21).
Any ideas on how to do this correctly? I've searched Google profusely.
Don't try to use CURLOPT_QUOTE for this. A LIST is a data transfer, so it is handled as part of the CURLOPT_URL.
CURL *curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, "ftp://ftp#ftp.gnu.org/pub/");
curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
CURLcode res = curl_easy_perform(curl);
if(res == CURLE_OK) std::cout << "FOLDER EXISTS\n";
else std::cout << "FOLDER DOESN'T EXIST\n";