Exemplo n.º 1
0
// ------------------------------------------------------------------------
int FskHeaderGenerateOutputBlob(char *blob, int blobLength, Boolean withTerminator, FskHeaders *headers)
{
	FskHeaderIterator iter;
	int blobLoc = 0, amt;

	iter = FskHeaderIteratorNew(headers);
	if (blob) {
		while ((amt = FskHeaderGenerateOutputLine(iter, &blob[blobLoc], blobLength - blobLoc)) > 0) {
			blobLoc += amt;
			iter = FskHeaderIteratorNext(iter);
		}
		if (withTerminator)
			FskStrCat(blob, "\r\n");
	}
	else {
		while ((amt = FskHeaderGenerateOutputLine(iter, NULL, blobLength - blobLoc)) > 0) {
			blobLoc += amt;
			iter = FskHeaderIteratorNext(iter);
		}
	}
	if (withTerminator)
		blobLoc += 2;

	FskHeaderIteratorDispose(iter);
	return blobLoc;
}
Exemplo n.º 2
0
static Boolean KprWebSocketEndpointValidateResponse(KprWebSocketEndpoint self, FskHeaders *response)
{
	FskErr err;
	char *value;
	char *encoded = NULL;
	Boolean result = false;

	{
		FskHeaderIterator iter = FskHeaderIteratorNew(response);
		FskDebugStr("HANDSHAKE: response headers\n");
		while (iter) {
			FskDebugStr(">  %s: %s\n", iter->name, iter->value);
			iter = FskHeaderIteratorNext(iter);
		}
		FskHeaderIteratorDispose(iter);
	}
	
	if (response->responseCode != 101) return false;
	if (!KprWebSocketEndpointCheckHeaderValue(response, "Upgrade", "websocket")) return false;
	if (!KprWebSocketEndpointCheckHeaderValue(response, "Connection", "Upgrade")) return false;
	
	value = FskHeaderFind("Sec-WebSocket-Accept", response);
	if (!value) return false;

	bailIfError(KprWebSocketCalculateHash(self->key, &encoded));

	result = (FskStrCompare(value, encoded) == 0);
bail:
	if (encoded) FskMemPtrDispose(encoded);
	return result;
}