| 1 | /* smplayer, GUI front-end for mplayer.
|
|---|
| 2 | Copyright (C) 2006-2013 Ricardo Villalba <rvm@users.sourceforge.net>
|
|---|
| 3 |
|
|---|
| 4 | This program is free software; you can redistribute it and/or modify
|
|---|
| 5 | it under the terms of the GNU General Public License as published by
|
|---|
| 6 | the Free Software Foundation; either version 2 of the License, or
|
|---|
| 7 | (at your option) any later version.
|
|---|
| 8 |
|
|---|
| 9 | This program is distributed in the hope that it will be useful,
|
|---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 12 | GNU General Public License for more details.
|
|---|
| 13 |
|
|---|
| 14 | You should have received a copy of the GNU General Public License
|
|---|
| 15 | along with this program; if not, write to the Free Software
|
|---|
| 16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|---|
| 17 | */
|
|---|
| 18 |
|
|---|
| 19 | #include "simplehttp.h"
|
|---|
| 20 | #include <QUrl>
|
|---|
| 21 |
|
|---|
| 22 | SimpleHttp::SimpleHttp( QObject * parent ) : QHttp(parent)
|
|---|
| 23 | {
|
|---|
| 24 | http_get_id = -1;
|
|---|
| 25 |
|
|---|
| 26 | connect( this, SIGNAL(requestFinished(int, bool)),
|
|---|
| 27 | this, SLOT(httpRequestFinished(int, bool)) );
|
|---|
| 28 |
|
|---|
| 29 | connect( this, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),
|
|---|
| 30 | this, SLOT(readResponseHeader(const QHttpResponseHeader &)) );
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | SimpleHttp::~SimpleHttp() {
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | void SimpleHttp::download(const QString & url) {
|
|---|
| 37 | qDebug("SimpleHttp::download: %s", url.toLatin1().constData());
|
|---|
| 38 |
|
|---|
| 39 | downloaded_text.clear();
|
|---|
| 40 |
|
|---|
| 41 | QUrl u(url);
|
|---|
| 42 | setHost( u.host() );
|
|---|
| 43 |
|
|---|
| 44 | /*
|
|---|
| 45 | qDebug("u.path: %s", u.path().toLatin1().constData());
|
|---|
| 46 | qDebug("u.query: %s", u.encodedQuery().constData());
|
|---|
| 47 | */
|
|---|
| 48 |
|
|---|
| 49 | QString p = u.path();
|
|---|
| 50 | if (!u.encodedQuery().isEmpty()) p += "?" + u.encodedQuery();
|
|---|
| 51 |
|
|---|
| 52 | if (user_agent.isEmpty()) {
|
|---|
| 53 | http_get_id = get( p );
|
|---|
| 54 | } else {
|
|---|
| 55 | QHttpRequestHeader header("GET", p);
|
|---|
| 56 | header.setValue("Host", u.host());
|
|---|
| 57 | header.setValue("User-Agent", user_agent);
|
|---|
| 58 | http_get_id = request(header);
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | emit connecting(u.host());
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | void SimpleHttp::readResponseHeader(const QHttpResponseHeader &responseHeader) {
|
|---|
| 65 | qDebug("SimpleHttp::readResponseHeader: statusCode: %d", responseHeader.statusCode());
|
|---|
| 66 |
|
|---|
| 67 | if (responseHeader.statusCode() == 301) {
|
|---|
| 68 | QString new_url = responseHeader.value("Location");
|
|---|
| 69 | qDebug("SimpleHttp::readResponseHeader: Location: '%s'", new_url.toLatin1().constData());
|
|---|
| 70 | download(new_url);
|
|---|
| 71 | }
|
|---|
| 72 | else
|
|---|
| 73 | if (responseHeader.statusCode() == 302) {
|
|---|
| 74 | QString location = responseHeader.value("Location");
|
|---|
| 75 | qDebug("SimpleHttp::readResponseHeader: Location: '%s'", location.toLatin1().constData());
|
|---|
| 76 | /* http_get_id = get( location ); */
|
|---|
| 77 | download(location);
|
|---|
| 78 | }
|
|---|
| 79 | else
|
|---|
| 80 | if (responseHeader.statusCode() != 200) {
|
|---|
| 81 | qDebug("SimpleHttp::readResponseHeader: error: '%s'", responseHeader.reasonPhrase().toLatin1().constData());
|
|---|
| 82 | emit downloadFailed(responseHeader.reasonPhrase());
|
|---|
| 83 | abort();
|
|---|
| 84 | }
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | void SimpleHttp::httpRequestFinished(int request_id, bool error) {
|
|---|
| 88 | qDebug("SimpleHttp::httpRequestFinished: http_get_id: %d request_id: %d, error: %d", http_get_id, request_id, error);
|
|---|
| 89 |
|
|---|
| 90 | if (request_id != http_get_id) return;
|
|---|
| 91 |
|
|---|
| 92 | downloaded_text += readAll();
|
|---|
| 93 |
|
|---|
| 94 | //qDebug("downloaded_text: '%s'", downloaded_text.constData());
|
|---|
| 95 |
|
|---|
| 96 | if ((!error) && (!downloaded_text.isEmpty())) {
|
|---|
| 97 | emit downloadFinished(downloaded_text);
|
|---|
| 98 | }
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | #include "moc_simplehttp.cpp"
|
|---|
| 102 |
|
|---|