Thread: Proxy server using threads - Bad file descriptor error

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    15

    Proxy server using threads - Bad file descriptor error

    Hello,
    My program is a proxy server that uses a pre-defined thread pool in order to handle client requests. If the proxy gets HTTP 1.1 it changes it and sends the origin server HTTP 1.0 request.

    I have a problem but I can't figure it out. When the web page starts to load, I get Bad file descriptor error from both read() (read client request) and write() (write response to client).
    I've tried playing with closing the client socket and origin socket, but As far as I udnerstand, the problem is not there.
    Here is my code, I'll appriciate it if someone can take a look at it.
    Thanks in advance.
    Code:
    int handleHttpRequest(void* socketNum){
    	char* response;	//Holds response
    	char* request=readClientRequest(socketNum);	//Holds request
    	
    	if (request==NULL){
    		response=composeHttpResponse(INTERNAL_SERVER_ERR);
    		sendResponse(socketNum, response);
    		closeAndDestroy(socketNum, 0, NULL, response, NULL, NULL);
    		return 0;
    	}
               .
               .
                //Some more code...
               .
               .
    	//Send request to destination server
    	struct sockaddr_in destSrvr;
    	int destSrvrFd;
    	
    	//Create the socket
    	if ((destSrvrFd=socket(AF_INET,SOCK_STREAM,0))==-1){
    		response=composeHttpResponse(INTERNAL_SERVER_ERR);
    		sendResponse(socketNum, response);
    		closeAndDestroy(socketNum, 0, request, response, host, path);
        		return 0;
    	}
        	
        	//Connection settings
    	destSrvr.sin_family = AF_INET;		//use the Internet address family
    	destSrvr.sin_port = htons(80);		//set port to 80
    	destSrvr.sin_addr.s_addr = ((struct in_addr*)(hostInfo->h_addr))->s_addr;
    	
    	
    	//Connect
    	if (connect(destSrvrFd, (struct sockaddr*) &destSrvr, sizeof(destSrvr)) < 0){
    		response=composeHttpResponse(INTERNAL_SERVER_ERR);
    		sendResponse(socketNum, response);
    		closeAndDestroy(socketNum, destSrvrFd, request, response, host, path);
        		return 0;
    	}
    	
    	//Write client request to destination server
    	int nbytes=0;
    	while (nbytes < strlen(request))
    		if ( (nbytes=write(destSrvrFd, request, strlen(request))) < 0){
    			response=composeHttpResponse(INTERNAL_SERVER_ERR);
    			sendResponse(socketNum, response);
    			closeAndDestroy(socketNum, destSrvrFd, request, response, host, path);
    			return 0;
    		}
    	
    	//Read response from destination server
    	const int BUFFER_SIZE=512;
    	char rBuffer[BUFFER_SIZE];
    	response=(char*)malloc(BUFFER_SIZE+1);
    	
    	if (response==NULL){
    		response=composeHttpResponse(INTERNAL_SERVER_ERR);
    		sendResponse(socketNum, response);
    		closeAndDestroy(socketNum, destSrvrFd, request, response, host, path);
    		return 0;
    	}
    			
    	nbytes=0;
    	if ((nbytes = read(destSrvrFd, rBuffer, BUFFER_SIZE)) < 0){
    		response=composeHttpResponse(INTERNAL_SERVER_ERR);
    		sendResponse(socketNum, response);
    		closeAndDestroy(socketNum, destSrvrFd, request, response, host, path);
    		return 0;
    	}
    	rBuffer[BUFFER_SIZE]='\0';
    	strcpy(response,rBuffer);
    	while (nbytes!=0){
    		if ((nbytes = read(destSrvrFd, rBuffer, BUFFER_SIZE)) < 0){
    			response=composeHttpResponse(INTERNAL_SERVER_ERR);
    			sendResponse(socketNum, response);
    			closeAndDestroy(socketNum, destSrvrFd, request, response, host, path);
    			return 0;
    		}
    		rBuffer[BUFFER_SIZE]='\0';
    		response=realloc(response, strlen(response) + BUFFER_SIZE+1);
    		strcat(response,rBuffer);
    	}
    	
    	//Write server response back to client
    	//sendResponse(socketNum, response);
    	nbytes=0;
    	while (nbytes < strlen(request))
    		if ( (nbytes=write(*(int*)socketNum, response, strlen(response))) < 0){
    			response=composeHttpResponse(INTERNAL_SERVER_ERR);
    			sendResponse(socketNum, response);
    			closeAndDestroy(socketNum, destSrvrFd, request, response, host, path);
    			return 0;
    		}
    	
    	//Close connections with client and destination server and free memory
    	closeAndDestroy(socketNum, destSrvrFd, request, response, host, path);
    	
    	return 0;
    }
    Code:
    //Reads client request. Returns request on success, NULL otherwise
    char* readClientRequest(void* socketNum){
    	int fd=*(int*)socketNum, nbytes=0;
    	const int BUFFER_SIZE=4096;
    	char buf[BUFFER_SIZE+1];					//Reading buffer
    	char* req=(char*)malloc(BUFFER_SIZE+1);		//Holds request
    
    	if (req==NULL)
    		return NULL;
    	
    	//Read request from client
    	if ((nbytes = read(fd, buf, BUFFER_SIZE)) < 0){
    		printf("%d\n", fd);
    		perror("error: read \n"); return NULL;
    	}
    	
    	buf[BUFFER_SIZE]='\0';
    	strcpy(req,buf);
    	while ( strstr(req,"\r\n\r\n")==NULL ){
    		if ((nbytes = read(fd, buf, BUFFER_SIZE)) < 0){
    			perror("error: read\n"); return NULL;
    		}
    		buf[BUFFER_SIZE]='\0';
    		req=realloc(req, strlen(req) + BUFFER_SIZE);
    		strcat(req,buf);
    	}
    	return req;
    }
    Code:
    //Writes response and closes connection with client.
    void sendResponse(void* socketNum, char* response){
    	int fd=*(int*)socketNum, nbytes=0;
    	
    	while (nbytes < strlen(response))
    		if ( (nbytes=write(fd, response, strlen(response))) < 0){
    			perror("error: write\n"); exit(EXIT_FAILURE);
    		}
    }
    Last edited by Stiletto; 01-24-2012 at 12:50 PM.

  2. #2
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    Can't edit original post, so I'm posting again for an easier review. I've made some changes to the code so it will be more clear and simple to me. Now only read() gives an error.
    How long should I read response from original server?
    Code:
            //**All code above is valid and OK**
    
    	//Write client request to destination server
    	sendMsg(destSrvrFd,request);
    	
    	//Read response from destination server
    	response=readMsgfromServer(destSrvrFd);
    	
    	//Write response back to client
    	sendMsg(*(int*)socketNum,response);
    	
    	//Close connections with client and destination server and free memory
    	closeAndDestroy(socketNum, destSrvrFd, request, response, host, path);
    	
    	return 0;
    Code:
    char* readMsgfromServer(int fd){
    	int nbytes=0;
    	const int BUFFER_SIZE=4096;
    	char buf[BUFFER_SIZE+1];					//Reading buffer
    	char* req=(char*)malloc(BUFFER_SIZE+1);		//Holds request
    	
    	if (req==NULL)
    		return NULL;
    	
    	if ((nbytes = read(fd, buf, BUFFER_SIZE)) < 0){
    		perror("error: read \n"); return NULL;
    	}
    	
    	buf[BUFFER_SIZE]='\0';
    	strcpy(req,buf);
    	while ( nbytes!=0 ){
    		if ((nbytes = read(fd, buf, BUFFER_SIZE)) < 0){
    			perror("error: read\n"); return NULL;
    		}
    		buf[BUFFER_SIZE]='\0';
    		req=realloc(req, strlen(req) + BUFFER_SIZE);
    		strcat(req,buf);
    	}
    	printf("%d End of while - nbytes==0\n", fd);
    	return req;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Connection between proxy server and the server
    By vbx_wx in forum Networking/Device Communication
    Replies: 2
    Last Post: 02-07-2011, 01:51 PM
  2. Proxy Server
    By joey2264 in forum Linux Programming
    Replies: 1
    Last Post: 11-18-2010, 11:48 PM
  3. write error with a bad file descriptor
    By techevo in forum C Programming
    Replies: 6
    Last Post: 03-04-2010, 02:58 PM
  4. proxy server
    By crvenkapa in forum Tech Board
    Replies: 5
    Last Post: 01-31-2008, 09:48 AM
  5. c++ proxy server?
    By marf in forum C++ Programming
    Replies: 0
    Last Post: 03-16-2003, 08:32 PM