patrik wrote:
@koaftder:
The reason why you are getting a "400 Bad Request" response is because you are specifying that your client is a HTTP/1.1 client, which requires you to supply the "Host: something.com" header-line, which is optional in HTTP/1.0, but required for virtual hosts to work, so it is definately recommended to supply it anyhow.
With a simple client, there is no advantage in telling the server that your client supports HTTP/1.1 instead of HTTP/1.0, rather disadvantages as then the server is allowed to send you dynamic pages as chunks using the so called "chunked transfer-coding".
/Patrik
ok, so i will just capture what fire fox sends out when i goto amiga.org, here is the fix :-)
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main ( void ) {
int socket_handle ;
struct sockaddr_in socket_detials ;
char * input_buffer;
char * httpget =
"GET / HTTP/1.1\r\n"
"Host: www.amiga.org\r\n"
"User-Agent: Mozilla/5.0 (X11; U; Linux ppc; en-US; rv:1.7.10) Gecko/20050825 Firefox/1.0.6 (Ubuntu package 1.0.6)\r\n"
"Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n"
"Accept-Language: en-us,en;q=0.5\r\n"
"Accept-Encoding: gzip,deflate\r\n"
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"
"Keep-Alive: 300\r\n"
"Connection: keep-alive\r\n"
"Referer: http://www.amiga.org/gallery/index.php?n=896=33\r\n"
"Cookie: PHPSESSID=442105507b7dca6d4042a641fc132c8f; AO_Session=442105507b7dca6d4042a641fc132c8f\r\n"
"Cache-Control: max-age=0\r\n"
"\r\n";
input_buffer = malloc(20000);
socket_handle = socket ( AF_INET, SOCK_STREAM, 0) ;
socket_detials.sin_family = AF_INET ;
socket_detials.sin_addr.s_addr=inet_addr("68.90.68.66");
socket_detials.sin_port = htons(80);
bzero ( &(socket_detials.sin_zero), 8 ) ;
if ( connect (socket_handle,(struct sockaddr*)&socket_detials, sizeof ( struct sockaddr)) == -1 ){
printf ( "Couldnt connect to server\n" ) ;
}
printf ( "Sending %d bytes\n", send ( socket_handle , httpget, strlen(httpget), 0 ) ) ;
printf ( "Received %d bytes\n", recv ( socket_handle , input_buffer , 20000, 0 ) ) ;
printf ( "%s\n", input_buffer ) ;
return 0 ;
}
and it returns the following now:
koft@macdev:~$ ./socket
Sending 612 bytes
Received 1460 bytes
HTTP/1.1 200 OK
Date: Sat, 21 Jan 2006 05:03:13 GMT
Server: Apache/1.3.34 (Unix) mod_auth_passthrough/1.8 mod_log_bytes/1.2 mod_bwlimited/1.4 PHP/4.4.1 FrontPage/5.0.2.2635 mod_ssl/2.8.25 OpenSSL/0.9.7a
X-Powered-By: PHP/4.4.1
Set-Cookie: PHPSESSID=442105507b7dca6d4042a641fc132c8f; path=/
Expires: Mon, 26 Jul 1997 05:00:00 GMT
Cache-Control: private, no-cache
Pragma: no-cache
Set-Cookie: AO_Session=442105507b7dca6d4042a641fc132c8f; expires=Saturday, 28-Jan-06 05:03:14 GMT; path=/
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=ISO-8859-1
d19
koft@macdev:~$
i broke up one of thoes lines because it was annoying
Writing network code can be a lot of fun, though it can be a major undertaking if you have to write something more than some hacks for a hobby project.
I have a few books about socket programming for windows and linux, they cover a lot of material but dont seem to dig into some detials i'd like to fill in without having to experiment for years and write mountians of code. Multithreaded socket programming comes into mind, most books seem to skirt around the subject. If i'm writing a server daemon for something, i'm going to need to handle a lot of simultaneous connections. Do i use blocking or non blocking sockets? Do i spawn off a thread for each socket, and make it a blocking socket? Should i spawn of a thread for every 10 sockets and do non blocking io? Should i fork the daemon 5 times and load balance the connections accross the processes? How do i effectively deal with resource starvation caused by jerks who write scripts that keep opening hundreds of connections and letting them hang? Thoes are the topics i'd like to see covered in a book, performance strategies and security issues. I really really dont have time to read all the socket code for apache.