00001 /**************************************************************** 00002 * Vidalia is distributed under the following license: 00003 * 00004 * Copyright (C) 2006, Matt Edman, Justin Hipple 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License 00008 * as published by the Free Software Foundation; either version 2 00009 * of the License, or (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 * Boston, MA 02110-1301, USA. 00020 ****************************************************************/ 00021 00022 /** 00023 * \file http.cpp 00024 * \version $Id: http.cpp 1563 2006-12-26 06:06:04Z edmanm $ 00025 * \brief Simple HTTP GET request object 00026 */ 00027 00028 #include <QCoreApplication> 00029 00030 #include "http.h" 00031 00032 /** Constructor */ 00033 Http::Http(QString host, quint16 port) 00034 : QHttp(host, port) 00035 { 00036 } 00037 00038 /** Executes a single request for the given file. User input is blocked while 00039 * the request is executing, so this should only be used for very small requests. 00040 * \param path the file to request. 00041 * \return true if the request executed successfully. 00042 */ 00043 bool 00044 Http::request(QString path) 00045 { 00046 get(path); 00047 00048 while (hasPendingRequests() || currentId() != 0) { 00049 QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents); 00050 } 00051 if (bytesAvailable() > 0) { 00052 _response = readAll(); 00053 return true; 00054 } 00055 return false; 00056 } 00057 00058 /** Returns the body of an HTTP response. If no body tags are present, then 00059 * the whole response is returned. */ 00060 QString 00061 Http::body() 00062 { 00063 int length, end; 00064 int start = _response.indexOf("<body>"); 00065 if (start >= 0) { 00066 start += 6; 00067 end = _response.indexOf("</body>"); 00068 length = (end > 0 ? end-start : -1); 00069 return _response.mid(start, length); 00070 } 00071 return _response; 00072 } 00073