Beispiel #1
0
bool HTTP4CLIProtocol::EnqueueForOutbound() {
	//1. Empty our local buffer
	_localOutputBuffer.IgnoreAll();

	//2. Get the HTTP protocol
	InboundHTTPProtocol *pHTTP = (InboundHTTPProtocol *) GetFarProtocol();

	//3. Prepare the HTTP headers
	//pHTTP->SetOutboundHeader(HTTP_HEADERS_CONTENT_TYPE, "application/json");
	pHTTP->SetOutboundHeader(HTTP_HEADERS_CONTENT_TYPE, "text/plain");
	pHTTP->SetOutboundHeader(HTTP_HEADERS_ACCESS_ORIGIN, "*");


	//4. Get the buffer from PT_INBOUND_JSONCLI
	IOBuffer *pBuffer = GetNearProtocol()->GetOutputBuffer();
	if (pBuffer == NULL)
		return true;

	//5. Put the data inside the local buffer and empty the buffer from
	//the PT_INBOUND_JSONCLI
	_localOutputBuffer.ReadFromBuffer(GETIBPOINTER(*pBuffer),
			GETAVAILABLEBYTESCOUNT(*pBuffer));
	pBuffer->IgnoreAll();

	//6. Trigger EnqueueForOutbound down the stack
	return pHTTP->EnqueueForOutbound();
}
Beispiel #2
0
bool EchoProtocol::SignalInputData(IOBuffer &buffer) {
	//PREAMBLE
	//First, check the transport type. If it is a 
	//http transport, wait for it to finish the request
	//before doing stuff with it. This is not mandatory.
	//We can start consume the buffer right away, but since
	//we do a echo protocol, let's keep the things simple
	//If the transport is direct TCP, we will echo back only after
	//getting a new line character.
	//I will try to keep the things extremely simple in this function
	//sacrificing "user input sanitize" for simplicity. Anyway, I think
	//we all know how an echo protocol should behave...

	//1. Check and see if the protocol is HTTP or not
	if (GetFarProtocol()->GetType() == PT_INBOUND_HTTP) {
		//2. This has HTTP protocol as carrier. Get it and
		//wait for it to complete
		InboundHTTPProtocol *pHTTP = (InboundHTTPProtocol *) GetFarProtocol();
		if (!pHTTP->TransferCompleted()) {
			FINEST("HTTP transfer not completed yet");
			return true;
		}

		//3. Ok, it is complete. Get the data and put it inside the output buffer
		//Actually, we are going to add the string "ECHO " first, and after that
		//the actual data. Just for fun...
		_outputBuffer.ReadFromString("ECHO ");
		_outputBuffer.ReadFromBuffer(
				GETIBPOINTER(buffer),
				GETAVAILABLEBYTESCOUNT(buffer));

		//3.1. Let's also dump the complete HTTP request. You might want to pick
		//up a thing or 2 from it. Just for fun...
		FINEST("HTTP request:\n%s", STR(pHTTP->GetHeaders().ToString()));

		//4. Ignore the input buffer now.
		buffer.IgnoreAll();

		//5. Add some fancy mime type and some custom HTTP headers... Just for fun...
		pHTTP->SetOutboundHeader(HTTP_HEADERS_CONTENT_TYPE, "text/plain");
		pHTTP->SetOutboundHeader("My-fancy-http-header", "aloha from C++ RTMP Server");

		//6. We are done. Enqueue the stack for outbound I/O
		return EnqueueForOutbound();
	} else {
		//7. Get the data inside a string
		string data = string((const char *) GETIBPOINTER(buffer), GETAVAILABLEBYTESCOUNT(buffer));

		//8. extremely minimal and dangerous test to see if we have a new-line,
		//but for the sake of simplicity, I'm just going to keep it like that
		if (data.length() == 0 || data[data.length() - 1] != '\n') {
			FINEST("Not enough data. So far I have %s. Wait for more...", STR(data));
			return true;
		}

		//9. Ok, it is complete. Get the data and put it inside the output buffer
		//Actually, we are going to add the string "ECHO " first, and after that
		//the actual data. Just for fun...
		_outputBuffer.ReadFromString("ECHO ");
		_outputBuffer.ReadFromBuffer(
				GETIBPOINTER(buffer),
				GETAVAILABLEBYTESCOUNT(buffer));

		//10. Ignore the input buffer now.
		buffer.IgnoreAll();
		
		//11. for demonstration purposes, whenever I get the string "testHttpRequest"
		//I'm just going to do a request to "http://www.rtmpd.com" and print out
		//the page on console. This should illustrate how to use the outbound HTTP protocol
		HTTPDownloadProtocol::DoSimpleGETRequestWithSomePayload("http://www.rtmpd.com/resources","Some data.... Hello World!");
		

		//11. We are done. Enqueue the stack for outbound I/O
		return EnqueueForOutbound();
	}
}