Exemplo n.º 1
0
void DH_AbortTransfer(DH_TransferStatus transfer_status_handle)
{
	sem_wait(&(transfer_status_handle->syncLock));
	if(transfer_status_handle->RequestToken != NULL)
	{
		transfer_status_handle->Reserved4 = 1;
	}
	sem_post(&(transfer_status_handle->syncLock));
	if(transfer_status_handle->ServerSession!=NULL)
	{
		if(transfer_status_handle->SessionFlag!=0)
		{
			ILibWebServer_Send_Raw(transfer_status_handle->ServerSession,"HTTP/1.1 500 Server Abort\r\n\r\n",29,ILibAsyncSocket_MemoryOwnership_STATIC,1);
		}
		ILibWebServer_DisconnectSession(transfer_status_handle->ServerSession);
	}
}
Exemplo n.º 2
0
DH_TransferStatus DHS_SavePostToLocalFile(struct ILibWebServer_Session *session, ILibThreadPool pool, struct packetheader *header, const char *file_name, int append_flag, void *user_obj, DHS_OnResponseDone callback_response)
{
    DH_TransferStatus RetVal = NULL;
    char *hdr_field = NULL;
    int temp = 0;
    FILE *f;
    struct DH_Data *data = NULL;

    int Start,End,TotalLength;
    int needClose = 0;

    //
    // Check the append flag
    //
    if(append_flag==0 && ILibGetHeaderLine(header,"content-range",13)!=NULL)
    {
        ILibWebClient_Parse_ContentRange(ILibGetHeaderLine(header,"content-range",13),&Start,&End,&TotalLength);
        if(Start!=0)
        {
            //
            // append_flag indicates overwrite, but the header's indicate partial content
            // this is not allowed
            //
            if(ILibGetHeaderLine(header,"Expect",6)==NULL)
            {
                needClose = 1;
            }
            ILibWebServer_Send_Raw(session,"HTTP/1.1 406 Not Acceptable\r\n\r\n",31,ILibAsyncSocket_MemoryOwnership_STATIC,1);
            if(needClose!=0)
            {
                // If the client did not send "Expect 100 Continue", then the request body may already be in transit, so we must close the socket
                ILibWebServer_DisconnectSession(session);
            }
            return(NULL);
        }
    }

    if(append_flag==0)
    {
        f = fopen(file_name,"wb");
    }
    else 
    {
        f = fopen(file_name,"r+b");
    }
    
    if(f==NULL)
    {
        //
        // Error
        //                                             
        if(ILibGetHeaderLine(header,"Expect",6)==NULL)
        {
            needClose = 1;
        }
        ILibWebServer_Send_Raw(session,"HTTP/1.1 404 File not found\r\n\r\n",31,ILibAsyncSocket_MemoryOwnership_STATIC,1);
        if(needClose!=0)
        {
            // If the client did not send "Expect 100 Continue", then the request body may already be in transit, so we must close the socket
            ILibWebServer_DisconnectSession(session);
        }
        return(NULL);
    }

    data = (struct DH_Data*)malloc(sizeof(struct DH_Data));
    memset(data,0,sizeof(struct DH_Data));

    data->callback_response = callback_response;
    data->f = f;
    data->header = header;
    data->session = session;
    data->pool = pool;
    data->OriginalReceivePtr = session->OnReceive;
    data->user_object = user_obj;
    RetVal = data->TransferStatus = DH_CreateNewTransferStatus();
    RetVal->ServerSession = session;
    RetVal->SessionFlag = 1;

    ILibWebServer_OverrideReceiveHandler(session,&DHS_OnReceive_SavePostToLocalFile);
    session->User3 = data;
    session->OnDisconnect = &DHS_OnDisconnect;
    
    //
    // Check to see if we need to advance the file pointer
    //
    if(append_flag!=0)
    {
        hdr_field = ILibGetHeaderLine(header,"content-range",13);
        if(hdr_field==NULL)
        {
            //
            // Just append to the end of the file
            //
            fseek(f,0,SEEK_END);
        }
        else
        {
            //
            // Advance the file pointer the specified amount
            //
            int SourceFileLength = 0;
            ILibWebClient_Parse_ContentRange(hdr_field,&Start,&End,&TotalLength);

            if(Start==-1 && End==-1 && TotalLength==-1)
            {
                // Invalid Range
                ILibWebServer_Send_Raw(session,"HTTP/1.1 400 Bad Request\r\n\r\n",28,ILibAsyncSocket_MemoryOwnership_STATIC,1);
                if(ILibGetHeaderLine(header,"Expect",6)==NULL)
                {
                    // If the client did not send "Expect 100 Continue", then the request body may already be in transit, so we must close the socket
                    ILibWebServer_DisconnectSession(session);
                }
                return(NULL);
            }
            else
            {
                // Valid Range
                fseek(f,0,SEEK_END);
                SourceFileLength = (int)ftell(f);
                if(SourceFileLength != Start)
                {
                    ILibWebServer_Send_Raw(session,"HTTP/1.1 409 Conflict\r\n\r\n",25,ILibAsyncSocket_MemoryOwnership_STATIC,1);
                    if(ILibGetHeaderLine(header,"Expect",6)==NULL)
                    {
                        // If the client did not send "Expect 100 Continue", then the request body may already be in transit, so we must close the socket
                        ILibWebServer_DisconnectSession(session);
                    }
                    return(NULL);
                }
            }
        }
    }


    if(session->done!=0)
    {
        //
        // All the data is here, so OnReceive won't be called anymore
        //
        DHS_OnReceive_SavePostToLocalFile(session,0,header,session->buffer,&temp,session->bufferLength,session->done);
    }
    else
    {
        hdr_field = ILibGetHeaderLine(header,"Content-Length",14);
        if(hdr_field!=NULL)
        {
            data->TransferStatus->TotalBytesToBeReceived = atol(hdr_field);
        }
        //
        // Check to see if we are supposed to send a 100 Continue response
        //
        hdr_field = ILibGetHeaderLine(header,"expect",6);
        if(hdr_field != NULL && strcasecmp(hdr_field,"100-continue")==0)
        {
            //
            // Send 100-Continue
            //
            ILibWebServer_Send_Raw(session,"HTTP/1.1 100 Continue\r\n\r\n",25,ILibAsyncSocket_MemoryOwnership_STATIC,0);
        }
        else if(hdr_field != NULL)
        {
            //
            // We don't recognize the expectation
            //
            ILibWebServer_Send_Raw(session,"HTTP/1.1 417 Expectation Failed\r\n\r\n",35,ILibAsyncSocket_MemoryOwnership_STATIC,1);
            if(ILibGetHeaderLine(header,"Expect",6)==NULL)
            {
                // If the client did not send "Expect 100 Continue", then the request body may already be in transit, so we must close the socket
                ILibWebServer_DisconnectSession(session);
            }
            RetVal = NULL;
        }
    }
    return(RetVal);
}