示例#1
0
int HttpRequestPacket::decode_get( string& sHttpHead )
{

		//LOG4CPLUS_DEBUG(logger, "http head: " << sHttpHead);
	//get first line, GET /xx?xxx
	string::size_type requestPos = sHttpHead.find("\r\n");
	if ( requestPos == string::npos )
	{   // unfinished recv data, need more
		MYLOG_ERROR( "error http request : %s" , sHttpHead.c_str());
		return -1;
	}
    
	//ready to parse param
	string sRequestLine = sHttpHead.substr(0, requestPos);
	string::size_type pos1 = sRequestLine.find("/");
	if ( pos1 == string::npos )
	{
			MYLOG_DEBUG(  "can't find /");
			return -1;
	}
	string::size_type pos2 = sRequestLine.find(" ", pos1);//find space
	if ( pos2 == string::npos )
	{
			MYLOG_DEBUG(  "can't find space");
			return -1;
	}
	m_str_GetRequest = sRequestLine.substr(pos1, pos2-pos1);
	ParseParam(m_str_GetRequest);
	//LOG4CPLUS_DEBUG(logger, "m_str_GetRequest: " << m_str_GetRequest);
	
	ParseHead(sHttpHead);

	/*std::map<std::string, std::string>::iterator iter;
	for (iter = mapHead.begin(); iter!= mapHead.end(); iter++)
	{
		LOG4CPLUS_DEBUG(logger, "key="<<iter->first<<"  value="<<iter->second);
	}*/

	
	//ready to parse cookie	
	string sCookie = GetLine("COOKIE", sHttpHead);
	ParseCookie(sCookie);

	return 0;
}
示例#2
0
nsresult
nsHttpTransaction::ProcessData(char *buf, PRUint32 count, PRUint32 *countRead)
{
    nsresult rv;

    LOG(("nsHttpTransaction::ProcessData [this=%x count=%u]\n", this, count));

    *countRead = 0;

    // we may not have read all of the headers yet...
    if (!mHaveAllHeaders) {
        PRUint32 bytesConsumed = 0;

        rv = ParseHead(buf, count, &bytesConsumed);
        if (NS_FAILED(rv)) return rv;

        count -= bytesConsumed;

        // if buf has some content in it, shift bytes to top of buf.
        if (count && bytesConsumed)
            memmove(buf, buf + bytesConsumed, count);

        // report the completed response header
        if (mActivityDistributor && mResponseHead && mHaveAllHeaders) {
            nsCAutoString completeResponseHeaders;
            mResponseHead->Flatten(completeResponseHeaders, PR_FALSE);
            completeResponseHeaders.AppendLiteral("\r\n");
            mActivityDistributor->ObserveActivity(
                mChannel,
                NS_HTTP_ACTIVITY_TYPE_HTTP_TRANSACTION,
                NS_HTTP_ACTIVITY_SUBTYPE_RESPONSE_HEADER,
                PR_Now(), LL_ZERO,
                completeResponseHeaders);
        }
    }

    // even though count may be 0, we still want to call HandleContent
    // so it can complete the transaction if this is a "no-content" response.
    if (mHaveAllHeaders) {
        PRUint32 countRemaining = 0;
        //
        // buf layout:
        // 
        // +--------------------------------------+----------------+-----+
        // |              countRead               | countRemaining |     |
        // +--------------------------------------+----------------+-----+
        //
        // count          : bytes read from the socket
        // countRead      : bytes corresponding to this transaction
        // countRemaining : bytes corresponding to next pipelined transaction
        //
        // NOTE:
        // count > countRead + countRemaining <==> chunked transfer encoding
        //
        rv = HandleContent(buf, count, countRead, &countRemaining);
        if (NS_FAILED(rv)) return rv;
        // we may have read more than our share, in which case we must give
        // the excess bytes back to the connection
        if (mResponseIsComplete && countRemaining) {
            NS_ASSERTION(mConnection, "no connection");
            mConnection->PushBack(buf + *countRead, countRemaining);
        }
    }

    return NS_OK;
}
示例#3
0
void
nsHttpTransaction::Close(nsresult reason)
{
    LOG(("nsHttpTransaction::Close [this=%x reason=%x]\n", this, reason));

    NS_ASSERTION(PR_GetCurrentThread() == gSocketThread, "wrong thread");

    if (mClosed) {
        LOG(("  already closed\n"));
        return;
    }

    if (mActivityDistributor) {
        // report the reponse is complete if not already reported
        if (!mResponseIsComplete)
            mActivityDistributor->ObserveActivity(
                mChannel,
                NS_HTTP_ACTIVITY_TYPE_HTTP_TRANSACTION,
                NS_HTTP_ACTIVITY_SUBTYPE_RESPONSE_COMPLETE,
                PR_Now(),
                static_cast<PRUint64>(mContentRead.mValue),
                EmptyCString());

        // report that this transaction is closing
        mActivityDistributor->ObserveActivity(
            mChannel,
            NS_HTTP_ACTIVITY_TYPE_HTTP_TRANSACTION,
            NS_HTTP_ACTIVITY_SUBTYPE_TRANSACTION_CLOSE,
            PR_Now(), LL_ZERO, EmptyCString());
    }

    // we must no longer reference the connection!  find out if the 
    // connection was being reused before letting it go.
    PRBool connReused = PR_FALSE;
    if (mConnection)
        connReused = mConnection->IsReused();
    mConnected = PR_FALSE;

    //
    // if the connection was reset or closed before we wrote any part of the
    // request or if we wrote the request but didn't receive any part of the
    // response and the connection was being reused, then we can (and really
    // should) assume that we wrote to a stale connection and we must therefore
    // repeat the request over a new connection.
    //
    // NOTE: the conditions under which we will automatically retry the HTTP
    // request have to be carefully selected to avoid duplication of the
    // request from the point-of-view of the server.  such duplication could
    // have dire consequences including repeated purchases, etc.
    //
    // NOTE: because of the way SSL proxy CONNECT is implemented, it is
    // possible that the transaction may have received data without having
    // sent any data.  for this reason, mSendData == FALSE does not imply
    // mReceivedData == FALSE.  (see bug 203057 for more info.)
    //
    if (reason == NS_ERROR_NET_RESET || reason == NS_OK) {
        if (!mReceivedData && (!mSentData || connReused)) {
            // if restarting fails, then we must proceed to close the pipe,
            // which will notify the channel that the transaction failed.
            if (NS_SUCCEEDED(Restart()))
                return;
        }
    }

    PRBool relConn = PR_TRUE;
    if (NS_SUCCEEDED(reason)) {
        // the server has not sent the final \r\n terminating the header
        // section, and there may still be a header line unparsed.  let's make
        // sure we parse the remaining header line, and then hopefully, the
        // response will be usable (see bug 88792).  related to that, we may
        // also have an empty response containing no headers.  we should treat
        // that as an empty HTTP/0.9 response (see bug 300613).
        if (!mHaveAllHeaders) {
            char data = '\n';
            PRUint32 unused;
            ParseHead(&data, 1, &unused);
        }

        // honor the sticky connection flag...
        if (mCaps & NS_HTTP_STICKY_CONNECTION)
            relConn = PR_FALSE;
    }
    if (relConn && mConnection)
        NS_RELEASE(mConnection);

    mStatus = reason;
    mTransactionDone = PR_TRUE; // forcibly flag the transaction as complete
    mClosed = PR_TRUE;

    // release some resources that we no longer need
    mRequestStream = nsnull;
    mReqHeaderBuf.Truncate();
    mLineBuf.Truncate();
    if (mChunkedDecoder) {
        delete mChunkedDecoder;
        mChunkedDecoder = nsnull;
    }

    // closing this pipe triggers the channel's OnStopRequest method.
    mPipeOut->CloseWithStatus(reason);
}
示例#4
0
int HttpRequestPacket::decode_post( string& sHttpHead )
{
	string::size_type requestPos = sHttpHead.find("\r\n");
	if ( requestPos == string::npos )
	{   // unfinished recv data, need more
		MYLOG_ERROR("error http request :%s " ,sHttpHead.c_str());
		return -1;
	}
    
	//ready to parse param
	string sRequestLine = sHttpHead.substr(0, requestPos);
	string::size_type pos1 = sRequestLine.find("/");
	if ( pos1 == string::npos )
	{
			MYLOG_DEBUG( "can't find /");
			return -1;
	}
	string::size_type pos2 = sRequestLine.find(" ", pos1);//find space
	if ( pos2 == string::npos )
	{
			MYLOG_DEBUG(  "can't find space");
			return -1;
	}
	m_str_GetRequest = sRequestLine.substr(pos1, pos2-pos1);
	ParseParam(m_str_GetRequest);
	//LOG4CPLUS_DEBUG(logger, "m_str_GetRequest: " << m_str_GetRequest);
	
	ParseHead(sHttpHead);

	std::map<std::string, std::string>::iterator iter;
	iter = mapHead.find("CONTENT-LENGTH");
	if (iter == mapHead.end())
	{
		MYLOG_DEBUG( "failed to find CONTENT-LENGTH from headers");
		return -1;
	}

	std::string str_length = iter->second;
	boost::trim(str_length);
	int intlength = SDUtility::atouint32(str_length);
	if (sHttpHead.size() < intlength)
	{
		return -1;
	}
	std::string str_params;
	str_params = sHttpHead.substr(sHttpHead.length()-intlength, intlength);
	string2map(str_params, mapParam);
	return 0;
	
	 
	

	/*std::map<std::string, std::string>::iterator iter;
	for (iter = mapHead.begin(); iter!= mapHead.end(); iter++)
	{
		LOG4CPLUS_DEBUG(logger, "key="<<iter->first<<"  value="<<iter->second);
	}*/

	
	//ready to parse cookie	
	//string sCookie = GetLine("COOKIE", sHttpHead);
	//ParseCookie(sCookie);


}
示例#5
0
	void KopeteImportThread::ParseFile (const QString& filename)
	{
		QFile file (filename);
		if (!file.open (QIODevice::ReadOnly))
		{
			qWarning () << Q_FUNC_INFO
					<< "unable to open file"
					<< file.fileName ()
					<< file.errorString ();
			return;
		}

		QDomDocument doc;
		if (!doc.setContent (&file))
		{
			qWarning () << Q_FUNC_INFO
					<< "error parsing file"
					<< file.fileName ();
			return;
		}

		int month = 0, year = 0;
		QString contact;
		QString ourContact;
		ParseHead (doc.documentElement (), month, year, contact, ourContact);
		if (contact.isEmpty ())
		{
			qWarning () << Q_FUNC_INFO
					<< "got empty contact for"
					<< file.fileName ();
			return;
		}

		QVariantList list;

		auto msg = doc.documentElement ().firstChildElement ("msg");
		while (!msg.isNull ())
		{
			const auto& rawTime = msg.attribute ("time").split (' ', QString::SkipEmptyParts);

			QVariantMap result;
			result ["EntryID"] = contact;
			result ["DateTime"] = QDateTime (QDate (year, month, rawTime.value (0).toInt ()),
						QTime::fromString (rawTime.value (1), "h:m:s"));
			result ["Direction"] = msg.attribute ("in") == "0" ? "in" : "out";
			result ["Body"] = msg.text ();
			result ["MessageType"] = "chat";

			list << result;

			msg = msg.nextSiblingElement ("msg");
		}

		if (list.isEmpty ())
			return;

		Entity e;
		e.Additional_ ["History"] = list;
		e.Mime_ = "x-leechcraft/im-history-import";
		e.Additional_ ["AccountName"] = ourContact;
		e.Additional_ ["AccountID"] = ourContact;
		e.Parameters_ = OnlyHandle | FromUserInitiated;
		Proxy_->GetEntityManager ()->HandleEntity (e);
	}