/** * xmlNanoHTTPSave: * @ctxt: the HTTP context * @filename: the filename where the content should be saved * * This function saves the output of the HTTP transaction to a file * It closes and free the context at the end * * Returns -1 in case of failure, 0 incase of success. */ int xmlNanoHTTPSave(void *ctxt, const char *filename) { char *buf = NULL; int fd; int len; int ret = 0; if ((ctxt == NULL) || (filename == NULL)) return(-1); if (!strcmp(filename, "-")) fd = 0; else { fd = open(filename, O_CREAT | O_WRONLY, 0666); if (fd < 0) { xmlNanoHTTPClose(ctxt); return(-1); } } xmlNanoHTTPFetchContent( ctxt, &buf, &len ); if ( len > 0 ) { if (write(fd, buf, len) == -1) { ret = -1; } } xmlNanoHTTPClose(ctxt); close(fd); return(ret); }
/** * xmlNanoHTTPFetch: * @URL: The URL to load * @filename: the filename where the content should be saved * @contentType: if available the Content-Type information will be * returned at that location * * This function try to fetch the indicated resource via HTTP GET * and save it's content in the file. * * Returns -1 in case of failure, 0 incase of success. The contentType, * if provided must be freed by the caller */ int xmlNanoHTTPFetch(const char *URL, const char *filename, char **contentType) { void *ctxt = NULL; char *buf = NULL; int fd; int len; if (filename == NULL) return(-1); ctxt = xmlNanoHTTPOpen(URL, contentType); if (ctxt == NULL) return(-1); if (!strcmp(filename, "-")) fd = 0; else { fd = open(filename, O_CREAT | O_WRONLY, 00644); if (fd < 0) { xmlNanoHTTPClose(ctxt); if ((contentType != NULL) && (*contentType != NULL)) { xmlFree(*contentType); *contentType = NULL; } return(-1); } } xmlNanoHTTPFetchContent( ctxt, &buf, &len ); if ( len > 0 ) { if (write(fd, buf, len)) {} } xmlNanoHTTPClose(ctxt); close(fd); return(0); }