libhttpd is a lib for supporting interaction using http protocols. you can use it to quickly build an http server. LibHttpd是一个开源轻量级嵌入式Web server,LibHttpd实现了下述功能:
- 实现了HTTP的子集;
- 使用表格技术自动处理Html Form数据;
- 产生的内容既可以是静态的网页,也可以是调用C函数动态产生的(callback);
LibHttpd提供API,利用这些API,用户可以很方便地将自己的Web内容加入到程序当中。 LibHTTPD相关函数介绍 ----------------------------------------------------------------------------------- httpdCreate ( ) 创建WEB服务器 httpd * httpdCreate ( host , port ) char * host int port Example : server = httpdCreate( “192.168.1.1”, HTTP_PORT); if ( server == NULL ) perror ( “Couldn’t create HTTP server” ); server2 = httpdCreate ( NULL , 2048 ); ------------------------------------------------------------------------------------ httpdSetAccessLog( ) 设置访问日志文件 httpdSetAccessLog ( server, fp ) httpd *server; FILE *fp; Example : fp = fopen ( “/tmp/access.log”, “a” ); httpdSetAccessLog ( server, fp ); -------------------------------------------------------------------- httpdSetErrorLog( ) 设置错误日志文件 httpdSetErrorLog ( server, fp ) httpd *server; FILE *fp; Example : httpdSetErrorLog ( server, stderr ); ------------------------------------------------------------------------ httpdSetFileBase( ) 设置文件基本路径名 httpdSetErrorLog ( server, path ) httpd *server; char *path; Example : httpdSetFileBase ( server, “/usr/local/www-pages” ); ------------------------------------------------------------------------ httpdAddCContent( ) 调用C函数产生输出内容 httpdAddCContent ( server, dir, name, indexFlag, preload, functPtr ) httpd *server; char *dir, *name; int indexFlag, (*)( )preload; void (*)( ) functPtr; Example : void index_callbackl ( server ) httpd *server; { httpdOutput(server, “ Hello There\n”); } httpdAddCContent( server, “/”, “index.html”, HTTP_TRUE, NULL, index_callback); ------------------------------------------------------------------------------------ httpdAddFileContent( ) 将一个外部文件加入到输出内容 httpdAddFileContent ( server, dir, name, indexFlag, preload, path ) httpd *server; char *dir, *name; int indexFlag, (*) ( ) preload; char *path; Example : httpdAddFileContent( server, “/”, “index.html”, HTTP_TRUE, NULL, “/usr/local/www/index.html” ); ----------------------------------------------------------------------------------- httpdAddStaticContent( ) 将一个内部文本BUFFER加入到HTML输出内容 httpdAddStaticContent ( server, dir, name, indexFlag, preload, buf ) httpd *server; char *dir, *name; int indexFlag, (*)( ) preload; char *buf; Example : #define index_content “ Hello There\n” httpdAddStaticContent( server, “/”, “index.html”, HTTP_TRUE, NULL, index_content ); ------------------------------------------------------------------------------------ httpdAddWildcardContent( ) 增加与通配符匹配的文件内容 httpdAddWildcardContent ( server, dir, preload, path ) httpd *server; char *dir; int (*) ( )preload; char *path; Example : httpdAddWildcardContent(server,“/graphics”, NULL, “/usr/local/www/graphics” ); ----------------------------------------------------------------------------------- httpdAddCWildcardContent( ) 请求指定目录中的任何文件时调用C回调函数 httpdAddCWildcardContent ( server, dir, preload, functPtr ) httpd *server; char *dir; int (*) ( )preload; void (*)( ) functPtr; Example : httpdAddCWildcardContent(server,“/users”, NULL, send_user_info ); --------------------------------------------------------------------------------- httpdGetConnection ( ) 接受一个HTTP连接请求 int httpdGetConnection ( server , timeout) httpd *server; struct timeval *timeout; ----------------------------------------------------------------------- httpdReadRequest ( ) 读取并保存从客户端发送过来的请求和数据 int httpdReadRequest ( server ) httpd *server; ------------------------------------------------------------------------- httpdProcessRequest ( ) 对请求进行处理,并将请求内容发送到客户端浏览器 httpdProcessRequest ( server ) httpd *server; ---------------------------------------------------------------------- httpdEndRequest ( ) 请求结束处理 httpdEndRequest ( server ) httpd *server; ---------------------------------------------------------------------- httpdOutput ( ) 将文本BUFFER内容发送到客户端浏览器 httpdOutput ( server, buffer ) httpd *server; char *buffer Example : httpdOutput ( server, “Hello $name. Welcome to the test server” ); ---------------------------------------------------------------------- httpdPrintf ( ) 按指定格式将内容输出到客户端浏览器 httpdPrintf ( server, format, arg, arg, … ) httpd *server; char *format; Example : httpdPrintf( server, “Hello %s. Welcome to the server running in process number %d”, username, getpid( ) ); ------------------------------------------------------------------------- httpdSetContentType ( ) 设置除HTML文本以外的内容类型 httpdSetContentType( server, type ) httpd *server; char *type Example : httpdSetContentType ( server, “image/jpeg” ); ------------------------------------------------------------------------ httpdSetResponse ( ) 设置返回给客户端浏览器的的响应代码 httpdSetResponse( server, responseInfo ) httpd *server; char *responseInfo; Example : httpdSetResponse ( server, “301 Moved Permanently” ); ------------------------------------------------------------------- httpdAddHeader ( ) 增加HTML头内容 httpdAddHeader( server, header ) httpd *server; char *header; Example : httpdSetResponse ( server, “307 Temporary Redirect” ); httpdAddHeader ( server, “Location: http://www.foo.com/some/new/location”); --------------------------------------------------------------------------------- httpdSendHeaders( ) 发送HTML头 httpdSendHeaders( server) httpd *server; Example : httpdSetContentType ( server, “image/jpeg” ); httpdSendHeaders ( server ); generateJpegData( server ); --------------------------------------------------------------------------------- httpVar * httpdGetVariableByName ( ) 在符号表中查找变量 httpdGetVariableByName( server, varName ) httpd *server; char *varName; Example : varPtr = httpdGetVariableByName ( server, “username” ); if ( varPtr != NULL) uname = varPtr->value ; ------------------------------------------------------------------------------ httpVar * httpdGetVariableByPrefix ( ) 获取第一个与指定前缀相匹配的变量 httpdGetVariableByPrefixe( server, prefix ) httpd *server; char *prefix; ------------------------------------------------------------------------- httpVar * httpdGetNextVariableByPrefix ( ) 获取下一个与指定前缀相匹配的变量 httpdGetNextVariableByPrefixe( varPtr, prefix ) httpVar *varPtr; char *prefix; Example : varPtr = httpdGetVariableByPrefix ( server, “hughes_” ); while ( varPtr != NULL ) { printf(“Name = %s, Value = %s \n”, varPtr->name, varPtr->value; varPtr = httpdGetNextVariableByPrefix ( varPtr, “hughes_” ); } ------------------------------------------------------------------------ httpVar * httpdGetVariableByPrefixedName ( ) 在符号表中查找变量 httpdGetVariableByPrefixedName( varPtr, prefix, remainder ) httpVar *varPtr; char *prefix, *remainder; Example : prefixPtr = httpdGetVariableByName ( server, “multi-select-values” ); while ( prefixPtr != NULL ) { prefix = prefixPtr->value; varPtr = httpdGetVariableByPrefixedName(server, prefix, “_username”); printf(“%s_username = %s\n”, prefix, varPtr->value; prefixPtr = prefixPtr->nextValue; } ------------------------------------------------------------------------------ httpdAddVariable( ) 在符号表中增加变量 httpdAddVariable( server, name, value ) httpd *server; char *name, *value; Example : httpdAddVariable( server, “background_color”, “#FFFF30” ); httpdOutput( server, “ \n”); --------------------------------------------------------------------------- httpdDumpVariables( ) Dump符号表内容 httpdDumpVariables( server ) httpd *server; ------------------------------------------------------------------------- httpdSet( ) 设置 httpdSet( server, name, value ) httpd *server; char *name, *value; --------------------------------------------------------------------------- httpdAuthenticate( ) 使用用户名和口令进行身份认证 httpdAuthenticate( server, realm ) httpd *server; char *realm; --------------------------------------------------------------------------- httpdForceAuthenticate( ) 强迫身份认证 httpdForceAuthenticate( server, realm ) httpd *server; char *realm; ------------------------------------------------------------------------------ httpdAddAcl( ) 在ACL表中增加访问控制项 httpdAddAcl( server, acl, cidrAddr, action ) httpd *server; httpAcl *acl; char *cidrAddr; int action ----------------------------------------------------------------------------- httpdSetDefaultAcl( ) 设置默认ACL httpdSetDefaultAcl( server, acl ) httpd *server; httpAcl *acl; -------------------------------------------------------------------- httpdCheckAcl( ) 进行ACL检查 httpdCheckAcl( server, acl ) httpd *server; httpAcl *acl; ------------------------------------------------------------------- httpdUrlEncode( ) 进行URL解码 char * httpdUrlEncode( buf ) char *buf; ------------------------------------------------------------------- httpdRequestMethod( ) 获取访问方式(HTTP_GET/HTTP_POST) int httpdRequestMethod( server ) httpd *server; -------------------------------------------------------------------- httpdRequestMethodName( ) 获取访问方式名字 char *httpdRequestMethodName( server ) httpd *server; ------------------------------------------------------------------ httpdRequestPath( ) 获取URL请求路径 char *httpdRequestPath( server ) httpd *server; ---------------------------------------------------------------------- httpdRequestContentType( ) 获取当前请求内容类型 char *httpdRequestContentType( server ) httpd *server; ----------------------------------------------------------------------- httpdRequestContentLength( ) 获取当前请求发送的内容长度 int httpdRequestContentLength( server ) httpd *server; ----------------------------------------------------------------------- (xgzhang |