sample_httpd.c
上传用户:sxxjsc
上传日期:2022-08-08
资源大小:5k
文件大小:2k
- /**
- @file sample_httpd.c
- @author Kevin Lynx
- @brief sample httpd, just for testing purpose.
- */
- #include <winsock2.h>
- #include <stdio.h>
- void http_response( SOCKET con, const char *request )
- {
- /* get the method */
- char *token = strtok( request, " " );
- char *uri = strtok( 0, " " );
- char file[64];
- sprintf( file, ".%s", uri );
- {
- /* load the file content */
- FILE *fp = fopen( file, "rb" );
- if( fp == 0 )
- {
- /* response 404 status code */
- char response[] = "HTTP/1.1 404 NOT FOUNDrnrn";
- send( con, response, strlen( response ), 0 );
- }
- else
- {
- /* response the resource */
- /* first, load the file */
- int file_size ;
- char *content;
- char response[1024];
- fseek( fp, 0, SEEK_END );
- file_size = ftell( fp );
- fseek( fp, 0, SEEK_SET );
- content = (char*)malloc( file_size + 1 );
- fread( content, file_size, 1, fp );
- content[file_size] = 0;
- sprintf( response, "HTTP/1.1 200 OKrnContent-Type: text/htmlrnContent-Length: %drnrn%s", file_size, content );
- send( con, response, strlen( response ), 0 );
- free( content );
- }
- }
- }
- int main()
- {
- WSADATA wd;
- int ret;
- SOCKET s;
- ret = WSAStartup( MAKEWORD( 2, 0 ), &wd );
- if( ret < 0 )
- {
- fprintf( stderr, "winsock startup failedn" );
- exit( -1 );
- }
- s = socket( AF_INET, SOCK_STREAM, 0 );
- {
- /* bind and listen */
- struct sockaddr_in addr;
- memset( &addr, 0, sizeof( addr ) );
- addr.sin_family = AF_INET;
- addr.sin_port = htons( 80 );
- addr.sin_addr.s_addr = inet_addr( "0.0.0.0" );
- ret = bind( s, (struct sockaddr*)&addr, sizeof( addr ) );
- if( ret < 0 )
- {
- fprintf( stderr, "bind failedn" );
- closesocket( s );
- exit( -1 );
- }
- ret = listen( s, 1024 );
- if( ret < 0 )
- {
- fprintf( stderr, "listen failedn" );
- closesocket( s );
- exit( -1 );
- }
- }
- {
- SOCKET con = accept( s, 0, 0 );
- /* recv request */
- char request[1024] = { 0 };
- ret = recv( con, request, sizeof( request ), 0 );
- printf( request );
- http_response( con, request );
- closesocket( con );
- }
- closesocket( s );
- WSACleanup();
- return 0;
- }