Skip to content

Instantly share code, notes, and snippets.

@feilongfl
Last active September 17, 2022 07:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feilongfl/f561ae12b6c57ac4de8417acb295b737 to your computer and use it in GitHub Desktop.
Save feilongfl/f561ae12b6c57ac4de8417acb295b737 to your computer and use it in GitHub Desktop.
parse webdav file list by libxml2
#!/usr/bin/env bash
curl -X PROPFIND -u jksv:jksv http://192.168.2.249:10000/JKSV/Ring%20Fit%20Adventure/
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
#include <string>
const char xmldata[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \
<D:multistatus xmlns:D=\"DAV:\"> \
<D:response> \
<D:href>/JKSV/Ring%20Fit%20Adventure/ \
</D:href> \
<D:propstat> \
<D:prop> \
<D:displayname>Ring Fit Adventure \
</D:displayname> \
<D:supportedlock> \
<D:lockentry xmlns:D=\"DAV:\"> \
<D:lockscope> \
<D:exclusive/> \
</D:lockscope> \
<D:locktype> \
<D:write/> \
</D:locktype> \
</D:lockentry> \
</D:supportedlock> \
<D:resourcetype> \
<D:collection xmlns:D=\"DAV:\"/> \
</D:resourcetype> \
<D:getlastmodified>Sat, 17 Sep 2022 04:39:31 GMT \
</D:getlastmodified> \
</D:prop> \
<D:status>HTTP/1.1 200 OK \
</D:status> \
</D:propstat> \
</D:response> \
<D:response> \
<D:href>/JKSV/Ring%20Fit%20Adventure/FeiLong%20-%202021.12.14%20@%2006.03.43.zip \
</D:href> \
<D:propstat> \
<D:prop> \
<D:displayname>FeiLong - 2021.12.14 @ 06.03.43.zip \
</D:displayname> \
<D:getcontentlength>104749 \
</D:getcontentlength> \
<D:supportedlock> \
<D:lockentry xmlns:D=\"DAV:\"> \
<D:lockscope> \
<D:exclusive/> \
</D:lockscope> \
<D:locktype> \
<D:write/> \
</D:locktype> \
</D:lockentry> \
</D:supportedlock> \
<D:resourcetype> \
</D:resourcetype> \
<D:getlastmodified>Sat, 17 Sep 2022 04:39:35 GMT \
</D:getlastmodified> \
<D:getcontenttype>application/zip \
</D:getcontenttype> \
<D:getetag>\"17158c018be06afe1992d\" \
</D:getetag> \
</D:prop> \
<D:status>HTTP/1.1 200 OK \
</D:status> \
</D:propstat> \
</D:response> \
\
<D:response> \
<D:href>/JKSV/Ring%20Fit%20Adventure/FeiLong%20-%202021.12.14%20@%2006.03.43.zip \
</D:href> \
<D:propstat> \
<D:prop> \
<D:displayname>FeiLong - 2021.12.14 @ 06.03.43.zip \
</D:displayname> \
<D:getcontentlength>104749 \
</D:getcontentlength> \
<D:supportedlock> \
<D:lockentry xmlns:D=\"DAV:\"> \
<D:lockscope> \
<D:exclusive/> \
</D:lockscope> \
<D:locktype> \
<D:write/> \
</D:locktype> \
</D:lockentry> \
</D:supportedlock> \
<D:resourcetype> \
</D:resourcetype> \
<D:getlastmodified>Sat, 17 Sep 2022 04:39:35 GMT \
</D:getlastmodified> \
<D:getcontenttype>application/zip \
</D:getcontenttype> \
<D:getetag>\"17158c018be06afe1992d\" \
</D:getetag> \
</D:prop> \
<D:status>HTTP/1.1 200 OK \
</D:status> \
</D:propstat> \
</D:response> \
<D:response> \
<D:href>/JKSV/Ring%20Fit%20Adventure/FeiLong%20-%202021.12.14%20@%2006.03.43.zip \
</D:href> \
<D:propstat> \
<D:prop> \
<D:displayname>FeiLong - 2021.12.14 @ 06.03.43.zip \
</D:displayname> \
<D:getcontentlength>104749 \
</D:getcontentlength> \
<D:supportedlock> \
<D:lockentry xmlns:D=\"DAV:\"> \
<D:lockscope> \
<D:exclusive/> \
</D:lockscope> \
<D:locktype> \
<D:write/> \
</D:locktype> \
</D:lockentry> \
</D:supportedlock> \
<D:resourcetype> \
</D:resourcetype> \
<D:getlastmodified>Sat, 17 Sep 2022 04:39:35 GMT \
</D:getlastmodified> \
<D:getcontenttype>application/zip \
</D:getcontenttype> \
<D:getetag>\"17158c018be06afe1992d\" \
</D:getetag> \
</D:prop> \
<D:status>HTTP/1.1 200 OK \
</D:status> \
</D:propstat> \
</D:response> \
<D:response> \
<D:href>/JKSV/Ring%20Fit%20Adventure/FeiLong%20-%202021.12.14%20@%2006.03.43.zip \
</D:href> \
<D:propstat> \
<D:prop> \
<D:displayname>FeiLong - 2021.12.14 @ 06.03.43.zip \
</D:displayname> \
<D:getcontentlength>104749 \
</D:getcontentlength> \
<D:supportedlock> \
<D:lockentry xmlns:D=\"DAV:\"> \
<D:lockscope> \
<D:exclusive/> \
</D:lockscope> \
<D:locktype> \
<D:write/> \
</D:locktype> \
</D:lockentry> \
</D:supportedlock> \
<D:resourcetype> \
</D:resourcetype> \
<D:getlastmodified>Sat, 17 Sep 2022 04:39:35 GMT \
</D:getlastmodified> \
<D:getcontenttype>application/zip \
</D:getcontenttype> \
<D:getetag>\"17158c018be06afe1992d\" \
</D:getetag> \
</D:prop> \
<D:status>HTTP/1.1 200 OK \
</D:status> \
</D:propstat> \
</D:response> \
</D:multistatus>";
void fault(char *error) {
printf("[error] %s\n", error);
exit(1);
}
int main() {
std::string xmlstr = std::string(xmldata);
xmlDocPtr doc = xmlParseMemory(xmlstr.c_str(), xmlstr.size());
xmlXPathContextPtr ctxt = xmlXPathNewContext(doc);
if (!ctxt)
fault("ctxt");
xmlXPathRegisterNs(ctxt, BAD_CAST "D", BAD_CAST "DAV:");
xmlChar *expression = BAD_CAST "//D:response[*][D:propstat[1]/D:prop[1]/D:getcontenttype[1]]/D:href";
xmlXPathObjectPtr res = xmlXPathEvalExpression(expression, ctxt);
if (!res)
fault("res");
xmlNodeSetPtr nodeset = res->nodesetval;
for (int i = 0; i < nodeset->nodeNr; i++) {
xmlNodePtr node = nodeset->nodeTab[i];
xmlChar *name = xmlNodeGetContent(node);
printf("%s\n", name);
}
xmlXPathFreeObject(res);
xmlXPathFreeContext(ctxt);
xmlFreeDoc(doc);
return 0;
}
all: test
test:
g++ -g `xml2-config --cflags --libs` -o test main.c
.PHONY: all test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment