Skip to content

Commit f84a83e

Browse files
Update url.cpp
1 parent ac50a9e commit f84a83e

1 file changed

Lines changed: 25 additions & 20 deletions

File tree

core/url.cpp

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,36 @@
11
#include <iostream>
22
#include <string>
3+
#include <curl/curl.h>
4+
5+
size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* output) {
6+
output->append((char*)contents, size * nmemb);
7+
return size * nmemb;
8+
}
39

410
int main() {
511
std::string url;
6-
712
std::cout << "Enter a URL: ";
8-
std::getline(std::cin, url); // user can type any URL
9-
10-
// Remove protocol (http:// or https://)
11-
std::string protocol = "https://";
12-
size_t start = url.find(protocol);
13-
if (start != std::string::npos) {
14-
start += protocol.length();
15-
} else {
16-
protocol = "http://";
17-
start = url.find(protocol);
18-
if (start != std::string::npos)
19-
start += protocol.length();
20-
else
21-
start = 0; // no protocol found
13+
std::getline(std::cin, url);
14+
15+
CURL* curl = curl_easy_init();
16+
if (!curl) {
17+
std::cerr << "Failed to initialize curl\n";
18+
return 1;
2219
}
2320

24-
// Find the end of the domain
25-
size_t end = url.find('/', start);
21+
std::string response;
2622

27-
// Extract the forum/domain name
28-
std::string forumName = url.substr(start, end - start);
23+
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
24+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
25+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
26+
27+
CURLcode res = curl_easy_perform(curl);
28+
curl_easy_cleanup(curl);
29+
30+
if (res != CURLE_OK) {
31+
std::cerr << "Request failed\n";
32+
return 1;
33+
}
2934

30-
std::cout << "Forum name: " << forumName << "\n";
35+
std::cout << "Page content:\n" << response << "\n";
3136
}

0 commit comments

Comments
 (0)