예제 #1
0
/**
 * \brief       Parse a buffer and generate a xPL_Message
 * \details	  Line based xPL parser
 * \param    _xPLMessage    the result xPL message
 * \param    _message         the buffer
 */
void xPL::Parse(xPL_Message* _xPLMessage, char* _buffer)
{
    int len = strlen(_buffer);

    byte j=0;
    byte line=0;
    int result=0;
    char lineBuffer[XPL_LINE_MESSAGE_BUFFER_MAX+1];

    // read each character of the message
    for(byte i = 0; i < len; i++)
    {
        // load byte by byte in 'line' buffer, until '\n' is detected
        if(_buffer[i] == XPL_END_OF_LINE) // is it a linefeed (ASCII: 10 decimal)
        {
            ++line;
            lineBuffer[j]='\0';	// add the end of string id

            if(line <= XPL_OPEN_SCHEMA)
            {
                // first part: header and schema determination
            	// we analyse the line, function of the line number in the xpl message
                result = AnalyseHeaderLine(_xPLMessage, lineBuffer ,line);
            }

            if(line > XPL_OPEN_SCHEMA)
            {
                // second part: command line
            	// we analyse the specific command line, function of the line number in the xpl message
                result = AnalyseCommandLine(_xPLMessage, lineBuffer, line-9, j);

                if(result == _xPLMessage->command_count+1)
                    break;
            }

            if (result < 0) break;

            j = 0; // reset the buffer pointer
            clearStr(lineBuffer); // clear the buffer
        }
        else
        {
            // next character
        	lineBuffer[j++] = _buffer[i];
        }
    }
}
예제 #2
0
//Split header to tokens
int HTTPHandler::AnalyseHeader( string &linesT )
{

    //Delete header tokens
    tokens.clear();

    string::size_type lastposition = 0;

    //Do "Tolerant Applications" - RFC 1945 - Hypertext Transfer Protocol -- HTTP/1.0
    while ( (lastposition = linesT.find( "\r", lastposition )) != string::npos )
    {
        linesT.replace( lastposition, 1, "" );
    }

    lastposition = 0;

    string::size_type length = linesT.length();
    string::size_type position, positiontmp;
    
    if ( (position = linesT.find( "\n", 0 )) == string::npos )
    {
        return -201;
    }

    int ret;
    string tempToken, headerbase;

    //Read first line with AnalyseFirstHeaderLine
    bool First = true;

    //Loop through headers
    while ( position != string::npos && lastposition != length )
    {
        tempToken = linesT.substr( lastposition, position - lastposition );

        if ( (lastposition = tempToken.find_last_not_of("\t ")) != string::npos )
        {
            tempToken = tempToken.substr( 0, lastposition + 1 );

            if ( First == true )
            {
                //Analyse request header
                if ( (ret = AnalyseFirstHeaderLine( tempToken )) < 0 )
                {
                    return ret;
                }
                First = false;
            }
            else
            {
                if ( (positiontmp = tempToken.find(":")) != string::npos )
                {
                    headerbase = tempToken.substr(0, positiontmp + 1);
                    headerbase += " ";

                    //Make sure we have "Header:<SPACE>value"
                    if ( (positiontmp = tempToken.find_first_not_of(" ", positiontmp + 1)) != string::npos )
                    {
                        tempToken = headerbase + tempToken.substr(positiontmp);

                        if ( (ret = AnalyseHeaderLine( tempToken )) < 0 )
                        {
                            return ret;
                        }
                    }

                }
            }

            //Add header to send queue
            tokens.push_back( tempToken );
        }

        lastposition = position + 1;
        position = linesT.find( "\n", lastposition );
    }

    return 0;
}