📄 httpdav.c
字号:
strcat( req.headers, EOL ); if( ! http_webdav_server ) { /* For non-WebDAV servers */ strcat( req.headers, "New-URI: " ); strcat( req.headers, to ); strcat( req.headers, EOL ); } ret = http_request( &req ); if( ret == PROTO_OK && req.class != 2 ) { ret = PROTO_ERROR; } http_request_end( &req ); return ret;}/* Deletes the specified resource on the server */int http_delete( const char *filename ) { http_req_t req; int ret; http_request_init( &req, "DELETE", filename ); ret = http_request( &req ); if( ret == PROTO_OK && req.class != 2 ) ret = PROTO_ERROR; http_request_end( &req ); return ret;}/* Deletes the specified resource on the server. * I wish we could do Depth: 0 with DELETE. */int dav_rmdir( const char *filename ) { http_req_t req; int ret; char *dirname; if( strlen( filename ) < 1 ) { return PROTO_ERROR; } if( *(filename+strlen(filename)) != '/' ) { dirname = malloc( strlen( filename ) + 2 ); strcpy( dirname, filename ); strcat( dirname, "/" ); } else { dirname = strdup( filename ); } http_request_init( &req, "DELETE", dirname ); ret = http_request( &req ); /* If we get a 207, something has gone wrong */ if( ret == PROTO_OK && ( (req.status == 207) || (req.class != 2) ) ) ret = PROTO_ERROR; free( dirname ); http_request_end( &req ); return ret;}int http_mkdir_with_put( const char *realdir ) { char filename[BUFSIZ]; http_req_t req; int ret; strcpy( filename, realdir ); strcat( filename, "SitecopyTempDirCreator.txt" ); http_request_init( &req, "PUT", filename ); /* Can't send Content-Type, would break digest auth entity * digest code strcat( req.headers, "Content-type: text/plain" EOL ); */ ret = http_request( &req ); if( ret == PROTO_OK && req.class == 2 ) { http_request_end( &req ); http_request_init( &req, "DELETE", filename ); ret = http_request( &req ); if( ret != PROTO_OK || req.class != 2 ) /* NEED an FE warning function here */ DEBUG( DEBUG_HTTP, "Can\'t delete the temporary file http://%s:%d%s", http_server_host.hostname, http_server_host.port, filename ); ret = PROTO_OK; } else { ret = PROTO_ERROR; } http_request_end( &req ); return ret;}int dav_mkcol( const char *dirname ) { http_req_t req; int ret; char *realdir; if( strlen( dirname ) < 1 ) { strcpy( http_error, "Invalid directory name." ); return PROTO_ERROR; } if( *(dirname+strlen(dirname)-1) == '/' ) { realdir = strdup( dirname ); } else { /* +2 since one for \0, one for / */ realdir = malloc( strlen(dirname) + 2 ); strcpy( realdir, dirname ); strcat( realdir, "/" ); } if( http_mkdir_works == 1 ) { /* Use MKDIR, since we know it works */ http_request_init( &req, "MKDIR", realdir ); ret = http_request( &req ); if( ret == PROTO_OK && req.class != 2 ) ret = PROTO_ERROR; } else { /* Try MKCOL first */ http_request_init( &req, "MKCOL", realdir ); ret = http_request( &req ); if( ret == PROTO_OK && req.class == 2 ) { /* MKCOL works -> MKDIR doesn't */ http_mkdir_works = -1; } else if( (http_mkdir_works > -1) && !http_webdav_server ) { /* MKCOL failed, we're not on a DAV server, lets try MKDIR */ http_request_end( &req ); http_request_init( &req, "MKDIR", realdir ); ret = http_request( &req ); if( ret == PROTO_OK && req.class == 2 ) { /* MKDIR does work */ http_mkdir_works = 1; ret = PROTO_OK; } else { /* MKDIR doesn't work */ if( req.status == 501 ) { /* MKDIR definitely isn't implemented */ http_mkdir_works = -1; } /* Try a dummy PUT/DELETE */ return http_mkdir_with_put( realdir ); } } else { ret = PROTO_ERROR; } } free( realdir ); http_request_end( &req ); return ret;}int http_open( void ) { if( http_use_proxy ) { DEBUG( DEBUG_SOCKET, "Connecting to proxy at %s:%d...\n", http_proxy_host.hostname, http_proxy_host.port ); } else { DEBUG( DEBUG_SOCKET, "Connecting to server at %s:%d...\n", http_server_host.hostname, http_server_host.port ); } fe_connection( fe_connecting ); http_sock = socket_connect( http_remoteaddr, http_remoteport); if( http_sock < 0 ) { DEBUG( DEBUG_SOCKET, "Could not connect: %s\n", strerror( errno ) ); return PROTO_CONNECT; } DEBUG( DEBUG_SOCKET, "Connected.\n" ); fe_connection( fe_connected ); http_connected = true; return PROTO_OK;}int http_close( void ) { DEBUG( DEBUG_SOCKET, "Closing socket.\n" ); socket_close( http_sock ); http_connected = false; DEBUG( DEBUG_SOCKET, "Socket closed.\n" ); return PROTO_OK;}int dav_mkref( const char *resref, const char *target ) { http_req_t req; int ret; http_request_init( &req, "MKREF", resref ); strcat( req.headers, "Ref-Target: <" ); strcat( req.headers, target ); strcat( req.headers, ">" EOL ); ret = http_request( &req ); if( ret == PROTO_OK && req.class != 2 ) ret = PROTO_ERROR; http_request_end( &req ); return ret;}int dav_chref( const char *resref, const char *target ) { int ret; /* Delete it */ ret = dav_rmref( resref ); if( ret != PROTO_OK ) return ret; /* Then create it again */ ret = dav_mkref( resref, target ); return ret;}int dav_rmref( const char *resref ) { http_req_t req; int ret; http_request_init( &req, "DELETE", resref ); strcat( req.headers, "No-Passthrough: 1" EOL ); ret = http_request( &req ); if( ret == PROTO_OK && req.class != 2 ) ret = PROTO_ERROR; http_request_end( &req ); return ret;}#ifdef HAVE_LIBEXPAT/* TODO: Go read the XML specs again and use the correct terminology * for everything. */XML_Parser dav_xml_parser;/* Parses the tag attributes, and handles XML namespaces. * With a little bit of luck. * Return true on success. */static bool dav_xml_parsetag( struct dav_xml_state *state, const char *tag, const char **atts ) { dav_xml_ns *ns; int attn; char *pnt; const char *tag_prefix, *tag_suffix; struct dav_xml_state *xmlt; DEBUG( DEBUG_XML, "Parsing tag of name: [%s]\n", tag ); /* Parse the atts for namespace declarations */ for( attn = 0; atts[attn]!=NULL; attn+=2 ) { DEBUG( DEBUG_XML, "Got attribute: [%s] = [%s]\n", atts[attn], atts[attn+1] ); if( strcasecmp( atts[attn], "xmlns" ) == 0 ) { /* New default namespace */ state->default_ns = strdup( atts[attn+1] ); DEBUG( DEBUG_XML, "New default namespace: %s\n", state->default_ns ); } else if( strncasecmp( atts[attn], "xmlns:", 6 ) == 0 ) { /* New namespace scope */ ns = malloc( sizeof( dav_xml_ns ) ); ns->next = state->nspaces; state->nspaces = ns; ns->name = strdup( atts[attn]+6 ); /* skip the xmlns= */ ns->value = strdup( atts[attn+1] ); DEBUG( DEBUG_XML, "New namespace scope: %s -> %s\n", ns->name, ns->value ); } } /* Now check the tag name for a namespace scope */ pnt = strchr( tag, ':' ); tag_prefix = NULL; tag_suffix = NULL; if( pnt == NULL ) { /* No scope - have we got a default? */ DEBUG( DEBUG_XML, "No scope found, searching for default.\n" ); for( xmlt = state; xmlt!=NULL; xmlt=xmlt->parent ) { if( xmlt->default_ns != NULL ) { tag_prefix = xmlt->default_ns; break; } } if( tag_prefix != NULL ) { DEBUG( DEBUG_XML, "Found default namespace [%s]\n", tag_prefix ); } else { DEBUG( DEBUG_XML, "No default namespace, using empty.\n" ); tag_prefix = ""; } tag_suffix = tag; } else { DEBUG( DEBUG_XML, "Got namespace scope. Trying to resolve..." ); /* Have a scope - resolve it */ for( xmlt = state; tag_prefix==NULL && xmlt!=NULL; xmlt=xmlt->parent ) { for( ns = xmlt->nspaces; ns!=NULL; ns=ns->next ) { /* Just compare against the bit before the : * pnt points to the colon. */ if( strncasecmp( ns->name, tag, pnt-tag ) == 0 ) { /* Scope matched! Hoorah */ tag_prefix = ns->value; /* end the search */ break; } } } if( tag_prefix != NULL ) { DEBUG( DEBUG_XML, "Resolved scope to [%s]\n", tag_prefix ); /* The suffix is everything after the ':' */ tag_suffix = pnt+1; if( *tag_suffix == '\0' ) { DEBUG( DEBUG_XML, "No element name after ':'. Failed.\n" ); return false; } } else { DEBUG( DEBUG_XML, "Smeg. We lost it somewhere.\n" ); return false; } } /* here, we have tag_suffix and tag_prefix */ DEBUG( DEBUG_XML, "prefix: [%s], suffix: [%s]\n", tag_prefix, tag_suffix ); pnt = state->tag_name = malloc( strlen(tag_prefix) + strlen(tag_suffix) + 1 ); strcpy( pnt, tag_prefix ); strcat( pnt, tag_suffix ); DEBUG( DEBUG_XML, "You gave me: %s, and I gave you this: %s\n", tag, pnt ); return true;}/* This is the XML_StartElementHandler... called with the start of a * new element. */static void dav_xml_startelm( void *userdata, const char *tag, const char **atts ) { struct dav_xmldoc *doc = (struct dav_xmldoc *)userdata; struct dav_xml_state *s; int n; if( !doc->valid ) { /* We've stopped parsing */ DEBUG( DEBUG_XML, "Parse died. Ignoring start of element: %s\n", tag ); return; } /* Set the new state */ s = malloc( sizeof(struct dav_xml_state) ); memset( s, 0, sizeof(struct dav_xml_state) ); s->parent = doc->current; doc->current = s; if( dav_xml_parsetag( s, tag, atts ) == false ) { /* nooo, it bombed */ doc->valid = false; return; } /* Map the name to a tag */ DEBUG( DEBUG_XML, "Mapping tag name... " ); s->tag = dav_xml_unknown; for( n = 0; dav_xml_tagnames[n] != NULL; n++ ) { if( strcasecmp( dav_xml_tagnames[n], s->tag_name ) == 0 ) { s->tag = n; break; } } if( s->tag == dav_xml_unknown ) { DEBUG( DEBUG_XML, "unknown tag, ignoring.\n" ); return; } DEBUG( DEBUG_XML, "mapped.\n" ); /* Normally, we don't want to collect cdata */ doc->want_cdata = false; /* expat is not a validating parser - check the new tag * is valid in the current context. */ DEBUG( DEBUG_XML, "Checking context of tag (parent: %s)\n", dav_xml_tagnames[s->parent->tag] ); switch( s->parent->tag ) { case dav_xml_root: switch( s->tag ) { case dav_xml_multistatus: case dav_xml_response: break; default: doc->valid = false; break; } break; case dav_xml_multistatus: switch( s->tag ) { case dav_xml_response: case dav_xml_responsedescription: break; default: doc->valid = false; break; } break; case dav_xml_response: switch( s->tag ) { case dav_xml_href: case dav_xml_propstat: case dav_xml_responsedescription: case dav_xml_status: break; default: doc->valid = false; } break; default: break; } if( doc->valid == false ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -