sebbu

MinGW Downloader

Jun 24th, 2011
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.01 KB | None | 0 0
  1. #include <string>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <iostream>
  5. #include <boost/asio.hpp>
  6. #include <boost/regex.hpp>
  7. #include "libutil/util_global.hpp"
  8.  
  9. using boost::asio::ip::tcp;
  10.  
  11. int list_dir(const std::string& host, const std::string& path);
  12. //int list_dir(const char* host, const char* path);
  13.  
  14. void show_entry_dir(const std::string& nhost, const std::string& npath, const std::string& path, const std::string& name, const std::string& date) {
  15.     assert( nhost.size() > 0);
  16.     assert( npath.size() > 0);
  17.     assert( path.size() > 0 );
  18.     assert( date.size() <= 20 );
  19.     //fprintf(stderr, "[INFO] dir  path '%s/'\n", path.c_str());
  20.     //fprintf(stderr, "[INFO] dir  name '%s/'\n", name.c_str());
  21.     printf("[DIR ] (%s) %s%s%s/\n", date.c_str(), nhost.c_str(), npath.c_str(), name.c_str() );
  22.     list_dir( nhost, npath+path+"/" );
  23.     //fprintf(stderr, "[INFO] dir  date '%s'\n", date.c_str());
  24. }
  25.  
  26. void show_entry_file(const std::string& nhost, const std::string& npath, const std::string& path, const std::string& name, const std::string& date) {
  27.     assert( nhost.size() > 0);
  28.     assert( npath.size() > 0);
  29.     assert( path.size() > 0 );
  30.     assert( date.size() <= 20 );
  31.     //fprintf(stderr, "[INFO] file path '%s'\n", path.c_str());
  32.     //fprintf(stderr, "[INFO] file name '%s'\n", name.c_str());
  33.     printf("[FILE] (%s) %s%s%s\n", date.c_str(), nhost.c_str(), npath.c_str(), name.c_str() );
  34.     //fprintf(stderr, "[INFO] file date '%s'\n", date.c_str());
  35. }
  36.  
  37. int main() {
  38.     const char* host="ovh.dl.sourceforge.net";
  39.     //const char* host="heanet.dl.sourceforge.net";
  40.     //const char* host="freefr.dl.sourceforge.net";//no date :p
  41.     const char* path="/project/mingw/";
  42.    
  43.     int res=list_dir( string(host), string(path) );
  44.    
  45.     cout << "done" << endl;
  46.     usleep(1000);//workaround
  47.     return res;
  48. }
  49.  
  50. /*int list_dir(const std::string& host, const std::string& path) {
  51.     return list_dir( host.c_str(), path.c_str() );
  52. }//*/
  53.  
  54. //int list_dir(const char* host, const char* path) {
  55. int list_dir(const std::string& host, const std::string& path) {
  56.     try {
  57.         boost::asio::io_service io_service;
  58.  
  59.         // Get a list of endpoints corresponding to the server name.
  60.         tcp::resolver resolver(io_service);
  61.         tcp::resolver::query query(host, "http");
  62.         tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
  63.         tcp::resolver::iterator endpoint;
  64.  
  65.         // Try each endpoint until we successfully establish a connection.
  66.         tcp::socket socket(io_service);
  67.         boost::system::error_code error = boost::asio::error::host_not_found;
  68.         while (error && endpoint_iterator != endpoint)
  69.         {
  70.             socket.close();
  71.             socket.connect(*endpoint_iterator++, error);
  72.         }
  73.         if (error)
  74.         throw boost::system::system_error(error);
  75.  
  76.         // Form the request. We specify the "Connection: close" header so that the
  77.         // server will close the socket after transmitting the response. This will
  78.         // allow us to treat all data up until the EOF as the content.
  79.         boost::asio::streambuf request;
  80.         std::ostream request_stream(&request);
  81.         request_stream << "GET " << path << " HTTP/1.0\r\n";
  82.         request_stream << "Host: " << host << "\r\n";
  83.         request_stream << "Accept: */*\r\n";
  84.         request_stream << "Connection: close\r\n\r\n";
  85.  
  86.         // Send the request.
  87.         boost::asio::write(socket, request);
  88.  
  89.         // Read the response status line. The response streambuf will automatically
  90.         // grow to accommodate the entire line. The growth may be limited by passing
  91.         // a maximum size to the streambuf constructor.
  92.         boost::asio::streambuf response(512);
  93.         boost::asio::read_until(socket, response, "\r\n");
  94.  
  95.         // Check that response is OK.
  96.         std::istream response_stream(&response);
  97.         std::string http_version;
  98.         response_stream >> http_version;
  99.         unsigned int status_code;
  100.         response_stream >> status_code;
  101.         std::string status_message;
  102.         std::getline(response_stream, status_message);
  103.         if (!response_stream || http_version.substr(0, 5) != "HTTP/")
  104.         {
  105.             std::cout << "Invalid response\n";
  106.             return 1;
  107.         }
  108.         if (status_code != 200)
  109.         {
  110.             std::cout << "Response returned with status code " << status_code << "\n";
  111.             return 1;
  112.         }
  113.  
  114.         // Read the response headers, which are terminated by a blank line.
  115.         boost::asio::read_until(socket, response, "\r\n\r\n");
  116.  
  117.         // Process the response headers.
  118.         std::string header;
  119.         while (std::getline(response_stream, header) && header != "\r") {
  120.             //std::cout << header << "\n";
  121.         }
  122.         //std::cout << "\n";
  123.  
  124.         // Write whatever content we already have to output.
  125.         /*if (response.size() > 0) {
  126.             //std::cout << &response;
  127.             std::istream is(&response);
  128.             string line;
  129.             while (std::getline(is, line) && line!="\r" && line!="\r\n" && line!="\n") {
  130.                 fprintf(stderr, "[DEBUG] len = '%d' str='%s'\n", line.size(), line.c_str() );
  131.                 //std::cout << line << endl;
  132.             }
  133.         }//*/
  134.        
  135.         boost::regex expression1("<td><a href=\"([^/\"]+)/\">\\s*([^/<\"]+)/\\s*</a></td><td(?:[^>]+)>\\s*([0-9]{2}-[A-Za-z]{3}-[0-9]{2,4} [0-9]{2}:[0-9]{2}(?:-[0-9]{2})?)\\s*</td>", boost::regex_constants::perl);
  136.         boost::regex expression1b("<td><a href=\"([^/\"]+)\">\\s*([^/<\"]+)\\s*</a></td><td(?:[^>]+)>\\s*([0-9]{2}-[A-Za-z]{3}-[0-9]{2,4} [0-9]{2}:[0-9]{2}(?:-[0-9]{2})?)\\s*</td>", boost::regex_constants::perl);
  137.         boost::regex expression2("<a href=\"([^/\"]+)/\">\\s*([^/\"]+)/\\s*</a>\\s+([0-9]{2}-[A-Za-z]{3}-[0-9]{2,4} [0-9]{2}:[0-9]{2}(?:-[0-9]{2})?)\\s+");
  138.         boost::regex expression2b("<a href=\"([^/\"]+)\">\\s*([^/\"]+)\\s*</a>\\s+([0-9]{2}-[A-Za-z]{3}-[0-9]{2,4} [0-9]{2}:[0-9]{2}(?:-[0-9]{2})?)\\s+");
  139.         boost::regex expression3("<li><a href=\"([^/\"]+)/\">\\s*([^/\"]+)/\\s*</a>\\s*([0-9]{2}-[A-Za-z]{3}-[0-9]{2,4} [0-9]{2}:[0-9]{2}(?:-[0-9]{2})?)?\\s*</li>");
  140.         boost::regex expression3b("<li><a href=\"([^/\"]+)\">\\s*([^/\"]+)\\s*</a>\\s*([0-9]{2}-[A-Za-z]{3}-[0-9]{2,4} [0-9]{2}:[0-9]{2}(?:-[0-9]{2})?)?\\s*</li>");
  141.        
  142.         string line;
  143.         // Read until EOF, writing data to output as we go.
  144.         std::string::const_iterator start, end;
  145.         //while (boost::asio::read(socket, response, boost::asio::transfer_at_least(1), error)) {
  146.         do { //already have a partial response from when we read the header
  147.             //printf("[DEBUG] loop 0\n");
  148.             std::istream is(&response);
  149.             if (std::getline(is, line)) {//don't do it more than once, read_until take cares of the buffer, that prevent from having a partial line
  150.                 //printf("[DEBUG] loop 1\n");
  151.                 start = line.begin();
  152.                 end = line.end();
  153.                 /*if(*end==0) {
  154.                     //workaround
  155.                     end=(line.begin()+line.size()-1);
  156.                     fprintf(stderr, "[DEBUG] workaround for *end==0 : size='%d', end='%d'\n", line.size(), *end);
  157.                 }//*/
  158.                 boost::match_results<std::string::const_iterator> what;
  159.                 //apache table extended view
  160.                 while(regex_search(start, end, what, expression1, boost::match_extra))
  161.                 {
  162.                     //fprintf(stderr, "---\n");
  163.                     //printf("[DEBUG] loop 2\n");
  164.                     /*fprintf(stderr, "[INFO] path '%s'\n", what[1].str().c_str());
  165.                     fprintf(stderr, "[INFO] directory '%s'\n", what[2].str().c_str());
  166.                     fprintf(stderr, "[INFO] date '%s'\n", what[3].str().c_str());//*/
  167.                     show_entry_dir( host, path, what[1].str(), what[2].str(), what[3].str() );
  168.                     start = what[0].second;
  169.                 }
  170.                 while(regex_search(start, end, what, expression1b, boost::match_extra))
  171.                 {
  172.                     show_entry_file( host, path, what[1].str(), what[2].str(), what[3].str() );
  173.                     start = what[0].second;
  174.                 }
  175.                 //apache list extended view
  176.                 while(regex_search(start, end, what, expression2, boost::match_extra))
  177.                 {
  178.                     //fprintf(stderr, "---\n");
  179.                     //printf("[DEBUG] loop 2\n");
  180.                     /*fprintf(stderr, "[INFO] path '%s'\n", what[1].str().c_str());
  181.                     fprintf(stderr, "[INFO] directory '%s'\n", what[2].str().c_str());
  182.                     fprintf(stderr, "[INFO] date '%s'\n", what[3].str().c_str());//*/
  183.                     show_entry_dir( host, path, what[1].str(), what[2].str(), what[3].str() );
  184.                     start = what[0].second;
  185.                 }
  186.                 while(regex_search(start, end, what, expression2b, boost::match_extra))
  187.                 {
  188.                     show_entry_file( host, path, what[1].str(), what[2].str(), what[3].str() );
  189.                     start = what[0].second;
  190.                 }
  191.                 //apache list standard/minimal view
  192.                 while(regex_search(start, end, what, expression3, boost::match_extra))
  193.                 {
  194.                     //fprintf(stderr, "---\n");
  195.                     //printf("[DEBUG] loop 2\n");
  196.                     /*fprintf(stderr, "[INFO] path '%s'\n", what[1].str().c_str());
  197.                     fprintf(stderr, "[INFO] directory '%s'\n", what[2].str().c_str());
  198.                     fprintf(stderr, "[INFO] date '%s'\n", what[3].str().c_str());//no date//*/
  199.                     show_entry_dir( host, path, what[1].str(), what[2].str(), what[3].str() );
  200.                     start = what[0].second;
  201.                 }
  202.                 while(regex_search(start, end, what, expression3b, boost::match_extra))
  203.                 {
  204.                     show_entry_file( host, path, what[1].str(), what[2].str(), what[3].str() );
  205.                     start = what[0].second;
  206.                 }
  207.             }
  208.         }
  209.         while (boost::asio::read_until(socket, response, std::string("\n"), error));
  210.         //fprintf(stderr, "---\n");
  211.         if (error != boost::asio::error::eof) {
  212.             throw boost::system::system_error(error);
  213.         }
  214.     }
  215.     catch (std::exception& e)
  216.     {
  217.         std::cout << "Exception: " << e.what() << "\n";
  218.     }
  219.     return 0;
  220. }
  221. //
Add Comment
Please, Sign In to add comment