void HttpHandler::responseTimeOut()
{
    this->statusCode = 408;
    composeStatusLine();
    composeHeader();
    composeResponse();
}
void HttpHandler::responseOptionsMethod()
{
    if(this->isNew)
    {
        if(this->serverHttp10)
            this->dataLeft = strlen(SUPPORTED_METHODS_HTTP10);
        else
            this->dataLeft = strlen(SUPPORTED_METHODS_HTTP11);
        this->fileSize = this->dataLeft;
        this->bufferSize = this->dataLeft;
        this->statusCode = 200;
        composeStatusLine();
        composeHeader();
    }
    else
    {
        if(this->msgBuffer != NULL)
        {
            delete[] this->msgBuffer;
            this->msgBuffer = NULL;
        }
        this->msgBuffer = new char[this->dataLeft];
        if(this->serverHttp10)
            strcpy(this->msgBuffer, SUPPORTED_METHODS_HTTP10);
        else
            strcpy(this->msgBuffer, SUPPORTED_METHODS_HTTP11);
        this->dataLeft = 0;
        this->statusLine = NULL;
        this->header = NULL;
    }
    composeResponse();
}
void HttpHandler::responsePutMethod()
{
    // Brand new connection
    if(this->isNew)
    {
        char *absolutePath;
        int lenAbsolutePath, sizeOfStatusLine;

        lenAbsolutePath = strlen(ROOT) + strlen(this->URL);
        absolutePath = new char[lenAbsolutePath+1];
        strcpy(absolutePath, ROOT);
        strcat(absolutePath, this->URL);

        this->fileToWrite = fopen(absolutePath, "w");
        if(this->fileToWrite)
            this->dataLeft = this->fileSize;
        else
        {
            this->msgBuffer = NULL;
            this->statusCode = 403;
            this->dataLeft = 0;
        }
    }
    // Old connection
    else
    {
        if(this->fileToWrite)
        {
            if(this->dataLeft > MAXBUFFSIZE)
            {
                this->bufferSize = MAXBUFFSIZE;
                this->dataLeft -= bufferSize;
            }
            else
            {
                this->bufferSize = this->dataLeft;
                this->dataLeft = 0;
            }
            if(this->msgBuffer != NULL)
            {
                delete[] this->msgBuffer;
                this->msgBuffer = NULL;
            }
            this->msgBuffer = new char[this->bufferSize];
            int posInClientBuffer = this->fileSize - this->dataLeft - 1;
            for(int i = bufferSize-1; i >= 0; i--)
                this->msgBuffer[i] = this->clientMsg[posInClientBuffer--];
            fwrite(this->msgBuffer, sizeof(char), this->bufferSize, this->fileToWrite);
        }
    }

    if(this->dataLeft == 0)
    {
        if(this->fileToWrite)
            fclose(this->fileToWrite);
        composeStatusLine();
        composeHeader();
        composeResponse();
    }
}
void HttpHandler::responseTraceMethod()
{
    if(this->isNew)
    {
        this->dataLeft = strlen(this->request);
        this->fileSize = this->dataLeft;
        this->bufferSize = this->dataLeft;
        this->statusCode = 200;
        composeStatusLine();
        composeHeader();
    }
    else
    {
        if(this->msgBuffer != NULL)
        {
            delete[] this->msgBuffer;
            this->msgBuffer = NULL;
        }
        this->msgBuffer = new char[this->dataLeft];
        strcpy(this->msgBuffer, this->request);
        this->dataLeft = 0;
        this->statusLine = NULL;
        this->header = NULL;
    }
    composeResponse();
}
Exemple #5
0
void Worker::pushToInBuf(int fd, std::string str) {

    assert(!str.empty());
    assert(clients.find(fd) != clients.end());

    clients[fd]->inBuf.append(str);
    int pos = clients[fd]->inBuf.find('\n');

    /* Compose response */
    while (pos != std::string::npos) {

        std::string query = clients[fd]->inBuf.substr(0, pos);
        clients[fd]->inBuf = clients[fd]->inBuf.substr(pos + 1);
        addResponse(fd, composeResponse(query));

        pos = clients[fd]->inBuf.find('\n');
    }
}
//according to the request this fuction composes 
//checks if the request is valid and construct 
//a response string to reply the client
void getResponse(rtspd_t* rtspd, char response[]){
	int status = BAD_REQUEST;
	printf("getting ----------- response\n");
	if(strcmp(rtspd->request, "SETUP") == 0){
		printf("send setup response\n");
		rtspd->sessionid = getSessionId();
		status = getVideo();
		printf("video file ok: %d\n", status);
		//initMovie(rtspd->videoName, rtspd->client_fd);
		rtspd->data = send_frame_data_new(rtspd->videoName, rtspd->client_fd);
		composeResponse(rtspd, status, response);
		//setup complete change state to ready
		strcpy(rtspd->current_state, "READY");
	}else if(strcmp(rtspd->request, "PLAY") == 0&& strcmp(rtspd->current_state,"READY")==0){
			status = OK;
			composeResponse(rtspd, status, response);
      
			streamVideo(rtspd->data);
			strcpy(rtspd->current_state, "PLAYING");

		
	}else if(strcmp(rtspd->request, "PLAY")==0 && strcmp(rtspd->current_state, "PLAYING")==0){
			status = OK;
			printf("start playing, %s, %d\n\n", rtspd->videoName,rtspd->client_fd);
			composeResponse(rtspd, status, response);
			streamVideo(rtspd->data);
			strcpy(rtspd->current_state, "PLAYING");
		
	}else if(strcmp(rtspd->request, "PAUSE")==0 && strcmp(rtspd->current_state, "PLAYING")==0){
			status = OK;
			printf("pause video \n");
			pauseVideo(rtspd->data);
			composeResponse(rtspd, status, response);
			strcpy(rtspd->current_state, "READY");
		
	}else if(strcmp(rtspd->request, "TEARDOWN")==0
		 &&(strcmp(rtspd->current_state, "PLAYING")==0 ||
			strcmp(rtspd->current_state, "READY")==0)){
			status = OK;
			printf("TEARDOWN video \n");
			deleteTimer(rtspd->data);
			composeResponse(rtspd, status, response);
			strcpy(rtspd->current_state, "INIT");
		
	}else{
		status = NOT_VALID;
		composeResponse(rtspd, status, response);
	}
}
void HttpHandler::responseDeleteMethod()
{
    char *absolutePath;
    int lenAbsolutePath;

    lenAbsolutePath = strlen(ROOT) + strlen(this->URL);
    absolutePath = new char[lenAbsolutePath+1];
    strcpy(absolutePath, ROOT);
    strcat(absolutePath, this->URL);
    if(remove(absolutePath) != 0)
    {
        perror("Errror deleting file");
        this->statusCode = 404;
    }
    else
        this->statusCode = 200;

    composeStatusLine();
    composeHeader();
    composeResponse();
}
void HttpHandler::responseHeadMethod()
{
    int lenAbsolutePath;
    char *absolutePath;

    lenAbsolutePath = strlen(ROOT) + strlen(this->URL);
    absolutePath = new char[lenAbsolutePath+1];
    strcpy(absolutePath, ROOT);
    strcat(absolutePath, this->URL);
    this->fileToFetch = fopen(absolutePath, "r");
    if(this->fileToFetch)
    {
        this->statusCode = 200;
        fclose(this->fileToFetch);
    }
    else
        this->statusCode = 404;

    composeStatusLine();
    composeHeader();
    composeResponse();
}
void HttpHandler::responseBadRequest()
{
    composeStatusLine();
    composeHeader();
    composeResponse();
}
void HttpHandler::responseGetMethod()
{
    // Brand new connection
    if(this->isNew)
    {
        char *absolutePath;
        int lenAbsolutePath;

        lenAbsolutePath = strlen(ROOT) + strlen(this->URL);
        absolutePath = new char[lenAbsolutePath+1];
        strcpy(absolutePath, ROOT);
        strcat(absolutePath, this->URL);
        this->fileToFetch = fopen(absolutePath, "r");
        if(this->fileToFetch)
        {
            if(!fseek(this->fileToFetch, 0, SEEK_END))
            {
                this->fileSize = ftell(this->fileToFetch);
                this->dataLeft = this->fileSize;
            }
            else
            {
                this->statusCode = 500;
                if(this->fileToFetch)
                {
                    fclose(this->fileToFetch);
                    this->fileToFetch = NULL;
                }
                this->msgBuffer = NULL;
                this->dataLeft = 0;
            }
            if(!fseek(this->fileToFetch, 0, SEEK_SET))
                this->statusCode = 200;
            else
            {
                this->statusCode = 500;
                if(this->fileToFetch)
                {
                    fclose(this->fileToFetch);
                    this->fileToFetch = NULL;
                }
                this->msgBuffer = NULL;
                this->dataLeft = 0;
            }
        }
        else
        {
            this->msgBuffer = NULL;
            this->statusCode = 404;
            this->dataLeft = 0;
        }
        composeStatusLine();
        composeHeader();
    }
    // Old connection
    else
    {
        if(this->fileToFetch)
        {
            if(this->dataLeft > MAXBUFFSIZE)
            {
                this->bufferSize = MAXBUFFSIZE;
                this->dataLeft -= bufferSize;
            }
            else
            {
                this->bufferSize = this->dataLeft;
                this->dataLeft = 0;
            }

            if(this->msgBuffer != NULL)
            {
                delete[] this->msgBuffer;
                this->msgBuffer = NULL;
            }
            this->msgBuffer = new char[this->bufferSize];
            fread(this->msgBuffer, sizeof(char), this->bufferSize, this->fileToFetch);
            this->statusLine = NULL;
            this->header = NULL;
        }
    }
    composeResponse();

    if(this->dataLeft == 0 && this->fileToFetch != NULL)
        fclose(this->fileToFetch);
}