void DHC_OnSendOK(ILibWebClient_StateObject sender, void *user1, void *user2)
{
	struct DHC_Data *data = (struct DHC_Data*)user2;
	int ok = 0;
	void *Abort = NULL;

	sem_wait(&(data->TransferStatus->syncLock));
	if(data->TransferStatus->Reserved1!=0)
	{
		data->TransferStatus->Reserved2 = 1;
		data->TransferStatus->Reserved3 = data;
	}
	else if(data->TransferStatus->Reserved4==0)
	{
		ok = 1;
	}
	if(data->TransferStatus->Reserved4!=0 && data->TransferStatus->RequestToken!=NULL)
	{
		//
		// Abort
		//
		Abort = data->TransferStatus->RequestToken;
		data->TransferStatus->RequestToken = NULL;
	}
	sem_post(&(data->TransferStatus->syncLock));

	if(Abort==NULL && ok && data->f!=NULL && data->GotContinue!=0)
	{
		ILibThreadPool_QueueUserWorkItem(data->pool,data,&DHC_Pool);
	}
	else if(Abort!=NULL)
	{
		ILibWebClient_CancelRequest(Abort);
	}
}
void DH_SendOK(struct ILibWebServer_Session *session)
{
    struct DH_Data *data = (struct DH_Data*)session->User3;

    session->OnSendOK = NULL;
    ILibThreadPool_QueueUserWorkItem(data->pool,data,&DH_Pool);
}
void DHC_OnResponse(ILibWebClient_StateObject WebStateObject,int InterruptFlag,struct packetheader *header,char *bodyBuffer,int *beginPointer,int endPointer,int done,void *user1,void *user2,int *PAUSE)
{
	struct DHC_Data *data = (struct DHC_Data*)user2;
	
	if(done)
	{
		if(data->f!=NULL && header!=NULL && header->StatusCode==100)
		{
			data->GotContinue=1;
			ILibThreadPool_QueueUserWorkItem(data->pool,data,&DHC_Pool);
			return;
		}
	}
	
	if(!done)
	{
		return;
	}

	if(header!=NULL && header->StatusCode<200)
	{
		return;
	}

	//
	// Completed Sending the File
	//
	if(data->Callback!=NULL)
	{
		if(header!=NULL && InterruptFlag==0)
		{
			header->StatusData[header->StatusDataLength]=0;
			if(header->StatusCode==200)
			{
				data->Callback(data->TransferStatus, DHC_ERRORS_NONE, header->StatusCode, header->StatusData,data->user);
			}
			else
			{
				data->Callback(data->TransferStatus, DHC_ERRORS_HTTP, header->StatusCode, header->StatusData,data->user);
			}
		}
		else
		{
			data->Callback(data->token, DHC_ERRORS_CONNECTION_FAILED_OR_ABORTED, 0, NULL,data->user);
		}
	}

	if(data->f!=NULL)
	{
		fclose(data->f);
		data->f = NULL;
	}
	DH_DestroyTransferStatus(data->TransferStatus);
	free(data->buffer);
	free(data);
	ILibWebClient_ResetUserObjects(WebStateObject,NULL, NULL);
}
void DHS_OnReceive_SavePostToLocalFile(struct ILibWebServer_Session *sender, int InterruptFlag, struct packetheader *header, char *bodyBuffer, int *beginPointer, int endPointer, int done)
{
    struct DH_Data *data = (struct DH_Data*)sender->User3;
    ILibWebServer_Pause(sender);

    *beginPointer = endPointer;
    ILibWebServer_AddRef(sender);
    ILibThreadPool_QueueUserWorkItem(data->pool,data,&DH_Pool_SavePostToLocalFile);
}
Beispiel #5
0
/* Implementation */
void _StartPlayListProcessing(PlayListManager_S state)
{
    sem_wait(&state->FirstBlockFinished);
    sem_wait(&state->BlocksFinished);

    ILibThreadPool_QueueUserWorkItem(state->Parent->ThreadPool, (void*)state, (ILibThreadPool_Handler)&_ThreadCallback);

    sem_wait(&state->FirstBlockFinished);
    sem_post(&state->FirstBlockFinished);
}
void DH_Resume(DH_TransferStatus tstatus)
{
    int WhatToDo = 0;
    struct DH_Data* data = NULL;

    lock_wait(&(tstatus->syncLock));
    WhatToDo = tstatus->Reserved2;
    data = (struct DH_Data*)tstatus->Reserved3;
    data->TransferStatus->Reserved1=0;
    data->TransferStatus->Reserved2=0;
    data->TransferStatus->Reserved3=NULL;
    lock_post(&(tstatus->syncLock));

    switch(WhatToDo)
    {
        case 1:
            ILibThreadPool_QueueUserWorkItem(data->pool,data,&DH_Pool_SavePostToLocalFile);
            break;
        case 2:
            ILibThreadPool_QueueUserWorkItem(data->pool,data,&DH_Pool);
            break;
    }
}
void DHC_Resume(DH_TransferStatus tstatus)
{
	int paused = 0;
	int ok = 0;
	struct DHC_Data* data = NULL;
	void *token = NULL;
	ILibWebClient_StateObject WebState=NULL;

	sem_wait(&(tstatus->syncLock));
	paused = tstatus->Reserved2;
	tstatus->Reserved2 = 0;
	tstatus->Reserved1 = 0;
	token = tstatus->RequestToken;
	data = (struct DHC_Data*)tstatus->Reserved3;

	if(data!=NULL && data->f!=NULL && data->GotContinue!=0)
	{
		ok = 1;
	}
	if(data==NULL)
	{
		ok = 1;
	}
	sem_post(&(tstatus->syncLock));

	if(paused)
	{
		if(ok && data!=NULL)
		{
			ILibThreadPool_QueueUserWorkItem(data->pool,data,&DHC_Pool);
		}
		else if(ok && data==NULL)
		{
			WebState = ILibWebClient_GetStateObjectFromRequestToken(token);
			ILibWebClient_Resume(WebState);
		}
	}
}
void DH_RequestResponse(ILibWebClient_StateObject WebStateObject,int InterruptFlag,struct packetheader *header,char *bodyBuffer,int *beginPointer,int endPointer,int done,void *user1,void *user2,int *PAUSE)
{
	struct DHC_Data *data = (struct DHC_Data*)user1;
	enum DHC_Errors status;
	void *Abort = NULL;
	char *rangeResult;
	struct parser_result *pr;
	long total;
	char *contentFeatures;
	struct DLNAProtocolInfo *pi;

	ILibReaderWriterLock rwLock = NULL;
	struct packetheader *GetRequest = NULL;
	struct sockaddr_in dest;

	int needToIssueRange = 0;
	int needToIssueRealRequest = 0;
	int MustNotResume = 0;
	struct packetheader *req;
	char *temp;

	if(data==NULL)
	{
		//
		// We appeared to be in the wrong state, most likely received
		// some bad responses, so if we ignore it, we should recover.
		//
		return;
	}

	if((header==NULL && done!=0) || (header!=NULL && header->StatusCode!=200 && header->StatusCode!=206 && done!=0))
	{
		fclose(data->f);
		if(header==NULL)
		{
			status = DHC_ERRORS_CONNECTION_FAILED_OR_ABORTED;
		}
		else
		{
			status = DHC_ERRORS_HTTP;
		}

		if(data->Callback!=NULL)
		{
			if(header!=NULL)
			{
				header->StatusData[header->StatusDataLength]=0;
			}
			data->Callback(data->TransferStatus, status, header==NULL?0:header->StatusCode, header==NULL?NULL:header->StatusData,data->user);
		}
		DH_DestroyTransferStatus(data->TransferStatus);
		if(data->ActualPacket!=NULL)
		{
			ILibDestructPacket(data->ActualPacket);
		}
		free(data);
		ILibWebClient_ResetUserObjects(WebStateObject,NULL, NULL);
	}
	else if(header!=NULL && (header->StatusCode==200 || header->StatusCode==206))
	{
		if(data->ActualPacket!=NULL)
		{
			if(data->DoneProcessingContentFeatures==0)
			{
				data->DoneProcessingContentFeatures = 1;

				//
				// Determine if Range is supported, then do another Head Range Request
				//
				if(data->TransferStatus->Reserved4!=0 && data->TransferStatus->RequestToken!=NULL)
				{
					//
					// Abort... Nothing really to do, because at this point we already finished.
					// Only thing to do, is to not actually issue the request.
					//
					data->TransferStatus->RequestToken = NULL;
				}
				else
				{

					//
					// Transfer Mode wasn't specified
					//
					contentFeatures = ILibGetHeaderLine(header,"contentFeatures.dlna.org",24);
					if(contentFeatures!=NULL)
					{
						temp = ILibString_Cat("http-get:*:*:",13,contentFeatures,(int)strlen(contentFeatures));
						pi = DLNAProtocolInfo_Parse(temp,(int)strlen(temp));
						if(pi!=NULL)
						{
							if(pi->TM_B!=0)
							{
								DH_AddHeader_transferMode(data->ActualPacket,DH_TransferMode_Bulk);
								data->TransferMode = DH_TransferMode_Bulk;
							}
							else if(pi->TM_I!=0)
							{
								DH_AddHeader_transferMode(data->ActualPacket,DH_TransferMode_Interactive);
								data->TransferMode = DH_TransferMode_Interactive;
							}
							else if(pi->TM_S!=0)
							{
								DH_AddHeader_transferMode(data->ActualPacket,DH_TransferMode_Streaming);
								data->TransferMode = DH_TransferMode_Streaming;
							}
							needToIssueRange = pi->SupportsByteBasedSeek;
							DLNAProtocolInfo_Destruct(pi);
						}
						free(temp);
					}
					else
					{
						//
						// since the server didn't respond with a DLNA Content Features, then
						// most likely this is a plain Media Server, in which case, if it doesn't support range
						// it won't return an error, and it'll just ignore the header, so we can issue a range
						// request anyways.
						//
						needToIssueRange = 1;
					}
					if(needToIssueRange!=0)
					{
						//
						// Issue a Range Request
						//
						req = ILibCreateEmptyPacket();
						ILibSetVersion(req,"1.1",3);
						ILibAddHeaderLine(req,"Host",4,ILibGetHeaderLine(data->ActualPacket,"Host",4),(int)strlen(ILibGetHeaderLine(data->ActualPacket,"Host",4)));					
						ILibSetDirective(req,"HEAD",4,data->ActualPacket->DirectiveObj,data->ActualPacket->DirectiveObjLength);
						DH_AddHeader_Range(req,0,-1);

						memset(&dest,0,sizeof(struct sockaddr_in));
						dest.sin_family = AF_INET;
						dest.sin_addr.s_addr = header->Source->sin_addr.s_addr;
						dest.sin_port = header->Source->sin_port;

						ILibReaderWriterLock_WriteLock(data->rwLock);
						switch(data->TransferMode)
						{
							case DH_TransferMode_Bulk:
								ILibWebClient_SetQosForNextRequest(data->manager,ILibAsyncSocket_QOS_BACKGROUND);
								break;
							case DH_TransferMode_Streaming:
								ILibWebClient_SetQosForNextRequest(data->manager,ILibAsyncSocket_QOS_AUDIO_VIDEO);
								break;
						}
						data->TransferStatus->RequestToken = data->token = ILibWebClient_PipelineRequest(data->manager,&dest,req,&DH_RequestResponse,data,NULL);
						ILibReaderWriterLock_WriteUnLock(data->rwLock);
					}
					else
					{
						//
						// Range is not supported, so just continue with making the request.
						// However, before we do, since range isn't supported, we need to make
						// sure that we aren't trying to resume.
						MustNotResume = 1;
						needToIssueRealRequest = 1;
					}
				}
			}
			else
			{
				//
				// Process the Range Response
				//
				rangeResult = ILibGetHeaderLine(header,"content-range",13);
				if(rangeResult!=NULL)
				{
					pr = ILibParseString(rangeResult,0,(int)strlen(rangeResult),"/",1);
					total = atol(pr->LastResult->data);
					ILibDestructParserResults(pr);
					total -= data->StartPosition;
					sem_wait(&(data->TransferStatus->syncLock));
					data->TransferStatus->TotalBytesToBeReceived = total;
					sem_post(&(data->TransferStatus->syncLock));
				}
				else
				{
					//
					// We couldn't get a range result, so we need to make sure
					// we don't try to do a resume.
					//
					MustNotResume = 1;
				}

				//
				// Continue with actually making the content request
				//
				needToIssueRealRequest = 1;
			}

			if(needToIssueRealRequest!=0 && !(data->TransferStatus->Reserved4!=0 && data->TransferStatus->RequestToken!=NULL))
			{
				GetRequest = data->ActualPacket;
				data->ActualPacket = NULL;

				rwLock = (ILibReaderWriterLock)ILibWebClient_GetUser(data->manager);
				memset(&dest,0,sizeof(struct sockaddr_in));
	
				dest.sin_family = AF_INET;
				dest.sin_addr.s_addr = header->Source->sin_addr.s_addr;
				dest.sin_port = header->Source->sin_port;

				if(MustNotResume!=0)
				{
					//
					// Range isn't supported, so we must not resume
					//
					if(data->StartPosition!=0)
					{
						data->StartPosition = 0;
						fseek(data->f,0,SEEK_SET);
					}
				}
				else if(data->StartPosition!=0)
				{
					DH_AddHeader_Range(GetRequest,data->StartPosition,-1);
				}

				ILibReaderWriterLock_WriteLock(rwLock);
				switch(data->TransferMode)
				{
					case DH_TransferMode_Bulk:
						ILibWebClient_SetQosForNextRequest(data->manager,ILibAsyncSocket_QOS_BACKGROUND);
						break;
					case DH_TransferMode_Streaming:
						ILibWebClient_SetQosForNextRequest(data->manager,ILibAsyncSocket_QOS_AUDIO_VIDEO);
						break;
				}
				data->TransferStatus->RequestToken = data->token = ILibWebClient_PipelineRequest(data->manager,&dest,GetRequest,&DH_RequestResponse,data,NULL);
				ILibReaderWriterLock_WriteUnLock(rwLock);
			}

			*beginPointer = endPointer; // Don't really need to do this, because it should always be zero
			return;
		}


		data->buffer = bodyBuffer;
		data->bufferLength = endPointer;
		*beginPointer = endPointer;
		data->GotContinue = done;
		if(endPointer!=0 || done)
		{
			*PAUSE = 1;
		}
		if(data->token!=NULL)
		{
			data->webState = ILibWebClient_GetStateObjectFromRequestToken(data->token);
		}
		if(data->TransferStatus->Reserved4!=0 && data->TransferStatus->RequestToken!=NULL)
		{
			//
			// Abort
			//
			Abort = data->TransferStatus->RequestToken;
			data->TransferStatus->RequestToken = NULL;
			ILibWebClient_CancelRequest(Abort);
		}
		else if(data->TransferStatus->RequestToken!=NULL)
		{
			if(endPointer>0 || done)
			{
				ILibThreadPool_QueueUserWorkItem(data->pool,data,&DH_Pool_RequestResponse);
			}
		}
	}
}
Beispiel #9
0
APW_Error call_method_through_thread_pool(APW instance, contex_method_call method)
{
    APWInternalState state;
    BOOL contextSwitch = FALSE;
    APW_Error err = check_this(instance);
    if (err != APW_ERROR_OK) {
        return err;
    }
    state = (APWInternalState)instance->internal_state;
    contextSwitch = TESTMASK(state->EventsOnThreadBitMask, method->method);
    switch (method->method) {
    case APW_ECS_GETAVPROTOCOLINFO:
        break;
    case APW_ECS_SETAVTRANSPORTURI:
        if (instance->Event_SetAVTransportURI != NULL && method->param_count == 2) {
            method->params[0] = (methods_param)(void *)String_Create((const char*)method->params[0]);
            method->params[1] = (methods_param)(void *)String_Create((const char*)method->params[1]);
        }
        break;
    case APW_ECS_STOP:
        break;
    case APW_ECS_PLAY:
        if (instance->Event_Play != NULL && method->param_count == 1) {
            method->params[0] = (methods_param)(void *)String_Create((const char *)method->params[0]);
        }
        break;
    case APW_ECS_PAUSE:
        break;
    case APW_ECS_SEEKTRACK:
        break;
    case APW_ECS_SEEKTRACKTIME:
        break;
    case APW_ECS_SEEKMEDIATIME:
        break;
    case APW_ECS_NEXT:
        break;
    case APW_ECS_PREVIOUS:
        break;
    case APW_ECS_SETPLAYMODE:
        break;
    case APW_ECS_SELECTPRESET:
        if (instance->Event_SelectPreset != NULL && method->param_count == 1) {
            method->params[0] = (methods_param)(void *)String_Create((const char *)method->params[0]);
        }
        break;
    case APW_ECS_SETBRIGHTNESS:
        break;
    case APW_ECS_SETCONTRAST:
        break;
    case APW_ECS_SETVOLUME:
        break;
    case APW_ECS_SETMUTE:
        break;
    default:
        return APW_ERROR_INVALIDARGUMENT;
    }

    ILibWebServer_AddRef((struct ILibWebServer_Session *)method->session);
    if (contextSwitch == TRUE) {
        ILibThreadPool_QueueUserWorkItem(instance->thread_pool, (void *)method, &callback_from_thread_pool);
    } else {
        callback_from_thread_pool(NULL, (void *)method);
    }

    return APW_ERROR_OK;
}
DH_TransferStatus DHS_RespondWithLocalFile(struct ILibWebServer_Session *session, ILibThreadPool pool, struct packetheader *header, size_t buffer_size, const char *file_name, unsigned int supported_transfer_mode, const char *mime_type, const char *content_features, const char* ifo_uri, void *user_obj, DHS_OnResponseDone callback_response)
{
    DH_TransferStatus retval = NULL;
    FILE *f;
    struct DH_Data *data = NULL;
    struct packetheader *resp = NULL;
    char *ifo, *cf = NULL;
    long RangeStart,RangeLength,FileLength;
    char len[255];
    enum ILibWebClient_Range_Result RangeResult = 0;
    enum DH_TransferModes transferMode;

    if(session->done==0)
    {
        //
        // If the get request wasn't completely received yet, then we don't need to do anything yet
        //
        return NULL;
    }

    f = fopen(file_name,"rb");
    if(f!=NULL)
    {
        data = (struct DH_Data*)malloc(sizeof(struct DH_Data));
        memset(data,0,sizeof(struct DH_Data));

        resp = ILibCreateEmptyPacket();
        ILibSetVersion(resp,"1.1",3);
        
        fseek(f,0,SEEK_END);
        FileLength = ftell(f);
        fseek(f,0,SEEK_SET);

        cf = ILibGetHeaderLine(header,"getcontentFeatures.dlna.org",27);

        if(cf!= NULL && memcmp(cf, "1", 1)!=0)
        {
            ILibWebServer_Send_Raw(session,"HTTP/1.1 400 Bad Request\r\n\r\n",28,ILibAsyncSocket_MemoryOwnership_STATIC,1);
            fclose(f);
            free(data);
            ILibDestructPacket(resp);
            return(NULL);
        }

        transferMode = DH_GetRequestedTransferMode(header);

        if(
            ((transferMode == DH_TransferMode_Bulk) ||
             (transferMode == DH_TransferMode_Interactive))
            &&
            ((ILibGetHeaderLine(header,"TimeSeekRange.dlna.org",22)!=NULL) ||
             (ILibGetHeaderLine(header,"PlaySpeed.dlna.org",18)!=NULL) ||
             (ILibGetHeaderLine(header,"realTimeInfo.dlna.org",21)!=NULL)
            )
        )
        {
            ILibWebServer_Send_Raw(session,"HTTP/1.1 400 Bad Request\r\n\r\n",28,ILibAsyncSocket_MemoryOwnership_STATIC,1);
            fclose(f);
            free(data);
            ILibDestructPacket(resp);
            return(NULL);            
        }

        if(ILibGetHeaderLine(header,"Range",5)!=NULL)
        {
            RangeResult = ILibWebClient_Parse_Range(ILibGetHeaderLine(header,"Range",5),&RangeStart,&RangeLength,FileLength);
            switch(RangeResult)
            {
                case ILibWebClient_Range_Result_OK:
                    fseek(f,RangeStart,SEEK_SET);
                    data->BytesLeft = RangeLength;
                    ILibSetStatusCode(resp,206,"Partial Content",15);
                    DH_AddHeader_ContentRange(resp,RangeStart,(RangeStart+RangeLength)-1,FileLength);
                    break;
                case ILibWebClient_Range_Result_INVALID_RANGE:                                 
                    ILibWebServer_Send_Raw(session,"HTTP/1.1 416 Invalid Range\r\n\r\n",30,ILibAsyncSocket_MemoryOwnership_STATIC,1);
                    fclose(f);
                    free(data);
                    ILibDestructPacket(resp);
                    return(NULL);
                    break;
                case ILibWebClient_Range_Result_BAD_REQUEST:
                    ILibWebServer_Send_Raw(session,"HTTP/1.1 400 Bad Request\r\n\r\n",28,ILibAsyncSocket_MemoryOwnership_STATIC,1);
                    fclose(f);
                    free(data);
                    ILibDestructPacket(resp);
                    return(NULL);
                    break;
            }
        }
        else if(ILibGetHeaderLine(header,"TimeSeekRange.dlna.org",22)!=NULL)
        {
            ILibWebServer_Send_Raw(session,"HTTP/1.1 406 Time-based seek not supported\r\n\r\n",46,ILibAsyncSocket_MemoryOwnership_STATIC,1);
            fclose(f);
            free(data);
            ILibDestructPacket(resp);
            return(NULL);
        }
        else if(ILibGetHeaderLine(header,"PlaySpeed.dlna.org",18)!=NULL)
        {
            ILibWebServer_Send_Raw(session,"HTTP/1.1 406 PlaySpeeds not supported\r\n\r\n",41,ILibAsyncSocket_MemoryOwnership_STATIC,1);
            fclose(f);
            free(data);
            ILibDestructPacket(resp);
            return(NULL);
        }
        else
        {
            ILibSetStatusCode(resp,200,"OK",2);
            data->BytesLeft = FileLength;
            sprintf(len,"%ld",data->BytesLeft);
            ILibAddHeaderLine(resp,"Content-Length",14,len,(int)strlen(len));
            ILibAddHeaderLine(resp,"Accept-Ranges",13,"bytes",5);
        }

        if(transferMode == DH_TransferMode_Unspecified && mime_type != NULL)    
        {
            if(ILibString_StartsWith(mime_type, (int) strlen(mime_type),  "video/", 6) != 0)
            {
                DH_AddHeader_transferMode(resp, DH_TransferMode_Streaming);    
            }
            else if(ILibString_StartsWith(mime_type, (int) strlen(mime_type),  "audio/", 6) != 0)
            {
                DH_AddHeader_transferMode(resp, DH_TransferMode_Streaming);    
            }
            else if(ILibString_StartsWith(mime_type, (int) strlen(mime_type),  "image/", 6) != 0)
            {
                DH_AddHeader_transferMode(resp, DH_TransferMode_Interactive);    
            }
        }
        else if(!(transferMode & supported_transfer_mode))
        {
            //
            // Specified transfer mode is not supported
            //
            ILibWebServer_Send_Raw(session,"HTTP/1.1 406 Not Acceptable\r\n\r\n",31,ILibAsyncSocket_MemoryOwnership_STATIC,1);
            fclose(f);
            free(data);
            ILibDestructPacket(resp);
            return(NULL);
        }
        else
        {
            DH_AddHeader_transferMode(resp,transferMode);    
        }

        data->callback_response = callback_response;
        data->f = f;
        data->header = header;
        data->session = session;
        data->pool = pool;
        data->TransferStatus = retval = DH_CreateNewTransferStatus();
        data->TransferStatus->TotalBytesToBeSent = data->BytesLeft;
        data->user_object = user_obj;
        retval->ServerSession = session;
        session->OnDisconnect = &DH_Disconnect;
        
        if(content_features!=NULL)
        {
            ILibAddHeaderLine(resp,"contentFeatures.dlna.org",24,(char*) content_features,(int)strlen(content_features));
        }
        if(mime_type!=NULL)
        {
            ILibAddHeaderLine(resp,"Content-Type",12,(char*)mime_type,(int)strlen(mime_type));
        }
        else
        {
            ILibAddHeaderLine(resp,"Content-Type",12,(char*)DHS_DEFAULT_MIMETYPE,(int)strlen(DHS_DEFAULT_MIMETYPE));
        }
        if(ifo_uri!=NULL)
        {
            ifo = (char*)malloc((int)strlen(ifo_uri)+25);
            sprintf(ifo,"ifoFileURI.dlna.org=\"%s\"",ifo_uri);
            ILibAddHeaderLine(resp,"PRAGMA",6,ifo,(int)strlen(ifo));
            free(ifo);
        }

        ILibWebServer_StreamHeader(session,resp);

        if(header->DirectiveLength==4 && strncasecmp(header->Directive,"HEAD",4)==0)
        {
            ILibWebServer_StreamBody(session,NULL,0,ILibAsyncSocket_MemoryOwnership_STATIC,1);
            fclose(data->f);
            data->f = NULL;
            DH_DestroyTransferStatus(data->TransferStatus);
            free(data);
            session->OnDisconnect = NULL;
            return(NULL);
        }
        else
        {
            lock_init(&(data->SendStatusLock),0,1);
            ILibThreadPool_QueueUserWorkItem(pool,data,&DH_Pool);
        }
    }
    else
    {
        //
        // Error opening file
        //
        ILibWebServer_Send_Raw(session,"HTTP/1.1 404 File Not Found\r\n\r\n",31,ILibAsyncSocket_MemoryOwnership_STATIC,1);
    }
    return retval;
}
void DH_Pool(ILibThreadPool sender, void *var)
{
    struct DH_Data *data = (struct DH_Data*)var;
    char *buffer = (char*)malloc(DHS_READ_BLOCK_SIZE*sizeof(char));
    int bytesRead = 0;
    int paused=0;
    int SendStatus=0;

    lock_wait(&(data->TransferStatus->syncLock));
    if(data->TransferStatus->Reserved1!=0)
    {
        data->TransferStatus->Reserved2 = 2;
        data->TransferStatus->Reserved3 = data;
        paused=1;
        free(buffer);
    }
    lock_post(&(data->TransferStatus->syncLock));

    if(paused)
    {
        printf("PAUSED!\r\n");
        return;
    }

    if(data->TransferStatus->TotalBytesToBeSent==-1)
    {
        lock_wait(&(data->TransferStatus->syncLock));
        fseek(data->f,0,SEEK_END);
        data->TransferStatus->TotalBytesToBeSent = ftell(data->f);
        fseek(data->f,0,SEEK_SET);
        lock_post(&(data->TransferStatus->syncLock));
    }

    bytesRead = (int)fread(buffer,sizeof(char),data->BytesLeft>DHS_READ_BLOCK_SIZE?DHS_READ_BLOCK_SIZE:data->BytesLeft,data->f);

    if(bytesRead>0)
    {
        data->BytesLeft -= bytesRead;
        data->session->User3 = data;
        data->session->OnSendOK = &DH_SendOK;
        data->session->OnDisconnect = &DH_Disconnect;

        lock_wait(&(data->SendStatusLock));
        SendStatus = data->SendStatus = ILibWebServer_StreamBody(data->session,buffer,bytesRead,ILibAsyncSocket_MemoryOwnership_CHAIN,0);
        lock_post(&(data->SendStatusLock));

        if(SendStatus>=0)
        {
            lock_wait(&(data->TransferStatus->syncLock));
            data->TransferStatus->ActualBytesSent += (long)bytesRead;
            lock_post(&(data->TransferStatus->syncLock));
        }
    }
    else
    {
        //
        // Read Error
        //
        free(buffer);
        printf("Read Error!\r\n");
    }

    if(SendStatus>=0)
    {
        if(feof(data->f) || data->BytesLeft==0)
        {
            //
            // Read all there is to read
            //
            data->session->OnSendOK = NULL;
            data->session->OnDisconnect = NULL;
            data->session->User3 = NULL;
            ILibWebServer_StreamBody(data->session,NULL,0,ILibAsyncSocket_MemoryOwnership_STATIC,1);
            //
            // Call the OnDone method
            //
            if(data->callback_response!=NULL)
            {
                data->callback_response(data->session, data->TransferStatus, DHS_ERRORS_NONE,data->user_object);
            }
            fclose(data->f);
            DH_DestroyTransferStatus(data->TransferStatus);
            lock_destroy(&(data->SendStatusLock));
            free(data);
        }
        else
        {
            //
            // More to read
            //
            
            if(data->SendStatus==0 && data->Disconnect==0)
            {
                //
                // Everything was sent, just need to read more
                //
                ILibThreadPool_QueueUserWorkItem(data->pool,data,&DH_Pool);
            }
            else if(data->SendStatus==0 && data->Disconnect!=0)
            {
                //
                // Clean up everything, because the session was disconnected
                //
                if(data->f!=NULL)
                {
                    if(data->callback_response!=NULL)
                    {
                        data->callback_response(data->session, data->TransferStatus, DHS_ERRORS_PEER_ABORTED_CONNECTION,data->user_object);
                    }
                    fclose(data->f);
                    data->f = NULL;
                }
                DH_DestroyTransferStatus(data->TransferStatus);
                lock_destroy(&(data->SendStatusLock));
                free(data);
            }

            //
            // if sendStatus > 0 that means we couldn't send everything,
            // and must wait for the SendOK event to continue.
            //
        }    
    }
}