Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Socket error with HTTP protocol.

Status
Not open for further replies.

monasa

IS-IT--Management
Jun 27, 2004
41
CA
Hi all,

I create a socket :

descripteur = socket(AF_INET, SOCK_STREAM,0);

After I try to connect to the server :

connect(descripteur, (struct sockaddr *)&infoserveur, sizeof(infoserveur))<0

I send a request like "HEAD <PATH> HTTP/1.0"

octetsenv = send(descrip, url,longr,0);

I try to receive with :

numoctets = recv(descrip,buf,300,0)

I receive :

HTTP/1.1 400 Bad request
Server: Netscape-Enterprise/6.1 AOL
Date: Sat, 03 Jul 2004 01:48:28 GMT
Content-length: 147
Content-type: text/html
Connection: close

<HTML><HEAD><TITLE>Bad request</TITLE></HEAD>
<BODY><H1>Bad request</H1>
Your browser sent a query this server could not understand.


Please if you can help.

M.N
 
> I send a request like "HEAD <PATH> HTTP/1.0"
Did you have "\r\n" on the end of this request?

Have you read this?
ftp://ftp.rfc-editor.org/in-notes/rfc2616.txt

Here are a couple of more tools for you

For example, using curl, I did this
[tt]curl --output foo --head [/tt]

And using ethereal, I saw that this was the message that got sent.
[tt]Hypertext Transfer Protocol
HEAD / HTTP/1.1\r\n
User-Agent: curl/7.9.8 (i686-pc-linux-gnu) libcurl 7.9.8 (OpenSSL 0.9.6b) (ipv6 enabled)\r\n
Host: Pragma: no-cache\r\n
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*\r\n
\r\n
[/tt]

--
 
Hi Salam,
Thanks for the help.

I show what I send :

[nassihmo@phobos nassihmo]$ ./a.out "HEAD / HTTP/1.0\n\r"

Socket created OK
descriptor : 3
connection to : cnn.com
L'adresse IP : 64.236.16.116
Connection OK

request : HEAD / HTTP/1.0\n\r
Sent : 21 bytes
received : 0 bytes


RECEIVED : HTTP/1.1 400 Bad request
Server: Netscape-Enterprise/6.1 AOL
Date: Sat, 03 Jul 2004 11:42:06 GMT
Content-length: 147
Content-type: text/html
Connection: close

<HTML><HEAD><TITLE>Bad request</TITLE></HEAD>
<BODY><H1>Bad request</H1>
Your browser sent a query this server could not understand.
[nassihmo@phobos nassihmo]$

Mohamed.
 
> "HEAD / HTTP/1.0\n\r"
1. It's \r\n
Study the example more closely

2. These are escape sequences - \r\n get converted into a pair of control characters when written in 'C' source code.
This translation DOES NOT HAPPEN on input you type in on the command line.

You need to parse your command line parameter for \x pairs, and make the appropriate substitutions before sending it to the remote server.

--
 
Hi Salem,

Yes I have \r\n at the end of the request but it is does not work !!

Mohamed Nassih
nassih50@hotmail.com
 
So read the rest of my previous post and convert the string.

Post the code that you have between reading the parameter on the command line, and passing it to the remote server.


--
 
I read the request as a parameter from a command line and passe it to this function : url is the request and descrip is the socket descriptor

int envoyer_URL(char* url, int descrip)
{

int longr;
int octetsenv;

longr = strlen(url)+1;
printf("descripteur de l'env: %d\n",descrip);
octetsenv = send(descrip, url,longr,0);
printf("\n%s\n", url);
}

Thanks Salem.

Mohamed
 
This is hard work

You need to do something like this then
Code:
void do_escapes ( char *out, const char *in ) {
    char ch;
    while ( ch = *in++ ) {
        if ( ch == '\\' ) {
            ch = *in++;
            switch ( ch ) {
                case 'n': *out++ = '\n'; break;
                case 'r': *out++ = '\r'; break;
                default:  *out++ = ch; break;
            }
        } else {
            *out++ = ch;
        }
    }
    *out = '\0';
}

int envoyer_URL(char* url, int descrip)
{
    char newurl[1000];
    do_escapes( newurl, url );
    // now send newurl
}

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top