#include
#include
#include
#include
#include
#include
#include
static void emit_response_header(int sock, char *content_type)
{
char resp_header[256];
/* Construct the response header (including the variable
* content type) and send it to the client through the
* socket.
*/
sprintf( resp_header,
"HTTP/1.1 200 OK\n"
"Server: C shttp\n"
"Connection: close\n"
"Content-Type: %s\n\n", content_type );
write( sock, resp_header, strlen(resp_header) );
return;
}
static char * define_content_type(char *filename)
{
return "text/plain";
}
static void handle_get_method( int sock, char *filename )
{
int fd, ret;
char buffer[255+1];
char file[254]={0};
char fileRoot[]="/home/qq";
strcat(file,fileRoot);
printf("length is %d",strlen(filename));
if(strlen(filename) < 3)
strcat(file,"/index.html");
else strcat(file,filename);
/* Try to open the file requested by the client */
fd = open( file, O_RDONLY );
if (fd == -1) {
/* Can't find, emit not found error to the client */
const char *notfound = {"HTTP/1.0 404\n\n File not found.\n\n"};
write( sock, notfound, strlen( notfound ) );
printf("File not found.\n");
} else {
char *content_type;
content_type = define_content_type( filename );
emit_response_header( sock, content_type );
/* Read and emit the file contents to the client
* through the socket.
*/
ret = 1;
while ( ret > 0 )
{
ret = read( fd, buffer, 255 );
if ( ret > 0 ) {
write( sock, buffer, ret );
} else break;
}
/* Emit a newline */
write( sock, "\015\012", 2 );
close( fd );
}
return;
}
static void getfilename( char *buff,char *filename,int s)
{
char *p=buff;
int num= 0;
for(p=buff+s; *p ; p++)
{
if(*p == ' ')
if( num == 0)
{
s++;
continue;
}else {
break;
}
num++;
}
printf("filename length is %d\n\n",strlen(filename));
strlcpy(filename,buff+s,num+1);
printf("filename length is %d\n\n",strlen(filename));
}
static void handle_connection( int sock )
{
int len, max, ret;
char inbuf[1024]={0};
max = 0;
/* Read in the HTTP request message from the socket */
while ( 1 ) {
len = read( sock, &inbuf[max], (1024-max) );
if (len <= 0) return;
/* Update the total string size */
max += len;
/* HTTP request message ends with CRLF/CRLF */
if ((inbuf[max-4] == 0x0d) && (inbuf[max-3] == 0x0a) &&
(inbuf[max-2] == 0x0d) && (inbuf[max-1] == 0x0a)) {
break;
}
inbuf[max] = 0;
}
/* Determine the request type */
if (!strncmp( inbuf, "GET", 3)) {
char filename[100];
getfilename( inbuf, filename, 4 );
handle_get_method( sock, filename );
} else {
const char *notimpl=
{"HTTP/1.0 501 Not Implemented.\n\n"};
printf("Unknown Method %s\n", inbuf);
/* Unknown file ?notify client */
write( sock, notimpl, strlen(notimpl) );
}
}
static void start( unsigned short port )
{
int serverfd, clientfd, on=1;
struct sockaddr_in servaddr;
/* Create a new TCP server */
serverfd = socket( AF_INET, SOCK_STREAM, 0 );
/* Make the port immediately reusable after this socket
* is close.
*/
// setsockopt( serverfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on) );
/* Initialize the address structure to which we'll bind */
bzero( (void *)&servaddr, sizeof(servaddr) );
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl( INADDR_ANY );
servaddr.sin_port = htons( port );
/* Bind the address to the socket */
bind( serverfd,
(struct sockaddr *)&servaddr, sizeof(servaddr) );
/* Make the new server visible for incoming connections */
listen( serverfd, 5 );
while ( 1 ) {
/* Await a connection from a client socket */
clientfd = accept( serverfd, NULL, NULL );
if (clientfd <= 0) break;
/* handle the new connection */
handle_connection( clientfd );
/* Close the client connection */
close( clientfd );
}
/* Close the server socket */
close( serverfd );
return;
}
int main (void)
{
start(8080);
return 0;
}