sebbu

test downloader mingw

Feb 5th, 2011
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.93 KB | None | 0 0
  1. #include "global.hpp"
  2.  
  3. int main() {
  4.     //const char* host="ovh.dl.sourceforge.net";
  5.     //const char* host="heanet.dl.sourceforge.net";
  6.     const char* host="freefr.dl.sourceforge.net";//no date :p
  7.     const char* path="/project/mingw/";
  8.     try {
  9.         boost::asio::io_service io_service;
  10.  
  11.         // Get a list of endpoints corresponding to the server name.
  12.         tcp::resolver resolver(io_service);
  13.         tcp::resolver::query query(host, "http");
  14.         tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
  15.         tcp::resolver::iterator endpoint;
  16.  
  17.         // Try each endpoint until we successfully establish a connection.
  18.         tcp::socket socket(io_service);
  19.         boost::system::error_code error = boost::asio::error::host_not_found;
  20.         while (error && endpoint_iterator != endpoint)
  21.         {
  22.             socket.close();
  23.             socket.connect(*endpoint_iterator++, error);
  24.         }
  25.         if (error)
  26.         throw boost::system::system_error(error);
  27.  
  28.         // Form the request. We specify the "Connection: close" header so that the
  29.         // server will close the socket after transmitting the response. This will
  30.         // allow us to treat all data up until the EOF as the content.
  31.         boost::asio::streambuf request;
  32.         std::ostream request_stream(&request);
  33.         request_stream << "GET " << path << " HTTP/1.0\r\n";
  34.         request_stream << "Host: " << host << "\r\n";
  35.         request_stream << "Accept: */*\r\n";
  36.         request_stream << "Connection: close\r\n\r\n";
  37.  
  38.         // Send the request.
  39.         boost::asio::write(socket, request);
  40.  
  41.         // Read the response status line. The response streambuf will automatically
  42.         // grow to accommodate the entire line. The growth may be limited by passing
  43.         // a maximum size to the streambuf constructor.
  44.         boost::asio::streambuf response(512);
  45.         boost::asio::read_until(socket, response, "\r\n");
  46.  
  47.         // Check that response is OK.
  48.         std::istream response_stream(&response);
  49.         std::string http_version;
  50.         response_stream >> http_version;
  51.         unsigned int status_code;
  52.         response_stream >> status_code;
  53.         std::string status_message;
  54.         std::getline(response_stream, status_message);
  55.         if (!response_stream || http_version.substr(0, 5) != "HTTP/")
  56.         {
  57.             std::cout << "Invalid response\n";
  58.             return 1;
  59.         }
  60.         if (status_code != 200)
  61.         {
  62.             std::cout << "Response returned with status code " << status_code << "\n";
  63.             return 1;
  64.         }
  65.  
  66.         // Read the response headers, which are terminated by a blank line.
  67.         boost::asio::read_until(socket, response, "\r\n\r\n");
  68.  
  69.         // Process the response headers.
  70.         std::string header;
  71.         while (std::getline(response_stream, header) && header != "\r") {
  72.             std::cout << header << "\n";
  73.         }
  74.         std::cout << "\n";
  75.  
  76.         // Write whatever content we already have to output.
  77.         /*if (response.size() > 0) {
  78.             //std::cout << &response;
  79.             std::istream is(&response);
  80.             string line;
  81.             while (std::getline(is, line) && line!="\r" && line!="\r\n" && line!="\n") {
  82.                 fprintf(stderr, "[DEBUG] len = '%d' str='%s'\n", line.size(), line.c_str() );
  83.                 //std::cout << line << endl;
  84.             }
  85.         }//*/
  86.        
  87.         boost::regex expression("<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);
  88.         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+");
  89.         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>");
  90.        
  91.         string line;
  92.         // Read until EOF, writing data to output as we go.
  93.         std::string::const_iterator start, end;
  94.         //while (boost::asio::read(socket, response, boost::asio::transfer_at_least(1), error)) {
  95.         do { //already have a partial response from when we read the header
  96.             //printf("[DEBUG] loop 0\n");
  97.             std::istream is(&response);
  98.             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
  99.                 //printf("[DEBUG] loop 1\n");
  100.                 start = line.begin();
  101.                 end = line.end();
  102.                 /*if(*end==0) {
  103.                     //workaround
  104.                     end=(line.begin()+line.size()-1);
  105.                     fprintf(stderr, "[DEBUG] workaround for *end==0 : size='%d', end='%d'\n", line.size(), *end);
  106.                 }//*/
  107.                 boost::match_results<std::string::const_iterator> what;
  108.                 //apache table extended view
  109.                 while(regex_search(start, end, what, expression, boost::match_extra))
  110.                 {
  111.                     fprintf(stderr, "---\n");
  112.                     //printf("[DEBUG] loop 2\n");
  113.                     fprintf(stderr, "[INFO] path '%s'\n", what[1].str().c_str());
  114.                     fprintf(stderr, "[INFO] directory '%s'\n", what[2].str().c_str());
  115.                     fprintf(stderr, "[INFO] date '%s'\n", what[3].str().c_str());
  116.                     start = what[0].second;
  117.                 }
  118.                 //apache list extended view
  119.                 while(regex_search(start, end, what, expression2, boost::match_extra))
  120.                 {
  121.                     fprintf(stderr, "---\n");
  122.                     //printf("[DEBUG] loop 2\n");
  123.                     fprintf(stderr, "[INFO] path '%s'\n", what[1].str().c_str());
  124.                     fprintf(stderr, "[INFO] directory '%s'\n", what[2].str().c_str());
  125.                     fprintf(stderr, "[INFO] date '%s'\n", what[3].str().c_str());
  126.                     start = what[0].second;
  127.                 }
  128.                 //apache list standard/minimal view
  129.                 while(regex_search(start, end, what, expression3, boost::match_extra))
  130.                 {
  131.                     fprintf(stderr, "---\n");
  132.                     //printf("[DEBUG] loop 2\n");
  133.                     fprintf(stderr, "[INFO] path '%s'\n", what[1].str().c_str());
  134.                     fprintf(stderr, "[INFO] directory '%s'\n", what[2].str().c_str());
  135.                     fprintf(stderr, "[INFO] date '%s'\n", what[3].str().c_str());//no date
  136.                     start = what[0].second;
  137.                 }
  138.             }
  139.         }
  140.         while (boost::asio::read_until(socket, response, std::string("\n"), error));
  141.         fprintf(stderr, "---\n");
  142.         if (error != boost::asio::error::eof) {
  143.             throw boost::system::system_error(error);
  144.         }
  145.     }
  146.     catch (std::exception& e)
  147.     {
  148.         std::cout << "Exception: " << e.what() << "\n";
  149.     }
  150.  
  151.     cout << "done" << endl;
  152.     usleep(1000);//workaround
  153.     return EXIT_SUCCESS;
  154. }
  155. //
Add Comment
Please, Sign In to add comment