📄 235.htm
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>CTerm非常精华下载</title>
</head>
<body bgcolor="#FFFFFF">
<table border="0" width="100%" cellspacing="0" cellpadding="0" height="577">
<tr><td width="32%" rowspan="3" height="123"><img src="DDl_back.jpg" width="300" height="129" alt="DDl_back.jpg"></td><td width="30%" background="DDl_back2.jpg" height="35"><p align="center"><a href="http://apue.dhs.org"><font face="黑体"><big><big>apue</big></big></font></a></td></tr>
<tr>
<td width="68%" background="DDl_back2.jpg" height="44"><big><big><font face="黑体"><p align="center"> ● UNIX网络编程 (BM: clown) </font></big></big></td></tr>
<tr>
<td width="68%" height="44" bgcolor="#000000"><font face="黑体"><big><big><p align="center"></big></big><a href="http://cterm.163.net"><img src="banner.gif" width="400" height="60" alt="banner.gif"border="0"></a></font></td>
</tr>
<tr><td width="100%" colspan="2" height="100" align="center" valign="top"><br><p align="center">[<a href="index.htm">回到开始</a>][<a href="189.htm">上一层</a>][<a href="236.htm">下一篇</a>]
<hr><p align="left"><small>发信人: guru ( Darkness), 信区: unp <br>
标 题: micro_httpd---really small httpd <br>
发信站: UNIX编程 (2001年06月29日13:27:02 星期五), 站内信件 <br>
<br>
发信人: scz (小四), 信区: PUE WWW-POST <br>
标 题: micro_httpd - really small HTTP server <br>
发信站: 武汉白云黄鹤站 (Fri Jun 29 12:57:55 2001) , 转信 <br>
/* micro_httpd - really small HTTP server <br>
** <br>
** Copyright ?1999 by Jef Poskanzer <jef@acme.com>. All rights reserved. <br>
** <br>
** Redistribution and use in source and binary forms, with or without <br>
** modification, are permitted provided that the following conditions <br>
** are met: <br>
** 1. Redistributions of source code must retain the above copyright <br>
** notice, this list of conditions and the following disclaimer. <br>
** 2. Redistributions in binary form must reproduce the above copyright <br>
** notice, this list of conditions and the following disclaimer in the <br>
** documentation and/or other materials provided with the distribution. <br>
** <br>
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND <br>
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE <br>
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOS <br>
E <br>
** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE <br>
** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIA <br>
L <br>
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS <br>
** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) <br>
** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC <br>
T <br>
** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY <br>
<br>
** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF <br>
** SUCH DAMAGE. <br>
*/ <br>
#include <unistd.h> <br>
#include <stdlib.h> <br>
#include <stdio.h> <br>
#include <string.h> <br>
#include <sys/types.h> <br>
#include <sys/stat.h> <br>
#include <sys/time.h> <br>
#define SERVER_NAME "micro_httpd" <br>
#define SERVER_URL "http://www.acme.com/software/micro_httpd/" <br>
#define PROTOCOL "HTTP/1.0" <br>
#define RFC1123FMT "%a, %d %b %Y %H:%M:%S GMT" <br>
/* Forwards. */ <br>
static void send_error( int status, char* title, char* extra_header, char* t <br>
ext <br>
); <br>
static void send_headers( int status, char* title, char* extra_header, char* <br>
mim <br>
e_type, int length, time_t mod ); <br>
static char* get_mime_type( char* name ); <br>
int <br>
main( int argc, char** argv ) <br>
{ <br>
char line[10000], method[10000], path[10000], protocol[10000], idx[20000 <br>
], l <br>
ocation[20000], command[20000]; <br>
char* file; <br>
int len, ich; <br>
struct stat sb; <br>
FILE* fp; <br>
if ( argc != 2 ) <br>
send_error( 500, "Internal Error", (char*) 0, "Config error - no dir <br>
spe <br>
cified. <br>
" ); <br>
if ( chdir( argv[1] ) < 0 ) <br>
send_error( 500, "Internal Error", (char*) 0, "Config error - couldn <br>
't c <br>
hdir(). <br>
" ); <br>
if ( fgets( line, sizeof(line), stdin ) == (char*) 0 ) <br>
send_error( 400, "Bad Request", (char*) 0, "No request found." ); <br>
if ( sscanf( line, "%[^ ] %[^ ] %[^ ]", method, path, protocol ) != 3 ) <br>
send_error( 400, "Bad Request", (char*) 0, "Can't parse request." ); <br>
<br>
while ( fgets( line, sizeof(line), stdin ) != (char*) 0 ) <br>
{ <br>
if ( strcmp( line, "\n" ) == 0 || strcmp( line, "\r\n" ) == 0 ) <br>
break; <br>
} <br>
if ( strcasecmp( method, "get" ) != 0 ) <br>
send_error( 501, "Not Implemented", (char*) 0, "That method is not i <br>
mple <br>
mented. <br>
mented. <br>
" ); <br>
if ( path[0] != '/' ) <br>
send_error( 400, "Bad Request", (char*) 0, "Bad filename." ); <br>
file = &(path[1]); <br>
if ( file[0] == '\0' ) <br>
file = "./"; <br>
len = strlen( file ); <br>
if ( file[0] == '/' || strcmp( file, ".." ) == 0 || strncmp( file, "../" <br>
, 3 <br>
) == 0 || strstr( file, "/../" ) != (char*) 0 || strcmp( &(file[len-3]), "/. <br>
." ) <br>
== 0 ) <br>
send_error( 400, "Bad Request", (char*) 0, "Illegal filename." ); <br>
if ( stat( file, &sb ) < 0 ) <br>
send_error( 404, "Not Found", (char*) 0, "File not found." ); <br>
if ( S_ISDIR( sb.st_mode ) ) <br>
{ <br>
if ( file[len-1] != '/' ) <br>
{ <br>
(void) snprintf( <br>
location, sizeof(location), "Location: %s/", path ); <br>
send_error( 302, "Found", location, "Directories must end with a <br>
sla <br>
sh." ); <br>
} <br>
(void) snprintf( idx, sizeof(idx), "%sindex.html", file ); <br>
if ( stat( idx, &sb ) >= 0 ) <br>
{ <br>
file = idx; <br>
goto do_file; <br>
} <br>
send_headers( 200, "Ok", (char*) 0, "text/html", -1, sb.st_mtime ); <br>
(void) printf( "<HTML><HEAD><TITLE>Index of %s</TITLE></HEAD>\n<BODY <br>
BGC <br>
OLOR=\" <br>
#99cc99\"><H4>Index of %s</H4>\n<PRE>\n", file, file ); <br>
if ( strchr( file, '\'' ) == (char*) 0 ) <br>
{ <br>
(void) snprintf( command, sizeof(command), <br>
"ls -lgF '%s' | tail +2 | sed -e 's/^\\([^ ][^ ]*\\)\\( *[^ <br>
][^ <br>
]* *[^ ][^ ] <br>
* *[^ ][^ ]*\\)\\( *[^ ][^ ]*\\) *\\([^ ][^ ]* *[^ ][^ ]* *[^ ][^ ]*\\) <br>
*\ <br>
*\ <br>
\(.*\\)$/\\1 \\3 \\4 |\\5/' -e '/ -> /!s,|\\([^*]*\\)$,|<A HREF=\"\\1\">\\ <br>
1</A <br>
>,' -e '/ -> /!s,|\\(.*\\)\\([*]\\)$,|<A HREF=\"\\1\">\\1</A>\\2,' -e '/ -> <br>
/s,| <br>
\\([^@]*\\)\\(@* -> \\),|<A HREF=\"\\1\">\\1</A>\\2,' -e 's/|//'", <br>
file ); <br>
(void) fflush( stdout ); <br>
system( command ); <br>
} <br>
(void) printf( "</PRE>\n<HR>\n<ADDRESS><A HREF=\"%s\">%s</A></ADDRES <br>
S>\n <br>
</BODY> <br>
</HTML>\n", SERVER_URL, SERVER_NAME ); <br>
} <br>
else <br>
{ <br>
do_file: <br>
fp = fopen( file, "r" ); <br>
if ( fp == (FILE*) 0 ) <br>
send_error( 403, "Forbidden", (char*) 0, "File is protected." ); <br>
<br>
send_headers( 200, "Ok", (char*) 0, get_mime_type( file ), sb.st_siz <br>
e, s <br>
b.st_mt <br>
ime ); <br>
while ( ( ich = getc( fp ) ) != EOF ) <br>
putchar( ich ); <br>
} <br>
(void) fflush( stdout ); <br>
exit( 0 ); <br>
} <br>
static void <br>
send_error( int status, char* title, char* extra_header, char* text ) <br>
{ <br>
send_headers( status, title, extra_header, "text/html", -1, -1 ); <br>
(void) printf( "<HTML><HEAD><TITLE>%d %s</TITLE></HEAD>\n<BODY BGCOLOR=\ <br>
"#cc <br>
9999\"><H4>%d %s</H4>\n", status, title, status, title ); <br>
(void) printf( "%s\n", text ); <br>
(void) printf( "<HR>\n<ADDRESS><A HREF=\"%s\">%s</A></ADDRESS>\n</BODY>< <br>
/HTM <br>
L>\n", SERVER_URL, SERVER_NAME ); <br>
(void) fflush( stdout ); <br>
exit( 1 ); <br>
exit( 1 ); <br>
} <br>
static void <br>
send_headers( int status, char* title, char* extra_header, char* mime_type, <br>
int <br>
length, time_t mod ) <br>
{ <br>
time_t now; <br>
char timebuf[100]; <br>
(void) printf( "%s %d %s\r\n", PROTOCOL, status, title ); <br>
(void) printf( "Server: %s\r\n", SERVER_NAME ); <br>
now = time( (time_t*) 0 ); <br>
(void) strftime( timebuf, sizeof(timebuf), RFC1123FMT, gmtime( &now ) ); <br>
<br>
(void) printf( "Date: %s\r\n", timebuf ); <br>
if ( extra_header != (char*) 0 ) <br>
(void) printf( "%s\r\n", extra_header ); <br>
if ( mime_type != (char*) 0 ) <br>
(void) printf( "Content-Type: %s\r\n", mime_type ); <br>
if ( length >= 0 ) <br>
(void) printf( "Content-Length: %d\r\n", length ); <br>
if ( mod != (time_t) -1 ) <br>
{ <br>
{ <br>
(void) strftime( timebuf, sizeof(timebuf), RFC1123FMT, gmtime( &mod <br>
) ); <br>
(void) printf( "Last-Modified: %s\r\n", timebuf ); <br>
} <br>
(void) printf( "Connection: close\r\n" ); <br>
(void) printf( "\r\n" ); <br>
} <br>
static char* get_mime_type( char* name ) <br>
{ <br>
char* dot; <br>
dot = strrchr( name, '.' ); <br>
if ( dot == (char*) 0 ) <br>
return "text/plain; charset=iso-8859-1"; <br>
if ( strcmp( dot, ".html" ) == 0 || strcmp( dot, ".htm" ) == 0 ) <br>
return "text/html; charset=iso-8859-1"; <br>
if ( strcmp( dot, ".jpg" ) == 0 || strcmp( dot, ".jpeg" ) == 0 ) <br>
return "image/jpeg"; <br>
if ( strcmp( dot, ".gif" ) == 0 ) <br>
return "image/gif"; <br>
if ( strcmp( dot, ".png" ) == 0 ) <br>
return "image/png"; <br>
if ( strcmp( dot, ".css" ) == 0 ) <br>
return "text/css"; <br>
if ( strcmp( dot, ".au" ) == 0 ) <br>
return "audio/basic"; <br>
if ( strcmp( dot, ".wav" ) == 0 ) <br>
return "audio/wav"; <br>
if ( strcmp( dot, ".avi" ) == 0 ) <br>
return "video/x-msvideo"; <br>
if ( strcmp( dot, ".mov" ) == 0 || strcmp( dot, ".qt" ) == 0 ) <br>
return "video/quicktime"; <br>
if ( strcmp( dot, ".mpeg" ) == 0 || strcmp( dot, ".mpe" ) == 0 ) <br>
return "video/mpeg"; <br>
if ( strcmp( dot, ".vrml" ) == 0 || strcmp( dot, ".wrl" ) == 0 ) <br>
return "model/vrml"; <br>
if ( strcmp( dot, ".midi" ) == 0 || strcmp( dot, ".mid" ) == 0 ) <br>
return "audio/midi"; <br>
if ( strcmp( dot, ".mp3" ) == 0 ) <br>
return "audio/mpeg"; <br>
if ( strcmp( dot, ".pac" ) == 0 ) <br>
return "application/x-ns-proxy-autoconfig"; <br>
return "text/plain; charset=iso-8859-1"; <br>
} <br>
-- <br>
也许有一天,他再从海上蓬蓬的雨点中升起, <br>
飞向西来,再形成一道江流,再冲倒两旁的石壁, <br>
再来寻夹岸的桃花。然而,我不敢说来生,也不敢信来生...... <br>
-- <br>
※ 来源:·UNIX编程 www.tiaozhan.com/unixbbs/·[FROM: 202.114.36.239] <br>
</small><hr>
<p align="center">[<a href="index.htm">回到开始</a>][<a href="189.htm">上一层</a>][<a href="236.htm">下一篇</a>]
<p align="center"><a href="http://cterm.163.net">欢迎访问Cterm主页</a></p>
</table>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -