Get size of a remote file

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2009
Posts: 9
Reputation: osmano807 is an unknown quantity at this point 
Solved Threads: 0
osmano807 osmano807 is offline Offline
Newbie Poster

Get size of a remote file

 
0
  #1
Jun 8th, 2009
Hello, I need to get remote file size through the url to a function. The problem is that I'm not getting! Trying with the functions of libcURL, but I am not getting. Is there any way to get the size of a remote file by url?
Last edited by osmano807; Jun 8th, 2009 at 2:01 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: Get size of a remote file

 
0
  #2
Jun 8th, 2009
Too bad you're not using PHP. http://ca2.php.net/filesize

But it seems like it might not be possible: http://bytes.com/groups/php/595984-f...ing-http-pages

On the other hand, wget determines file sizes. Apparently it requires HTTP 1.1 (not 1.0) to do this.

I really don't know. You'd have to do some research.
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 9
Reputation: osmano807 is an unknown quantity at this point 
Solved Threads: 0
osmano807 osmano807 is offline Offline
Newbie Poster

Re: Get size of a remote file

 
0
  #3
Jun 9th, 2009
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <curl/curl.h>
  4. #include <boost/regex.hpp>
  5. using namespace boost;
  6. using namespace std;
  7.  
  8. //
  9. // For this demonstration, the contents of the web page are put
  10. // into this global variable.
  11. //
  12. string contents;
  13. //
  14. // This is the callback function that is called by
  15. // curl_easy_perform(curl)
  16. //
  17. size_t handle_data(void *ptr, size_t size, size_t nmemb, void *stream)
  18. {
  19. const boost::regex content_length_regex("Content-Length: [0-9]{1,}"); // Regex do video do youtube...
  20. const boost::regex content_length_remove_regex("Content-Length: ");
  21. int numbytes = size*nmemb;
  22. // The data is not null-terminated, so get the last character, and replace
  23. // it with '\0'.
  24. char lastchar = *((char *) ptr + numbytes - 1);
  25. //string last_char = char_to_string(lastchar);
  26. // {
  27. string nothing = "";
  28. // contents = regex_replace(last_char, content_length_remove_regex, nothing);
  29. *((char *) ptr + numbytes - 1) = '\0';
  30. string last_char = ((char *)ptr);
  31. //cout << last_char << endl;
  32. //cout << endl;
  33. //cout << regex_search(last_char,content_length_regex) << endl;
  34. //cout << endl;
  35. //cout << "antes" << endl;
  36. //cout << ((char*)ptr) << endl;
  37. if (regex_search(last_char,content_length_regex) == 1) // Se for 1, foi retornado sim para o match
  38. {
  39. //cout << "depois" << endl;
  40. //cout << ((char*)ptr) << endl;
  41. //cout << regex_replace(last_char, content_length_remove_regex, nothing) << endl;
  42. contents = regex_replace(last_char, content_length_remove_regex, nothing);
  43. //cout << contents << endl;
  44. }
  45. else
  46. //{
  47. //contents = "-1";
  48. //}
  49. //contents.append((char *)ptr);
  50. //contents.append(1,lastchar);
  51. //*((char *) ptr + numbytes - 1) = lastchar; // Might not be necessary.
  52. // }
  53.  
  54.  
  55. return size*nmemb;
  56. }
  57.  
  58. long remote_filesize(string url)
  59. {
  60. //string url = "http://www.youtube.com/get_video?video_id=3455GI_uGs4&t=vjVQa1PpcFNWk-V5Ndo6HE9KWZGtRxrct9PNWGe5Nhs=&el=detailpage&ps=&fmt=34";
  61. //string url = "http://www.google.com";
  62. CURL *curl;
  63. CURLcode res;
  64. double length = 0.0;
  65. //double length2 = 0.0;
  66. curl = curl_easy_init();
  67. if(curl) {
  68. //static const char *headerfilename = "head.out";
  69. //FILE *headerfile;
  70. //headerfile = fopen(headerfilename,"w");
  71. curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  72. //curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
  73. //curl_easy_setopt(curl, CURLOPT_RANGE, "0-4"); // só por garantia, não faz o donwload inteiro não...
  74. curl_easy_setopt(curl, CURLOPT_NOBODY, 1); // acho que é aqui que bloqueia o donwload...
  75. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
  76. curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, handle_data);
  77. curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 500);
  78. res = curl_easy_perform(curl);
  79.  
  80. //curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &length);
  81. //cout << length << endl;
  82. //const boost::regex youtube_video_id("^video_id="); // Regex do video do youtube...
  83.  
  84. //cout << contents << endl;
  85. //unlink(headerfilename);
  86. }
  87. curl_easy_cleanup(curl);
  88. //return length;
  89. if (contents == "")
  90. {
  91. return(-1);
  92. }
  93. else
  94. {
  95. return atol(contents.c_str());
  96. }
  97. }
  98.  
  99. int main()
  100. {
  101. // Content-Length: 222
  102. string url = "http://www.google.com.br";
  103. remote_filesize(url);
  104. cout << "contents " << contents << endl;
  105. return 0;
  106. }
g++ example.cpp -lcurl -lboost_regex
Sorry for the comments in Portuguese and snippets of code. I had to use the libboost_regex because I still do not know another way to do a search for regular expression.
Last edited by osmano807; Jun 9th, 2009 at 2:32 pm.
http://www.thundercache.org
ThunderCache, a proxy that cache's youtube and more!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC