Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. #include <curl/curl.h>
  2. //#include <Ultralight/Ultralight.h>
  3. #include <iostream>
  4. #include <fstream>
  5. //#include </home/samuel/Downloads/ultralight-sdk-1.0-linux/include/>
  6. #include <string>
  7. using namespace std;
  8.  
  9. //callback function to write data
  10. size_t writeFunction(void *contents, size_t size, size_t nmemb, string *raw_html) {
  11. size_t newLength = size*nmemb;
  12. try {
  13. raw_html->append((char*)contents, newLength);
  14. }
  15. catch(std::bad_alloc &e) {
  16. //handle memory problem
  17. return 0;
  18. }
  19. return newLength;
  20. }
  21.  
  22. int main() {
  23. bool browse;
  24. string enteredURL, finalURL, raw_html, keepBrowsingChoice;
  25. ofstream writeHistoryFile;
  26.  
  27. //create curl instance
  28. CURL *curl = curl_easy_init();
  29. CURLcode res;
  30.  
  31. cout << "Welcome to BruhBrowser!" << endl;
  32. do {
  33. cout << "Enter URL: https://";
  34. cin >> enteredURL;
  35. finalURL = "http://" + enteredURL;
  36.  
  37. /*^^^ THIS DOESN't WORK: CURLOPT_URL CAN'T TAKE A STRING?
  38. DOCS: https://curl.haxx.se/libcurl/c/CURLOPT_URL.html
  39. I want to use user input for the url, but to test the program just replace
  40. finalURL with an actual url
  41. */
  42.  
  43. //cURL CONFIGURATION
  44. curl_easy_setopt (curl, CURLOPT_VERBOSE, 1L); //verbose output
  45. curl_easy_setopt(curl, CURLOPT_URL, finalURL); //normal dns request
  46. curl_easy_setopt(curl, CURLOPT_DOH_URL,
  47. "https://doh.appliedprivacy.net/query"); //DNS over https
  48. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); //follow redirects
  49. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeFunction); //callback for writing data
  50. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &raw_html); //write data to string raw_html
  51.  
  52. //cURL to site
  53. res = curl_easy_perform(curl);
  54.  
  55. cout << raw_html;
  56.  
  57. writeHistoryFile.open("history.txt", ios_base::app);
  58. writeHistoryFile << finalURL << "\n";
  59. writeHistoryFile.close();
  60.  
  61. cout << "Navigate to another website? [y/n]: ";
  62. cin >> keepBrowsingChoice;
  63. if ((keepBrowsingChoice.compare("y")) == 0) {
  64. browse = true;
  65. }
  66. else {
  67. browse = false;
  68. }
  69.  
  70. } while(browse);
  71.  
  72.  
  73. cout << "Clear History? [y/n]: ";
  74. string userHistoryChoice;
  75. cin >> userHistoryChoice;
  76. if ((userHistoryChoice.compare("y")) == 0) {
  77. writeHistoryFile.open("history.txt");
  78. writeHistoryFile << "";
  79. writeHistoryFile.close();
  80. cout << "History Cleared!" << endl;
  81. }
  82.  
  83. curl_easy_cleanup(curl);
  84. return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement