⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 davsample.c

📁 www工具包. 这是W3C官方支持的www支撑库. 其中提供通用目的的客户端的WebAPI: complete HTTP/1.1 (with caching, pipelining, PUT, POS
💻 C
📖 第 1 页 / 共 2 页
字号:
/* ** HTDAV test file **** Using this program, you may test all WebDAV high level functions available ** in libwww, and also test your webserver, if you want. **** Authors: **      MKP     Manuele Kirsch Pinheiro (Manuele.Kirsch_Pinheiro@inrialpes.fr, manuele@inf.ufrgs.br)**              _ Project CEMT  (INRIA Rhone-Alpes,France / UFRGS-II,Brazil) _**** History:**      fev, 2002       created                 MKP**                                ** $Id: davsample.c,v 1.4 2002/05/30 18:08:56 kirschpi Exp $*/#include "WWWLib.h"#include "WWWInit.h"#include "WWWApp.h"#include "HTDAV.h"                /* here - WebDAV high level functions*/#define APP_NAME                "DAVSample"#define APP_VERSION             "2.0"#define ERR_UNKNOWN               0x0#define ERR_FATAL                 0x1#define ERR_NON_FATAL             0x2#define ERR_WARN                  0x4#define ERR_INFO                  0x8/***  command line struct */typedef struct _cmdline {    char   method;    int    func;    char * D;    char * I;    char * T;    char   O;        char * request_uri;    char * base_str;    char * arg1;    char *arg2;} Cmdline;/* ** callbacks */PRIVATE HTNetAfter terminate_handler;PRIVATE HTNetAfter error_callback; PRIVATE int printer (const char * fmt, va_list pArgs);PRIVATE int tracer (const char * fmt, va_list pArgs);/*** prototypes */ PRIVATE void Init (void);PRIVATE void my_headers (HTRequest *request);PRIVATE char * create_body (char * owner);PRIVATE char * create_propbody (char * prop);PRIVATE HTRequest * create_request (void);PRIVATE BOOL lock_request (Cmdline * arg); PRIVATE BOOL propfind_request (Cmdline * arg); PRIVATE BOOL unlock_request (Cmdline * arg); PRIVATE BOOL proppatch_request(Cmdline * arg); PRIVATE BOOL mkcol_request (Cmdline * arg); PRIVATE BOOL copy_request (Cmdline * arg); Cmdline * handle_args (int argc, char **argv);/* -------------------------------------------------------------------------------- * *                                 Filters                                          * * -------------------------------------------------------------------------------- *//*** printer : output messages function*/PRIVATE int printer (const char * fmt, va_list pArgs){    return (vfprintf(stdout, fmt, pArgs));}/*** trace : trace messages function*/PRIVATE int tracer (const char * fmt, va_list pArgs){    return (vfprintf(stderr, fmt, pArgs));}/*** request_terminater : global filter to delete the requests** Funtion's type : HTNetAfter*/ PRIVATE int terminate_handler (HTRequest * request, HTResponse * response,                               void * param, int status) {    my_headers(request);     if (response) {        HTPrint ("\tStatus:%d\n\tContent-length:%d\n\tIs Cachable:%c\n\tis Cached:%c\n\tReason: %s\n",\                  status, HTResponse_length(response),\                  (HTResponse_isCachable(response))?'Y':'N',\                  (HTResponse_isCached(response,YES))?'Y':'N', \                  (HTResponse_reason(response))?HTResponse_reason(response):"NULL");            HTPrint ("\tFormat : %s \n",(char *)HTAtom_name(HTResponse_format(response)));     }    else         HTPrint ("\tResponse NULL\n");        /* Terminate libwww */    HTProfile_delete();    exit(0);}/*** error_callback : global after filter to treat the request's errors** Function's type : HTNetAfter*/PRIVATE int error_callback (HTRequest * request, HTResponse * response,                                  void * param, int status) {    HTList * error_list = NULL;    HTError * error = NULL;        HTPrint ("%s: ERROR CALLBACK\n",APP_NAME);    HTPrint ("\trequest %s \n\tresponse %s \n\tstatus %d\n", \            (request)?"OK":"NULL",\            (response)?"OK":"NULL",status);        if (request) {        error_list = HTRequest_error (request);        while (error_list && (error = (HTError *) HTList_nextObject(error_list))) {            HTPrint ("\tError location %s\n",HTError_location(error));            switch (HTError_severity(error)) {                case ERR_UNKNOWN :                    HTPrint ("\tSeverity : UNKNOWN\n");                    break;                case ERR_FATAL :                    HTPrint ("\tSeverity : FATAL\n");                    break;                case ERR_NON_FATAL :                    HTPrint ("\tSeverity : NON FATAL\n");                    break;                case ERR_WARN :                    HTPrint ("\tSeverity : WARN\n");                    break;                case ERR_INFO :                    HTPrint ("\tSeverity : INFO\n");                    break;                default :                    HTPrint ("\tSeverity : %Xd\n",HTError_severity(error));                    break;           }            }    }    return HT_OK;}/* -------------------------------------------------------------------------------- * *                               Common Functions                                   * * -------------------------------------------------------------------------------- *//*** my_headers : shows anchor's headers** Parameters : HTRequest * request*/  PRIVATE void my_headers (HTRequest *request) {    HTAssoc * h = NULL;        HTAssocList * headers = NULL;     HTParentAnchor * anchor = NULL;    HTPrint ("%s: Searching headers...\n",APP_NAME);          anchor = HTRequest_anchor (request);    headers = HTAnchor_header(anchor);        HTPrint ("\tanchor %s\n",(anchor)?"OK":"NULL");          h = HTAssocList_nextObject(headers);    while (anchor && headers && h ) {       HTPrint ("\t%s : %s\n",HTAssoc_name(h),HTAssoc_value(h));       h = HTAssocList_nextObject(headers);    }}/*** Init : application start */ PRIVATE void Init (void) {    /* Create a new premptive client */    HTProfile_newNoCacheClient(APP_NAME, APP_VERSION);    /* Gotta set up our own traces */    HTPrint_setCallback(printer);    HTTrace_setCallback(tracer);    /* Add our own filter to update the history list */    HTNet_addAfter(terminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST);    HTNet_addAfter(error_callback, NULL, NULL, HT_ERROR, HT_FILTER_LAST);     /* Set trace messages and alert messages*/#if 0    HTSetTraceMessageMask("spolc");#endif}PRIVATE HTRequest * create_request (void) {    HTRequest * me = HTRequest_new();    /* set output and debug streans */    HTRequest_setOutputStream(me, HTFWriter_new(me, stdout, YES));    HTRequest_setDebugStream(me, HTFWriter_new(me, stderr, YES));    HTRequest_setOutputFormat(me,HTAtom_for("text/xml"));    HTRequest_setDebugFormat(me,WWW_SOURCE);    return me;       }/* -------------------------------------------------------------------------------- * *                               XML Body functions                                 * * -------------------------------------------------------------------------------- */PRIVATE char * create_body (char * owner) {     char tmp[4096];     char * body = NULL;     char nl[3];          sprintf (nl,"%c%c",CR,LF);              if (owner && *owner) {         sprintf (tmp,"<?xml version=\"1.0\" encoding=\"utf-8\" ?>");         strcat (tmp,nl);         strcat (tmp,"<D:lockinfo xmlns:D=\"DAV:\">");         strcat (tmp,nl);         strcat (tmp,"    <D:lockscope><D:exclusive/></D:lockscope>");         strcat (tmp,nl);         strcat (tmp,"    <D:locktype><D:write/></D:locktype>");         strcat (tmp,nl);         strcat (tmp,"    <D:owner>");         strcat (tmp,nl);         strcat (tmp,"        <D:href>");         strcat (tmp,owner);         strcat (tmp,"</D:href>");         strcat (tmp,nl);         strcat (tmp,"    </D:owner>");         strcat (tmp,nl);         strcat (tmp,"</D:lockinfo>");         if ( (body = HT_MALLOC (strlen(tmp)+4)) != NULL)             sprintf (body,"%s%c",tmp,'\0');     }          return body;}PRIVATE char * create_propbody (char * prop) {     char tmp[5120];     char * body = NULL;     char nl[3];        sprintf (nl,"%c%c",CR,LF);              if (prop && *prop) {         sprintf (tmp,"<?xml version=\"1.0\" encoding=\"utf-8\" ?>");         strcat (tmp,nl);         strcat (tmp,"<D:propfind xmlns:D=\"DAV:\">");         strcat (tmp,nl);         if (!strcmp(prop,"allprop")) strcat (tmp,"<D:allprop />");         else if (!strcmp(prop,"propname")) strcat (tmp,"<D:propname />");          else strcat (tmp,prop);         strcat (tmp,nl);         strcat (tmp,"</D:propfind>");         if ( (body = HT_MALLOC (strlen(tmp)+4)) != NULL)             sprintf (body,"%s%c",tmp,'\0');     }          return body;}/* -------------------------------------------------------------------------------- * *                                WebDAV Methods                                    * * -------------------------------------------------------------------------------- *//*** LOCK requests*/PRIVATE BOOL lock_request (Cmdline * arg) {    HTDAVHeaders * headers = HTDAVHeaders_new();    HTRequest * request = create_request ();    HTAnchor * dst = HTAnchor_findAddress(arg->request_uri);    HTParentAnchor * src = NULL;    HTParentAnchor * base = NULL;    BOOL status = NO;    char * data = NULL;            if (arg->I) {        HTPrint ("Adding If header %s\n",arg->I);        HTDAV_setIfHeader (headers,arg->I);    }    if (arg->arg1)  {        data = create_body (arg->arg1);        HTPrint ("xml body  %s\n",data);                   /* chose the func */        if (arg->func==1) {            src = HTTmpAnchor(NULL);            HTAnchor_setDocument(src, data);            HTAnchor_setFormat(src, HTAtom_for ("text/xml"));            HTAnchor_setLength(src, strlen(data));        }     }    if (arg->base_str && *(arg->base_str))         base = (HTParentAnchor *) HTAnchor_findAddress(arg->base_str);    if (arg->D) HTDAV_setDepthHeader (headers,arg->D);    if (arg->T) HTDAV_setTimeoutHeader (headers,arg->T);    HTPrint ("function %d src? %s\n",arg->func,(src)?"yes":"no");    switch (arg->func) {        case 1: status = HTLOCKDocumentAnchor (request,dst,src,headers);                break;        case 2: status = HTLOCKAnchor (request,dst,data,headers);                break;        case 3: status = HTLOCKAbsolute (request,arg->request_uri,data,headers);                break;        case 4: status = HTLOCKRelative (request,arg->request_uri,base,data,headers);                break;    }    return status;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -