SongSearch::SongSearch(QWidget *parent) :
			SoSSubWindow(parent),
			ui(new Ui::SongSearch),
			searchForGroup(new QButtonGroup()),
			searchTypeGroup(new QButtonGroup()),
			maxResults(10),
			currentTask(TT_SEARCH),
			timerId(0),
			skinMgr(0)
		{
			ui->setupUi(this);

			setSubWidgets(ui->windowBar, ui->content);
			connect(&httpHandler, SIGNAL(receivedResponse(QString)),
					this, SLOT(gotResponse(QString)));
			connect(&httpHandler, SIGNAL(responseError(QString)),
					this, SLOT(gotError(QString)));

			searchForGroup->addButton(ui->titleButton, 1);
			searchForGroup->addButton(ui->artistButton, 2);
			searchForGroup->addButton(ui->searchInAllButton, 3);

			searchTypeGroup->addButton(ui->ultraStarButton, 1);
			searchTypeGroup->addButton(ui->midiButton, 2);
			searchTypeGroup->addButton(ui->anyTypeButton, 3);
		}
Ejemplo n.º 2
0
bool KviHttpRequest::processHeader(KviCString &szHeader)
{
	int idx = szHeader.findFirstIdx("\r\n");
	KviCString szResponse;
	if(idx != -1)
	{
		szResponse = szHeader.left(idx);
		szHeader.cutLeft(idx + 2);
	} else {
		szResponse = szHeader;
		szHeader = "";
	}

	szResponse.trim();

	bool bValid = false;

	unsigned int uStatus = 0;

	// check the response value
	if(kvi_strEqualCSN(szResponse.ptr(),"HTTP",4))
	{
		KviCString szR = szResponse;
		szR.cutToFirst(' ');
		szR.trim();
		int idx = szR.findFirstIdx(' ');
		KviCString szNumber;
		if(idx != -1)szNumber = szR.left(idx);
		else szNumber = szR;
		bool bOk;
		uStatus = szNumber.toUInt(&bOk);
		if(bOk)bValid = true;
	}

	QString szUniResponse = QString::fromUtf8(szResponse.ptr());

	if(!bValid)
	{
		// the response is invalid ?
		resetInternalStatus();
		m_szLastError = __tr2qs("Invalid HTTP response: %1").arg(szUniResponse);
		emit terminated(false);
		return false;
	}

	emit status(__tr2qs("Received HTTP response: %1").arg(szUniResponse));

	KviPointerList<KviCString> hlist;
	hlist.setAutoDelete(true);

	idx = szHeader.findFirstIdx("\r\n");
	while(idx != -1)
	{
		if(idx > 0)
		{
			hlist.append(new KviCString(szHeader.ptr(),idx));
			szHeader.cutLeft(idx + 2);
		}
		idx = szHeader.findFirstIdx("\r\n");
	}
	if(szHeader.hasData())hlist.append(new KviCString(szHeader));

	KviPointerHashTable<const char *,KviCString> hdr(11,false,true);
	hdr.setAutoDelete(true);

	for(KviCString * s = hlist.first();s;s = hlist.next())
	{
		idx = s->findFirstIdx(":");
		if(idx != -1)
		{
			KviCString szName = s->left(idx);
			s->cutLeft(idx + 1);
			s->trim();
			hdr.replace(szName.ptr(),new KviCString(*s));
			//qDebug("FOUND HEADER (%s)=(%s)",szName.ptr(),s->ptr());
		}
	}

	KviCString * size = hdr.find("Content-length");
	if(size)
	{
		bool bOk;
		m_uTotalSize = size->toUInt(&bOk);
		if(!bOk)m_uTotalSize = 0;
	}

	KviCString * contentEncoding = hdr.find("Content-encoding");
	if(contentEncoding)
	{
		m_bGzip = contentEncoding->equalsCI("gzip");
	}

	KviCString * transferEncoding = hdr.find("Transfer-Encoding");
	if(transferEncoding)
	{
		if(kvi_strEqualCI(transferEncoding->ptr(),"chunked"))
		{
			// be prepared to handle the chunked transfer encoding as required by HTTP/1.1
			m_bChunkedTransferEncoding = true;
			m_uRemainingChunkSize = 0;
		}
	}

	// check the status

	//				case 200: // OK
	//				case 206: // Partial content

	//				case 100: // Continue ??
	//				case 101: // Switching protocols ???
	//				case 201: // Created
	//				case 202: // Accepted
	//				case 203: // Non-Authoritative Information
	//				case 204: // No content
	//				case 205: // Reset content
	//				case 300: // Multiple choices
	//				case 301: // Moved permanently
	//				case 302: // Found
	//				case 303: // See Other
	//				case 304: // Not modified
	//				case 305: // Use Proxy
	//				case 306: // ???
	//				case 307: // Temporary Redirect
	//				case 400: // Bad request
	//				case 401: // Unauthorized
	//				case 402: // Payment Required
	//				case 403: // Forbidden
	//				case 404: // Not found
	//				case 405: // Method not allowed
	//				case 406: // Not acceptable
	//				case 407: // Proxy authentication required
	//				case 408: // Request timeout
	//				case 409: // Conflict
	//				case 410: // Gone
	//				case 411: // Length required
	//				case 412: // Precondition failed
	//				case 413: // Request entity too large
	//				case 414: // Request-URI Too Long
	//				case 415: // Unsupported media type
	//				case 416: // Requested range not satisfiable
	//				case 417: // Expectation Failed
	//				case 500: // Internal server error
	//				case 501: // Not implemented
	//				case 502: // Bad gateway
	//				case 503: // Service unavailable
	//				case 504: // Gateway timeout
	//				case 505: // HTTP Version not supported

	if(
		(uStatus != 200) && // OK
		(uStatus != 206)    // Partial content
	)
	{
		// This is not "OK" and not "Partial content"
		// Error, redirect or something confusing
		if(m_eProcessingType != HeadersOnly)
		{
			switch(uStatus)
			{
				case 301: // Moved permanently
				case 302: // Found
				case 303: // See Other
				case 307: // Temporary Redirect
				{
					if(!m_bFollowRedirects)
					{
						resetInternalStatus();
						m_szLastError = szResponse.ptr();
						emit terminated(false);
						return false;
					}
					
					m_uRedirectCount++;
					
					if(m_uRedirectCount > m_uMaximumRedirectCount)
					{
						resetInternalStatus();
						m_szLastError = __tr2qs("Too many redirects");
						emit terminated(false);
						return false;
					}
					
					KviCString * location = hdr.find("Location");

					if(!location)
					{
						resetInternalStatus();
						m_szLastError = __tr2qs("Bad redirect");
						emit terminated(false);
						return false;
					}
					
					KviUrl url(location->ptr());
					
					if(
							(url.url() == m_connectionUrl.url()) ||
							(url.url() == m_url.url())
						)
					{
						resetInternalStatus();
						m_szLastError = __tr2qs("Redirect loop");
						emit terminated(false);
						return false;
					}

					m_connectionUrl = url;

					emit status(__tr2qs("Following Redirect to %1").arg(url.url()));

					if(!start())
						emit terminated(false);

					return false; // will exit the call stack
				}
				break;
				break;
				default:
					// assume error
					resetInternalStatus();
					m_szLastError = szResponse.ptr();
					emit terminated(false);
					return false;
				break;
			}
			// this is an error then
		} // else the server will terminate (it was a HEAD request)
	}

	emit receivedResponse(szUniResponse);

	emit header(&hdr);

	if((m_uMaxContentLength > 0) && (m_uTotalSize > ((unsigned int)m_uMaxContentLength)))
	{
		resetInternalStatus();
		m_szLastError=__tr2qs("The amount of received data exceeds the maximum length");
		emit terminated(false);
		return false;
	}

	// fixme: could check for data type etc...

	return true;
}
Ejemplo n.º 3
0
void Receiver::parseMessage(QString message, const QHostAddress& address)
{
    QString command = message.left(message.indexOf(" "));
    if (QString::compare(command, CMD::HELLO) == 0)
    {
        message.remove(0, message.indexOf(" ") + 1);
        std::cout << "Received 'HELLO " << message.toStdString() << "' from " << address.toString().toStdString() << std::endl;
        if (message.length() == 0 || message.indexOf(" ") != -1)
        {
            std::cout << "Invalid HELLO!" << std::endl;
            return;
        }
        emit receivedHello(message, address);
    }
    else if (QString::compare(command, CMD::RESPONSE) == 0)
    {
        message.remove(0, message.indexOf(" ") + 1);
        std::cout << "Received 'RESPONSE " << message.toStdString() << "' from " << address.toString().toStdString() << std::endl;
        emit receivedResponse(message, address);
    }
    else if (QString::compare(command, CMD::JOIN) == 0)
    {
        message.remove(0, message.indexOf(" ") + 1);
        std::cout << "Received 'JOIN " << message.toStdString() << "' from " << address.toString().toStdString() << std::endl;
        QString address = message.left(message.indexOf(" "));
        message.remove(0, message.indexOf(" ") + 1);
        QString nickname = message;
        if (address.length() == 0 || nickname.length() == 0)
        {
            std::cout << "Invalid JOIN!" << std::endl;
            return;
        }
        emit receivedJoin(nickname, QHostAddress(address));
    }
    else if (QString::compare(command, CMD::MESSAGE) == 0)
    {
        message.remove(0, message.indexOf(" ") + 1);
        std::cout << "Received 'MESSAGE " << message.toStdString() << "' from " << address.toString().toStdString() << std::endl;
        emit receivedMessage(message, address);
    }
    else if (QString::compare(command, CMD::ACCEPTED) == 0)
    {
        message.remove(0, message.indexOf(" ") + 1);
        std::cout << "Received 'ACCEPTED " << message.toStdString() << "' from " << address.toString().toStdString() << std::endl;
        if (message.length() == 0 || message.indexOf(" ") == -1)
        {
            std::cout << "Invalid ACCEPTED!" << std::endl;
            return;
        }
        emit receivedAccepted(message);
    }
    else if (QString::compare(command, CMD::QUIT) == 0)
    {
        message.remove(0, message.indexOf(" ") + 1);
        std::cout << "Received 'QUIT " << message.toStdString() << "' from " << address.toString().toStdString() << std::endl;
        if (message.length() == 0 || message.indexOf(" ") != -1)
        {
            std::cout << "Invalid QUIT!" << std::endl;
            return;
        }
        emit receivedQuit(QHostAddress(message));
    }
    else if (QString::compare(command, CMD::GET) == 0)
    {
        std::cout << "Received 'GET' from " << address.toString().toStdString() << std::endl;
        emit receivedGet(address);
    }
    else if (QString::compare(command, CMD::KEEPALIVE) == 0)
    {
        std::cout << "Received 'KEEPALIVE' from " << address.toString().toStdString() << std::endl;
        emit receivedKeepalive(address);
    }
    else
    {
        std::cout << "Recieved unknown message '" << message.toStdString() << "'" << std::endl;
        emit receivedUnknownMessage(QString("Received message \"%1\" from %2").arg(message, address.toString()));
    }
}