コード例 #1
0
ファイル: RS.cpp プロジェクト: truszkowski/librs
  void stage_3(string &url)
  {
    rsprintf("Lacze z '%s' (3)", url.c_str());
    
    rs_progress_start = Time::in_usec();
    rs_info.status = RS::Downloading;
    rs_info.bytes = 0;
    rs_info.tmpspeed = 0;
    off_t length = 0;

    try { // TODO
      int code = http.get(rs_download_get_path(rs_info.url.c_str()).string().c_str(), length, url.c_str(), 
          "mirror=", NULL, rs_progress_callback, NULL);
     
      filesystem::ofstream head(rs_sessions_get_path(url, "-head3.http"));
      head.write(http.get_recv_header(), strlen(http.get_recv_header()));

      if (code == HTTP_OK) // Oczekujemy kodu 200
        rsprintf("Polaczono, code = %d, length = %lld", code, length);
      else {
        rsprintf("Polaczono, brak strony? code = %d, length = %lld, try again", code, length);
        throw DBreak();
      }

      //string clen = http.get_recv_header("content-length");
      //rsprintf("");
    } 
    catch (Exception &e) {
      rsprintf("Nastapil wyjatek: %s", e.what());
      throw DBreak();
    }  
  }
コード例 #2
0
ファイル: test_http.cpp プロジェクト: truszkowski/librs
int main(int argc, char **argv)
{
  Http http;
  char *page = NULL;
  size_t size = 0;

  for (int i = 1; i < argc; ++i) {
    cerr << "== GET '" << argv[i] << "' ==" << endl;
    
    try {
      //http.set_verbose(true);
      int code = http.get(page, size, argv[i]);
      if (page) delete[] page;

      const Http::Headers &hdrs = http.get_headers();

      Http::Headers::const_iterator it;
      const char *loc = http.get_recv_header("location");
      const char *coo = http.get_recv_header("set-cookie");
      const char *len = http.get_recv_header("content-length");
    
      cerr << "== code = " << code << endl;
      cerr << "== headers size = " << hdrs.size() << endl;
      cerr << "== location = " << (loc?loc:"?") << endl;
      cerr << "== set-cookie = " << (coo?coo:"?") << endl;
      cerr << "== content-length = " << (len?len:"?") << " (strona: " << size << ")"<< endl;
    } 
    catch (Exception &e) {
      cerr << "== exception: " << e.what() << endl;
    }
  }

  return 0;
}
コード例 #3
0
ファイル: RS.cpp プロジェクト: truszkowski/librs
  void stage_1(string &url)
  {
    rsprintf("Lacze z '%s'... (1)", url.c_str());
    char *buffer = NULL;
    size_t buflen = 0;

    try {
      int code = http.get(buffer, buflen, url.c_str());

      filesystem::ofstream head(rs_sessions_get_path(url, "-head1.http"));
      head.write(http.get_recv_header(), strlen(http.get_recv_header()));

      if (code == HTTP_OK) // Oczekujemy kodu 200
        rsprintf("Polaczono, code = %d, page = %p,%zd", code, buffer, buflen);
      else { 
        rsprintf("Polaczono, brak strony? code = %d, page = %p,%zd, sprobuje za chwile...", code, buffer, buflen);
        if (buffer) delete[] buffer;
        throw DBreak();
      }

      filesystem::ofstream page(rs_sessions_get_path(url, "-page1.html"));
      page.write(buffer, buflen);
    }
    catch (Exception &e) {
      rsprintf("Nastapil wyjatek: %s", e.what());
      throw DBreak();
    }

    // Sprawdzamy czy plik jest dostepny...
    if (regex_search(buffer, rs_regex_illegal_file) ||
        regex_search(buffer, rs_regex_illegal_file2) ||
        regex_search(buffer, rs_regex_not_available) ||
        regex_search(buffer, rs_regex_not_found)) {
      rsprintf("Plik '%s' jest niedostepny", url.c_str());
      delete[] buffer;
      //rs_info.status = RS::NotFound;
      throw DAbort(RS::NotFound);
    }

    // Szukamy nastepnego url'a...
    cmatch what;
    if (!regex_search(buffer, what, rs_regex_url)) {
      rsprintf("Brak odnosnika do nastepnej strony, sprobuje jeszcze raz...");
      delete[] buffer;
      throw DBreak();
    }

    url = what[1]; // ustawiamy nowy url
    delete[] buffer;
  }
コード例 #4
0
ファイル: RS.cpp プロジェクト: truszkowski/librs
  void stage_2(string &url)
  {
    rsprintf("Lacze z '%s' (2)", url.c_str());
    char *buffer = NULL;
    size_t buflen = 0;

    try {
      int code = http.get(buffer, buflen, url.c_str(), "dl.start=Free");

      filesystem::ofstream head(rs_sessions_get_path(url, "-head2.http"));
      head.write(http.get_recv_header(), strlen(http.get_recv_header()));

      if (code == HTTP_OK) // Oczekujemy kodu 200
        rsprintf("Polaczono, code = %d, page = %p,%zd", code, buffer, buflen);
      else {
        rsprintf("Polaczono, brak strony? code = %d, page = %p,%zd, try again", code, buffer, buflen);
        if (buffer) delete[] buffer;
        throw DBreak();
      }

      filesystem::ofstream page(rs_sessions_get_path(url, "-page2.html"));
      page.write(buffer, buflen);
    }
    catch (Exception &e) {
      rsprintf("Nastapil wyjatek: %s", e.what());
      throw DBreak();
    }

    if (regex_search(buffer, rs_regex_try_later)) {
      rsprintf("Nie mozna teraz pobierac pliku, sprobuje za chwile, czekam...");
      rs_wait(RS::Waiting, RS::Preparing, rs_waiting_try_later);
      delete[] buffer;
      throw DAgain();
    }

    if (regex_search(buffer, rs_regex_reached_limit)) {
      rsprintf("Wyczerpal sie limit pobran, czekam...");
      rs_wait(RS::Limit, RS::Preparing, rs_waiting_limit);
      delete[] buffer;
      throw DAgain();
    }

    if (regex_search(buffer, rs_regex_server_busy)) {
      rsprintf("Serwery sa przeciazone, czekam...");
      rs_wait(RS::Busy, RS::Preparing, rs_waiting_busy);
      delete[] buffer;
      throw DAgain();
    }

    if (regex_search(buffer, rs_regex_server_busy)) {
      rsprintf("Brak wolnych slotow, czekam...");
      rs_wait(RS::Busy, RS::Preparing, rs_waiting_busy);
      delete[] buffer;
      throw DAgain();
    }

    if (regex_search(buffer, rs_regex_already_downloading)) {
      rsprintf("Ktos o tym samym IP pobiera juz jakis plik, czekam...");
      rs_wait(RS::Rivalry, RS::Preparing, rs_waiting_rivalry);
      delete[] buffer;
      throw DAgain();
    }

    size_t wait_for = 0;
    string ssize;
    cmatch what;

    if (regex_search(buffer, what, rs_regex_time)) {
      wait_for = strtol(what[1].str().c_str(), 0, 10) + 5;
      rsprintf("Czekam %zd sek...", wait_for);
    } else {
      wait_for = 5;
      rsprintf("Nieznany czas oczekiwania, sprobuje %zd sek...", wait_for);
    }

    if (!regex_search(buffer, what, rs_regex_size)) {
      rsprintf("Nieznany rozmiar pliku, jeszcze raz...");
      delete[] buffer;
      throw DBreak();
    }
    ssize = what[1];
    rs_info.all_bytes = 1000*strtol(ssize.c_str(), 0, 10);

    try {
      vector<pair<string, string> > srvs;
      for (cregex_iterator it(buffer, buffer+buflen, rs_regex_server), end; it != end; ++it)
        srvs.push_back(make_pair<string, string>((*it)[2], (*it)[1]));

      if (!srvs.size()) {
        rsprintf("Brak serwerow, przerywam...");
        throw DBreak();
      }

      size_t i = 0;
      while (rs_favorites_servers[i]) {
        size_t found = 0, end = srvs.size();

        while (found < end) {
          if (srvs[found].first == rs_favorites_servers[i]) break;
          ++found;
        }

        if (found == end) {
          rsprintf("Brak serwera '%s', szukam dalej...", rs_favorites_servers[i]);
          ++i;
          continue;
        }

        rsprintf("Wybieram serwer '%s'...", rs_favorites_servers[i]);
        url = srvs[found].second;
        break;
      }

      if (!rs_favorites_servers[i]) {
        url = srvs[0].second;
        rsprintf("Brak jakiegokolwiek ulubionego serwera, wybieram pierwszy z brzegu '%s'",
            srvs[0].first.c_str());
      }
    }
    catch (DBreak) { delete[] buffer; throw; }
    catch (DExc &e) { delete[] buffer; throw e; }

    rs_wait(RS::Waiting, RS::Preparing, wait_for);
    delete[] buffer;
  }