0

I am working on a small project (C programming) to tunnel through a proxy and when I try to do it I get a error:

HTTP/1.0 400 Bad Request
Server: squid/2.7.STABLE9
Date: Mon, 07 Jan 2013 22:20:56 GMT
Content-Type: text/html 
Content-Length: 1456
X-Squid-Error: ERR_INVALID_REQ 0
X-Cache: MISS from xt03
Via: 1.0 xt03:80 (squid/2.7.STABLE9)
Connection: close

The data I am sending to the proxy server is:

 char msg[] = "CONNECT example.com:80 HTTP/1.0 <CR><LF> HOST example.com:80 <CR><LF> \n";

Any help would be great!

------ UPDATE ------

I have tried the methods you guys suggested and I am still getting an error. I will post my entire set of code and see if it is something in the socket causing it to send the wrong information.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>

#define maxlen 2048

int main(int argc, char *argv[])
{
int mysocket;
int len;
char buffer[2000];
char msg[] = "CONNECT example.org:80 HTTP/1.0 \r\n HOST example.org:80 \r\n\r\n";


mysocket = socket(AF_INET, SOCK_STREAM, 0);

struct sockaddr_in dest;

memset(&dest, 0, sizeof(dest));

dest.sin_family = AF_INET;
dest.sin_addr.s_addr = inet_addr("125.39.66.150");
dest.sin_port = htons(80);

connect(mysocket, (struct sockaddr *)&dest, sizeof(struct sockaddr));

send(mysocket, msg, strlen(msg), 0);
len = recv(mysocket, buffer, maxlen, 0);

buffer[len] = '\0';

printf("%s \n", buffer);
close(mysocket);
return 0;

}

----- UPDATE ------

New error:

HTTP/1.0 400 Bad Request
Server: squid/2.7.STABLE5
Date: Tue, 08 Jan 2013 22:40:24 GMT
Content-Type: text/html
Content-Length: 1158
X-Squid-Error: ERR_INVALID_REQ 0
X-Cache: MISS from DXT-BJ-219
X-Cache-Lookup: NONE from DXT-BJ-219:80
Connection: close

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"         "http://www.w3.org/TR/html4/loose.dtd">
<HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<TITLE>ERROR: The requested URL could not be retrieved</TITLE>
<STYLE type="text/css"><!--BODY{background-color:#ffffff;font-family:verdana,sans-    serif}PRE{font-family:sans-serif}--></STYLE>
</HEAD><BODY>
<H1>ERROR</H1>
<H2>The requested URL could not be retrieved</H2>
<HR noshade size="1px">
<P> 
While trying to process the request:
<PRE>
CONNECT example.com:80 HTTP/1.1


</PRE>
<P>
The following error was encountered:
<UL>
<LI>
<STRONG>
Invalid Request
</STRONG>
</UL>

<P>
Some aspect of the HTTP Request is invalid.  Possible problems:
<UL>
<LI>Missing or unknown request method
<LI>Missing URL
<LI>Missing HTTP Identifier (HTTP/1.0)
<LI>Request is too large
<LI>Content-Length missing for POST or PUT requests
<LI>Illegal character in hostname; underscores are not allowed
</UL>
<P>Your cache administrator is <A HREF="mailto:noc_admin@163.com">noc_admin@163.com</A>. 

<BR clear="all">
<HR noshade size="1px">
<ADDRESS>
by DXT-BJ-219
</ADDRESS>
</BODY></HTML>
Bryce
  • 447
  • 2
  • 9
  • 24

2 Answers2

3

“Bad request” meant that the request you sent to the server was malformed and couldn't be understood. I think the reason for this is that you are sending the 8 characters <CR><LF> (as in, '<', 'C', 'R' etc.) rather than the 2 characters that they should represent, i.e. \r\n.

Try

char msg[] = "CONNECT example.com:80 HTTP/1.0\r\nHOST example:80\r\n\r\n";
dreamlax
  • 93,976
  • 29
  • 161
  • 209
  • I tried the suggested above but I still am getting the same error. I posted the rest of my code on the main section. Maybe it has something to do with the socket. – Bryce Jan 08 '13 at 18:59
  • @user1626342: Don't put any spaces around the `\r\n`. I have a feeling that each new line must *not* start with a space. – dreamlax Jan 08 '13 at 21:15
  • I tried it without spaces and got the exact same error. I attached the rest of my code up above? Is there something else I am doing wrong? Thanks for all the help btw! – Bryce Jan 08 '13 at 21:46
  • @user1626342: Try using just the CONNECT line, e.g. `"CONNECT example.com:80 HTTP/1.0\r\n\r\b"`. Maybe it doesn't like the `HOST` command in the request. – dreamlax Jan 08 '13 at 22:38
  • When I tried the new way I got a different type of error. It more or less produced a html page that stated an error. I posted it on the main section as a update. – Bryce Jan 08 '13 at 22:46
  • When I used the "GET" command it pulled up all the data (html) from the page and worked correctly. However I am pretty sure I need to use the connect command since that is what is mentioned all over the web. – Bryce Jan 08 '13 at 23:05
  • Fixed it by changing to an https server. Apparently the connect function does not work well with only http proxys. – Bryce Jan 09 '13 at 00:59
0

Refer this: rfc2616

10.4.1 400 Bad Request

The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.

When you are passing:

char msg[] = "CONNECT example.com:80 HTTP/1.0 <CR><LF> HOST example.com:80 <CR><LF> \n";

Then perhaps the <CR> and <LF> parts are sent as it is as strings, instead of the ASCII 0xA and 0xD, which represent the control characters you intend to send. The simplest fix would be using C equivalents: \r for <CR> and \n for <LF>:

char msg[] = "CONNECT example.com:80 HTTP/1.0 \r\n HOST example.com:80 \r\n\r\n";

If you intend to expand the use of the msg[] array to contain many more requests you might have to write a wrapper function to translate the CR,LF and other control strings in the msg[] into appropriate C char equivalents to form proper message.

Hope this helps.

askmish
  • 6,464
  • 23
  • 42