Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 25th, 2005, 12:14 AM   #1
kch_86
Newbie
 
Join Date: Nov 2005
Posts: 2
Rep Power: 0 kch_86 is on a distinguished road
send() problem w/ http server

I'm writing a simple server for my cs class. Whenever i run the server, i can access it just fine with firefox/iexplorer and it displays my simple home page. It handles 404's just fine too. My problem is that whenever i connect to my server w/ telnet/netcat it doesnt return the header info. i.e. telnet/netcat never displays "HTTP 200 OK...." etc. So obviously there's a problem, it's just not affecting the browser. Anyone have any ideas?

// handles a get header f/m client.
static void handle_get (int connection_fd, const char* page)
{
       char response[1024];
       //char *string = "HTTP/1.0 OK 200";

  /* Test for root/home page*/
  if (!strcmp(page,"/")) {
            //fprintf(stderr, "handle_get\n");
            //send(connection_fd, string, strlen(string),0);
         send_head(connection_fd, 200, strlen(HOME_PAGE));
         send (connection_fd, HOME_PAGE, strlen (HOME_PAGE),0);
  }

  else {
    /* Send the HTTP response indicating success, and the HTTP header
       for an HTML page.  */
      send_head(connection_fd, 404, strlen(ERROR_404));
    send (connection_fd, response, strlen (response),0);
  }
}
/* Process an HTTP "HEAD" request for PAGE, and send the results to the
   file descriptor CONNECTION_FD.  */
void handle_head(int connection_fd, const char* page)
{
     if(*page == '/')
     {
         send_head(connection_fd, 200, 0);
     }
}


//send_head - send an HTTP 1.0 header with given status and content-len
 
void send_head(int conn_fd, int status, int len)
{
	char *statstr, buff[BUFFSIZE];

	/* convert the status code to a string */

	switch(status) {
	case 200:
		statstr = "OK";
		break;
	case 400:
		statstr = "Bad Request";
		break;
	case 404:
		statstr = "Not Found";
		break;
	default:
		statstr = "Unknown";
		break;
	}
	/*
	 * send an HTTP/1.0 response with Server, Content-Length,
	 * and Content-Type headers.
	 */
	(void) sprintf(buff, "HTTP/1.0 %d %s\r\n", status, statstr);
	(void) send(conn_fd, buff, strlen(buff), 0);

	(void) sprintf(buff, "Server: %s\r\n", SERVER_NAME);
	(void) send(conn_fd, buff, strlen(buff), 0);

	(void) sprintf(buff, "Content-Length: %d\r\n", len);
	(void) send(conn_fd, buff, strlen(buff), 0);

	(void) sprintf(buff, "Content-Type: text/html\r\n");
	(void) send(conn_fd, buff, strlen(buff), 0);

	(void) sprintf(buff, "\r\n");
	(void) send(conn_fd, buff, strlen(buff), 0);
}
kch_86 is offline   Reply With Quote
Old Nov 25th, 2005, 12:18 AM   #2
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
I'd reccomend using a packet sniffer such as Ethereal or Ettercap to determine what the browser and server are exchanging compared to nc or telnet.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270
Dameon is offline   Reply With Quote
Old Nov 25th, 2005, 12:53 AM   #3
kch_86
Newbie
 
Join Date: Nov 2005
Posts: 2
Rep Power: 0 kch_86 is on a distinguished road
alright, thanks ill try that
kch_86 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:42 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC