Thread: http proxy - receive problem

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    4

    http proxy - receive problem

    Hello,

    I have coded a http proxy in c that do the work very well. It wait for an connection (http request), send it back to the browser and so on. But i can't receive *.gif's, *.jpg's .... Only what I see in the browser ist a "broken" image. The rest, such full html code, style sheets, received i completely. Anyone have an idea, where the problem can be?

    ...sry for the bad english.

    thx
    Last edited by laphoo; 05-13-2004 at 06:43 PM.

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    maybe the types you are recieving have to be typcasted or maybe tructuated to meet your requirments. Obviously something is happening between the server and you. Some of data is being damaged somehow. Do you have any ideas now?

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    4
    Do you mean the Accept header part:

    Accept: image/png,image/jpeg,image/gif;q=0.2,*/*;q=0.1

    ...it will correct send and received.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Post a snippet of the code that does the reading/writing on the socket.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    4
    buffer size for each array == 5000 chars
    pSock // return socket from local server
    bSock // return socket from accept
    tSock // reuurn socket from connected target

    Code:
    while(1)
    	{
    		if((bSock = accept(pSock, (struct sockaddr*)&client, &clientSize)) == -1)
    		{
    			fprintf(stdout, "error: proxy - accept()\n");
    			continue;
    		}
    		
    		if((childId = fork()) == 0)
    		{
    			close(pSock);
    		
    			fprintf(stdout, "Connection from: %s:%d [successfully]\n",
    				inet_ntoa(client.sin_addr.s_addr), htons(client.sin_port));
    			
    			memset(&recvBuf, 0x00, sizeof(recvBuf));
    			
    			if((recvBytes = recv(bSock, (char*)recvBuf, sizeof(recvBuf) - 1, 0)) == -1)
    			{
    				fprintf(stdout, "error: client - recv()\n");
    				return -4;
    			}
    			recvBuf[recvBytes] = '\0';
    		
    			if(verbose == 0)
    			{
    				fprintf(stdout, "< RECEIVING HTTP HEADER >");
    				fprintf(stdout, "\n%s\n", recvBuf);
    			}
    
    			memset(&sendBuf, 0x00, sizeof(sendBuf));
    			
    			if(ParseHttpHeader(recvBuf, the.hdrRef, sendBuf) < 0)
    				return -5;
    
    			if((tSock = SetupTargetServer(the.hdrHost)) < 0)
    				return -6;
    
    			if(verbose == 0)
    			{
    				fprintf(stdout, "< SENDING HTTP HEADER >");
    				fprintf(stdout, "\n%s\n", sendBuf);
    			}
    
    			if(send(tSock, (char*)sendBuf, strlen(sendBuf), 0) == -1)
    			{
    				fprintf(stdout, "error: target - send()\n");
    				return -7;
    			}
    		
    			while(1)
    			{
    				memset(&htmlBuf, 0x00, sizeof(htmlBuf));
    
    				if((recvBytes = recv(tSock, (char*)htmlBuf, sizeof(htmlBuf) - 1, 0)) == -1)
    				{
    					fprintf(stdout, "error: target - recv()\n");
    					return -8;
    				}
    				htmlBuf[recvBytes] = '\0';
    				
    				if(send(bSock, (char*)htmlBuf, strlen(htmlBuf), 0) == -1)
    				{
    					printf("error: client - send()\n");
    					return -9;
    				}
    			}
    			close(tSock);
    			close(bSock);
    			exit(EXIT_FAILURE);
    		}
    		else
    		{
    			if(childId < 0)
    			{
    				fprintf(stdout, "error: fork()\n");
    				exit(EXIT_FAILURE);
    			}
    			else
    			{
    				close(bSock);
    			}
    		}
    	}

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>if(send(bSock, (char*)htmlBuf, strlen(htmlBuf), 0) == -1)
    Is this part sending the image ? If so, it won't work too well, strcpy() isn't the correct function to use. Only the recvBytes variable knows how many bytes you've got in the buffer.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    4
    Yes it is...do you mean, in correct form this:

    if(send(bSock, (char*)htmlBuf, strlen(recvBytes), 0) == -1)

    that I must put the recvBytes variable into strlen?

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    strlen() will count from the start of the buffer up to the first byte that have value \0 (0x00). It is designed to work with text strings, like words and sentences, not the internals of a GIF image. There is most likely many \0 bytes scatter throughout the image file, which will confuse your code completely. You should be sending all parts of the image, so I guess you'd want to go with something like
    >>if(send(bSock, (char*)htmlBuf, recvBytes, 0) == -1)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  2. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  3. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  4. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM