/** ** File ......... TrackerSock.cpp ** Published .... 2004-04-20 **/ /* Copyright (C) 2004 grymse@alhem.net 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. */ //#include #include #include #include "MyHandler.h" #include "TrackerSock.h" TrackerSock::TrackerSock(SocketHandler& h) :HTTPSocket(h) { SetLineProtocol(); } TrackerSock::~TrackerSock() { //printf("~TrackerSock\n"); } void TrackerSock::OnFirst() { if (GetMethod() == "GET") { std::string url = GetUrl(); std::string params; { Parse pa(url,"?"); m_uri = pa.getword(); params = pa.getrest(); } Parse pa(params,"&"); std::string tmp = pa.getword(); while (tmp.size()) { { Parse pa(tmp,"="); std::string key = pa.getword(); std::string val = pa.getrest(); m_par[key] = val; } tmp = pa.getword(); } m_info_hash = static_cast(Handler()).escape(m_par["info_hash"]); std::string ip = m_par["ip"]; ip = ip.size() ? ip : GetRemoteAddress(); MyHandler::PEER *p = static_cast(Handler()).reg_peer( m_info_hash, m_par["peer_id"], ip, atoi(m_par["port"].c_str())); if (m_par["event"] == "completed") { p -> complete(); } else if (m_par["event"] == "stopped") { p -> stop(); } else { p -> update(m_par["uploaded"],m_par["downloaded"],m_par["left"]); } } } void TrackerSock::OnHeaderComplete() { AddResponseHeader("Connection", "close"); AddResponseHeader("Server", static_cast(Handler()).GetString("server/identity")); if (m_uri == "/" || m_uri == "/index.html") { SetHttpVersion("HTTP/1.0"); SetStatus("501"); SetStatusText("Not Implemented"); SendResponse(); SetCloseAndDelete(); } else if (m_uri == "/scrape") { SetHttpVersion("HTTP/1.0"); SetStatus("501"); SetStatusText("Not Implemented"); SendResponse(); SetCloseAndDelete(); } else if (m_uri == "/file") { SetHttpVersion("HTTP/1.0"); SetStatus("501"); SetStatusText("Not Implemented"); SendResponse(); SetCloseAndDelete(); } else if (m_uri == "/favicon.ico") { SetHttpVersion("HTTP/1.0"); SetStatus("501"); SetStatusText("Not Implemented"); SendResponse(); SetCloseAndDelete(); } else if (m_uri != "/announce") { SetHttpVersion("HTTP/1.0"); SetStatus("404"); SetStatusText("Not Found"); SendResponse(); SetCloseAndDelete(); return; } SetHttpVersion("HTTP/1.0"); SetStatus("200"); SetStatusText("OK"); SendResponse(); { time_t t = time(NULL); struct tm *tp = localtime(&t); printf("%d-%02d-%02d %02d:%02d:%02d :: %s: %s %s %s\n",tp -> tm_year + 1900, tp -> tm_mon + 1,tp -> tm_mday, tp -> tm_hour,tp -> tm_min,tp -> tm_sec, GetRemoteAddress().c_str(),GetMethod().c_str(),GetUrl().c_str(),GetHttpVersion().c_str()); } std::string interval = "i" + static_cast(Handler()).GetString("server/reannounce_interval") + "e"; size_t numwant = atoi(m_par["numwant"].c_str()); size_t response_size = static_cast(Handler()).GetInt("server/response_size"); Send("d"); Send("8:interval"); Send(interval); Send("5:peers" "l"); static_cast(Handler()).CreatePeers(this, m_info_hash, numwant ? numwant : response_size); // end peers list Send("e"); // end dictionary Send("e"); SetCloseAndDelete(); Detach(); }