// Aria - yet another download tool // Copyright (C) 2000, 2002 Tatsuhiro Tsujikawa // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // // $Id: Session.cc,v 1.16 2002/04/03 13:33:52 tujikawa Exp $ #include "Session.h" Session::Session(const string& label_in, const string& get_string, const string& referer_string, const string& keylink_string, const string& command_string, const string& command_status_string, const string& cond_in, const string& cond_succ_in, const string& cond_fail_in, int post_offset_size_in, const string& post_offset_string_in, const string& option_string) { label = label_in; if((cond_type = Process_cond_string(cond_in)) > 0) { cond_succ = cond_succ_in; cond_fail = cond_fail_in; } nodown = false; nocookie = false; deletecookie = false; getkeylink = false; getkeylinkf = false; addhref = false; readconfig = false; noresume = false; bad_flag = false; try { Process_get_string(get_string); Process_referer_string(referer_string); Process_option_string(option_string); Process_keylink_string(keylink_string); if(Remove_white(command_string).size()) { Command command_temp(command_string, command_status_string); if(command_temp.bad()) { throw EPARSE; } else { command = command_temp; command.set_valid(true); } } // offset if(post_offset_size_in < 0) post_offset_size = 0; else post_offset_size = post_offset_size_in; post_offset_string = post_offset_string_in; } catch (ErrorType err) { cerr << "error occurred in tag" << endl; bad_flag = true; } } Session::Session() { } bool Session::bad() const { return bad_flag; } bool Session::Is_program_avaiable() const { return command.Is_valid(); } string Session::ret_program_line(const string& filename, const string& save_dir, const URLcontainer& urlcon) const { return command.Create_commandline(filename, save_dir, urlcon); } Session::SessionExecStatus Session::Execute_program(const string& filename, const string& save_dir, const URLcontainer& urlcon) const { int status = command.Execute_commandline(filename, save_dir, urlcon); int ex_stat = WIFEXITED(status); int return_stat = 0; if(ex_stat) { return_stat = WEXITSTATUS(status); } if(command.Is_ignore_return_status()) { if(ex_stat) { return SESSION_EXEC_IGN; } else { return SESSION_EXEC_IGN_FATAL; } } else if(!ex_stat) { return SESSION_EXEC_IGN_FATAL; } else if(command.Is_in_succ_status_list(return_stat)) { return SESSION_EXEC_SUCC; } else { return SESSION_EXEC_FAIL; } } string Session::MyToken_splitter(string& line) { unsigned int start_pos = line.find_first_not_of(" \t\n\r"); if(start_pos == string::npos) { line.erase(); return ""; } string token; if(line.at(start_pos) == '$') { // reserved word // $(****) unsigned int end_pos = line.find(')', start_pos); if(end_pos == string::npos) { cerr << "error: paren mismatch: '" << line.substr(start_pos) << "'" << endl; throw EPARSE; } token = line.substr(start_pos, end_pos-start_pos+1); line.erase(0, end_pos+1); } else if(line.at(start_pos) == '"') { //phrase ++start_pos; unsigned int end_pos = line.find('"', start_pos); if(end_pos == string::npos) { cerr << "error: unterminated phrase '" << line.substr(start_pos) << "'" << endl; throw EPARSE; } token = line.substr(start_pos, end_pos-start_pos); line.erase(0, end_pos+1); } else { // unreserved word bool flag = true; unsigned int end_pos = start_pos; while(flag) { unsigned int lstart_pos = end_pos; end_pos = line.find_first_of(" \t\"$", lstart_pos); if(end_pos == string::npos) { end_pos = line.size(); flag = false; } else { unsigned int bkslash_pos; // '\\' must be converted to '\' bkslash_pos = lstart_pos; while((bkslash_pos = line.find("\\\\", bkslash_pos)) < end_pos) { line.erase(bkslash_pos, 1); ++bkslash_pos; --end_pos; } if(line.at(end_pos) == '"' && end_pos != 0 && line.at(end_pos-1) == '\\') { // '\"' is '"' line.erase(end_pos-1, 1); //end_pos; } else { flag = false; } } } token = line.substr(start_pos, end_pos-start_pos); line.erase(0, end_pos); } //cerr << token << endl; return token; } int Session::Process_cond_string(string cond_string) { if(cond_string.empty()) return -1; if(startwith(cond_string, "status_branch(")) { Token_splitter(cond_string, "( "); string status = Token_splitter(cond_string, ")"); if(status == "HTTP_OK") { cond_type = Session::COND_HTTP_OK; } else if(status == "HTTP_ERROR") { cond_type = Session::COND_HTTP_ERROR; } else if(status == "HTTP_FORBIDDEN") { cond_type = Session::COND_HTTP_FORBIDDEN; } else if(status == "HTTP_NOTFOUND") { cond_type = Session::COND_HTTP_NOTFOUND; } else if(status == "HTTP_MOVED") { cond_type = Session::COND_HTTP_MOVED; } else { throw EPARSE; } } else if(startwith(cond_string, "find_string(")) { Token_splitter(cond_string, "( "); string str_find = Token_splitter(cond_string, ")"); if(str_find.empty()) throw EPARSE; cond_type = Session::COND_FIND_STRING; cond_str_find = str_find; } return cond_type; } void Session::Process_option_string(string option_string) { while(option_string.size()) { string token = Token_splitter(option_string); if(token == "nodown") { nodown = true; } else if(token == SOPTION_NOCOOKIE) { nocookie = true; } else if(token == SOPTION_DELETECOOKIE) { deletecookie = true; } else if(token == SOPTION_GETKEYLINK) { getkeylink = true; } else if(token == SOPTION_GETKEYLINKF) { getkeylinkf = true; } else if(token == SOPTION_ADDHREF) { addhref = true; } else if(token == SOPTION_READCONFIG) { readconfig = true; } else if(token == SOPTION_NORESUME) { noresume = true; } else { cerr << "error: undefined option: '" << token << "' in