예제 #1
0
//Parses the request
QTSS_Error RTSPRequest::Parse()
{
    StringParser parser(this->GetValue(qtssRTSPReqFullRequest));
    Assert(this->GetValue(qtssRTSPReqFullRequest)->Ptr != NULL);

    //parse status line.
    QTSS_Error error = ParseFirstLine(parser);

    //handle any errors that come up    
    if (error != QTSS_NoErr)
        return error;
        
    error = this->ParseHeaders(parser);
    if (error != QTSS_NoErr)
        return error;
    
    //Response headers should set themselves up to reflect what's in the request headers
    fResponseKeepAlive = fRequestKeepAlive;
    
    //Make sure that there was some path that was extracted from this request. If not, there is no way
    //we can process the request, so generate an error
    if (this->GetValue(qtssRTSPReqFilePath)->Len == 0)
        return QTSSModuleUtils::SendErrorResponse(this, qtssClientBadRequest, qtssMsgNoURLInRequest,this->GetValue(qtssRTSPReqFullRequest));
    
    return QTSS_NoErr;
}
예제 #2
0
bool BaseHTTPProtocol::ParseHeaders(IOBuffer& buffer) {
	//1. We have to have at least 4 bytes (double \r\n)
	if (GETAVAILABLEBYTESCOUNT(buffer) < 4) {
		return true;
	}

	//2. Detect the headers boundaries
	uint32_t headersSize = 0;
	bool markerFound = false;
	uint8_t *pBuffer = GETIBPOINTER(buffer);
	for (uint32_t i = 0; i <= GETAVAILABLEBYTESCOUNT(buffer) - 4; i++) {
		if ((pBuffer[i] == 0x0d)
				&& (pBuffer[i + 1] == 0x0a)
				&& (pBuffer[i + 2] == 0x0d)
				&& (pBuffer[i + 3] == 0x0a)) {
			markerFound = true;
			headersSize = i;
			break;
		}
		if (i >= HTTP_MAX_HEADERS_SIZE) {
			FATAL("Headers section too long");
			return false;
		}
	}

	//3. Are the boundaries correct?
	//Do we have enough data to parse the headers?
	if (headersSize == 0) {
		if (markerFound)
			return false;
		else
			return true;
	}

	//4. Get the raw headers and plit it into lines
	string rawHeaders = string((char *) GETIBPOINTER(buffer), headersSize);
	vector<string> lines;
	split(rawHeaders, "\r\n", lines);
	if (lines.size() == 0) {
		FATAL("Incorrect HTTP request");
		return false;
	}

	//4. Get the fisrt line and parse it. This is either a status code
	//for a previous request made by us, or the request that we just received
	if (!ParseFirstLine(lines[0], _headers[HTTP_FIRST_LINE])) {
		FATAL("Unable to parse the first line");
		return false;
	}

	//5. Consider the rest of the lines as key: value pairs and store them
	//0. Reset the headers
	_headers[HTTP_HEADERS].IsArray(false);
	for (uint32_t i = 1; i < lines.size(); i++) {
		string line = lines[i];
		string::size_type splitterPos = line.find(": ");

		if ((splitterPos == string::npos)
				|| (splitterPos == 0)
				|| (splitterPos == line.size() - 2)) {
			FATAL("Invalid header line");
			return false;
		}
		_headers[HTTP_HEADERS][line.substr(0, splitterPos)] = line.substr(splitterPos + 2, string::npos);
	}

	//6. default a transfer type to Content-Length: 0 if necessary
	if ((!_headers[HTTP_HEADERS].HasKey(HTTP_HEADERS_CONTENT_LENGTH, false)) &&
			(!_headers[HTTP_HEADERS].HasKey(HTTP_HEADERS_TRANSFER_ENCODING, false))) {
		_headers[HTTP_HEADERS][HTTP_HEADERS_CONTENT_LENGTH] = "0";
	}

	//7. read the transfer type and set this request or response flags
	if (_headers[HTTP_HEADERS].HasKey(HTTP_HEADERS_CONTENT_LENGTH, false)) {
		string contentLengthString = _headers[HTTP_HEADERS].GetValue(
				HTTP_HEADERS_CONTENT_LENGTH, false);
		replace(contentLengthString, " ", "");
		if (!isNumeric(contentLengthString)) {
			FATAL("Invalid HTTP headers:\n%s", STR(_headers.ToString()));
			return false;
		}
		_contentLength = atoi(STR(contentLengthString));
		_chunkedContent = false;
		_lastChunk = false;
	} else if (_headers[HTTP_HEADERS].HasKey(HTTP_HEADERS_TRANSFER_ENCODING, false)) {
		if (lowerCase(_headers[HTTP_HEADERS].GetValue(HTTP_HEADERS_TRANSFER_ENCODING, false)) !=
				lowerCase(HTTP_HEADERS_TRANSFER_ENCODING_CHUNKED)) {
			FATAL("The only supported %s is %s",
					HTTP_HEADERS_TRANSFER_ENCODING,
					HTTP_HEADERS_TRANSFER_ENCODING_CHUNKED);
			return false;
		}
		_chunkedContent = true;
		_lastChunk = false;
		_contentLength = 0;
	}

	//7. Advance the state and ignore the headers part from the buffer
	_state = HTTP_STATE_PAYLOAD;
	buffer.Ignore(headersSize + 4);

	return Authenticate();
}
int DataHttpParser::ParseData(const char* buffer, int len) {
	int ret = 0;

	int recvLen = (len < MAX_BUFFER_LEN - mIndex)?len:MAX_BUFFER_LEN - mIndex;
	if( recvLen > 0 ) {
		memcpy(mBuffer + mIndex, buffer, recvLen);
		mIndex += recvLen;
		mBuffer[mIndex + 1] = '\0';

	} else {
		return -1;
	}

//	printf("# DataHttpParser::ParseData( mbReceiveHeaderFinish : %s ) \n", mbReceiveHeaderFinish?"true":"false");

	if( !mbReceiveHeaderFinish ) {
		string headers = mBuffer;
		int headerIndex = 0;
		string::size_type pos = headers.find("\r\n\r\n");

		if( pos != string::npos ) {
			mbReceiveHeaderFinish = true;
			headerIndex = pos + strlen("\r\n\r\n");

			// Read first line
			pos = headers.find("\r\n");
			if( pos != string::npos ) {
				string firstLine = headers.substr(0, pos);
				char firstLineBuff[4096];
				strcpy(firstLineBuff, firstLine.c_str());

//				LogManager::GetLogManager()->Log(
//						LOG_MSG,
//						"DataHttpParser::ParseData( "
//						"headerIndex : %d, "
//						"headers : %s "
//						")",
//						headerIndex,
//						headers.c_str()
//						);
//				headers = headers.substr(pos + strlen("\r\n"), headers.length() - (pos + strlen("\r\n")));
				if( ParseFirstLine(firstLineBuff) ) {
					string header;
					string::size_type posStart, posEnd;
					string::size_type posPre = pos + strlen("\r\n");

					// Get all headers
					pos = headers.find("\r\n", posPre);
					while( pos != string::npos ) {
						header = headers.substr(posPre, pos - posPre);
						LogManager::GetLogManager()->Log(
								LOG_MSG,
								"DataHttpParser::ParseData( "
								"header : %s "
								")",
								header.c_str()
								);

						if( header.length() > 0 ) {
							// Get Host
							posStart = header.find("Host: ");
							if( posStart != string::npos ) {
								mHost = header.substr(posStart + strlen("Host: "), header.length() - (posStart + strlen("Host:")));
//								printf("# DataHttpParser::ParseData( Host: %s ) \n", mHost.c_str());
							}

							// Get Content-Length
							posStart = header.find("Content-Length: ");
							if( posStart != string::npos ) {
								string contentLength = header.substr(posStart + strlen("Content-Length: "), header.length() - (posStart + strlen("Content-Length:")));
//								LogManager::GetLogManager()->Log(
//										LOG_MSG,
//										"DataHttpParser::ParseData( "
//										"Content-Length: %s "
//										")",
//										contentLength.c_str()
//										);
								miContentLength = atoi(contentLength.c_str());
							}

							mHeaders.push_back(header);
						}

						posPre = pos + strlen("\r\n");
						pos = headers.find("\r\n", posPre);
					}
				}
			}

			if( mIndex >= headerIndex ) {
				mIndex -= headerIndex;
				if( mIndex > 0 ) {
					memcpy(mBuffer, mBuffer + headerIndex, mIndex);
				}
				mBuffer[mIndex] = '\0';
			}

		}
	}

	// Receive all body
	if( mbReceiveHeaderFinish ) {
		LogManager::GetLogManager()->Log(
				LOG_MSG,
				"DataHttpParser::ParseData( "
				"miContentLength : %d, "
				"mIndex : %d "
				")",
				miContentLength,
				mIndex
				);

		if( miContentLength == -1 || (mIndex == miContentLength) ) {
			ret = 1;
		}
	}

	return ret;
}
int WSClientParser::ParseData(char* buffer, int len) {
	switch_log_printf(
			SWITCH_CHANNEL_UUID_LOG(this->uuid),
			SWITCH_LOG_DEBUG,
			"WSClientParser::ParseData( "
			"this : %p, "
			"len : %d "
//			"buffer : \n%s\n"
			") \n",
			this,
			len
//			buffer
			);

	int ret = 0;
//	DataParser::ParseData(NULL, len);

	Lock();
	switch( mState ) {
	case WSClientState_UnKnow:{
		int lineNumber = 0;

		char line[HTTP_URL_MAX_PATH];
		int lineLen = 0;

		const char* header = buffer;
		const char* sepHeader = strstr(buffer, HTTP_HEADER_SEP);
		const char* sep = NULL;
		if( sepHeader ) {
			switch_log_printf(
					SWITCH_CHANNEL_UUID_LOG(this->uuid),
					SWITCH_LOG_INFO,
					"WSClientParser::ParseData( "
					"[HandShake], "
					"this : %p, "
					"len : %d, "
					"buffer : \n%s\n"
					") \n",
					this,
					len,
					buffer
					);

			// Parse HTTP header separator
			ret = sepHeader - buffer + strlen(HTTP_HEADER_SEP);

			// Parse HTTP header line separator
			while( true ) {
				if( (sep = strstr(header, HTTP_LINE_SEP)) && sep != sepHeader ) {
					lineLen = sep - header;
					if( lineLen < sizeof(line) - 1 ) {
						memcpy(line, header, lineLen);
						line[lineLen] = '\0';

						if( lineNumber == 0 ) {
							// Get First Line
							if( !ParseFirstLine(line) ) {
								break;
							}
						} else {
							ParseHeader(line);
							if( CheckHandShake() ) {
								break;
							}
						}
					}

					header += lineLen + strlen(HTTP_LINE_SEP);

					lineNumber++;

				} else {
					break;
				}
			}
		}

		if( mState == WSClientState_Handshake ) {
			if( mpCallback ) {
				mpCallback->OnWSClientParserHandshake(this);
			}
		} else {
			if( mpCallback ) {
				mpCallback->OnWSClientDisconected(this);
			}
		}
	}break;
	case WSClientState_Handshake:{

	}
	case WSClientState_Data:{
//		char temp[4096] = {0};
//		char *p = temp;
//		unsigned char c;
//		for(int i = 0; i< len; i++) {
//			c = buffer[i];
//			sprintf(p, "%c%c,", hex[c >> 4], hex[c & (16 - 1)]);
//			p += 3;
//		}
//		switch_log_printf(
//				SWITCH_CHANNEL_UUID_LOG(this->uuid),
//				SWITCH_LOG_INFO,
//				"WSClientParser::ParseData( "
//				"parser : %p, "
//				"hex : \n%s\n"
//				") \n",
//				this,
//				temp
//				);

		WSPacket* packet = (WSPacket *)buffer;

//		switch_log_printf(
//				SWITCH_CHANNEL_UUID_LOG(this->uuid),
//				SWITCH_LOG_INFO,
//				"WSClientParser::ParseData( "
//				"parser : %p, "
//				"packet->Fin : %s, "
//				"packet->RSV : %u, "
//				"packet->Opcode : %u, "
//				"packet->Mask : %s, "
//				"packet->Playload Length : %u, "
//				"packet->Extend Playload Length : %llu, "
//				"packet->Mask Key : %s "
//				") \n",
//				this,
//				packet->baseHeader.IsFin()?"true":"false",
//				packet->baseHeader.GetRSVType(),
//				packet->baseHeader.GetOpcode(),
//				packet->baseHeader.IsMask()?"true":"false",
//				packet->baseHeader.GetPlayloadLength(),
//				packet->GetPlayloadLength(),
//				packet->GetMaskKey()
//				);

		const char* data = (const char*)packet->GetData();//buffer;//packet->GetData();
		int playloadLength = packet->GetPlayloadLength();//len;//packet->GetPlayloadLength();
		ret = packet->GetHeaderLength() + playloadLength;

		if( mpCallback) {
			mpCallback->OnWSClientParserData(this, data, playloadLength);
		}

	}break;
	case WSClientState_Disconnected:{
		ret = len;
	}break;
	default:{
		break;
	}
	}

	Unlock();

	return ret;
}
예제 #5
0
//-------------------------------------------------------------------------
bool CWebserverRequest::ParseRequest()
{
int ende;
	if(rawbuffer_len > 0 )
	{
		if((ende = rawbuffer.find_first_of('\n')) == 0)
		{
			aprintf("ParseRequest: End of line not found\n");
			Send500Error();
			return false;
		}
		string zeile1 = rawbuffer.substr(0,ende-1);

		if(ParseFirstLine(zeile1))
		{
			unsigned int i;
			for(i = 0; ((rawbuffer[i] != '\n') || (rawbuffer[i+2] != '\n')) && (i < rawbuffer.length());i++);
			int headerende = i;
//			dprintf("headerende: %d buffer_len: %d\n",headerende,rawbuffer_len);
			if(headerende == 0)
			{
				aprintf("ParseRequest: no headers found\n");
				Send500Error();
				return false;
			}
			string header = rawbuffer.substr(ende+1,headerende - ende - 2);
			ParseHeader(header);
			Host = HeaderList["Host"];
			if(Method == M_POST) // TODO: Und testen ob content = formdata
			{				

				string t = "multipart/form-data; boundary=";
				if(HeaderList["Content-Type"].compare(0,t.length(),t) == 0)
				{
					SocketWriteLn("Sorry, momentan broken\n");
					/*Boundary = "--" + HeaderList["Content-Type"].substr(t.length(),HeaderList["Content-Type"].length() - t.length());
					dprintf("Boundary: '%s'\n",Boundary.c_str());
					if((headerende + 3) < rawbuffer_len)
						ParseBoundaries(rawbuffer.substr(headerende + 3,rawbuffer_len - (headerende + 3)));
					HandleUpload();*/
				}			
				else if(HeaderList["Content-Type"].compare("application/x-www-form-urlencoded") == 0)
				{
					dprintf("Form Daten in Parameter String\n");
					if((headerende + 3) < rawbuffer_len)
					{
						string params = rawbuffer.substr(headerende + 3,rawbuffer_len - (headerende + 3));
						if(params[params.length()-1] == '\n')
							params.substr(0,params.length() -2);
						ParseParams(params);
					}
				}
				
				dprintf("Method Post !\n");
			}

/*
			if(Method == M_POST) // TODO: Und testen ob content = formdata
			{
				if( (ende + 3) < rawbuffer + rawbuffer_len)
				{
//					Parent->Debug("Post Parameter vorhanden\n");
					anfang = ende + 3;
					Param_String = string(anfang,rawbuffer + rawbuffer_len - anfang);
					dprintf("Post Param_String: %s\n",Param_String.c_str());
					ParseParams(Param_String);
				}
				if(HeaderList->GetIndex("Content-Type") != -1)
				{
					dprintf("Content-Type: %s\n",HeaderList->GetValue(HeaderList->GetIndex("Content-Type")));
					if(strcasecmp("application/x-www-form-urlencoded",HeaderList->GetValue(HeaderList->GetIndex("Content-Type"))) == 0)
						dprintf("Form Daten in Parameter String\n");
					if(strstr(HeaderList->GetValue(HeaderList->GetIndex("Content-Type")),"multipart/form-data") != 0)
					{
						char * boundary;
						boundary = strstr(HeaderList->GetValue(HeaderList->GetIndex("Content-Type")),"boundary=");
						if(boundary)
						{
							boundary += strlen("boundary=");

							dprintf("boundary : %s\n",boundary);
							Upload = new TUpload(this);
							Upload->Boundary = new TString(boundary);
							Boundary = new TString(boundary);
							dprintf("Form Daten in Parameter String und Datei upload\nBoundary: %ld\n",Boundary);
						}
					}					
				}
			}
*/
			return true;
		}
		else {
			SocketWrite("HTTP/1.0 501 Not implemented\r\n");
			SocketWrite("Content-Type: text/plain\r\n\r\n");
			SocketWrite("501 : Request-Method not implemented.\n");
			HttpStatus = 501;
//			dprintf("501 : Request-Method not implemented.\n");
			return false;
		}
	}
	return false;
}
예제 #6
0
파일: request.cpp 프로젝트: UkCvs/commando
bool CWebserverRequest::ParseRequest()
{
	int ende;

	if(rawbuffer_len > 0 )
	{
		if((ende = rawbuffer.find_first_of('\n')) == 0)
		{
			aprintf("ParseRequest: End of line not found\n");
			Send500Error();
			return false;
		}
		std::string zeile1 = rawbuffer.substr(0,ende-1);

		if(ParseFirstLine(zeile1))
		{
			unsigned int i;
			for(i = 0; ((rawbuffer[i] != '\n') || (rawbuffer[i+2] != '\n')) && (i < rawbuffer.length());i++);
			int headerende = i;
//			dprintf("headerende: %d buffer_len: %d\n",headerende,rawbuffer_len);
			if(headerende == 0)
			{
				aprintf("ParseRequest: no headers found\n");
				Send500Error();
				return false;
			}
			std::string header = rawbuffer.substr(ende+1,headerende - ende - 2);
			ParseHeader(header);
			Host = HeaderList["Host"];
			if(Method == M_POST) // TODO: Und testen ob content = formdata
			{
				std::string t = "multipart/form-data; boundary=";
				if(HeaderList["Content-Type"].compare(0,t.length(),t) == 0)
				{
//					SocketWriteLn("Sorry, momentan broken\n");
					Boundary = "--" + HeaderList["Content-Type"].substr(t.length(),HeaderList["Content-Type"].length() - t.length());
					aprintf("Boundary: '%s'\n",Boundary.c_str());
//					if((headerende + 3) < rawbuffer_len)
//						ParseBoundaries(rawbuffer.substr(headerende + 3,rawbuffer_len - (headerende + 3)));
					HandleUpload();
				}
				else if(HeaderList["Content-Type"].compare("application/x-www-form-urlencoded") == 0)
				{
//					dprintf("Form Daten in Parameter String\n");
					if((headerende + 3) < rawbuffer_len)
					{
						std::string params = rawbuffer.substr(headerende + 3,rawbuffer_len - (headerende + 3));
						if(params[params.length()-1] == '\n')
							params.substr(0,params.length() -2);
						ParseParams(params);
					}
				}

				dprintf("Method Post !\n");
			}


			return true;
		}
		else {
			SocketWrite("HTTP/1.0 501 Not implemented\r\n");
			SocketWrite("Content-Type: text/plain\r\n\r\n");
			SocketWrite("501 : Request-Method not implemented.\n");
			HttpStatus = 501;
			aprintf("501 : Request-Method not implemented.\n");
			return false;
		}
	}
	else
	{
		aprintf("rawbuffer_len = %ld\n",rawbuffer_len);
		return false;
	}
}